-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
51 lines (41 loc) · 1.13 KB
/
ast.c
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
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include "compiler.h"
#include "ast.h"
Node *progNode(int id, Token *tok, InstructionType type, ReturnType ret) {
Node *n = malloc(sizeof(Node));
n->type = type;
n->ret = ret;
n->children_index = 0;
n->children_capacity = 20;
n->memory_address = -1;
n->id = id;
n->tok = tok;
n->children = malloc(sizeof(Node *) * n->children_capacity);
return n;
}
Node *progEmptyNode(int id, InstructionType type, ReturnType ret) {
Node *n = malloc(sizeof(Node));
n->children_index = 0;
n->children_capacity = 20;
n->id = id;
n->type = type;
n->ret = ret;
n->tok = malloc(sizeof(Token));
n->tok->str = "Group";
n->tok->line = 0;
n->tok->column = 0;
n->tok->size = 0;
n->tok->filename = "";
n->tok->lexCode = 0;
n->memory_address = -1;
n->children = malloc(sizeof(Node *) * n->children_capacity);
return n;
}
void progAddChild(Node *n, Node *child) {
if (n->children_capacity >= n->children_index + 1) {
n->children_capacity += 10;
n->children = realloc(n->children, sizeof(Node *) * n->children_capacity);
}
n->children[n->children_index++] = child;
}