summaryrefslogtreecommitdiff
path: root/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'ast.c')
-rw-r--r--ast.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/ast.c b/ast.c
index fda03d3..b6fcbcf 100644
--- a/ast.c
+++ b/ast.c
@@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
+#include <string.h>
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: