Author | Jakob Wakeling <[email protected]> |
Date | 2022-03-31 03:46:44 |
Commit | b2691843413f657ab3cc611e833e57dc67122e1f |
Parent | 30b1fa8700e75102ebe5aa17d1d41b0a95fb40af |
lex: Merge keyword finding into the lexer
Diffstat
D | src/keyword.c | | | 26 | -------------------------- |
D | src/keyword.h | | | 18 | ------------------ |
M | src/lex.c | | | 21 | ++++++++++----------- |
3 files changed, 10 insertions, 55 deletions
diff --git a/src/keyword.c b/src/keyword.c deleted file mode 100644 index 8706fd8..0000000 --- a/src/keyword.c +++ /dev/null @@ -1,26 +0,0 @@ -// keyword.c -// Keyword source file for G -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - -#include "keyword.h" -#include "util/fnv.h" -#include "util/util.h" - -#include <string.h> - -typedef enum { KK_PROC, KK_RETURN, } kwd_k; - -static kwd kwds[] = { - { TK_PROC, 0x85729B0E3537BC61, "proc" }, - { TK_RETURN, 0xC5C7B983377CAD5F, "return" }, -}; - -kwd *k_proc = &kwds[KK_PROC]; kwd *k_return = &kwds[KK_RETURN]; - -/* Return pointer to keyword or null if invalid. */ -kwd *kwd_find(u64 hash) { - for (UINT i = 0; i < sizeof (kwds) / sizeof (*kwds); ++i) { - if (hash == kwds[i].h) { return &kwds[i]; } - } return NULL; -} diff --git a/src/keyword.h b/src/keyword.h deleted file mode 100644 index 3fb7834..0000000 --- a/src/keyword.h +++ /dev/null @@ -1,18 +0,0 @@ -// keyword.h -// Keyword header file for G -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - -#ifndef G_KEYWORD_H_UEY4FULO -#define G_KEYWORD_H_UEY4FULO - -#include "lex.h" -#include "util/util.h" - -typedef struct { tok_k k; u64 h; char *name; } kwd; - -extern kwd *k_proc, *k_return; - -extern kwd *kwd_find(u64 hash); - -#endif // G_KEYWORD_H_UEY4FULO diff --git a/src/lex.c b/src/lex.c index 849f57a..89ff592 100644 --- a/src/lex.c +++ b/src/lex.c @@ -3,7 +3,6 @@ // Copyright (C) 2021, Jakob Wakeling // All rights reserved. -#include "keyword.h" #include "lex.h" #include "symbol.h" #include "type.h" @@ -11,7 +10,6 @@ #include "util/util.h" #include <ctype.h> -#include <stddef.h> #include <stdint.h> #include <stdlib.h> #include <string.h> @@ -54,11 +52,10 @@ tok lex_peek(lex *l) { return T; } /* Lex the next token, and return the current one. */ tok lex_next(lex *l) { if (T.k == TK_EOF) { return T; } - tok t = T; T = (tok){ TK_NULL, 0, 0, 0 }; + tok t = T; T = (tok){ 0 }; -skip:; /* Skip null characters and whitespace */ - for (; P != Q && (!C || isspace(C)); P += 1) switch (C) { + skip:; for (; P != Q && (!C || isspace(C)); P += 1) switch (C) { case '\0': { /* TODO warn user of null character */ } break; case '\n': { LN += 1; CL = 0; } break; default: { CL += 1; } break; @@ -91,11 +88,14 @@ skip:; char *s = P; UINT sl; for (P += 1; isalpha(C) || isdigit(C) || C == '_'; P += 1); - sl = P - s; CL += sl; + sl = P - s; CL += sl; T.h = syt_hash(s, sl); - T.s = strndup(s, sl); - T.h = syt_hash(s, sl); kwd *k = kwd_find(T.h); - if (k) { T.k = k->k; } else { T.k = TK_ID; } + if (strncmp(s, "return", sl) == 0) { T.k = TK_RETURN; } + else if (strncmp(s, "if", sl) == 0) { T.k = TK_IF; } + else if (strncmp(s, "else", sl) == 0) { T.k = TK_ELSE; } + else if (strncmp(s, "for", sl) == 0) { T.k = TK_FOR; } + else if (strncmp(s, "proc", sl) == 0) { T.k = TK_PROC; } + else { T.k = TK_ID; if (!(T.s = strndup(s, sl))) { error(1, SERR); }} } /* Handle number literals */ @@ -105,8 +105,7 @@ skip:; for (P += 1; isalnum(C); P += 1); sl = P - s; CL += sl; - T.s = strndup(s, sl); - T.k = TK_INT; + T.k = TK_INT; if (!(T.s = strndup(s, sl))) { error(1, SERR); } } /* Handle punctuators and operators */