From 2e7cab34d22a0174909030942671e95d5a3c769f Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 30 Aug 2026 03:38:46 +0200 Subject: add: top level data declarations --- src/parser/ast.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/parser/ast.c') diff --git a/src/parser/ast.c b/src/parser/ast.c index 979dff0..3f360cc 100644 --- a/src/parser/ast.c +++ b/src/parser/ast.c @@ -8,17 +8,25 @@ struct Program create_program(void) program.consts = NULL; program.const_count = 0; program.const_capacity = 0; + program.data_decls = NULL; + program.data_count = 0; + program.data_capacity = 0; return program; } void free_program(struct Program* program) { free(program->consts); + free(program->data_decls); program->consts = NULL; program->const_count = 0; program->const_capacity = 0; + program->data_decls = NULL; + program->data_count = 0; + program->data_capacity = 0; } +// TODO: generalize this growable-array boilerplate once a third list appears void add_const(struct Program* program, struct ConstDecl decl) { if (program->const_count == program->const_capacity) @@ -31,3 +39,16 @@ void add_const(struct Program* program, struct ConstDecl decl) program->consts[program->const_count] = decl; program->const_count += 1; } + +void add_data(struct Program* program, struct DataDecl decl) +{ + if (program->data_count == program->data_capacity) + { + size_t capacity = program->data_capacity < 8 ? 8 : program->data_capacity * 2; + program->data_decls = realloc(program->data_decls, capacity * sizeof(struct DataDecl)); + program->data_capacity = capacity; + } + + program->data_decls[program->data_count] = decl; + program->data_count += 1; +} -- cgit v1.3