summaryrefslogtreecommitdiff
path: root/ast.c
blob: 07c0f341d5c7dde8e2dd8050b82ca3e5d4b0e98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "ast.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>

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 ++;
}

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()
		}
	};
}