aboutsummaryrefslogtreecommitdiff
path: root/src/parser/ast.c
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-30 04:11:27 +0200
committerhachem <im@hachem.wtf>2026-08-30 04:11:27 +0200
commitaffa20ab497911353babc9ed6c580c8a12b2bf15 (patch)
tree6d7c8d2164cbf1403819991baf62d8201049b778 /src/parser/ast.c
parent2e7cab34d22a0174909030942671e95d5a3c769f (diff)
add: parse proc declarations
Diffstat (limited to 'src/parser/ast.c')
-rw-r--r--src/parser/ast.c46
1 files changed, 46 insertions, 0 deletions
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;
+}