-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.c
More file actions
232 lines (219 loc) · 6.73 KB
/
util.c
File metadata and controls
232 lines (219 loc) · 6.73 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
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
/********************************************/
/* File: util.c */
/* Utility function implementation */
/* for the C- compiler */
/* tallate */
/********************************************/
#include "globals.h"
#include "util.h"
/* 打印一个token及其lexeme到listing文件
*/
void printToken(TokenType token, const char *tokenString) {
switch(token) {
/* book-keeping tokens */
case ENDFILE: fprintf(listing, "EOF\n"); break;
case ERROR: fprintf(listing, "ERROR: %s\n", tokenString); break;
/* reserved words */
case IF:
case ELSE:
case INT:
case RETURN:
case VOID:
case WHILE:
fprintf(listing, "reserved word: %s\n", tokenString);
break;
/* special symbols */
case PLUS: fprintf(listing, "+\n"); break;
case MINUS: fprintf(listing, "-\n"); break;
case TIMES: fprintf(listing, "*\n"); break;
case OVER: fprintf(listing, "/\n"); break;
case LT: fprintf(listing, "<\n"); break;
case LTET: fprintf(listing, "<=\n"); break;
case MT: fprintf(listing, ">\n"); break;
case MTET: fprintf(listing, ">=\n"); break;
case EQ: fprintf(listing, "==\n"); break;
case NEQ: fprintf(listing, "!=\n"); break;
case ASIGN: fprintf(listing, "=\n"); break;
case SEMI: fprintf(listing, ";\n"); break;
case COMM: fprintf(listing, ",\n"); break;
case LPAREN: fprintf(listing, "(\n"); break;
case RPAREN: fprintf(listing, ")\n"); break;
case LSPAREN: fprintf(listing, "[\n"); break;
case RSPAREN: fprintf(listing, "]\n"); break;
case LCPAREN: fprintf(listing, "{\n"); break;
case RCPAREN: fprintf(listing, "}\n"); break;
/* others */
case ID:
fprintf(listing, "ID, name = %s\n", tokenString);
break;
case NUM:
fprintf(listing, "NUM, val = %s\n", tokenString);
break;
default:
fprintf(listing, "Unknown token: %d\n", token);
}
}
/* newDeclNode creates a new declaration
* node for the syntax tree construction
*/
TreeNode *newDeclNode(DeclKind kind) {
TreeNode *node = (TreeNode*) malloc(sizeof(TreeNode));
int i;
if(node == NULL) {
fprintf(listing, "Out of memory error at line %d\n", lineno);
} else {
/* 对节点初始化 */
for(i = 0; i < MAXCHILDREN; i++) {
node->child[i] = NULL;
}
node->sibling = NULL;
node->nodeKind = DeclK;
node->kind.decl = kind;
node->scope = NULL;
node->lineno = lineno;
node->funcEntryLoc = 0;
}
return node;
}
TreeNode *newStmtNode(StmtKind kind) {
TreeNode *node = (TreeNode*) malloc(sizeof(TreeNode));
int i;
if(node == NULL) {
fprintf(listing, "Out of memory error at line %d\n", lineno);
} else {
for(i = 0; i < MAXCHILDREN; i++) {
node->child[i] = NULL;
}
node->sibling = NULL;
node->nodeKind = StmtK;
node->kind.stmt = kind;
node->scope = NULL;
node->lineno = lineno;
node->funcEntryLoc = 0;
}
return node;
}
TreeNode *newExpNode(ExpKind kind) {
TreeNode *node = (TreeNode*) malloc(sizeof(TreeNode));
int i;
if(node == NULL) {
fprintf(listing, "Out of memory error at line %d\n", lineno);
} else {
for(i = 0; i < MAXCHILDREN; i++) {
node->child[i] = NULL;
}
node->sibling = NULL;
node->nodeKind = ExpK;
node->kind.exp = kind;
node->scope = NULL;
node->lineno = lineno;
node->funcEntryLoc = 0;
}
}
/* copyString 拷贝一个字符串并为其分配空间
*/
char *copyString(char *s) {
if(s == NULL) { return NULL; }
char *newString = NULL;
newString = malloc(strlen(s) + 1);
if(newString == NULL) {
fprintf(listing, "Out of memory error at line %dn", lineno);
} else {
strcpy(newString, s);
}
return newString;
}
/* indentno 标识当前缩进空间
*/
static int indentno = 0;
#define INDENT (indentno += 2)
#define UNINDENT (indentno -= 2)
/* printSpace 通过打印空格来缩进
*/
static void printSpace(void) {
int i;
for(i = 0; i < indentno; i++) {
fprintf(listing, " ");
}
}
/* printTree 打印一棵syntax tree到listing file
* 适当的缩进代表子树
*/
void printTree(TreeNode *tree) {
INDENT;
while(tree != NULL) {
printSpace();
if(tree->nodeKind == DeclK) {
switch(tree->kind.decl) {
case VarD:
fprintf(listing, "Var: %s\n", tree->attr.name);
break;
case ArrD:
fprintf(listing, "Array: %s\n", tree->attr.name);
break;
case FunD:
fprintf(listing, "Fun: %s\n", tree->attr.name);
break;
default:
fprintf(listing, "Unknown DeclNode kind\n");
break;
}
} else if(tree->nodeKind == StmtK) {
switch(tree->kind.stmt) {
case ExpS:
fprintf(listing, "Expression\n");
break;
case ComS:
fprintf(listing, "Compound\n");
break;
case SelS:
fprintf(listing, "Select\n");
break;
case IteS:
fprintf(listing, "Iteration\n");
break;
case RetS:
fprintf(listing, "Return\n");
break;
default:
fprintf(listing, "Unknown StmtNode kind\n");
break;
}
} else if(tree->nodeKind == ExpK) {
switch(tree->kind.exp) {
case AssignE:
fprintf(listing, "Assign: ");
printToken(tree->attr.op, "\0");
break;
case OpE:
fprintf(listing, "Op: ");
printToken(tree->attr.op, "\0");
break;
case ConstE:
fprintf(listing, "Const: %d\n", tree->attr.val);
break;
case VarE:
fprintf(listing, "Var: %s\n", tree->attr.name);
break;
case ArrE:
fprintf(listing, "Array: %s\n", tree->attr.name);
break;
case CallE:
fprintf(listing, "Call: %s\n", tree->attr.name);
break;
default:
fprintf(listing, "Unknown ExpNode kind\n");
break;
}
} else {
fprintf(listing, "Unknown node kind\n");
}
/* 递归调用child和sibling */
int i;
for(i = 0; i < MAXCHILDREN; i++) {
printTree(tree->child[i]);
}
tree = tree->sibling;
}
UNINDENT;
}