-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.c
More file actions
178 lines (139 loc) · 3.94 KB
/
ast.c
File metadata and controls
178 lines (139 loc) · 3.94 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "ast.h"
int G_iVarMaxIndex = 0; /* Maximum number of Varblies */
int G_iVarCurIndex = -1; /* Current index of the Variable */
Node *set_int(int value) {
Node *p;
size_t sizeNode;
/* Allocate the space for tree node */
sizeNode = sizeof(Node);
if((p = (Node *)malloc(sizeNode)) == NULL){
yyerror("out of memory");
return NULL;
}
p->kind = TYPE_CONST;
p->type = TYPE_NUM;
p->numI = value;
return p;
}
Node *set_float(float value){
Node *p;
size_t sizeNode;
/* Allocate the space for tree node */
sizeNode = sizeof(Node);
if((p = (Node *)malloc(sizeNode)) == NULL){
yyerror("out of memory");
return NULL;
}
p->kind = TYPE_CONST;
p->type = TYPE_FLOAT;
p->numF = value;
return p;
}
Node *set_var(int value) {
Node *p;
size_t sizeNode;
/* Allocate the space for tree node */
sizeNode = sizeof(Node);
if((p = (Node *)malloc(sizeNode)) == NULL){
yyerror("out of memory");
return NULL;
}
p->kind = TYPE_VAR;
p->index = value;
return p;
}
/* Generate a terminal node */
Node *set_terminal(int value){
Node *p;
size_t sizeNode;
/* Allocate the space for tree node */
sizeNode = sizeof(Node);
if((p = (Node *)malloc(sizeNode)) == NULL){
yyerror("out of memory");
return NULL;
}
p->kind = TYPE_TERMINAL;
p->index = value;
return p;
}
/* Generate a non-terminal node */
Node *set_vn(char* name, int num, ...) {
va_list valist;
Node *p;
size_t sizeNode;
int i;
/* Allocate the space for tree node */
sizeNode = sizeof(Node);
if((p = (Node *)malloc(sizeNode)) == NULL){
yyerror("out of memory");
return NULL;
}
p->kind = TYPE_UNTREMINAL;
strcpy(p->vn.name, name);
p->vn.num = num;
va_start(valist, num);
if((p->vn.node = (Node **)malloc(num * sizeof(Node *))) == NULL){
yyerror("out of memory");
return NULL;
}
for(i = 0; i < num; i++)
p->vn.node[i] = va_arg(valist, Node*);
va_end(valist);
if(num == 1){
p->index = p->vn.node[0]->index;
p->type = p->vn.node[0]->type;
}
return p;
}
void add_var(char *mark){
strcpy(G_Var[G_iVarMaxIndex].mark, mark);
G_iVarCurIndex = G_iVarMaxIndex;
G_iVarMaxIndex++;
}
/* Free the space */
void NodeFree(Node *p){
int i;
if(!p)
return;
if(p->kind == TYPE_UNTREMINAL){
for(i = 0; i < p->vn.num; i++){
NodeFree(p->vn.node[i]);
p->vn.node[i] = NULL;
}
p->vn.node = NULL;
}
free(p);
p = NULL;
}
void tabprint(FILE* target_file, int num){
int i = 0;
for(i=0; i<num; i++){
fprintf(target_file, "\t");
}
}
/* Write the syntax tree to file */
int NodeExecute(FILE* target_file, Node *p, int num){
int i = 0;
if(p == NULL)
return 0;
switch(p->kind){
case TYPE_CONST: tabprint(target_file, num);
if(p->type == TYPE_FLOAT)
fprintf(target_file, "FLOAT_NUM: %.3f\n", p->numF);
else
fprintf(target_file, "INT_NUM: %d\n", p->numI);
return 0;
case TYPE_VAR: tabprint(target_file, num);
fprintf(target_file, "Iden: %s\n", G_Var[p->index].mark);
return 0;
case TYPE_TERMINAL: tabprint(target_file, num);
fprintf(target_file, "%s\n", G_Def[p->index].name);
return 0;
case TYPE_UNTREMINAL: tabprint(target_file, num);
fprintf(target_file, "%s\n", p->vn.name);
for(i=0; i<(p->vn.num); i++)
NodeExecute(target_file, p->vn.node[i], num+1);
return 0;
default: return 0;
}
}