diff options
| author | 0x221e | 2026-06-15 21:12:35 +0200 |
|---|---|---|
| committer | 0x221e | 2026-06-16 02:19:01 +0200 |
| commit | aab8263f58be6479cc73033a745459849fba7f0a (patch) | |
| tree | 10c1b871c38c8c97a8f70ef20d418ae20d974221 /lex | |
| parent | 56796c08d6388ad0eb0539d166cd12964cfeb7e9 (diff) | |
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.
Diffstat (limited to 'lex')
| -rw-r--r-- | lex/lex.c | 54 |
1 files changed, 51 insertions, 3 deletions
@@ -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"); } } |
