-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
307 lines (267 loc) · 9.1 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
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "interpreter.h"
#include "parser.h"
#include "modules.h"
#define MAX_READ_STRING_SIZE 255
Value* InterpretExpression(AST* ast, Scope* scope, IDTable* id_table);
#define PERFORM_OP(op) \
left = InterpretExpression(ast->child, scope, id_table); \
Typecheck(left, TYPE_INTEGER); \
right = InterpretExpression(ast->child->sibling, scope, id_table); \
Typecheck(right, TYPE_INTEGER); \
return CreateIntegralValue((left->v.integral) op (right->v.integral))
void FailWithInvalidArgumentNumber(Value* function) {
fprintf(stderr, "Invalid number of arguments passed to function: expected %d\n",
function->v.function.argcount);
exit(-1);
}
/* TODO: Do not mix coding styles */
/*Value* CallFunction(AST* ast, Scope* outer_scope, Value* owner) {*/
Value* CallFunction(Scope* outer_scope, Value* function, AST* arglist, Value* owner, IDTable* id_table) {
int arg_num = 0;
/*Value* function = GetValue(outer_scope, ast->value->v.integral); */
Typecheck(function, TYPE_FUNCTION);
Scope* local_scope = CreateScope();
local_scope->parent = outer_scope;
Value** arguments = (Value**)malloc(sizeof(Value*) * function->v.function.argcount);
AST *arg;
for(arg = arglist->child; arg; arg = arg->sibling) {
if(arg_num == function->v.function.argcount) {
FailWithInvalidArgumentNumber(function);
}
arguments[arg_num++] = InterpretExpression(arg, outer_scope, id_table);
}
if(arg_num < function->v.function.argcount) {
FailWithInvalidArgumentNumber(function);
}
/* For each formal argument, set local values from @param arguments */
for(arg_num = 0; arg_num < function->v.function.argcount; ++arg_num) {
SetLocalValue(local_scope, function->v.function.arguments[arg_num], arguments[arg_num]);
}
SetLocalValue(local_scope, RETURN_VALUE_ID, NULL);
SetLocalValue(local_scope, SELF_VALUE_ID, owner);
InterpretAST(function->v.function.code, local_scope, id_table);
/* Value* return_value = GetValue(local_scope, RETURN_VALUE_ID); */
Value* return_value = local_scope->ids[RETURN_VALUE_ID]; /* Ignoring existence check */
free(arguments);
free(local_scope); /* XXX: When refcount is implemented, simple free call will not suffice */
return return_value; /* TODO: Add new type NONE, as in Python, return it */
}
Value** ResolveIndex(AST* ast, Scope* scope, IDTable* id_table) {
int id = ast->value->v.integral;
Value* index = InterpretExpression(ast->child, scope, id_table);
Typecheck(index, TYPE_INTEGER);
Value* array = GetValue(scope, id);
Typecheck(array, TYPE_ARRAY);
if(index->v.integral >= array->v.array.size) {
fprintf(stderr, "Array index out of bounds : %d out of %d\n", index->v.integral,
array->v.array.size);
exit(-1);
}
return &array->v.array.data[index->v.integral];
}
static void SetObjectField(AST* ast, Scope* scope, Value* value) {
int id = ast->value->v.integral;
Value* object = GetValue(scope, id);
Typecheck(ast->child->value, TYPE_STRING);
SetField(object, ast->child->value->v.string, value);
}
static Value* GetObjectField(AST* ast, Scope* scope) {
int id = ast->value->v.integral;
Value* object = GetValue(scope, id);
Typecheck(ast->child->value, TYPE_STRING);
return GetField(object, ast->child->value->v.string);
}
static Value* InterpretFunctionDefinition(AST* ast) {
int* func_args = (int*)malloc(sizeof(int) * MAX_FUNCTION_ARGUMENTS_COUNT);
int arg_num = 0;
AST* arg;
for(arg = ast->child->child; arg; arg = arg->sibling) {
func_args[arg_num++] = arg->value->v.integral;
}
return CreateFunctionValue(func_args, arg_num, ast->child->sibling);
}
Value* InterpretExpression(AST* ast, Scope* scope, IDTable* id_table) {
Value *left, *right, *value, *index, *size;
int nvalue, id;
char *read_buf;
switch(ast->semantic) {
case SEM_ID:
return GetValue(scope, ast->value->v.integral);
case SEM_CONSTANT:
return ast->value;
case SEM_INTREAD:
printf("INPUT : ");
scanf("%d", &nvalue);
return CreateIntegralValue(nvalue);
case SEM_READ:
printf("INPUT : ");
read_buf = (char*)malloc(sizeof(char) * MAX_READ_STRING_SIZE);
scanf("%s", read_buf);
return CreateStringValue(read_buf);
case SEM_ARRAY:
size = InterpretExpression(ast->child, scope, id_table);
Typecheck(size, TYPE_INTEGER);
return CreateArrayValue(size->v.integral);
case SEM_OBJECT:
return CreateObject();
case SEM_INDEX:
return *ResolveIndex(ast, scope, id_table);
case SEM_FIELD:
value = GetObjectField(ast, scope);
if(!value) {
fprintf(stderr, "Unknown field : %s\n", ast->child->value->v.string);
exit(-1);
}
return value;
case SEM_FUNCTION:
return InterpretFunctionDefinition(ast);
case SEM_FUNCCALL:
return CallFunction(scope, GetValue(scope, ast->value->v.integral), ast->child, NULL, id_table);
case SEM_NEW:
{
Value* object = CreateObject();
Value* constructor = GetValue(scope, ast->child->value->v.integral);
CallFunction(scope, constructor, ast->child->child, object, id_table);
SetField(object, PROTOTYPE_VALUE_NAME, GetFieldGeneric(constructor, PROTOTYPE_VALUE_NAME, 0));
return object;
}
case SEM_METHOD_CALL:
return CallFunction(scope, GetObjectField(ast->child, scope), ast->child->sibling,
GetValue(scope, ast->child->value->v.integral), id_table);
case SEM_ADDITION:
PERFORM_OP(+);
case SEM_SUBTRACTION:
PERFORM_OP(-);
case SEM_MULTIPLICATION:
PERFORM_OP(*);
case SEM_DIVISION:
PERFORM_OP(/);
case SEM_AND:
PERFORM_OP(&&);
case SEM_OR:
PERFORM_OP(||);
case SEM_NOT:
value = InterpretExpression(ast->child, scope, id_table);
Typecheck(value, TYPE_INTEGER);
return CreateIntegralValue(!value->v.integral);
case SEM_LT:
PERFORM_OP(<);
case SEM_EQ:
PERFORM_OP(==);
case SEM_GT:
PERFORM_OP(>);
case SEM_LTE:
PERFORM_OP(<=);
case SEM_GTE:
PERFORM_OP(>=);
default:
fprintf(stderr, "Interpreting error : unexpected expression semantic : %s\n", semanticToString[ast->semantic]);
return 0;
}
}
void Assign(AST* ast, Scope* scope, int local, IDTable* id_table) {
AST* lvalue = ast->child;
int id = lvalue->value->v.integral;
Value* value = InterpretExpression(lvalue->sibling, scope, id_table);
Value *index;
Value **array_element;
switch(lvalue->semantic) {
case SEM_ID:
if(local)
SetLocalValue(scope, id, value);
else
SetValue(scope, id, value);
break;
case SEM_INDEX:
/* TODO: Mutates value directly. Work out strict semantic
* for value access and modification!!!*/
array_element = ResolveIndex(lvalue, scope, id_table);
*array_element = value;
break;
case SEM_FIELD:
SetObjectField(lvalue, scope, value);
break;
default:
fprintf(stderr, "Invalid lvalue semantic : %d\n", lvalue->semantic);
exit(-1);
break;
}
}
void InterpretStatement(AST* ast, Scope* scope, IDTable* id_table) {
Value *value, *cond;
char* value_repr;
int* func_args;
int arg_num;
AST* arg;
switch(ast->semantic) {
case SEM_ASSIGNMENT:
case SEM_LOCAL_ASSIGNMENT:
Assign(ast, scope, ast->semantic == SEM_LOCAL_ASSIGNMENT, id_table);
break;
case SEM_WHILE_CYCLE:
while(InterpretExpression(ast->child, scope, id_table)->v.integral) {
InterpretStatement(ast->child->sibling, scope, id_table);
}
break;
case SEM_IF_STATEMENT:
cond = InterpretExpression(ast->child, scope, id_table);
if(cond->v.integral) {
InterpretStatement(ast->child->sibling, scope, id_table);
} else if(ast->child->sibling->sibling) {
InterpretStatement(ast->child->sibling->sibling, scope, id_table);
}
break;
case SEM_PRINT:
value = InterpretExpression(ast->child, scope, id_table);
value_repr = ValueToString(value);
printf("OUTPUT: %s\n", value_repr);
free(value_repr);
break;
case SEM_RETURN:
SetLocalValue(scope, RETURN_VALUE_ID, InterpretExpression(ast->child, scope, id_table));
break;
case SEM_FUNCTION:
SetLocalValue(scope, ast->value->v.integral, InterpretFunctionDefinition(ast));
break;
case SEM_FUNCCALL:
CallFunction(scope, GetValue(scope, ast->value->v.integral), ast->child, NULL, id_table);
break;
case SEM_METHOD_CALL:
CallFunction(scope, GetObjectField(ast->child, scope), ast->child->sibling, ast->child->value, id_table);
break;
case SEM_EMPTY:
InterpretAST(ast->child, scope, id_table);
break;
case SEM_INCLUDE:
{
Value* file_name = ast->value;
Typecheck(file_name, TYPE_STRING);
AST* module_ast = IncludeModule(file_name->v.string, id_table);
InterpretAST(module_ast, scope, id_table);
}
break;
case SEM_LOAD:
{
Value* file_name = ast->value;
Typecheck(file_name, TYPE_STRING);
/* TODO: Refactor */
/* TODO: Use blocks to hold locals, instead of declaring
* them in the beginning */
/* TODO: Make macro for accessing values in Value instances */
LoadModule(file_name->v.string, id_table, scope);
}
break;
default:
fprintf(stderr, "Interpreting error : unexpected statement semantic : %s\n", semanticToString[ast->semantic]);
break;
}
}
void InterpretAST(AST* ast, Scope* scope, IDTable* id_table) {
AST* currentAST;
for(currentAST = ast; currentAST; currentAST = currentAST->sibling) {
InterpretStatement(currentAST, scope, id_table);
}
}