-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.c
More file actions
32 lines (28 loc) · 795 Bytes
/
ast.c
File metadata and controls
32 lines (28 loc) · 795 Bytes
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
#include "ast.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Node *node_new(NodeKind k, const char *value) {
Node *n = calloc(1, sizeof(Node));
if (!n) { perror("calloc"); return NULL; }
n->kind = k;
n->value = value ? strdup(value) : NULL;
return n;
}
void node_add(Node *parent, Node *child) {
parent->children = realloc(parent->children, sizeof(Node*)*(parent->n_children+1));
if (!parent->children) { perror("realloc"); return; }
parent->children[parent->n_children++] = child;
}
void node_free(Node *n) {
if (!n) return;
for (size_t i=0;i<n->n_children;i++){
node_free(n->children[i]);
}
free(n->children);
n->children = NULL;
free(n->value);
n->value = NULL;
free(n);
n = NULL;
}