From 5adb568da4dfda6d5d9699ee4b92fe44b43fc4cf Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Thu, 30 Jul 2026 22:54:31 -0400 Subject: token names --- README.md | 16 +++++---- lexer.c | 53 ++++++++++++++++++++++++++++++ lexer.h | 101 +++++++++++++++++++++++++++++---------------------------- parser.c | 10 ++++-- test/dogshit.c | 3 ++ 5 files changed, 125 insertions(+), 58 deletions(-) create mode 100644 test/dogshit.c diff --git a/README.md b/README.md index 4ff2649..2cfb498 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,14 @@ fuck it, we ball TODO soon: -- [ ] type checking +- [ ] let cgen use more than one register + - [ ] basic used/unused register tracking so allocate_temporary can allocate + a register + - [ ] proper deallocation of temporaries when they are no longer used + - [ ] spilling of values occupying calling convention registers when a + function is called + - [ ] allowing variable values to be kept in registers temporarily and then + spilled to their permanent memory locations - [ ] use evaluated types to make code gen around math better -- [ ] ideally we don't want to recompute the definitions that live in scopes - - [ ] add a `struct scope* scope` to expr_node - - [ ] make the root scope live on a `struct ast` that houses the root node as well - - [ ] handle scope deallocation in ast.c - - [ ] `group_node` and `fn_decl_node` can have a `struct scope* scope` that they own +- [ ] support for functions/function pointers as types + - [ ] move the "expression not callable" stuff to the type checker \ No newline at end of file diff --git a/lexer.c b/lexer.c index c8c98db..b1d7f55 100644 --- a/lexer.c +++ b/lexer.c @@ -4,6 +4,59 @@ #include #include +const char* token_labels[] = { + "not found", + "identifier", + "integer literal", + "floating point literal", + "character literal", + "string literal", + "#", + "(", + ")", + "{", + "}", + "[", + "]", + ":", + ";", + ",", + ".", + "?", + "!", + "!=", + "^", + "^=", + "&", + "&&", + "&=", + "*", + "*=", + "-", + "-=", + "->", + "=", + "==", + "+", + "+=", + "\\", + "|", + "||", + "|=", + "/", + "/=", + "%", + "%=", + "<", + ">", + "<=", + ">=", + ">>", + ">>=", + "<<", + "<<=", +}; + static FILE* file = NULL; static int lookahead; static const char* PATH; diff --git a/lexer.h b/lexer.h index f228b2a..6386c0c 100644 --- a/lexer.h +++ b/lexer.h @@ -4,57 +4,58 @@ #include "ccc.h" enum token_type { - TK_NOT_FOUND, - TK_IDENT, - TK_INT_LIT, - TK_FLOAT_LIT, - TK_CHAR_LIT, - TK_STR_LIT, - TK_HASHTAG, - TK_LPAREN, - TK_RPAREN, - TK_LCURLY, - TK_RCURLY, - TK_LSQUARE, - TK_RSQUARE, - TK_COLON, - TK_SEMI, - TK_COMMA, - TK_DOT, - TK_QMARK, - TK_NOT, - TK_NEQ, - TK_XOR, - TK_XEQ, - TK_AMP, - TK_LOG_AND, - TK_AND_EQ, - TK_STAR, - TK_MUL_EQ, - TK_NEG, - TK_NEG_EQ, - TK_ARROW, - TK_ASSIGN, - TK_TEST_EQ, - TK_PLUS, - TK_PLUS_EQ, - TK_BSLASH, - TK_PIPE, - TK_LOG_PIPE, - TK_PIPE_EQ, - TK_DIV, - TK_DIV_EQ, - TK_MOD, - TK_MOD_EQ, - TK_LT, - TK_GT, - TK_LEQ, - TK_GEQ, - TK_SHR, - TK_SHR_EQ, - TK_SHL, - TK_SHL_EQ + TK_NOT_FOUND = 0, + TK_IDENT = 1, + TK_INT_LIT = 2, + TK_FLOAT_LIT = 3, + TK_CHAR_LIT = 4, + TK_STR_LIT = 5, + TK_HASHTAG = 6, + TK_LPAREN = 7, + TK_RPAREN = 8, + TK_LCURLY = 9, + TK_RCURLY = 10, + TK_LSQUARE = 11, + TK_RSQUARE = 12, + TK_COLON = 13, + TK_SEMI = 14, + TK_COMMA = 15, + TK_DOT = 16, + TK_QMARK = 17, + TK_NOT = 18, + TK_NEQ = 19, + TK_XOR = 20, + TK_XEQ = 21, + TK_AMP = 22, + TK_LOG_AND = 23, + TK_AND_EQ = 24, + TK_STAR = 25, + TK_MUL_EQ = 26, + TK_NEG = 27, + TK_NEG_EQ = 28, + TK_ARROW = 29, + TK_ASSIGN = 30, + TK_TEST_EQ = 31, + TK_PLUS = 32, + TK_PLUS_EQ = 33, + TK_BSLASH = 34, + TK_PIPE = 35, + TK_LOG_PIPE = 36, + TK_PIPE_EQ = 37, + TK_DIV = 38, + TK_DIV_EQ = 39, + TK_MOD = 40, + TK_MOD_EQ = 41, + TK_LT = 42, + TK_GT = 43, + TK_LEQ = 44, + TK_GEQ = 45, + TK_SHR = 46, + TK_SHR_EQ = 47, + TK_SHL = 48, + TK_SHL_EQ = 49, }; +extern const char* token_labels[]; struct token { enum token_type type; diff --git a/parser.c b/parser.c index 29aea36..bdf266d 100644 --- a/parser.c +++ b/parser.c @@ -22,7 +22,10 @@ static struct scope* scope; static void unexpected_token(enum token_type expected) { /* TODO: print what token was expected */ - PARSER_PANIC("unexpected token; expected %d", expected); + PARSER_PANIC( + "unexpected token %s; expected %s", + token_labels[tok.type], + token_labels[expected]); } static void peek_or_panic() { @@ -42,7 +45,10 @@ static void expect_kw(const char* kw) { PARSER_PANIC("unexpected EOF, expected %s", kw); if (tok.type != TK_IDENT) - PARSER_PANIC("unexpected token, expected %s", kw); + PARSER_PANIC( + "unexpected token %s, expected %s", + token_labels[tok.type], + kw); if (strcmp(kw, tok.data.ident) != 0) PARSER_PANIC( diff --git a/test/dogshit.c b/test/dogshit.c new file mode 100644 index 0000000..d83d1dc --- /dev/null +++ b/test/dogshit.c @@ -0,0 +1,3 @@ +int main )int argc, char** argv( { + return 4; +} -- cgit v1.2.3