From 63206b605c36140f2bd476e054b8318c8b08f472 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 26 Jun 2024 12:45:41 +0100 Subject: support multiple input filenames --- ast.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'ast.c') diff --git a/ast.c b/ast.c index fda03d3..b6fcbcf 100644 --- a/ast.c +++ b/ast.c @@ -3,6 +3,7 @@ #include #include #include +#include AstVec astvec_new() { AstNode *vals = malloc(2 * sizeof(AstNode)); @@ -48,24 +49,44 @@ AstNode astnode_new_list() { }; } -AstNode astnode_new_ident(char *s) { +AstNode astnode_new_ident(const char *s) { return (AstNode){ .ty =AST_IDENT, .as = { - .str = s + .str = strdup(s) } }; } -AstNode astnode_new_string(char *s) { +AstNode astnode_new_string(const char *s) { return (AstNode){ .ty =AST_STRING, .as = { - .str = s + .str = strdup(s) } }; } +void astnode_free(AstNode *a) { + switch (a->ty) { + case AST_NUM: break; + case AST_IDENT: + case AST_STRING: + free(a->as.str); + break; + case AST_LIST:; + AstVec *v = &a->as.list; + for (int i = 0; i < v->len; i++) { + astnode_free(&v->vals[i]); + } + free(v->vals); + v->vals = NULL; + v->len = 0; + v->cap = 0; + break; + } +} + void astnode_disp(AstNode *a) { switch (a->ty) { case AST_NUM: -- cgit v1.2.3