diff options
| -rw-r--r-- | main.c | 36 |
1 files changed, 14 insertions, 22 deletions
@@ -17,18 +17,12 @@ int no_opt = 0; typedef struct { - DYNARR(Proc) procs; + Arena *arena; + XAR(Proc) procs; } Unit; -void unit_init(Unit *u) { - (void)u; -} - -void unit_free(Unit *u) { - for (int i = 0; i < u->procs.len; i++) { - proc_free(&u->procs.data[i]); - } - free(u->procs.data); +Proc *unit_new_proc(Unit *u) { + return XAR_PUT(&u->procs, u->procs.n++, u->arena); } /* parsing */ @@ -423,8 +417,7 @@ void proc_opt(Proc *p, Lexer *l) { Proc *parse_proc(Lexer *l, Unit *u) { int has_ret = l->tok == TOK_FUNC; - DA_FIT(&u->procs, u->procs.len + 1); - Proc *proc = &u->procs.data[u->procs.len++]; + Proc *proc = unit_new_proc(u); lex_expect(l, TM_IDENT); proc_init(proc, l->ident); scope_push(&proc->scope); @@ -576,16 +569,11 @@ void parse_toplevel(Lexer *l, Unit *u) { } } - void unit_print(Unit *u); -void parse_unit(Lexer *l) { - Unit u = { 0 }; - unit_init(&u); +void parse_unit(Lexer *l, Unit *u) { while (l->tok != TOK_EOF) { - parse_toplevel(l, &u); + parse_toplevel(l, u); } - unit_print(&u); - unit_free(&u); } /* graph output */ @@ -671,8 +659,8 @@ void proc_print(Proc *p) { void unit_print(Unit *u) { puts("digraph {"); - for (int i = 0; i < u->procs.len; i++) { - proc_print(&u->procs.data[i]); + for (unsigned i = 0; i < u->procs.n; i++) { + proc_print(XAR_GET(&u->procs, i)); } puts("}"); } @@ -680,13 +668,17 @@ void unit_print(Unit *u) { /* main */ int main(int argc, const char **argv) { + Arena perm = { 0 }; if (argc != 2) { fprintf(stderr, "Usage: %s FILE\n", argv[0]); return 1; } Lexer l = { 0 }; + Unit u = { .arena = &perm }; lex_start(&l, argv[1]); - parse_unit(&l); + parse_unit(&l, &u); + unit_print(&u); lex_free(&l); + fprintf(stderr, "%zu\n", sizeof(Node)); return 0; } |
