From affa20ab497911353babc9ed6c580c8a12b2bf15 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 30 Aug 2026 04:11:27 +0200 Subject: add: parse proc declarations --- src/parser/ast.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/parser/ast.c') diff --git a/src/parser/ast.c b/src/parser/ast.c index 3f360cc..b002ff2 100644 --- a/src/parser/ast.c +++ b/src/parser/ast.c @@ -11,6 +11,9 @@ struct Program create_program(void) program.data_decls = NULL; program.data_count = 0; program.data_capacity = 0; + program.procs = NULL; + program.proc_count = 0; + program.proc_capacity = 0; return program; } @@ -18,12 +21,20 @@ void free_program(struct Program* program) { free(program->consts); free(program->data_decls); + + for (size_t i = 0; i < program->proc_count; i += 1) + free(program->procs[i].params); + free(program->procs); + program->consts = NULL; program->const_count = 0; program->const_capacity = 0; program->data_decls = NULL; program->data_count = 0; program->data_capacity = 0; + program->procs = NULL; + program->proc_count = 0; + program->proc_capacity = 0; } // TODO: generalize this growable-array boilerplate once a third list appears @@ -52,3 +63,38 @@ void add_data(struct Program* program, struct DataDecl decl) program->data_decls[program->data_count] = decl; program->data_count += 1; } + +struct ProcDecl create_proc(void) +{ + struct ProcDecl proc; + proc.params = NULL; + proc.param_count = 0; + proc.param_capacity = 0; + return proc; +} + +void add_param(struct ProcDecl* proc, struct Param param) +{ + if (proc->param_count == proc->param_capacity) + { + size_t capacity = proc->param_capacity < 4 ? 4 : proc->param_capacity * 2; + proc->params = realloc(proc->params, capacity * sizeof(struct Param)); + proc->param_capacity = capacity; + } + + proc->params[proc->param_count] = param; + proc->param_count += 1; +} + +void add_proc(struct Program* program, struct ProcDecl decl) +{ + if (program->proc_count == program->proc_capacity) + { + size_t capacity = program->proc_capacity < 8 ? 8 : program->proc_capacity * 2; + program->procs = realloc(program->procs, capacity * sizeof(struct ProcDecl)); + program->proc_capacity = capacity; + } + + program->procs[program->proc_count] = decl; + program->proc_count += 1; +} -- cgit v1.3