#include "ast.h" #include #include #include #include AstVec astvec_new() { AstNode *vals = malloc(2 * sizeof(AstNode)); return (AstVec){ .len = 0, .cap = 2, vals = vals }; } void astvec_append(AstVec *v, AstNode val) { if (v->len == v->cap) { size_t newcap = v->cap * 2; // printf("%ld to %ld\n", v->cap, newcap); v->vals = realloc(v->vals, newcap * sizeof(AstNode)); v->cap = newcap; } v->vals[v->len] = val; v->len ++; } void astnode_append(AstNode *l, AstNode val) { // printf(" astnode_append: %d\n",l->ty); assert(l->ty == AST_LIST); astvec_append(&l->as.list, val); } AstNode astnode_new_num(int n) { return (AstNode){ .ty = AST_NUM, .as = { .num = n, }, }; } AstNode astnode_new_list() { return (AstNode){ .ty = AST_LIST, .as = { .list = astvec_new() } }; } AstNode astnode_new_symbol(char *s) { return (AstNode){ .ty =AST_SYMBOL, .as = { .str = s } }; } void astnode_disp(AstNode *a) { switch (a->ty) { case AST_NUM: printf("n:%d ",a->as.num); break; case AST_LIST:; AstVec *v = &a->as.list; printf("l:("); for (int i = 0; i < v->len; i++) { astnode_disp(&v->vals[i]); } printf(")"); break; case AST_SYMBOL:; printf("s:%s ",a->as.str); break; } }