From aab8263f58be6479cc73033a745459849fba7f0a Mon Sep 17 00:00:00 2001 From: 0x221e Date: Mon, 15 Jun 2026 21:12:35 +0200 Subject: lex: add lex_get_token_name() Add lex_get_token_name() to lex/lex.c and refactor platform/elf/main.c so that it prints the token identifier string rather than int. --- include/lex.h | 2 ++ include/platform/osal.h | 1 + lex/lex.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++--- platform/elf/main.c | 4 ++-- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/include/lex.h b/include/lex.h index a712204..4b8b1ac 100644 --- a/include/lex.h +++ b/include/lex.h @@ -31,6 +31,8 @@ #define TOKEN_STAR 23 #define TOKEN_SLASH 24 +const char* lex_get_token_name(int id); + struct token { int type; // size_t line; diff --git a/include/platform/osal.h b/include/platform/osal.h index 8eee16b..b6a35d9 100644 --- a/include/platform/osal.h +++ b/include/platform/osal.h @@ -19,5 +19,6 @@ struct file { struct file open_file(const char *path, int flags); void print(const char *fmt, ...); +void exitc(int c); #endif diff --git a/lex/lex.c b/lex/lex.c index 7ff470b..f98d82c 100644 --- a/lex/lex.c +++ b/lex/lex.c @@ -17,6 +17,41 @@ static struct token keywords[] = { TOKEN(TOKEN_RETURN, "return") }; +#define __GET_TKN_NAME(TKN) case TKN: return #TKN + +const char *lex_get_token_name(int id) +{ + switch (id) { + __GET_TKN_NAME(TOKEN_INVALID); + __GET_TKN_NAME(TOKEN_EOP); + __GET_TKN_NAME(TOKEN_NUMBER); + __GET_TKN_NAME(TOKEN_HEX); + __GET_TKN_NAME(TOKEN_BINARY); + __GET_TKN_NAME(TOKEN_FUNCTION); + __GET_TKN_NAME(TOKEN_SEMICOLON); + __GET_TKN_NAME(TOKEN_NEWLINE); + __GET_TKN_NAME(TOKEN_G8); + __GET_TKN_NAME(TOKEN_G16); + __GET_TKN_NAME(TOKEN_G32); + __GET_TKN_NAME(TOKEN_G64); + __GET_TKN_NAME(TOKEN_IDENTIFIER); + __GET_TKN_NAME(TOKEN_PACK); + __GET_TKN_NAME(TOKEN_BRACES_OPEN); + __GET_TKN_NAME(TOKEN_BRACES_CLOSE); + __GET_TKN_NAME(TOKEN_PARAN_OPEN); + __GET_TKN_NAME(TOKEN_PARAN_CLOSE); + __GET_TKN_NAME(TOKEN_RETURN); + __GET_TKN_NAME(TOKEN_EQUALSIGN); + __GET_TKN_NAME(TOKEN_PLUS); + __GET_TKN_NAME(TOKEN_MINUS); + __GET_TKN_NAME(TOKEN_STAR); + __GET_TKN_NAME(TOKEN_SLASH); + default: + print("lex_get_token_name: token not found: %d!", id); + exitc(1); + } +} + inline int lex_is_digit(char c) { return c >= '0' && c <= '9'; @@ -203,10 +238,23 @@ struct token lex_next(struct lex *l) case '=': l->pos++; return TOKEN(TOKEN_EQUALSIGN, "="); - case 'a'...'z': - case 'A'...'Z': - return lex_keyword(l); + case '*': + l->pos++; + return TOKEN(TOKEN_STAR, "*"); + case '+': + l->pos++; + return TOKEN(TOKEN_PLUS, "+"); + case '-': + l->pos++; + return TOKEN(TOKEN_MINUS, "-"); + case '/': + l->pos++; + return TOKEN(TOKEN_SLASH, "/"); default: + char curr = lex_seek(l, 0); + if (lex_is_char(curr) || (curr == '_')) { + return lex_keyword(l); + } return TOKEN(TOKEN_INVALID, "INVALID"); } } diff --git a/platform/elf/main.c b/platform/elf/main.c index b2ebb34..7dba99a 100644 --- a/platform/elf/main.c +++ b/platform/elf/main.c @@ -59,10 +59,10 @@ int main(int argc, char **argv) if (token_storage.buffer[i] == NULL) break; - printf("Tokenized '%.*s' with type: %d\n", //, on line: %d at position: %d\n", + printf("Tokenized '%.*s' with type: %s\n", //, on line: %d at position: %d\n", (int)((struct token*) token_storage.buffer[i])->length, ((struct token*)token_storage.buffer[i])->lexeme, - (int)((struct token*)token_storage.buffer[i])->type + lex_get_token_name((int)((struct token*)token_storage.buffer[i])->type) // ((struct token*)token_storage.buffer[i])->line, // ((struct token*)token_storage.buffer[i])->pos ); -- cgit v1.2.3