-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_eval.c
More file actions
352 lines (339 loc) · 11.7 KB
/
Copy pathmain_eval.c
File metadata and controls
352 lines (339 loc) · 11.7 KB
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "syntax.h"
typedef struct {
char** args;
size_t args_size;
NodeArray body;
} Function;
typedef enum {
BUILTIN_FUNC_PRINT,
/* BUILTIN_FUNC_READ, */
} BuiltinFunc;
typedef struct {
enum {
VALUE_KIND_NUMBER, // .number
VALUE_KIND_FUNC, // .function
VALUE_KIND_BUILTIN_FUNC, // .builtin_func
} kind;
union {
Number number;
Function function;
BuiltinFunc builtin_func;
};
} Value;
typedef struct {
char** names;
Value* values;
size_t size;
size_t _capacity;
} Env;
Env Env_new(size_t capacity) {
return (Env){
.names = (char**)malloc(sizeof(char*) * capacity),
.values = (Value*)malloc(sizeof(Value) * capacity),
.size = 0,
._capacity = capacity,
};
}
ssize_t Env_find(Env* const self, const char* const name) {
if (self->size == 0) return -1;
for (ssize_t i = self->size - 1; i >= 0; --i) {
if (strcmp(self->names[i], name) == 0) {
return i;
}
}
return -1;
}
Value Env_get_value(Env* const self, const char* const name) {
ssize_t index = Env_find(self, name);
if (index == -1) {
if (strcmp(name, "print") == 0) {
return (Value){
.kind = VALUE_KIND_BUILTIN_FUNC,
.builtin_func = BUILTIN_FUNC_PRINT,
};
} else {
fprintf(stderr, "error: `%s` is not defined\n", name);
exit(EXIT_FAILURE);
}
}
return self->values[index];
}
void Env_insert_new(Env* const self, char* const name, Value value) {
if (self->size == self->_capacity) {
self->_capacity = self->_capacity == 0 ? 1 : self->_capacity * 2;
self->names = (char**)realloc(self->names, sizeof(char*) * self->_capacity);
self->values = (Value*)realloc(self->values, sizeof(Value) * self->_capacity);
}
size_t index = self->size++;
self->names[index] = name;
self->values[index] = value;
}
size_t Env_enter_scope(Env* const self) {
return self->size;
}
void Env_leave_scope(Env* const self, size_t scope_marker) {
assert(scope_marker <= self->size);
self->size = scope_marker;
}
Value eval(Env* env, Node node) {
size_t is_n_op = 0;
switch (node.kind) {
case ITEM_LIST: {
Value result = (Value){
.kind = VALUE_KIND_NUMBER,
.number = 0,
};
for (size_t i = 0; i < node.data.children.length; ++i) {
result = eval(env, node.data.children.data[i]);
}
return result;
}
case ITEM: return eval(env, node.data.children.data[0]);
case EXPR: return eval(env, node.data.children.data[0]);
case IF: {
size_t children_size = node.data.children.length;
assert(children_size == 2 || children_size == 3);
Value cond = eval(env, node.data.children.data[0]);
if (cond.kind != VALUE_KIND_NUMBER) {
fprintf(stderr, "error: condition is not a number\n");
exit(EXIT_FAILURE);
}
if (cond.number != 0) {
return eval(env, node.data.children.data[1]);
}
return children_size == 3
? eval(env, node.data.children.data[2])
: (Value){
.kind = VALUE_KIND_NUMBER,
.number = 0,
};
}
case FUNCTION: {
assert(node.data.children.length == 3);
char* name = node.data.children.data[0].data.text;
size_t args_size = node.data.children.data[1].data.children.length;
Function function = {
.args = (char**)malloc(sizeof(char*) * args_size),
.args_size = args_size,
.body = node.data.children.data[2].data.children,
};
for (size_t i = 0; i < args_size; ++i) {
function.args[i] = node.data.children.data[1].data.children.data[i].data.text;
}
Value result = (Value){
.kind = VALUE_KIND_FUNC,
.function = function,
};
Env_insert_new(env, name, result);
return result;
}
case CONST_VAR: {
assert(node.data.children.length == 2);
char* name = node.data.children.data[0].data.text;
Value value = eval(env, node.data.children.data[1]);
Env_insert_new(env, name, value);
return value;
}
case FUNC_CALL: {
assert(node.data.children.length == 2);
Value func = eval(env, node.data.children.data[0]);
if (func.kind == VALUE_KIND_BUILTIN_FUNC) {
switch (func.builtin_func) {
case BUILTIN_FUNC_PRINT: {
for (size_t i = 0; i < node.data.children.data[1].data.children.length; ++i) {
Value value = eval(env, node.data.children.data[1].data.children.data[i]);
if (value.kind == VALUE_KIND_NUMBER) {
Number number = value.number;
if (number == (int)number) {
printf("%d\n", (int)number);
} else {
printf("%f\n", number);
}
} else {
printf("<function>\n");
}
}
return (Value){.kind = VALUE_KIND_NUMBER, .number = 0};
}
}
assert(0);
}
if (func.kind != VALUE_KIND_FUNC) {
fprintf(stderr, "error: %s is not a function\n", node.data.children.data[0].data.text);
exit(EXIT_FAILURE);
}
Function function = func.function;
size_t args_size = node.data.children.data[1].data.children.length;
if (function.args_size != args_size) {
fprintf(stderr, "error: argument count mismatch, expected %zu but got %zu\n", function.args_size, args_size);
exit(EXIT_FAILURE);
}
size_t scope_marker = Env_enter_scope(env);
for (size_t i = 0; i < function.args_size; ++i) {
char* name = function.args[i];
Value value = eval(env, node.data.children.data[1].data.children.data[i]);
Env_insert_new(env, name, value);
}
Value result = (Value){
.kind = VALUE_KIND_NUMBER,
.number = 0,
};
for (size_t i = 0; i < function.body.length; ++i) {
result = eval(env, function.body.data[i]);
}
Env_leave_scope(env, scope_marker);
return result;
}
case VAR_REF: {
char* name = node.data.text;
return Env_get_value(env, name);
}
case NUMBER: return (Value){
.kind = VALUE_KIND_NUMBER,
.number = node.data.number,
};
case OR: {
assert(node.data.children.length == 2);
Value result = eval(env, node.data.children.data[0]);
if (result.kind != VALUE_KIND_NUMBER) {
fprintf(stderr, "error: lhs is not a number\n");
exit(EXIT_FAILURE);
}
if (result.number == 0) {
result = eval(env, node.data.children.data[1]);
}
return result;
}
case AND: {
assert(node.data.children.length == 2);
Value result = eval(env, node.data.children.data[0]);
if (result.kind != VALUE_KIND_NUMBER) {
fprintf(stderr, "error: lhs is not a number\n");
exit(EXIT_FAILURE);
}
if (result.number != 0) {
result = eval(env, node.data.children.data[1]);
}
return result;
}
case EQ:
case NE:
case LT:
case GT:
case LE:
case GE:
case ADD:
case SUB:
case MUL:
case DIV:
is_n_op = 2;
break;
case NOT:
case NEG:
is_n_op = 1;
break;
case PARAM_LIST: fprintf(stderr, "bug: PARAM_LIST should not be evaluated\n");
case PARAM: fprintf(stderr, "bug: PARAM should not be evaluated\n");
case NAME: fprintf(stderr, "bug: NAME should not be evaluated\n");
default:
assert(0);
}
assert(is_n_op == 1 || is_n_op == 2);
assert(node.data.children.length == is_n_op);
switch (is_n_op) {
case 1: {
Value result = eval(env, node.data.children.data[0]);
if (result.kind != VALUE_KIND_NUMBER) {
fprintf(stderr, "error: operand is not a number\n");
exit(EXIT_FAILURE);
}
switch (node.kind) {
case NOT: return (Value){
.kind = VALUE_KIND_NUMBER,
.number = (result.number == 0 ? 1 : 0),
};
case NEG: return (Value){
.kind = VALUE_KIND_NUMBER,
.number = -result.number,
};
default: assert(0);
}
}
case 2: {
Value lhs = eval(env, node.data.children.data[0]);
Value rhs = eval(env, node.data.children.data[1]);
if (lhs.kind != VALUE_KIND_NUMBER || rhs.kind != VALUE_KIND_NUMBER) {
fprintf(stderr, "error: operands are not numbers\n");
exit(EXIT_FAILURE);
}
double result;
switch (node.kind) {
case EQ:
result = (lhs.number == rhs.number ? 1 : 0);
break;
case NE:
result = (lhs.number != rhs.number ? 1 : 0);
break;
case LT:
result = (lhs.number < rhs.number ? 1 : 0);
break;
case GT:
result = (lhs.number > rhs.number ? 1 : 0);
break;
case LE:
result = (lhs.number <= rhs.number ? 1 : 0);
break;
case GE:
result = (lhs.number >= rhs.number ? 1 : 0);
break;
case ADD:
result = lhs.number + rhs.number;
break;
case SUB:
result = lhs.number - rhs.number;
break;
case MUL:
result = lhs.number * rhs.number;
break;
case DIV:
if (rhs.number == 0) {
fprintf(stderr, "error: division by zero\n");
exit(EXIT_FAILURE);
}
result = lhs.number / rhs.number;
break;
default: assert(0);
}
return (Value){
.kind = VALUE_KIND_NUMBER,
.number = result,
};
}
}
assert(0);
}
void start(Node root) {
Env env = Env_new(2);
eval(&env, root);
}
#include "lex.yy.h"
#include "y.tab.h"
int main(int argc, char** argv) {
if (argc > 1) {
FILE* file = fopen(argv[1], "r");
if (!file) {
fprintf(stderr, "Error: Could not open file '%s'\n", argv[1]);
return 1;
}
yyset_in(file);
}
yyparse();
if (argc > 1) {
fclose(yyget_in());
}
return 0;
}