-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinterpreter.c
250 lines (238 loc) · 9.47 KB
/
interpreter.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "interpreter.h"
#include "parser.h"
#include "token.h"
static Environment* createEnvironment(Environment* enclosing) {
Environment* env = malloc(sizeof(Environment));
env->variables = NULL;
env->variable_count = 0;
env->enclosing = enclosing;
return env;
}
static void defineVariable(Environment* env, const char* name, Value value) {
env->variable_count++;
env->variables = realloc(env->variables, env->variable_count * sizeof(Variable));
env->variables[env->variable_count - 1].name = strdup(name);
env->variables[env->variable_count - 1].value = value;
}
static Value* getVariable(Environment* env, const char* name) {
for (int i = 0; i < env->variable_count; i++) {
if (strcmp(env->variables[i].name, name) == 0) {
return &env->variables[i].value;
}
}
if (env->enclosing) {
return getVariable(env->enclosing, name);
}
return NULL;
}
void initInterpreter(Interpreter* interpreter) {
interpreter->global_env = createEnvironment(NULL);
interpreter->current_env = interpreter->global_env;
}
static Value evaluateExpression(Interpreter* interpreter, Node* node) {
switch (node->type) {
case NODE_BINARY: {
Value left = evaluateExpression(interpreter, node->as.binary.left);
Value right = evaluateExpression(interpreter, node->as.binary.right);
Value result = {VALUE_INT, {0}};
if (left.type == VALUE_FLOAT || right.type == VALUE_FLOAT) {
result.type = VALUE_FLOAT;
float left_val = (left.type == VALUE_FLOAT) ? left.as.float_value : (float)left.as.int_value;
float right_val = (right.type == VALUE_FLOAT) ? right.as.float_value : (float)right.as.int_value;
switch (node->token.type) {
case TOKEN_PLUS:
result.as.float_value = left_val + right_val;
break;
case TOKEN_MINUS:
result.as.float_value = left_val - right_val;
break;
case TOKEN_ASTERISK:
result.as.float_value = left_val * right_val;
break;
case TOKEN_SLASH:
result.as.float_value = left_val / right_val;
break;
default:
fprintf(stderr, "Unknown operator for float operation\n");
exit(1);
}
} else {
switch (node->token.type) {
case TOKEN_PLUS:
result.as.int_value = left.as.int_value + right.as.int_value;
break;
case TOKEN_MINUS:
result.as.int_value = left.as.int_value - right.as.int_value;
break;
case TOKEN_ASTERISK:
result.as.int_value = left.as.int_value * right.as.int_value;
break;
case TOKEN_SLASH:
result.as.int_value = left.as.int_value / right.as.int_value;
break;
default:
fprintf(stderr, "Unknown operator for int operation\n");
exit(1);
}
}
return result;
}
case NODE_UNARY: {
Value operand = evaluateExpression(interpreter, node->as.unary.operand);
Value result = {operand.type, {0}};
switch (node->token.type) {
case TOKEN_MINUS:
if (operand.type == VALUE_INT) {
result.as.int_value = -operand.as.int_value;
} else {
result.as.float_value = -operand.as.float_value;
}
break;
default:
fprintf(stderr, "Unknown unary operator\n");
exit(1);
}
return result;
}
case NODE_LITERAL: {
Value result;
if (node->token.type == TOKEN_INTEGER_LITERAL) {
result.type = VALUE_INT;
result.as.int_value = atoi(node->token.lexeme);
} else if (node->token.type == TOKEN_FLOAT_LITERAL) {
result.type = VALUE_FLOAT;
result.as.float_value = atof(node->token.lexeme);
}
return result;
}
case NODE_IDENTIFIER: {
Value* value = getVariable(interpreter->current_env, node->token.lexeme);
if (value == NULL) {
fprintf(stderr, "Undefined variable: %s\n", node->token.lexeme);
exit(1);
}
return *value;
}
case NODE_ASSIGNMENT: {
Value value = evaluateExpression(interpreter, node->as.assignment.right);
Value* variable = getVariable(interpreter->current_env, node->as.assignment.left->token.lexeme);
if (variable == NULL) {
fprintf(stderr, "Undefined variable: %s\n", node->as.assignment.left->token.lexeme);
exit(1);
}
*variable = value;
return value;
}
default:
fprintf(stderr, "Unknown node type in expression\n");
exit(1);
}
}
static void executeStatement(Interpreter* interpreter, Node* node) {
switch (node->type) {
case NODE_EXPRESSION_STATEMENT: {
evaluateExpression(interpreter, node->as.expression_statement.expression);
break;
}
case NODE_VARIABLE_DECLARATION: {
Value value = {VALUE_INT, {0}}; // Default initialization
if (node->as.variable_declaration.type->token.type == TOKEN_FLOAT) {
value.type = VALUE_FLOAT;
}
if (node->as.variable_declaration.initializer != NULL) {
value = evaluateExpression(interpreter, node->as.variable_declaration.initializer);
}
defineVariable(interpreter->current_env, node->as.variable_declaration.identifier->token.lexeme, value);
break;
}
case NODE_IF_STATEMENT: {
Value condition = evaluateExpression(interpreter, node->as.if_statement.condition);
if (condition.type == VALUE_INT ? condition.as.int_value : condition.as.float_value) {
executeStatement(interpreter, node->as.if_statement.then_branch);
} else if (node->as.if_statement.else_branch != NULL) {
executeStatement(interpreter, node->as.if_statement.else_branch);
}
break;
}
case NODE_WHILE_STATEMENT: {
while (1) {
Value condition = evaluateExpression(interpreter, node->as.while_statement.condition);
if (!(condition.type == VALUE_INT ? condition.as.int_value : condition.as.float_value)) {
break;
}
executeStatement(interpreter, node->as.while_statement.body);
}
break;
}
case NODE_BLOCK: {
Environment* previous = interpreter->current_env;
interpreter->current_env = createEnvironment(previous);
for (int i = 0; i < node->as.block.statement_count; i++) {
executeStatement(interpreter, node->as.block.statements[i]);
}
interpreter->current_env = previous;
break;
}
case NODE_RETURN_STATEMENT: {
// This will be handled in evaluateNode
break;
}
default:
fprintf(stderr, "Unknown statement type\n");
exit(1);
}
}
Value evaluateNode(Interpreter* interpreter, Node* node) {
switch (node->type) {
case NODE_PROGRAM:
for (int i = 0; i < node->as.program.declaration_count; i++) {
evaluateNode(interpreter, node->as.program.declarations[i]);
}
return (Value){VALUE_VOID, {0}};
case NODE_FUNCTION_DECLARATION:
// Handle function declaration (if implemented)
return (Value){VALUE_VOID, {0}};
case NODE_VARIABLE_DECLARATION:
case NODE_IF_STATEMENT:
case NODE_WHILE_STATEMENT:
case NODE_BLOCK:
case NODE_EXPRESSION_STATEMENT:
executeStatement(interpreter, node);
return (Value){VALUE_VOID, {0}};
case NODE_RETURN_STATEMENT:
if (node->as.return_statement.expression != NULL) {
return evaluateExpression(interpreter, node->as.return_statement.expression);
} else {
return (Value){VALUE_VOID, {0}};
}
case NODE_BINARY:
case NODE_UNARY:
case NODE_LITERAL:
case NODE_IDENTIFIER:
case NODE_ASSIGNMENT:
return evaluateExpression(interpreter, node);
default:
fprintf(stderr, "Unknown node type in evaluateNode\n");
exit(1);
}
}
void interpret(Interpreter* interpreter, Node* program) {
Value result = evaluateNode(interpreter, program);
printValue(result);
}
void printValue(Value value) {
switch(value.type) {
case VALUE_INT:
printf("%d\n", value.as.int_value);
break;
case VALUE_FLOAT:
printf("%f\n", value.as.float_value);
break;
case VALUE_VOID:
printf("void\n");
break;
}
}