diff options
| author | David Moc <personal@cdatgoose.org> | 2026-03-05 23:38:49 +0100 |
|---|---|---|
| committer | David Moc <personal@cdatgoose.org> | 2026-03-05 23:38:49 +0100 |
| commit | 0385817bb1301a778bb33f8405a435293b9f8905 (patch) | |
| tree | 53f4b6f13e393bd368c37ba4363826b46940dfd3 /src/token.h | |
| parent | 262abf9b552a168ef3ae91f91af97683f16420a7 (diff) | |
Pushing to repo for safety.
Diffstat (limited to 'src/token.h')
| -rw-r--r-- | src/token.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/token.h b/src/token.h new file mode 100644 index 0000000..e99294e --- /dev/null +++ b/src/token.h @@ -0,0 +1,104 @@ +#ifndef INCLUDE_token +#define INCLUDE_token + +#include <string.h> + +/* Token and type definitions shared by lexer, parser, and JIT. + * The type system uses a base kind plus pointer/array decorations. */ +typedef enum { + TY_INT = 0, + TY_CHAR = 1, + TY_BOOL = 2, +} _TYBASE; + +typedef struct { + _TYBASE base; /* TY_INT, TY_CHAR */ + int ptr_level; /* 0 for non-pointer, 1 for T*, 2 for T**, ... */ + int array_size; /* -1 for non-array, 0 for unknown size [], >0 for fixed size [N] */ +} _TY; + +static inline const char *tybase_name(_TYBASE b) { + switch (b) { + case TY_INT: return "int"; + case TY_CHAR: return "char"; + case TY_BOOL: return "bool"; + default: return "?"; + } +} + +#define _TL(_) \ + _(TK_BEGIN, "begin") \ + _(TK_INT, "int") \ + _(TK_CHAR, "char") \ + _(TK_ASSIGN, "=") \ + _(TK_EQ, "==") \ + _(TK_NE, "!=") \ + _(TK_LT, "<") \ + _(TK_LE, "<=") \ + _(TK_GT, ">") \ + _(TK_GE, ">=") \ + _(TK_RETURN, "return") \ + _(TK_IF, "if") \ + _(TK_ELSE, "else") \ + _(TK_FOR, "for") \ + _(TK_WHILE, "while") \ + _(TK_BOOL, "bool") \ + _(TK_IDENT, "ident") \ + _(TK_NUMBER, "number") \ + _(TK_CHARLIT, "charlit") \ + _(TK_LPAREN, "(") \ + _(TK_RPAREN, ")") \ + _(TK_LBRACE, "{") \ + _(TK_RBRACE, "}") \ + _(TK_SEMI, ";") \ + _(TK_PLUS, "+") \ + _(TK_MINUS, "-") \ + _(TK_STAR, "*") \ + _(TK_SLASH, "/") \ + _(TK_PERCENT, "%") \ + _(TK_AMP, "&") \ + _(TK_AND, "&&") \ + _(TK_BAR, "|") \ + _(TK_OR, "||") \ + _(TK_CARET, "^") \ + _(TK_SHL, "<<") \ + _(TK_SHR, ">>") \ + _(TK_BANG, "!") \ + _(TK_SQUOTE, "'") \ + _(TK_DQUOTE, "\"") \ + _(TK_LBRACKET, "[") \ + _(TK_RBRACKET, "]") \ + _(TK_STRING, "string") \ + _(TK_EOF, "eof") \ + _(TK_COMMA, ",") \ + _(TK_INVALID, "invalid") \ + _(TK__COUNT, "<count>") + +typedef enum { +#define MAKE_ENUM(_, s) _, + _TL(MAKE_ENUM) +#undef MAKE_ENUM +} _TK; + +static const char *_TN[] = { +#define MAKE_STR(_, s) s, + _TL(MAKE_STR) +#undef MAKE_STR +}; + +typedef struct { + _TK kind; + int val; // only valid if kind == TK_NUMBER + char *lxem; // malloc’d lexeme string, or NULL +} _T; + +static _TK checkkw(const char *kw) { +#define _(k, s) \ + if (strcmp(kw, s) == 0) \ + return k; + _TL(_) +#undef _ + return TK_IDENT; +} + +#endif /* INCLUDE_token */ |
