diff options
author | kdx <kdx@42l.fr> | 2023-01-22 19:51:45 +0100 |
---|---|---|
committer | kdx <kdx@42l.fr> | 2023-01-22 19:51:45 +0100 |
commit | 7f46f6eb685a9baef491b714aa940e199d446ee1 (patch) | |
tree | 9fcdabd39696ad1be3b6ea32d5ad2eebd273498b | |
parent | 8e142cd9e0f5a05c36025624e6c4897951f3e775 (diff) | |
download | golem-7f46f6eb685a9baef491b714aa940e199d446ee1.tar.gz |
add lexer tokens
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | Token.c | 9 | ||||
-rw-r--r-- | Token.h | 11 | ||||
-rw-r--r-- | lexer.c | 12 |
4 files changed, 35 insertions, 11 deletions
@@ -79,9 +79,9 @@ I fucking wanted to make it. * `[x]` `var` * `[x]` `const` * `[x]` `fn` -* `[ ]` `if` -* `[ ]` `else` -* `[ ]` `while` +* `[x]` `if` +* `[x]` `else` +* `[x]` `while` * `[x]` `(` * `[x]` `)` * `[x]` `{` @@ -114,8 +114,8 @@ I fucking wanted to make it. * `[x]` `!=` * `[x]` `<` * `[ ]` `>` -* `[ ]` `<=` -* `[ ]` `>=` -* `[ ]` `||` -* `[ ]` `&&` +* `[x]` `<=` +* `[x]` `>=` +* `[x]` `||` +* `[x]` `&&` * `[x]` `//` @@ -39,12 +39,19 @@ const char *token_type_str(unsigned int type) case TOK_COMMA: return "TOK_COMMA"; case TOK_COMP_LESS: return "TOK_COMP_LESS"; case TOK_MODULO: return "TOK_MODULO"; - case TOK_STAR: return "TOK_STAR"; + case TOK_MULT: return "TOK_MULT"; case TOK_COMP_EQ: return "TOK_COMP_EQ"; case TOK_COMP_NEQ: return "TOK_COMP_NEQ"; + case TOK_COMP_LESSEQ: return "TOK_COMP_LESSEQ"; + case TOK_COMP_MOREEQ: return "TOK_COMP_MOREEQ"; + case TOK_COMP_OR: return "TOK_COMP_OR"; + case TOK_COMP_AND: return "TOK_COMP_AND"; case TOK_KW_CONST: return "TOK_KW_CONST"; case TOK_KW_VAR: return "TOK_KW_VAR"; case TOK_KW_FN: return "TOK_KW_FN"; + case TOK_KW_IF: return "TOK_KW_IF"; + case TOK_KW_ELSE: return "TOK_KW_ELSE"; + case TOK_KW_WHILE: return "TOK_KW_WHILE"; default: return "TOK_?"; } } @@ -19,15 +19,22 @@ enum { TOK_COMMA, TOK_COMP_LESS, TOK_MODULO, - TOK_STAR, + TOK_MULT, /* double char toks */ + TOK_COMMENT, TOK_COMP_EQ, TOK_COMP_NEQ, - TOK_COMMENT, + TOK_COMP_LESSEQ, + TOK_COMP_MOREEQ, + TOK_COMP_OR, + TOK_COMP_AND, /* keywords */ TOK_KW_VAR, TOK_KW_CONST, TOK_KW_FN, + TOK_KW_IF, + TOK_KW_ELSE, + TOK_KW_WHILE, }; union TokenValue { @@ -31,7 +31,7 @@ static unsigned int one_wide_tok(const char *s) case ',': return TOK_COMMA; case '<': return TOK_COMP_LESS; case '%': return TOK_MODULO; - case '*': return TOK_STAR; + case '*': return TOK_MULT; default: return TOK_NONE; } } @@ -43,6 +43,10 @@ static unsigned int two_wide_tok(const char *s) case PAIR('=', '='): return TOK_COMP_EQ; case PAIR('!', '='): return TOK_COMP_NEQ; case PAIR('/', '/'): return TOK_COMMENT; + case PAIR('<', '='): return TOK_COMP_LESSEQ; + case PAIR('>', '='): return TOK_COMP_MOREEQ; + case PAIR('|', '|'): return TOK_COMP_OR; + case PAIR('&', '&'): return TOK_COMP_AND; default: return TOK_NONE; } } @@ -109,6 +113,12 @@ Token *lexer(const char *s) toks[tok_i].type = TOK_KW_VAR; else if (strcmp("const", toks[tok_i].s) == 0) toks[tok_i].type = TOK_KW_CONST; + else if (strcmp("if", toks[tok_i].s) == 0) + toks[tok_i].type = TOK_KW_IF; + else if (strcmp("else", toks[tok_i].s) == 0) + toks[tok_i].type = TOK_KW_ELSE; + else if (strcmp("while", toks[tok_i].s) == 0) + toks[tok_i].type = TOK_KW_WHILE; else toks[tok_i].type = TOK_WORD; tok_i += 1; |