-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.c
252 lines (225 loc) · 4.75 KB
/
resolve.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
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "hash_table.h"
static void resolve_decl(struct decl *);
static void resolve_stmt(struct stmt *);
static void resolve_expr(struct expr *);
static void resolve_param(struct param *);
static struct prog *prog;
static FILE *fout;
static FILE *ferr;
static int should_print;
void ast_resolve(struct prog *p, struct config *cfg)
{
prog = p;
fout = cfg->fout;
ferr = cfg->ferr;
should_print = cfg->flags & FLAG_PRINT_RESOLVE;
resolve_decl(p->ast);
}
static void resolve_print(struct symbol *s)
{
if (!should_print) {
return;
}
char *kind;
switch (s->kind) {
case SYMBOL_LOCAL:
kind = "local";
break;
case SYMBOL_PARAM:
kind = "parameter";
break;
case SYMBOL_GLOBAL:
kind = "global";
break;
}
fprintf(fout, "%s resolves to %s %d\n", s->name, kind, s->which);
}
static int scope_enter(void);
static int scope_level(void);
static int scope_exit(void);
static int scope_bind(char *name, struct symbol *symbol);
static struct symbol *scope_lookup(const char *name);
static int param_count;
static int local_count;
/*
not sure this is entirely right. this code allows all declarations at innermore levels to "shadow"
declarations at outermore levels. this is similar to C, but not exactly the same. the main
difference i'm aware of is that function locals should not be able to shadow function params.
function locals with the same name as formal parameters should cause an error.
*/
void resolve_decl(struct decl *d)
{
if (!d) {
return;
}
struct type *t = d->type;
if (scope_level() == SYMBOL_GLOBAL) {
d->symbol = scope_lookup(d->name);
if (!d->symbol) {
d->symbol = symbol_make(SYMBOL_GLOBAL, t, d->name, prog);
scope_bind(d->name, d->symbol);
}
int init = t->kind == TYPE_FUNCTION
? d->code != NULL
: d->value != NULL;
if (init) {
if (d->symbol->init) {
fprintf(ferr, "resolve: redefinition of global %s\n", d->name);
exit(1);
}
d->symbol->init = 1;
}
resolve_print(d->symbol);
if (t->kind == TYPE_FUNCTION) {
scope_enter();
param_count = 0;
resolve_param(t->params);
local_count = 0;
resolve_stmt(d->code);
d->num_locals = local_count;
scope_exit();
}
} else {
d->symbol = symbol_make(SYMBOL_LOCAL, t, d->name, prog);
d->symbol->offset = local_count++;
if (!scope_bind(d->name, d->symbol)) {
fprintf(ferr, "resolve: local %s has already been declared\n", d->name);
exit(1);
}
resolve_print(d->symbol);
resolve_expr(d->value);
}
resolve_decl(d->next);
}
void resolve_stmt(struct stmt *s)
{
if (!s) {
return;
}
switch (s->kind) {
case STMT_DECL:
resolve_decl(s->decl);
break;
case STMT_EXPR:
case STMT_PRINT:
case STMT_RETURN:
resolve_expr(s->expr);
break;
case STMT_IF_ELSE:
resolve_expr(s->expr);
scope_enter();
resolve_stmt(s->body);
scope_exit();
scope_enter();
resolve_stmt(s->ebody);
scope_exit();
break;
case STMT_WHILE:
resolve_expr(s->expr);
scope_enter();
resolve_stmt(s->body);
scope_exit();
break;
case STMT_BLOCK:
scope_enter();
resolve_stmt(s->body);
scope_exit();
break;
}
resolve_stmt(s->next);
}
void resolve_expr(struct expr *e)
{
if (!e) {
return;
}
switch (e->kind) {
case EXPR_NAME:
case EXPR_CALL:
case EXPR_ASSIGN:
e->symbol = scope_lookup(e->name);
if (e->symbol) {
resolve_print(e->symbol);
} else {
fprintf(ferr, "resolve: use of undeclared variable %s\n", e->name);
exit(1);
}
resolve_expr(e->right); /* no-op for EXPR_NAME */
break;
case EXPR_STRING:
prog_add_string(prog, e->name);
break;
default:
resolve_expr(e->left);
resolve_expr(e->right);
break;
}
}
void resolve_param(struct param *p)
{
if (!p) {
return;
}
struct symbol *s = symbol_make(SYMBOL_PARAM, p->type, p->name, prog);
scope_bind(p->name, s);
s->offset = param_count++;
resolve_print(s);
resolve_param(p->next);
}
#define SCOPE_MAX 100
static int level;
static struct hash_table *scope[SCOPE_MAX];
static int which;
int scope_enter(void)
{
++level;
if (level >= SCOPE_MAX) {
fprintf(ferr, "resolve: max scope exceeded\n");
exit(1);
}
scope[level] = hash_table_create(0, 0);
return level;
}
int scope_level(void)
{
return level;
}
int scope_exit(void)
{
hash_table_delete(scope[level]);
--level;
return level;
}
static void scope_init(void)
{
if (!scope[0]) {
scope[0] = hash_table_create(0, 0);
}
}
int scope_bind(char *name, struct symbol *s)
{
scope_init();
if (hash_table_lookup(scope[level], name)) {
return 0;
} else {
s->which = ++which;
hash_table_insert(scope[level], name, s, NULL);
return 1;
}
}
struct symbol *scope_lookup(const char *name)
{
scope_init();
int i;
struct symbol *s;
for (i = level; i >= 0; --i) {
s = hash_table_lookup(scope[i], name);
if (s) {
return s;
}
}
return NULL;
}