-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
95 lines (86 loc) · 1.88 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdlib.h>
#include <assert.h>
#include "ast.h"
extern int yynewlines;
AST *newNode(NodeType t, int l) {
AST *p;
p = (AST *)malloc(sizeof(AST));
p -> child = NULL;
p -> sym = t;
p -> brother = NULL;
p -> lineno = l;
p -> reloadType = NA;
return p;
}
AST *appendChild(AST *parent, AST * childNd) {
assert(parent != NULL && childNd != NULL);
AST *ptr;
if (parent -> child == NULL) {
parent -> child = childNd;
return parent;
}
ptr = parent -> child;
while (ptr -> brother != NULL) {
ptr = ptr -> brother;
}
ptr -> brother = childNd;
return parent;
}
AST *insertChild(AST *parent, AST * childNd) {
assert(parent != NULL && childNd != NULL);
AST *ptr;
ptr = childNd;
while (ptr -> brother != NULL) {
ptr = ptr -> brother;
}
ptr -> brother = parent -> child;
parent -> child = childNd;
return parent;
}
AST *appendBrother(AST *leftNode, AST *tobeappend) {
AST *ptr;
ptr = tobeappend;;
assert(leftNode != NULL && tobeappend != NULL);
while (ptr -> brother != NULL) {
ptr = ptr -> brother;
}
ptr -> brother = leftNode -> brother;
leftNode -> brother = tobeappend;
return leftNode;
}
AST *appendBrotherAtLast(AST *leftNode, AST *tobeappend) {
AST *ptr;
assert(leftNode != NULL && tobeappend != NULL);
ptr = leftNode;
while( ptr -> brother != NULL) {
ptr = ptr -> brother;
}
tobeappend -> brother = ptr -> brother;
ptr -> brother = tobeappend;
return leftNode;
}
AST *newStrNode(char *va) {
AST *node;
node = newNode(ASTN_L_string,yynewlines);
node -> value.str = va;
return node;
}
AST *newIntNode(int va) {
AST *node;
node = newNode(ASTN_L_int,yynewlines);
node -> value.num = va;
return node;
}
AST *newBoolNode(BOOL va) {
AST *node;
node = newNode(ASTN_L_bool,yynewlines);
node -> value.b = va;
return node;
}
AST *newIdNode(char *va) {
AST *node;
node = newNode(ASTN_L_id,yynewlines);
node -> value.str = va;
return node;
}
AST ASTroot;