From cbdb9073480c3e4d8a088b306c23406cf42e25c0 Mon Sep 17 00:00:00 2001 From: hachem Date: Mon, 31 Aug 2026 17:55:16 +0200 Subject: feat: hex/binary integer literals and constant-folding const expressions --- src/parser/ast.c | 2 ++ src/parser/ast.h | 2 +- src/parser/parser.c | 6 ++++-- 3 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/parser') diff --git a/src/parser/ast.c b/src/parser/ast.c index f9f857d..704e9a4 100644 --- a/src/parser/ast.c +++ b/src/parser/ast.c @@ -78,6 +78,8 @@ struct Program create_program(void) void free_program(struct Program* program) { + for (size_t i = 0; i < program->const_count; i += 1) + free_expr(program->consts[i].value); free(program->consts); free(program->data_decls); diff --git a/src/parser/ast.h b/src/parser/ast.h index fc7af2b..98e8dea 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -9,7 +9,7 @@ struct ConstDecl { struct Token name; - struct Token value; + struct Expr* value; }; struct DataDecl diff --git a/src/parser/parser.c b/src/parser/parser.c index 969ed86..87dedbd 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -52,6 +52,8 @@ static bool consume(struct Parser* parser, enum TokenType type, const char* mess return false; } +static struct Expr* parse_expression(struct Parser* parser); + static bool parse_const(struct Parser* parser, struct Program* program) { struct ConstDecl decl; @@ -63,9 +65,9 @@ static bool parse_const(struct Parser* parser, struct Program* program) if (!consume(parser, TOKEN_EQUAL, "expected '=' after constant name")) return false; - if (!consume(parser, TOKEN_INTEGER, "expected integer value after '='")) + decl.value = parse_expression(parser); + if (decl.value == NULL) return false; - decl.value = parser->previous; add_const(program, decl); return true; -- cgit v1.3