-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.c
389 lines (363 loc) · 9.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* ast.c */
#include "ast.h"
#include <stdbool.h>
#include <setjmp.h>
static jmp_buf break_env;
void execute_switch_statement(ASTNode *node)
{
int switch_value = evaluate_expression(node->data.switch_stmt.expression);
CaseNode *current_case = node->data.switch_stmt.cases;
int matched = 0;
if (setjmp(break_env) == 0)
{
while (current_case)
{
if (current_case->value)
{
int case_value = evaluate_expression(current_case->value);
if (case_value == switch_value || matched)
{
matched = 1;
execute_statements(current_case->statements);
}
}
else
{
// Default case
if (matched || !matched)
{
execute_statements(current_case->statements);
break;
}
}
current_case = current_case->next;
}
}
else
{
// Break encountered; do nothing
}
}
/* Include the symbol table functions */
extern bool set_variable(char *name, int value);
extern int get_variable(char *name);
extern void yyerror(const char *s);
extern void yapping(const char *format, ...);
extern void baka(const char *format, ...);
/* Function implementations */
ASTNode *create_number_node(int value)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_NUMBER;
node->data.value = value;
return node;
}
ASTNode *create_identifier_node(char *name)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_IDENTIFIER;
node->data.name = strdup(name);
return node;
}
ASTNode *create_assignment_node(char *name, ASTNode *expr)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_ASSIGNMENT;
node->data.op.left = create_identifier_node(name);
node->data.op.right = expr;
node->data.op.op = '=';
return node;
}
ASTNode *create_operation_node(OperatorType op, ASTNode *left, ASTNode *right)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_OPERATION;
node->data.op.left = left;
node->data.op.right = right;
node->data.op.op = op;
return node;
}
ASTNode *create_unary_operation_node(OperatorType op, ASTNode *operand)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_UNARY_OPERATION;
node->data.unary.operand = operand;
node->data.unary.op = op;
return node;
}
ASTNode *create_for_statement_node(ASTNode *init, ASTNode *cond, ASTNode *incr, ASTNode *body)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_FOR_STATEMENT;
node->data.for_stmt.init = init;
node->data.for_stmt.cond = cond;
node->data.for_stmt.incr = incr;
node->data.for_stmt.body = body;
return node;
}
ASTNode *create_print_statement_node(ASTNode *expr)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_PRINT_STATEMENT;
node->data.op.left = expr;
return node;
}
ASTNode *create_error_statement_node(ASTNode *expr)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_ERROR_STATEMENT;
node->data.op.left = expr;
return node;
}
ASTNode *create_statement_list(ASTNode *statement, ASTNode *next_statement)
{
if (!next_statement)
{
/* Create a new statement list */
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_STATEMENT_LIST;
node->data.statements = malloc(sizeof(StatementList));
node->data.statements->statement = statement;
node->data.statements->next = NULL;
return node;
}
else
{
/* Prepend to existing list */
StatementList *list = malloc(sizeof(StatementList));
list->statement = statement;
list->next = next_statement->data.statements;
next_statement->data.statements = list;
return next_statement;
}
}
int evaluate_expression(ASTNode *node)
{
if (!node)
return 0;
switch (node->type)
{
case NODE_NUMBER:
return node->data.value;
case NODE_IDENTIFIER:
return get_variable(node->data.name);
case NODE_ASSIGNMENT:
{
int value = evaluate_expression(node->data.op.right);
set_variable(node->data.op.left->data.name, value);
return value;
}
case NODE_OPERATION:
{
int left = evaluate_expression(node->data.op.left);
int right = evaluate_expression(node->data.op.right);
switch (node->data.op.op)
{
case OP_PLUS:
return left + right;
case OP_MINUS:
return left - right;
case OP_TIMES:
return left * right;
case OP_DIVIDE:
return left / right;
case OP_MOD:
return left % right;
case OP_LT:
return left < right;
case OP_GT:
return left > right;
case OP_LE:
return left <= right;
case OP_GE:
return left >= right;
case OP_EQ:
return left == right;
case OP_NE:
return left != right;
case OP_AND:
return left && right;
case OP_OR:
return left || right;
default:
yyerror("Unknown operator");
return 0;
}
}
case NODE_UNARY_OPERATION:
{
int operand = evaluate_expression(node->data.unary.operand);
switch (node->data.unary.op)
{
case OP_NEG:
return -operand;
default:
yyerror("Unknown unary operator");
return 0;
}
}
case NODE_STRING_LITERAL:
yyerror("Cannot evaluate a string literal as an integer");
return 0;
default:
yyerror("Unknown expression type");
return 0;
}
}
void execute_statement(ASTNode *node)
{
if (!node)
return;
switch (node->type)
{
case NODE_ASSIGNMENT:
case NODE_OPERATION:
case NODE_UNARY_OPERATION:
case NODE_NUMBER:
case NODE_IDENTIFIER:
evaluate_expression(node);
break;
case NODE_FOR_STATEMENT:
execute_for_statement(node);
break;
case NODE_PRINT_STATEMENT:
{
ASTNode *expr = node->data.op.left;
if (expr->type == NODE_STRING_LITERAL)
{
yapping("%s\n", expr->data.name);
}
else
{
int value = evaluate_expression(expr);
yapping("%d\n", value);
}
break;
}
case NODE_ERROR_STATEMENT:
{
ASTNode *expr = node->data.op.left;
if (expr->type == NODE_STRING_LITERAL)
{
baka("%s\n", expr->data.name);
}
else
{
int value = evaluate_expression(expr);
baka("%d\n", value);
}
break;
}
case NODE_STATEMENT_LIST:
execute_statements(node);
break;
case NODE_IF_STATEMENT:
if (evaluate_expression(node->data.if_stmt.condition))
{
execute_statement(node->data.if_stmt.then_branch);
}
else if (node->data.if_stmt.else_branch)
{
execute_statement(node->data.if_stmt.else_branch);
}
break;
case NODE_SWITCH_STATEMENT:
execute_switch_statement(node);
break;
case NODE_BREAK_STATEMENT:
// Signal to break out of the current loop/switch
longjmp(break_env, 1);
break;
default:
yyerror("Unknown statement type");
break;
}
}
void execute_statements(ASTNode *node)
{
if (!node)
return;
if (node->type != NODE_STATEMENT_LIST)
{
execute_statement(node);
return;
}
StatementList *current = node->data.statements;
/* Reverse the list to maintain correct order */
StatementList *reversed = NULL;
while (current)
{
StatementList *next = current->next;
current->next = reversed;
reversed = current;
current = next;
}
current = reversed;
while (current)
{
execute_statement(current->statement);
current = current->next;
}
}
void execute_for_statement(ASTNode *node)
{
evaluate_expression(node->data.for_stmt.init);
while (evaluate_expression(node->data.for_stmt.cond))
{
execute_statement(node->data.for_stmt.body);
evaluate_expression(node->data.for_stmt.incr);
}
}
ASTNode *create_if_statement_node(ASTNode *condition, ASTNode *then_branch, ASTNode *else_branch)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_IF_STATEMENT;
node->data.if_stmt.condition = condition;
node->data.if_stmt.then_branch = then_branch;
node->data.if_stmt.else_branch = else_branch;
return node;
}
ASTNode *create_string_literal_node(char *string)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_STRING_LITERAL;
node->data.name = string;
return node;
}
ASTNode *create_switch_statement_node(ASTNode *expression, CaseNode *cases)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_SWITCH_STATEMENT;
node->data.switch_stmt.expression = expression;
node->data.switch_stmt.cases = cases;
return node;
}
CaseNode *create_case_node(ASTNode *value, ASTNode *statements)
{
CaseNode *node = malloc(sizeof(CaseNode));
node->value = value;
node->statements = statements;
node->next = NULL;
return node;
}
CaseNode *create_default_case_node(ASTNode *statements)
{
return create_case_node(NULL, statements); // NULL value indicates default case
}
CaseNode *append_case_list(CaseNode *list, CaseNode *case_node)
{
if (!list)
return case_node;
CaseNode *current = list;
while (current->next)
current = current->next;
current->next = case_node;
return list;
}
ASTNode *create_break_node()
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_BREAK_STATEMENT;
node->data.break_stmt = NULL;
return node;
}