nooc

nooc programming language compiler
git clone git://git.nihaljere.xyz/nooc
Log | Files | Refs | LICENSE

commit 227eff85dc68dbd4941a1c5771172aca98654604
parent 5c8ed5dbe722b706e462c26396a9b3484ec15234
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sun, 26 Dec 2021 22:48:00 -0600

parse: extract number parsing into parsenum

Diffstat:
Mparse.c | 28+++++++++++++++++++++++-----
1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/parse.c b/parse.c @@ -1,3 +1,4 @@ +#include <errno.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> @@ -87,6 +88,27 @@ parsestring(struct expr *expr) tok = tok->next; } +static void +parsenum(struct expr *expr) +{ + expr->start = tok; + expr->kind = EXPR_LIT; + expr->class = C_INT; + + errno = 0; + if (sizeof(long) == 8) + expr->d.v.v.i64 = strtol(tok->slice.data, NULL, 10); + else if (sizeof(long long) == 8) + expr->d.v.v.i64 = strtoll(tok->slice.data, NULL, 10); + else + die("parsenum: unhandled long size"); + + if (errno) + error(tok->line, tok->col, "failed to parse number"); + + tok = tok->next; +} + enum class typetoclass(struct type *type) { @@ -212,11 +234,7 @@ parseexpr(struct block *block) } break; case TOK_NUM: - expr.kind = EXPR_LIT; - expr.class = C_INT; - // FIXME: error check - expr.d.v.v.i64 = strtol(tok->slice.data, NULL, 10); - tok = tok->next; + parsenum(&expr); break; case TOK_EQUAL: expr.kind = EXPR_BINARY;