-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemant.c
executable file
·328 lines (273 loc) · 7.66 KB
/
semant.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "semant.h"
#include "type.h"
#include "symtab.h"
#include "ast.h"
#include <stdio.h>
static void semant(list* program, struct symtab* M, struct symtab* V);
static void semant_(AST* node, struct symtab* M, struct symtab* V);
void analyze(list* program)
{
struct symtab* M = enterscope(NULL);
struct symtab* V = enterscope(NULL);
semant(program, M, V);
}
void semant(list* l, struct symtab* M, struct symtab* V)
{
for (struct node* tmp = l->head; tmp != NULL; tmp = tmp->next)
semant_(tmp->data, M, V);
}
static void semant_error(char* msg, AST* node)
{
fprintf(stderr, "error:%d: %s\n",node->lineno_, msg);
}
static void set_type(AST* node, int t)
{
node->type_ = t;
}
static void set_symtab(AST* node, struct symtab* M, struct symtab* V)
{
node->M = M;
node->V = V;
}
static void semant_bexpr(AST* node, char* s, struct symtab* M, struct symtab* V)
{
semant_(node->d.be.lhs_, M, V);
semant_(node->d.be.rhs_, M, V);
set_symtab(node, M, V);
if(node->d.be.lhs_->type_ == TYPE_INT && node->d.be.rhs_->type_ == TYPE_INT)
{
set_type(node,TYPE_INT);
}
else
{
char ar[100];
sprintf(ar,"Type to %s should be integer",s);
semant_error(ar,node);
set_type(node,TYPE_ERR);
}
}
static void semant_assign(AST* node, struct symtab* M, struct symtab* V)
{
set_symtab(node, M, V);
semant_(node->d.assign.to_, M, V);
semant_(node->d.assign.expr_, M, V);
if(node->d.assign.expr_->type_ != TYPE_INT || node->d.assign.to_->type_ != TYPE_INT)
{
char ar[100];
sprintf(ar, "Mismatch type, Cannot assign %s to %s",
node->d.assign.expr_->type_ == TYPE_INT ? "int": "void",
node->d.assign.to_->type_ == TYPE_INT ? "int": "void");
semant_error(ar, node);
set_type(node,TYPE_ERR);
}
else
{
set_type(node, TYPE_INT);
}
}
static void semant_array(AST* node, struct symtab* M, struct symtab* V)
{
set_symtab(node, M, V);
semant_(node->d.access.expr_, M, V);
if(node->d.access.expr_->type_ != TYPE_INT)
{
semant_error("Array Index should be of integer type", node);
set_type(node,TYPE_ERR);
}
else
{
set_type(node,TYPE_INT);
}
}
static void semant_variable(AST* node, struct symtab* M, struct symtab* V)
{
set_symtab(node, M, V);
if(lookup(V, node->d.variable.name_) == NULL)
{
char ar[100];
sprintf(ar,"Undefined variable %s",node->d.variable.name_);
semant_error(ar, node);
}
else
{
set_type(node, TYPE_INT);
}
}
static void semant_ret(AST* node, struct symtab* M, struct symtab* V)
{
set_symtab(node, M, V);
if(node->d.ret.expr_ != NULL)
{
semant_(node->d.ret.expr_, M, V);
if(node->d.ret.expr_->type_ != TYPE_INT)
{
semant_error("return type should be int",node);
set_type(node, TYPE_ERR);
}
else
{
set_type(node, TYPE_INT);
}
}
else
{
set_type(node, TYPE_VOID);
}
}
static void semant_while(AST* node, struct symtab* M, struct symtab* V)
{
set_symtab(node, M, V);
semant_(node->d.while_loop.expr_, M, V);
semant_(node->d.while_loop.stmt_, M, V);
if(node->d.while_loop.expr_->type_ != TYPE_INT)
{
semant_error("Predicate to while should be of type int",node);
}
}
static void semant_if_then(AST* node, struct symtab* M, struct symtab* V)
{
set_symtab(node, M, V);
semant_(node->d.if_then.expr_, M, V);
semant_(node->d.if_then.if_stmt_, M, V);
if(node->d.if_then.else_stmt_ != NULL)
semant_(node->d.if_then.else_stmt_, M, V);
if(node->d.if_then.expr_->type_ != TYPE_INT)
{
set_type(node, TYPE_ERR);
semant_error("Predicate to if should be int",node);
}
else
{
set_type(node, TYPE_INT);
}
}
static void semant_compound(AST* node, struct symtab* M, struct symtab* V)
{
V = enterscope(V);
set_symtab(node, M, V);
semant(node->d.compound.decls_, M, V);
semant(node->d.compound.stmts_, M, V);
V = exitscope(V);
}
static void semant_param(AST* node, struct symtab* M, struct symtab* V)
{
V = add_symbol(V, node->d.param.name_, node);
if (node->d.param.type_spec_ != TYPE_VOID)
set_type(node, TYPE_INT);
else
{
semant_error("Invalid parameter type", node);
set_type(node,TYPE_ERR);
}
}
static void semant_function(AST* node, struct symtab* M, struct symtab* V)
{
M = add_symbol(M, node->d.function.name_, node);
V = enterscope(V);
set_symtab(node, M, V);
// check parameters
semant(node->d.function.params_, M, V);
// check body
semant_(node->d.function.body_, M, V);
V = exitscope(V);
/*if(node->d.function.ret_type_ != node->d.function.body_->type_)
{
semant_error("Body does not conform to function's return type",node);
set_type(node,TYPE_ERR);
}
else*/
{
set_type(node, node->d.function.body_->type_);
}
}
static void semant_var_decl(AST* node, struct symtab* M, struct symtab* V)
{
V = add_symbol(V, node->d.var_decl.name_, node);
set_symtab(node, M, V);
if(node->d.var_decl.type_spec_ != TYPE_INT)
{
semant_error("Type of a variable should be int", node);
set_type(node, TYPE_ERR);
}
else
{
set_type(node, TYPE_INT);
}
}
static void semant_(AST* node, struct symtab* M, struct symtab* V)
{
switch (node->kind_)
{
case K_NUM:
set_type(node, TYPE_INT);
set_symtab(node, M, V);
break;
case BE_LT:
semant_bexpr(node, "<", M, V);
break;
case BE_LTE:
semant_bexpr(node, "<=", M, V);
break;
case BE_GT:
semant_bexpr(node, ">", M, V);
break;
case BE_GTE:
semant_bexpr(node, ">=", M, V);
break;
case BE_EQ:
semant_bexpr(node, "==", M, V);
break;
case BE_NEQ:
semant_bexpr(node, "!=", M, V);
break;
case BE_ADD:
semant_bexpr(node, "+", M, V);
break;
case BE_SUB:
semant_bexpr(node, "-", M, V);
break;
case BE_MUL:
semant_bexpr(node, "*", M, V);
break;
case BE_DIV:
semant_bexpr(node, "/", M, V);
break;
case K_CALL:
set_symtab(node, M, V);
semant(node->d.call.args_, M, V);
break;
case E_ASSIGN:
semant_assign(node, M, V);
break;
case ARRAY_ACCESS:
semant_array(node, M, V);
break;
case VAR_NAME:
semant_variable(node, M, V);
break;
case RETURN:
semant_ret(node, M, V);
break;
case WHILE:
semant_while(node, M, V);
break;
case IF_THEN:
semant_if_then(node, M, V);
break;
case COMPOUND:
semant_compound(node, M, V);
break;
case PARAM:
semant_param(node, M, V);
break;
case FUNCTION:
semant_function(node, M, V);
break;
case VAR_DECL:
semant_var_decl(node, M, V);
break;
default:
fprintf(stderr,"unknown node kind\n");
break;
}
}