From f3df011c18821ee6b09f9c2ee8fd48163ed92096 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sun, 23 Aug 2026 09:12:47 +0000 Subject: core: lexer+parser: Handle integer literals Signed-off-by: Chloe M. --- core/lexer.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/parser.c | 4 ++++ 2 files changed, 61 insertions(+) (limited to 'core') diff --git a/core/lexer.c b/core/lexer.c index ac5194f..59aaa13 100644 --- a/core/lexer.c +++ b/core/lexer.c @@ -13,6 +13,7 @@ /* Constants */ #define MAX_KW_LEN 32 +#define MAX_NUM_LEN 20 /* * Returns true if the given character is a whitespace character @@ -125,6 +126,51 @@ lexer_scan_ident(struct defconf_state *state, int lc) return NULL; } +/* + * Scan a number + * + * @state: Defconf state + * @lc: Last character provided + * @result: Result is written here + */ +static int +lexer_scan_num(struct defconf_state *state, int lc, struct token *result) +{ + char c, buf[MAX_NUM_LEN + 1]; + size_t bufind = 0; + + if (state == NULL || result == NULL) { + return -1; + } + + if (!isdigit(lc)) { + return -1; + } + + buf[bufind++] = lc; + for (;;) { + if (bufind >= sizeof(buf) - 1) { + trace_fatal(state, "digits exceed maximum length\n"); + return -1; + } + + c = lexer_nom(state, false); + if (!isdigit(c)) { + lexer_putback(state, c); + buf[bufind] = '\0'; + + /* Set the token */ + result->type = TT_INTLIT; + result->v = atoi(buf); + return 0; + } + + buf[bufind++] = c; + } + + return -1; +} + /* * Check if a keyword matches against known keywords * @@ -172,6 +218,7 @@ lexer_scan(struct defconf_state *state, struct token *result) { int c; char *ident; + int error; if (state == NULL || result == NULL) { return -1; @@ -192,6 +239,16 @@ lexer_scan(struct defconf_state *state, struct token *result) return 0; } + /* Is this a digit? */ + if (isdigit(c)) { + error = lexer_scan_num(state, c, result); + if (error < 0) { + return -1; + } + + return 0; + } + trace_fatal(state, "unexpected token '%c'\n", c); return -1; } diff --git a/core/parser.c b/core/parser.c index 698691e..79fda93 100644 --- a/core/parser.c +++ b/core/parser.c @@ -50,6 +50,10 @@ token_to_data(struct token *tok, struct data_value *result) result->v = 0; result->type = DATA_TYPE_INT; return 0; + case TT_INTLIT: + result->v = tok->v; + result->type = DATA_TYPE_INT; + return 0; default: return -1; } -- cgit v1.2.3