From 9bc47590c3c2108902e87a1059ad867d57357068 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sat, 23 May 2026 07:59:47 -0400 Subject: core: lexer: Add keyword tokens Signed-off-by: Chloe M. --- core/lexer.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'core') diff --git a/core/lexer.c b/core/lexer.c index 51e4d40..1d1e900 100644 --- a/core/lexer.c +++ b/core/lexer.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "cescal/lexer.h" #include "cescal/log.h" #include "cescal/ptrbox.h" @@ -135,6 +136,49 @@ lexer_scan_ident(struct cescal_state *state, char lc, struct token *res) return 0; } +/* + * Checks if an identifier token is actually a keyword + * + * @state: Compiler state + * @res: Token result + */ +static void +lexer_check_kw(struct cescal_state *state, struct token *res) +{ + if (state == NULL || res == NULL) { + return; + } + + switch (*res->s) { + case 'p': + if (strcmp(res->s, "pub") == 0) { + res->type = TT_PUB; + return; + } + + if (strcmp(res->s, "proc") == 0) { + res->type = TT_PROC; + return; + } + + break; + case 'b': + if (strcmp(res->s, "begin") == 0) { + res->type = TT_BEGIN; + return; + } + + break; + case 'e': + if (strcmp(res->s, "end") == 0) { + res->type = TT_END; + return; + } + + break; + } +} + int lexer_nom(struct cescal_state *state, struct token *res) { @@ -164,6 +208,7 @@ lexer_nom(struct cescal_state *state, struct token *res) return 0; default: if (lexer_scan_ident(state, c, res) == 0) { + lexer_check_kw(state, res); return 0; } } -- cgit v1.2.3