From 7f46f6eb685a9baef491b714aa940e199d446ee1 Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 22 Jan 2023 19:51:45 +0100 Subject: add lexer tokens --- README.md | 14 +++++++------- Token.c | 9 ++++++++- Token.h | 11 +++++++++-- lexer.c | 12 +++++++++++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 29a6b52..51695a7 100644 --- a/README.md +++ b/README.md @@ -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]` `//` diff --git a/Token.c b/Token.c index a406332..ac549bb 100644 --- a/Token.c +++ b/Token.c @@ -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_?"; } } diff --git a/Token.h b/Token.h index 1767829..0cd7b21 100644 --- a/Token.h +++ b/Token.h @@ -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 { diff --git a/lexer.c b/lexer.c index e45f949..ff63bff 100644 --- a/lexer.c +++ b/lexer.c @@ -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; -- cgit v1.2.3