summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-09 02:28:02 +0200
committerkdx <kikoodx@paranoici.org>2023-06-09 02:28:02 +0200
commit953bb6499d9fb1c1446cd36dde9da1f4238bf5cb (patch)
tree2a51a811c06beaaab5630d9e56edef792352fbae
parentd898d39c98e575624aed7c19da88c02885bfea0e (diff)
downloadgolem-953bb6499d9fb1c1446cd36dde9da1f4238bf5cb.tar.gz
i'm not sure of what i acomplished here
-rw-r--r--src/main.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index 666f847..ba67715 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;