SterlingLang/include/SterlingCompiler.h

82 lines
1.9 KiB
C

#ifndef STERLING_COMPILER_H
# define STERLING_COMPILER_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//simd
# ifdef __x86_64__
# include <x86intrin.h>
# endif
typedef enum {
TOK_NONE = 1 << 0,
TOK_RAW = 1 << 1,
TOK_STRING = 1 << 2,//"asdfasf"; L"WideString"
TOK_LITERAL = 1 << 3,//INT: 42, 0xff, 0777, 100L; FLOAT:3.14, 1e-5, 2.0f; ENUM; char CONST 'a' '\n' L'x'
TOK_OP = 1 << 4,
TOK_PREPROC = 1 << 5,
TOK_COMMENT = 1 << 6,
TOK_KEY = 1 << 7,
TOK_ID = 1 << 8,
TOK_NUM = 1 << 9
} TKN_CTX;
typedef struct Token_s {
size_t size;
TKN_CTX ctx;
char *data;
} Token_t;
typedef struct {
char *op;
size_t len;
} MultiOp;
typedef struct {
const char *name;
TKN_CTX ctx;
} KeywordEntry;
const char *SYMBOLS = ";(){}[]$%&*#@!?:,.<>|-+=~`^";
// Common C operators (Order matters: put longer ones first if you add 3-char ops)
static const MultiOp MUNCH_TABLE[] = {
{"%:%:", 4},
{"<<=", 3}, {">>=", 3}, {"...", 3},
{"\?\?=", 3}, {"\?\?/", 3}, {"\?\?'", 3}, {"\?\?(", 3},
{"\?\?)", 3}, {"\?\?!", 3}, {"\?\?<", 3},
{"\?\?>", 3}, {"\?\?-", 3}, //trigraph
{"==", 2}, {"!=", 2}, {"<=", 2}, {">=", 2}, {"##", 2},
{"++", 2}, {"--", 2}, {"->", 2}, {"+=", 2}, {"%=",2},
{"-=", 2}, {"*=", 2}, {"/=", 2}, {"&&", 2}, {"||", 2},
{"^=", 2}, {"<<", 2}, {">>", 2}, {"|=", 2}, {"&=", 2},//
{"<:", 2}, {":>", 2}, {"<%", 2}, {"%>", 2}, {"%:", 2},//digraphs
{NULL, 0}
};
// This can be expanded at runtime if you use a dynamic array instead of a static one
static const KeywordEntry KEYWORD_TABLE[] = {
{"if", TOK_KEY},
{"else", TOK_KEY},
{"while", TOK_KEY},
{"return", TOK_KEY},
{"var", TOK_KEY},
{"int", TOK_KEY},
{"float", TOK_KEY},
{"void", TOK_KEY},
{"include", TOK_PREPROC},
{"define", TOK_PREPROC},
{"comptime",TOK_KEY},
{"reflect", TOK_KEY},
{NULL, TOK_NONE}
};
#endif