diff options
author | Ian Moffett <ian@osmora.org> | 2025-10-01 02:25:49 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-10-01 02:25:49 -0400 |
commit | e2503def53445f74d37fcbc5d67b956bd795092f (patch) | |
tree | dcee8336db27796afef480d78f6d60896152d022 /src | |
parent | de12f97c8d41898255c7d9eed2c24b3c567b5747 (diff) |
np: lex: Handle digits in the stream
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/sys/include/np/lex.h | 7 | ||||
-rw-r--r-- | src/sys/np/core/np_lex.c | 31 | ||||
-rw-r--r-- | src/sys/np/core/np_parse.c | 9 |
3 files changed, 47 insertions, 0 deletions
diff --git a/src/sys/include/np/lex.h b/src/sys/include/np/lex.h index daca954..235e997 100644 --- a/src/sys/include/np/lex.h +++ b/src/sys/include/np/lex.h @@ -63,19 +63,26 @@ typedef enum { /* Types */ TT_U8, /* 'u8' */ + /* Values */ + TT_NUMBER, /* <numbers> */ + /* Keywords */ TT_BEGIN, /* 'begin' */ TT_PROC, /* 'proc' */ TT_END, /* 'end' */ } tt_t; +typedef uint64_t tokval_t; + /* * Represents a lexer token * * @token: Token type + * @val: Integer value */ struct lex_token { tt_t token; + tokval_t val; }; /* diff --git a/src/sys/np/core/np_lex.c b/src/sys/np/core/np_lex.c index e6dd07c..620a7ad 100644 --- a/src/sys/np/core/np_lex.c +++ b/src/sys/np/core/np_lex.c @@ -215,6 +215,32 @@ lex_cmpop(struct np_work *work, char c, struct lex_token *res) } /* + * Parse a number and get a token value + * + * @work: Input work + * @c: First character [digit] + * @res: Result + */ +static int +lex_nomnum(struct np_work *work, char c, struct lex_token *res) +{ + uint64_t num = 0; + + if (work == NULL || res == NULL) { + return -EINVAL; + } + + while (is_num(c)) { + num = num * 10 + (c - '0'); + c = lex_pop(work); + } + + res->token = TT_NUMBER; + res->val = num; + return 0; +} + +/* * Nom a token */ int @@ -256,6 +282,11 @@ lex_nom(struct np_work *work, struct lex_token *res) res->token = TT_EQUALS; break; default: + if (is_num(c)) { + lex_nomnum(work, c, res); + break; + } + if (lex_arithop(work, c, res) >= 0) break; if (lex_cmpop(work, c, res) >= 0) diff --git a/src/sys/np/core/np_parse.c b/src/sys/np/core/np_parse.c index 165fed5..253b017 100644 --- a/src/sys/np/core/np_parse.c +++ b/src/sys/np/core/np_parse.c @@ -56,6 +56,9 @@ static const char *stoktab[] = { /* Types */ [TT_U8] = "<TT_U8>", + /* Values */ + [TT_NUMBER] = "<TT_NUMBER>", + /* Keywords */ [TT_BEGIN] = "<TT_BEGIN>", [TT_PROC] = "<TT_PROC>", @@ -79,11 +82,17 @@ parse_work(struct np_work *work) return error; } + /* Don't overflow the table */ if (tok.token > NELEM(stoktab)) { pr_error("bad token %d\n", tok.token); return -1; } + + /* Log the token */ printf("tok.type: %s\n", stoktab[tok.token]); + if (tok.token == TT_NUMBER) { + printf("tok.val: %d\n", tok.val); + } } return 0; |