-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.c
508 lines (467 loc) · 13.1 KB
/
run.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <stdlib.h>
#include <memory.h>
#include <assert.h>
#include <stdbool.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include "crossplatform/std.h"
#include "crossplatform/endian.h"
#include "run.h"
#include "scope.h"
#include "util.h"
#include "builtins/std.h"
#include "compile.h"
Variant cpy_var(Variant var)
{
Variant ret = var;
if (ret.type == TYPE_STRING) {
ret.u.str = strdup(var.u.str);
}
return ret;
}
void free_var(Variant var)
{
if (var.type == TYPE_STRING) {
free(var.u.str);
}
}
static lineno_t get_current_line(Runtime* R)
{
if (R->function == NULL) {
return 0;
}
return R->function->lineinfo[R->ip - R->function->code];
}
void runtimeerror(Runtime* R, char* fmt)
{
printf("Runtime Error: %s:%d\n", fmt, get_current_line(R));
R->hasError = true;
}
void raise_fatal(Runtime* R, char* fmt, ...)
{
printf("Fatal Error: ");
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(" in %s:%u\n", R->file, get_current_line(R));
R->hasError = true;
}
static Runtime* create_runtime(State* S)
{
Runtime* ret = malloc(sizeof(Runtime));
ret->stackcapacity = 10;
ret->stacksize = 0;
ret->stack = calloc(ret->stackcapacity, sizeof(*ret->stack));
ret->scope = create_scope();
ret->hasError = false;
ret->state = S;
ret->file = NULL;
ret->function = NULL;
return ret;
}
static void destroy_runtime(Runtime* R)
{
for (int i = 0; i < (int) R->stacksize; ++i) {
if (stackidx(R, i)->type == TYPE_STRING) {
free(stackidx(R, i)->u.str);
}
}
free(R->stack);
destroy_scope(R->scope);
free(R->file);
free(R);
}
static FunctionWrapper* find_function(State* S, const char* name)
{
for (size_t i = 0; i < S->funlen; ++i) {
if (strcmp(S->functions[i].name, name) == 0) {
return &S->functions[i];
}
}
return NULL;
}
static void run_call(Runtime* R)
{
const char* fnname = tostring(R, -1);
pop(R);
FunctionWrapper* callee = find_function(R->state, fnname);
if (!callee) {
raise_fatal(R, "Call to undefined function %s()", fnname);
free((void*)fnname);
return;
}
free((void*)fnname);
const uint8_t param_count = fetch8(R->ip++);
if (callee->type == FUNCTION) {
if (param_count != callee->u.function->paramlen) {
raise_fatal(R, "Parameter number mismatch. %u expected, %u given",
callee->u.function->paramlen, param_count);
return;
}
Runtime* newruntime = create_runtime(R->state);
for (int i = 0; i < param_count; ++i) {
set_var(newruntime, callee->u.function->params[i], *top(R), 0); // Transfer arguments
pop(R);
}
run_function(newruntime, callee->u.function);
push(R, *top(newruntime)); // Return variable
destroy_runtime(newruntime);
} else {
Runtime* newruntime = create_runtime(R->state);
for (int i = 0; i < param_count; ++i) {
push(newruntime, *top(R)); // Transfer arguments
pop(R);
}
callee->u.cfunction(newruntime);
push(R, *top(newruntime)); // Return variable
destroy_runtime(newruntime);
}
}
static void run_echo(Runtime* R)
{
char* str = tostring(R, -1);
printf("%s", str);
free(str);
pop(R);
}
static void run_stringaddexpr(Runtime* R)
{
char* rhs = tostring(R, -1);
pop(R);
char* lhs = tostring(R, -1);
pop(R);
char* ret = calloc((strlen(lhs) + strlen(rhs) + 1), sizeof(char));
strcat(ret, lhs);
strcat(ret, rhs);
pushstr(R, ret);
free(ret);
free(lhs);
free(rhs);
}
// https://github.com/php/php-langspec/blob/1dc4793ede53b12a2b193698d9ef44709bcf9b10/spec/10-expressions.md#relational-operators
static bool compare_equal(Variant lhs, Variant rhs)
{
switch (lhs.type) {
case TYPE_UNDEF:
return rhs.type == TYPE_UNDEF;
case TYPE_NULL:
if (lhs.type == rhs.type) {
return true;
}
return compare_equal(vartotype(lhs, rhs.type), rhs);
case TYPE_STRING:
if (rhs.type == TYPE_STRING) {
return strcmp(lhs.u.str, rhs.u.str) == 0;
}
if (rhs.type == TYPE_NULL) {
return compare_equal(lhs, vartotype(rhs, TYPE_STRING));
} else {
return compare_equal(vartotype(lhs, rhs.type), rhs);
}
case TYPE_LONG:
if (rhs.type == TYPE_LONG) {
return lhs.u.lint == rhs.u.lint;
}
if (rhs.type == TYPE_STRING || rhs.type == TYPE_NULL) {
return compare_equal(lhs, vartotype(rhs, TYPE_LONG));
} else {
return compare_equal(vartotype(lhs, rhs.type), rhs);
}
case TYPE_BOOL:
if (rhs.type == TYPE_BOOL) {
return lhs.u.boolean == rhs.u.boolean;
}
return compare_equal(lhs, vartotype(rhs, TYPE_BOOL));
case TYPE_CFUNCTION:
return rhs.type == TYPE_CFUNCTION && rhs.u.cfunction == lhs.u.cfunction;
case TYPE_FUNCTION:
return rhs.type == TYPE_FUNCTION && rhs.u.function == lhs.u.function;
case TYPE_MAX_VALUE:
assert(false && "Undefined Type given");
break;
}
assert(false);
return false;
}
static void run_binop_long(Runtime* R, int op)
{
int64_t rhs = tolong(R, -1);
pop(R);
int64_t lhs = tolong(R, -1);
pop(R);
int64_t result;
switch (op) {
case '+':
result = lhs + rhs;
break;
case '-':
result = lhs - rhs;
break;
case '*':
result = lhs * rhs;
break;
case '/':
result = lhs / rhs;
break;
case TK_SHL:
result = lhs << rhs;
break;
case TK_SHR:
result = lhs >> rhs;
break;
default:
assert(false);
return;
}
pushlong(R, result);
}
static void run_binop_bool(Runtime* R, int op)
{
int64_t rhs = tolong(R, -1);
pop(R);
int64_t lhs = tolong(R, -1);
pop(R);
bool result;
switch (op) {
case '<':
result = lhs < rhs;
break;
case '>':
result = lhs > rhs;
break;
case TK_AND:
result = lhs && rhs;
break;
case TK_OR:
result = lhs || rhs;
break;
case TK_LTEQ:
result = lhs <= rhs;
break;
case TK_GTEQ:
result = lhs >= rhs;
break;
default:
assert(false);
return;
}
pushbool(R, result);
}
static void run_eq(Runtime* R)
{
Variant* rhs = stackidx(R, -1);
Variant* lhs = stackidx(R, -2);
bool result = compare_equal(*lhs, *rhs);
popn(R, 2);
pushbool(R, result);
}
static void run_notop(Runtime* R)
{
int64_t lint = tolong(R, -1);
pop(R);
pushlong(R, !lint);
}
static void run_assignmentexpr(Runtime* R, Function* fn, int flags)
{
Variant* val = top(R);
set_var(R, fn->strs[fetch16(R->ip)], *val, flags);
R->ip += 2;
pop(R);
}
void run_function(Runtime* R, Function* fn)
{
R->function = fn;
R->ip = fn->code;
while ((size_t)(R->ip - fn->code) < fn->codesize && !R->hasError) {
Operator op = (Operator) *R->ip++;
int64_t lint;
Variant var;
switch (op) {
case OP_NOP:
break;
case OP_RETURN:
return; // Finish executing Function
case OP_CALL:
run_call(R);
break;
case OP_ECHO:
run_echo(R);
break;
case OP_STR:
pushstr(R, fn->strs[fetch16(R->ip)]);
R->ip += 2;
break;
case OP_LONG:
lint = (int64_t) fetch64(R->ip);
R->ip += 8;
pushlong(R, lint);
break;
case OP_TRUE:
pushbool(R, 1);
break;
case OP_FALSE:
pushbool(R, 0);
break;
case OP_NULL:
pushnull(R);
break;
case OP_LTE:
run_binop_bool(R, TK_LTEQ);
break;
case OP_GTE:
run_binop_bool(R, TK_GTEQ);
break;
case OP_LT:
run_binop_bool(R, '<');
break;
case OP_GT:
run_binop_bool(R, '>');
break;
case OP_NOT:
run_notop(R);
break;
case OP_AND:
run_binop_bool(R, TK_AND);
break;
case OP_OR:
run_binop_bool(R, TK_OR);
break;
case OP_EQ:
run_eq(R);
break;
case OP_CONCAT:
run_stringaddexpr(R);
break;
case OP_ADD:
run_binop_long(R, '+');
break;
case OP_SUB:
run_binop_long(R, '-');
break;
case OP_MUL:
run_binop_long(R, '*');
break;
case OP_DIV:
run_binop_long(R, '/');
break;
case OP_SHL:
run_binop_long(R, TK_SHL);
break;
case OP_SHR:
run_binop_long(R, TK_SHR);
break;
case OP_ADD1:
lint = tolong(R, -1);
pop(R);
pushlong(R, lint + 1);
break;
case OP_SUB1:
lint = tolong(R, -1);
pop(R);
pushlong(R, lint - 1);
break;
case OP_LOOKUP:
push(R, lookup(R, fn->strs[fetch16(R->ip)]));
R->ip += 2;
break;
case OP_CLOOKUP:
push(R, lookupWithFlags(R, fn->strs[fetch16(R->ip)], VAR_FLAG_CONST));
R->ip += 2;
break;
case OP_ASSIGN:
run_assignmentexpr(R, fn, 0);
break;
case OP_CONSTDECL:
run_assignmentexpr(R, fn, VAR_FLAG_CONST);
break;
case OP_DUP:
push(R, *top(R));
break;
case OP_JMP:
R->ip = fn->code + fetch32(R->ip);
break;
case OP_JMPZ:
if (tolong(R, -1) == 0) {
R->ip = fn->code + fetch32(R->ip);
} else {
R->ip += 4; // jump over jmpaddr
}
break;
case OP_CAST:
var = vartotype(*stackidx(R, -1), (VARIANTTYPE) fetch8(R->ip++));
pop(R);
push(R, var);
free_var(var);
break;
case OP_GETLINE:
pushlong(R, get_current_line(R));
break;
case OP_INVALID:
runtimeerror(R, "OP_INVALID");
break;
default:
runtimeerror(R, "Unexpected OP");
}
}
R->function = NULL;
}
static void init_builtin_functions(State* S)
{
for (size_t i = 0; i < arrcount(std_functions); ++i) {
CFunctionPair pair = std_functions[i];
addfunction(S, wrap_cfunction(pair.cfunction, strdup(pair.name)));
}
}
void run_file(const char* filepath) {
FILE* handle = fopen(filepath, "r");
AST* ast = parse(handle);
// print_ast(ast,0);
Function* fn = create_function();
State* S = create_state();
addfunction(S, wrap_function(fn, strdup("<pseudomain>")));
init_builtin_functions(S);
compile(S, fn, ast);
Runtime* R = create_runtime(S);
R->file = strdup(filepath);
// print_code(fn, "<pseudomain>");
run_function(R, fn);
destroy_state(S);
destroy_runtime(R);
destroy_ast(ast);
}
void print_stack(Runtime* R)
{
printf("Stack, size: %zu\n", R->stacksize);
puts("===========================\n");
for (int i = 0; i < (int) R->stacksize; ++i) {
Variant* var = stackidx(R, i);
printf("#%d ", i);
switch (var->type) {
case TYPE_UNDEF:
printf("UNDEFINED");
break;
case TYPE_STRING:
printf("STRING: %s", var->u.str);
break;
case TYPE_LONG:
printf("LONG: %" PRId64, var->u.lint);
break;
case TYPE_NULL:
printf("NULL");
break;
case TYPE_BOOL:
printf(var->u.boolean ? "TRUE" : "FALSE");
break;
case TYPE_FUNCTION:
case TYPE_CFUNCTION:
printf("FUNCTION");
break;
case TYPE_MAX_VALUE:
assert(false && "Undefined Type given");
break;
}
puts("");
}
}