diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-09 02:28:02 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-09 02:28:02 +0200 |
commit | 953bb6499d9fb1c1446cd36dde9da1f4238bf5cb (patch) | |
tree | 2a51a811c06beaaab5630d9e56edef792352fbae | |
parent | d898d39c98e575624aed7c19da88c02885bfea0e (diff) | |
download | golem-953bb6499d9fb1c1446cd36dde9da1f4238bf5cb.tar.gz |
i'm not sure of what i acomplished here
-rw-r--r-- | src/main.c | 21 |
1 files changed, 19 insertions, 2 deletions
@@ -8,9 +8,17 @@ typedef enum { TOK_PUNCT, TOK_NUM, + TOK_WORD, TOK_EOF, } TokenType; +static const char * str_tok[] = { + [TOK_PUNCT] = "TOK_PUNCT", + [TOK_NUM] = "TOK_NUM", + [TOK_WORD] = "TOK_WORD", + [TOK_EOF] = "TOK_EOF", +}; + typedef struct Token Token; struct Token { TokenType type; @@ -35,7 +43,7 @@ static void expect(const Token *tok, TokenType type) { if (tok->type != type) - error("expected a %u", type); + error("expected a %s", str_tok[type]); } static bool @@ -91,12 +99,20 @@ tokenize(char *p) continue; } - if (strchr("+-/*", *p) != NULL) { + if (strchr("+-/*()", *p) != NULL) { cur = cur->next = new_token(TOK_PUNCT, p, p + 1); p += 1; continue; } + if (isalpha(*p)) { + char *q = p; + while (isalnum(*p)) + p += 1; + cur = cur->next = new_token(TOK_WORD, q, p); + continue; + } + error("invalid token"); } cur->next = new_token(TOK_EOF, p, p); @@ -126,6 +142,7 @@ main(int argc, char **argv) case '-': punct = "SUB"; break; case '*': punct = "MUL"; break; case '/': punct = "DIV"; break; + default: break; } tok = tok->next; |