-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSyntaxTree.c
312 lines (310 loc) · 9.27 KB
/
SyntaxTree.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SyntaxTree.h"
/* new tree node */
TreeNode *CreateNode(NodeKind kind){
TreeNode *p = (TreeNode *)malloc(sizeof(TreeNode));
p->children[0] = p->children[1] = p->children[2] = p->children[3] = p->sibling = 0;
p->node = kind;
p->label = 0;
p->system = 0;
p->lineNo = lineNo;
p->dtype = NULL;
return p;
}
/*=============================universal over=================================*/
/*=============================header=========================================*/
/*=============================routine=========================================*/
TreeNode *CreateRoutine(TreeNode* routine_head, TreeNode* routine_body){
TreeNode *p = CreateNode(routine_kind);
p->children[0] = routine_head;
p->children[1] = routine_body;
return p;
}
/*=============================const_part=========================================*/
/* const part */
TreeNode *CreateConst(char* ConstName, TreeNode* ConstValue){
TreeNode *p = CreateNode(const_kind);
p->children[0] = ConstValue;
strcpy(p->name,ConstName);
return p;
}
/*=============================var_part=========================================*/
/* var part */
TreeNode *CreateVar(TreeNode* VarNameList, TreeNode* TypeDecl){
TreeNode *p = CreateNode(var_kind);
p->children[0] = VarNameList;
p->dtype = TypeDecl;
return p;
}
/*=============================type_part=========================================*/
/* type part */
TreeNode *CreateSimpleType(TypeKind type){
TreeNode *p = CreateNode(type_kind);
p->type = type;
return p;
}
TreeNode *CreateEnumType(TreeNode *exp1, TreeNode *exp2){
TreeNode *p = CreateNode(type_kind);
p->children[0] = exp1; p->children[1] = exp2;
p->type = enum_type;
return p;
}
TreeNode *CreateArrayType(TreeNode *enumT, TreeNode *baseT){
TreeNode *p = CreateNode(type_kind);
p->children[0] = enumT; p->children[1] = baseT;
p->type = array_type;
return p;
}
TreeNode *CreateRecordType(TreeNode *domain){
TreeNode *p = CreateNode(type_kind);
p->children[0] = domain;
p->type = record_type;
return p;
}
/*
TreeNode *CreateDomainType(TreeNode *namelist, TreeNode *type){
TreeNode *p = CreateNode(type_kind);
p->children[0] = namelist; p->children[1] = type;
p->type = record_domain_type;
return p;
}
*/
TreeNode *CreateSelfDefineType(char *name){
TreeNode *p = CreateNode(type_kind);
p->type = selfdefined_type;
strcpy(p->name, name);
return p;
}
TreeNode *CreateTypeDef(char *TypeName, TreeNode *type){
TreeNode *p = CreateNode(type_kind);
p->type = decl_type;
p->dtype = type;
strcpy(p->name, TypeName);
return p;
}
/*=============================routine_part=========================================*/
/* function part */
TreeNode *CreateFuncHead(char *name, TreeNode *param, TreeNode *type){
TreeNode *p = CreateNode(sub_kind);
p->children[0] = param; p->dtype = type;
p->sub = func_kind;
strcpy(p->name, name);
return p;
}
/* procedure part */
TreeNode *CreateProcHead(char *name, TreeNode *param){
TreeNode *p = CreateNode(sub_kind);
p->children[0] = param;
p->sub = proc_kind;
strcpy(p->name, name);
return p;
}
/* param part */
TreeNode *CreateParam(TreeNode *list, TreeNode *type, SubKind k){
TreeNode *p = CreateNode(sub_kind);
p->children[0] = list; p->dtype = type;
p->sub = k;
return p;
}
/*=============================header over====================================*/
/* Init a const exp node */
TreeNode *CreateConstExp(TypeKind type){
TreeNode *p = CreateNode(expr_kind);
p->dtype = CreateSimpleType(type);
p->expr = con_kind;
return p;
}
/* Init an identifier exp node */
TreeNode *CreateIdExp(char *name){
TreeNode *p = CreateNode(expr_kind);
p->expr = id_kind;
p->id = Basic;
strcpy(p->name, name);
return p;
}
TreeNode *CreateIdArrayExp(char *name, TreeNode *exp){
TreeNode *p = CreateNode(expr_kind);
p->children[0] = exp;
p->expr = id_kind;
p->id = Array;
strcpy(p->name, name);
return p;
}
TreeNode *CreateIdRecordExp(char *name, char *domain){
TreeNode *p = CreateNode(expr_kind);
p->children[0] = CreateIdExp(domain);
p->expr = id_kind;
p->id = Record;
strcpy(p->name, name);
return p;
}
/* Init an Operation exp node */
TreeNode *CreateOpExp(OpKind op){
TreeNode *p = CreateNode(expr_kind);
p->expr = op_kind;
p->op = op;
return p;
}
/* Init a function exp node */
TreeNode *CreateFuncExp(char *name, TreeNode *args){
TreeNode *p = CreateNode(expr_kind);
p->children[0] = args;
p->expr = fn_kind;
strcpy(p->name, name);
return p;
}
/*===========================expr over==========================================*/
/* Init a stmt node */
TreeNode *CreateStmtNode(StmtKind stmt){
TreeNode *p = CreateNode(stmt_kind);
p->stmt = stmt;
return p;
}
/*==========================stmt over===========================================*/
/* Connect nodes */
TreeNode *ConnectNodes(TreeNode *p, TreeNode*q){
TreeNode * ret;
if (!p) return q;
ret = p;
while (p->sibling!=NULL) p = p->sibling;
p->sibling = q;
return ret;
}
/* extract string content */
void strCatch(char *d, char *s){
int i;
for (i=1; i<strlen(s)-1; i++){
d[i-1] = s[i];
}
d[strlen(s)-1] = 0;
}
TreeNode *makeLabel(TreeNode *p, int label){
p->label = label;
return p;
}
/* print nodes to debug */
void print_const(TreeNode *p){
fprintf(fout, "const: %s :", p->name);
}
void print_type(TreeNode *p){
switch (p->type){
case decl_type: fprintf(fout, "type of %s is:", p->name); break;
case integer_type: fprintf(fout, ": integer"); break;
case real_type: fprintf(fout, ": real"); break;
case boolean_type: fprintf(fout, ": boolean"); break;
case char_type: fprintf(fout, ": char"); break;
case string_type: fprintf(fout, ": string"); break;
case enum_type: {
fprintf(fout, ": enum:");
if (!p->children[1]) fprintf(fout, "names:"); else fprintf(fout, "exprs");
break;
}
case array_type: fprintf(fout, ": array"); break;
case record_type: fprintf(fout, ": record"); break;
//case record_domain_type: fprintf(fout, "type: record_domain"); break;
case selfdefined_type: fprintf(fout, ": %s", p->name); break;
}
}
void print_var(TreeNode *p){
fprintf(fout, "var:");
}
void print_expr_op(TreeNode *p){
fprintf(fout, "Op_expr:");
switch (p->op){
case plus_kind: fprintf(fout, "plus"); break;
case minus_kind: fprintf(fout, "minus"); break;
case or_kind: fprintf(fout, "or"); break;
case mul_kind: fprintf(fout, "mul"); break;
case div_kind: fprintf(fout, "div"); break;
case mod_kind: fprintf(fout, "mod"); break;
case and_kind: fprintf(fout, "and"); break;
case ge_kind: fprintf(fout, "ge"); break;
case gt_kind: fprintf(fout, "gt"); break;
case le_kind: fprintf(fout, "le"); break;
case lt_kind: fprintf(fout, "lt"); break;
case eq_kind: fprintf(fout, "eq"); break;
case ueq_kind: fprintf(fout, "ueq"); break;
case not_kind: fprintf(fout, "not"); break;
case neg_kind: fprintf(fout, "neg"); break;
}
}
void print_expr_id(TreeNode *p){
switch (p->id){
case Basic: fprintf(fout, "Id_expr_basic: %s", p->name); break;
case Array: fprintf(fout, "Id_expr_array: %s", p->name); break;
case Record: fprintf(fout, "Id_expr_record: %s", p->name); break;
}
}
void print_expr_fn(TreeNode *p){
fprintf(fout, "Fn_expr: %s, args:", p->name);
}
void print_expr_con(TreeNode *p){
fprintf(fout, "const_expr: ");
}
void print_expr(TreeNode *p){
switch (p->expr){
case op_kind: {
print_expr_op(p);
if(p->dtype)
print_type(p->dtype);
} break;
case id_kind: {
print_expr_id(p);
if(p->dtype)
print_type(p->dtype);
} break;
case fn_kind: {
print_expr_fn(p);
if(p->dtype)
print_type(p->dtype);
} break;
case con_kind: {
print_expr_con(p);
if(p->dtype)
print_type(p->dtype);
} break;
}
}
void print_stmt(TreeNode *p){
if (p->label) fprintf(fout, "label:%d;", p->label);
switch (p->stmt){
case if_stmt: { fprintf(fout, "if_stmt:"); } break;
case for_stmt: { fprintf(fout, "for:%s", p->name); if (p->attr.intVal) fprintf(fout, " to: "); else fprintf(fout, "downto"); } break;
case repeat_stmt: { fprintf(fout, "repeat_stmt:"); } break;
case while_stmt: { fprintf(fout, "while_stmt:"); } break;
case goto_stmt: { fprintf(fout, "goto: %d", p->attr.intVal); } break;
case proc_stmt: { fprintf(fout, "procedure name:%s", p->name); } break;
case assign_stmt: { fprintf(fout, "assign_stmt:"); } break;
case case_stmt: { fprintf(fout, "case_stmt:"); } break;
case case_exp_stmt: { fprintf(fout, "case:"); } break;
}
}
void print_sub(TreeNode *p){
switch (p->sub){
case func_kind: { fprintf(fout, "function %s:", p->name); } break;
case proc_kind: { fprintf(fout, "procedure %s:", p->name); } break;
case param_var_kind: { fprintf(fout, "var param:"); } break;
case param_val_kind: { fprintf(fout, "val param:"); } break;
}
}
void print_tree(TreeNode *p, int level){
int i;
if (!p) return;
else fprintf(fout, "\n%d\t", p->lineNo);
for (i=0; i<level; i++) fprintf(fout, " ");
fprintf(fout, "|-");
switch (p->node){
case routine_kind: { fprintf(fout, "routine:"); } break;
case const_kind: { print_const(p); } break;
case type_kind: { print_type(p); } break;
case var_kind: { print_var(p); } break;
case expr_kind: { print_expr(p); } break;
case stmt_kind: { print_stmt(p); } break;
case sub_kind: { print_sub(p); } break;
}
print_tree(p->children[0], level+1); print_tree(p->children[1], level+1);
print_tree(p->children[2], level+1); print_tree(p->children[3], level+1);
print_tree(p->sibling, level);
}