-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
195 lines (175 loc) · 3.86 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
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
#ifndef __AST_C
#define __AST_C
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "ast.h"
// 哈希
static unsigned hash(char *s) {
unsigned int hash = 0;
unsigned c;
while (c = *s++) hash = hash * 9 ^ c;
return hash;
}
static struct IdentifierSymbol symtab[NHASH];
// 查询变量是否存在
struct IdentifierSymbol* getSym(char *s) {
struct IdentifierSymbol *res = &symtab[hash(s) % NHASH];
int scount = NHASH;
while (--scount >= 0) {
if (res->name && !strcmp(res->name, s)) {
return res;
}
if (!res->name) {
return NULL;
}
if(++res >= symtab + NHASH) res = symtab;
}
yyerror("symbol table overflow.");
abort();
}
// 修改变量的值
void setSym(char *s, T val) {
struct IdentifierSymbol *res = &symtab[hash(s) % NHASH];
int scount = NHASH;
while (--scount >= 0) {
if (res->name && !strcmp(res->name, s)) {
res->val = val;
return;
}
if (!res->name) {
res->name = strdup(s);
res->val = val;
return;
}
if (++res >= symtab + NHASH) res = symtab;
}
yyerror("symbol table overflow.");
abort();
}
// 输出符号表
void emitSymbol() {
int i;
for (i = 0; i < NHASH; i++) {
if (symtab[i].name != 0) {
printf("%s %d\n", symtab[i].name, symtab[i].val);
}
}
}
// 构造抽象语法树
Ast newAst(int tokenType, Ast left, Ast right) {
Ast res = malloc(sizeof(struct AbstractSyntaxTree));
if (!res) {
yyerror("Insufficient memory space.");
exit(0);
}
res->tokenType = tokenType;
res->left = left;
res->right = right;
return res;
}
Ast newNum(T val) {
struct Number *res = malloc(sizeof(struct Number));
if (!res) {
yyerror("Insufficient memory space.");
exit(0);
}
res->tokenType = NUM;
res->val = val;
return (Ast)res;
}
Ast newId(char* val) {
struct Identifier *res = malloc(sizeof(struct Identifier));
if (!res) {
yyerror("Insufficient memory space.");
exit(0);
}
res->tokenType = ID;
res->val = strdup(val);
return (Ast)res;
}
Stmt newStmt(Ast ast) {
Stmt res = malloc(sizeof(struct Statement));
if (!res) {
yyerror("Insufficient memory space.");
exit(0);
}
res->ast = ast;
res->nxt = NULL;
return res;
}
Stmt mergeStmt(Stmt stmt1, Stmt stmt2) {
stmt2->nxt = stmt1;
return stmt2;
}
void freeAst(Ast ast) {
if (ast == NULL) return;
// did not finish
// too lazy to do it
}
void freeStmt(Stmt stmt) {
if(stmt == NULL) return;
freeAst(stmt->ast);
freeStmt(stmt->nxt);
free(stmt);
}
// 遍历抽象语法树
T evalAst(Ast ast) {
// printf("token: %d\n", ast->tokenType);
T v = 0, val_r;
char *name;
switch (ast->tokenType) {
case NUM:
v = ((struct Number *)ast)->val;
break;
case ID:;
name = ((struct Identifier *)ast)->val;
struct IdentifierSymbol *id = getSym(name);
if(id == NULL) {
yyerror("Undefined identifier %s.", name);
abort();
} else {
v = id->val;
}
break;
case NEG:
v = -evalAst(ast->left);
break;
case '+':
v = evalAst(ast->left) + evalAst(ast->right);
break;
case '-':
v = evalAst(ast->left) - evalAst(ast->right);
break;
case '*':
v = evalAst(ast->left) * evalAst(ast->right);
break;
case '/':
val_r = evalAst(ast->right);
if (val_r == 0) {
yyerror("Divide by 0.");
abort();
}
v = evalAst(ast->left) / evalAst(ast->right);
break;
case '=':
val_r = evalAst(ast->right);
name = ((struct Identifier *)(ast->left))->val;
setSym(name, val_r);
val_r = evalAst(ast->left);
break;
default:
yyerror("Unknown token [%d].", ast->tokenType);
}
return v;
}
// 遍历抽象语法树
void evalStmt(Stmt stmt) {
if(stmt->nxt) {
evalStmt(stmt->nxt);
}
evalAst(stmt->ast);
}
#endif