-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.c
778 lines (735 loc) · 24.8 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "stmt.h"
#include "expr.h"
#include "display.h"
#include "environment.h"
#include "allocator.h"
#include "io.h"
#include "interpreter.h"
#include "native.h"
#define EPSILON 0.0000000000000000000000001
static Object resolveExpression(Expression* expression, Environment *env);
static Object executeBlock(Block b, Environment *env);
static int instanceCount = 0;
static Environment *globalEnv = NULL;
static int brk = 0, ret = 0;
static int isNumeric(Literal l){
return l.type == LIT_INT || l.type == LIT_DOUBLE;
}
static Literal resolveLiteral(Expression *expression, int line, Environment *env){
Object o = resolveExpression(expression, env);
if(o.type == OBJECT_NULL)
return nullLiteral;
if(o.type != OBJECT_LITERAL){
printf(runtime_error("Expected literal, Received %d!"), line, o.type);
stop();
}
return o.literal;
}
static Object fromLiteral(Literal l){
Object o = {OBJECT_LITERAL, {l}};
return o;
}
static Object resolveBinary(Binary expr, Environment *env){
Literal left = resolveLiteral(expr.left, expr.line, env);
Literal right = resolveLiteral(expr.right, expr.line, env);
// printf("\n[Binary] Got %s and %s for operator %s", literalNames[left.type], literalNames[right.type], tokenNames[expr.op.type]);
if(left.type == LIT_STRING && right.type == LIT_STRING && expr.op.type == TOKEN_PLUS){
Literal ret = {expr.line, LIT_STRING, {0}};
ret.sVal = (char *)mallocate(sizeof(char) * (strlen(left.sVal) + strlen(right.sVal) + 1));
ret.sVal[0] = 0;
strcat(ret.sVal, left.sVal);
strcat(ret.sVal, right.sVal);
return fromLiteral(ret);
}
else if (!isNumeric(left) || !isNumeric(right)){
printf(runtime_error("Binary operation can only be done on numerical values!"), expr.line);
stop();
return nullObject;
}
Literal ret = {expr.line, LIT_NULL, {0}};
ret.line = left.line;
if(left.type == LIT_INT && right.type == LIT_INT){
ret.type = LIT_INT;
switch(expr.op.type){
case TOKEN_PLUS:
ret.iVal = left.iVal + right.iVal;
break;
case TOKEN_MINUS:
ret.iVal = left.iVal - right.iVal;
break;
case TOKEN_STAR:
ret.iVal = left.iVal * right.iVal;
break;
case TOKEN_SLASH:
ret.iVal = left.iVal / right.iVal;
break;
case TOKEN_CARET:
ret.iVal = pow(left.iVal, right.iVal);
break;
case TOKEN_PERCEN:
ret.iVal = left.iVal % right.iVal;
break;
default:
break;
}
}
else{
ret.type = LIT_DOUBLE;
double a = left.type == LIT_INT?left.iVal:left.dVal;
double b = right.type == LIT_INT?right.iVal:right.dVal;
switch(expr.op.type){
case TOKEN_PLUS:
ret.dVal = a + b;
break;
case TOKEN_MINUS:
ret.dVal = a - b;
break;
case TOKEN_STAR:
ret.dVal = a * b;
break;
case TOKEN_SLASH:
ret.dVal = a / b;
break;
case TOKEN_CARET:
ret.dVal = pow(a, b);
break;
case TOKEN_PERCEN:
printf(runtime_error("%% can only be applied between two integers!"), expr.line);
stop();
break;
default:
break;
}
}
return fromLiteral(ret);
}
static Object compareInstance(Logical expr, Environment *env){
Object a = resolveExpression(expr.left, env);
Object b = resolveExpression(expr.right, env);
if(a.type == OBJECT_INSTANCE && b.type == OBJECT_INSTANCE){
Literal ret = {expr.line, LIT_LOGICAL, {0}};
switch(expr.op.type){
case TOKEN_EQUAL_EQUAL:
ret.lVal = a.instance == b.instance;
break;
case TOKEN_BANG_EQUAL:
ret.lVal = a.instance != b.instance;
break;
default:
printf(runtime_error("Can't compare container instances!"), expr.line);
stop();
break;
}
return fromLiteral(ret);
}
else if((a.type == OBJECT_INSTANCE && b.type == OBJECT_LITERAL)
|| (a.type == OBJECT_LITERAL && b.type == OBJECT_INSTANCE)){
Literal lit = a.type == OBJECT_LITERAL ? a.literal : b.literal;
Instance* o = a.type == OBJECT_INSTANCE ? a.instance : b.instance;
if(lit.type != LIT_NULL){
printf(runtime_error("Unable to compare between literal and instances!"), expr.line);
stop();
}
Literal ret = {expr.line, LIT_LOGICAL, {0}};
switch(expr.op.type){
case TOKEN_EQUAL_EQUAL:
ret.lVal = o == NULL;
break;
case TOKEN_BANG_EQUAL:
ret.lVal = o != NULL;
break;
default:
printf(runtime_error("Null can't be compared!"), expr.line);
stop();
break;
}
return fromLiteral(ret);
}
return nullObject;
}
static Object resolveLogical(Logical expr, Environment *env){
Object r = compareInstance(expr, env);
if(r.type != OBJECT_NULL)
return r;
Literal left = resolveLiteral(expr.left, expr.line, env);
Literal right = resolveLiteral(expr.right, expr.line, env);
// printf("\n[Logical] Got %s and %s for operator %s", literalNames[left.type], literalNames[right.type], tokenNames[expr.op.type]);
if(left.type == LIT_NULL || right.type == LIT_NULL){
Literal ret = {expr.line, LIT_LOGICAL, {0}};
switch(expr.op.type){
case TOKEN_EQUAL_EQUAL:
ret.lVal = left.type == LIT_NULL && right.type == LIT_NULL;
break;
case TOKEN_BANG_EQUAL:
ret.lVal = left.type != LIT_NULL || right.type != LIT_NULL;
break;
default:
printf(runtime_error("Unable to compare Null!"), expr.line);
break;
}
return fromLiteral(ret);
}
if(left.type == LIT_STRING && right.type == LIT_STRING){
Literal ret = {expr.line, LIT_LOGICAL, {0}};
ret.line = left.line;
switch(expr.op.type){
case TOKEN_GREATER:
ret.lVal = strlen(left.sVal) > strlen(right.sVal);
break;
case TOKEN_GREATER_EQUAL:
ret.lVal = strlen(left.sVal) >= strlen(right.sVal);
break;
case TOKEN_LESS:
ret.lVal = strlen(left.sVal) < strlen(right.sVal);
break;
case TOKEN_LESS_EQUAL:
ret.lVal = strlen(left.sVal) <= strlen(right.sVal);
break;
case TOKEN_EQUAL_EQUAL:
ret.lVal = strcmp(left.sVal, right.sVal) == 0?1:0;
break;
case TOKEN_BANG_EQUAL:
ret.lVal = strcmp(left.sVal, right.sVal) == 0?0:1;
break;
default:
printf(runtime_error("Bad logical operator between string operands!"), expr.line);
stop();
break;
}
return fromLiteral(ret);
}
else if((!isNumeric(left) && left.type != LIT_LOGICAL)
|| (!isNumeric(right) && right.type != LIT_LOGICAL)){
printf(runtime_error("Bad operand for logical operator!"), expr.line);
stop();
return nullObject;
}
Literal ret;
ret.type = LIT_LOGICAL;
ret.line = left.line;
double a = left.type == LIT_INT?left.iVal:left.dVal;
double b = right.type == LIT_INT?right.iVal:right.dVal;
switch(expr.op.type){
case TOKEN_GREATER:
ret.lVal = a > b;
break;
case TOKEN_GREATER_EQUAL:
ret.lVal = a >= b;
break;
case TOKEN_LESS:
ret.lVal = a < b;
break;
case TOKEN_LESS_EQUAL:
ret.lVal = a <= b;
break;
case TOKEN_EQUAL_EQUAL:
ret.lVal = fabs(a - b) <= EPSILON;
break;
case TOKEN_BANG_EQUAL:
ret.lVal = fabs(a - b) > EPSILON;
break;
case TOKEN_AND:
if(left.type != LIT_LOGICAL || right.type != LIT_LOGICAL){
printf(runtime_error("'And' can only be applied over logical expressions!"), expr.line);
stop();
}
ret.lVal = left.lVal & right.lVal;
break;
case TOKEN_OR:
if(left.type != LIT_LOGICAL || right.type != LIT_LOGICAL){
printf(runtime_error("'Or' can only be applied over logical expressions!"), expr.line);
stop();
}
ret.lVal = left.lVal | right.lVal;
break;
default:
break;
}
return fromLiteral(ret);
}
static Object resolveVariable(Variable expr, Environment *env){
return env_get(expr.name, expr.line, env);
}
static Object resolveArray(ArrayExpression ae, Environment *env){
Literal index = resolveLiteral(ae.index, ae.line, env);
if(index.type != LIT_INT){
printf(runtime_error("Array index must be an integer!"), ae.line);
stop();
}
Object get = env_get(ae.identifier, ae.line, env);
if(get.type == OBJECT_LITERAL && get.literal.type == LIT_STRING){
char *s = get.literal.sVal;
long le = strlen(s);
long in = index.lVal;
if(in < 1 || in > (le+1)){
printf(runtime_error("String index out of range [%ld]!"), ae.line, in);
stop();
}
if(in == le+1)
return nullObject;
char c = s[in - 1];
char *cs = (char *)mallocate(sizeof(char) * 2);
cs[0] = c;
cs[1] = '\0';
Literal l;
l.type = LIT_STRING;
l.sVal = cs;
return fromLiteral(l);
}
return env_arr_get(ae.identifier, ae.line, index.iVal, env);
}
static Object resolveRoutineCall(Call c, Environment *env){
Routine r = env_routine_get(c.identifer, c.line, globalEnv);
//printf("\nResolving call to %s", c.identifer);
if(r.arity != c.argCount){
printf(runtime_error("Argument count mismatch for routine %s! Expected : %d Received %d!"),
c.line, c.identifer, r.arity, c.argCount);
stop();
return nullObject;
}
Environment *routineEnv = env_new(globalEnv);
int i = 0;
// printf("\n[Call] Executing %s Arity : %d\n", r.name, r.arity);
while(i < r.arity){
// printf(debug("Argument %s"), r.arguments[i]);
env_put(r.arguments[i], c.line, resolveExpression(c.arguments[i], env), routineEnv);
i++;
}
Object obj;
if(r.isNative == 1)
obj = handle_native(c, routineEnv);
// printf("\n[Call] Executing %s\n", r.name);
else
obj = executeBlock(r.code, routineEnv);
if(ret)
ret = 0;
env_free(routineEnv);
return obj;
}
static Object resolveContainerCall(Call c, Environment *env){
Container r = env_container_get(c.identifer, c.line, globalEnv);
//printf("\nResolving call to %s", c.identifer);
if(r.arity != c.argCount){
printf(runtime_error("Argument count mismatch for container %s! Expected : %d Received %d!"),
c.line, c.identifer, r.arity, c.argCount);
stop();
return nullObject;
}
Environment *containerEnv = env_new(globalEnv);
int i = 0;
// printf("\n[Call] Executing container %s\n", r.name);
while(i < r.arity){
env_put(r.arguments[i], c.line, resolveExpression(c.arguments[i], env), containerEnv);
i++;
}
// printf("\n[Call] Executing %s\n", r.name);
executeBlock(r.constructor, containerEnv);
Object o;
o.type = OBJECT_INSTANCE;
o.instance = (Instance *)mallocate(sizeof(Instance));
o.instance->name = r.name;
o.instance->environment = containerEnv;
o.instance->refCount = 0;
o.instance->insCount = ++instanceCount;
o.instance->fromReturn = 0;
return o;
}
static Object resolveCall(Call c, Environment *env){
if(strcmp(c.identifer, "Main") == 0)
return resolveRoutineCall(c, env);
Object callee = env_get(c.identifer, c.line, env);
if(callee.type == OBJECT_ROUTINE)
return resolveRoutineCall(c, env);
else
return resolveContainerCall(c, env);
}
static Object resolveReference(Reference ref, Environment *env){
Object o = resolveExpression(ref.containerName, env);
if(o.type != OBJECT_INSTANCE){
printf(runtime_error("Invalid member reference!"), ref.line);
stop();
}
return resolveExpression(ref.member, (Environment *)o.instance->environment);
}
static Object resolveExpression(Expression* expression, Environment *env){
// printf("\nSolving expression : %s", expressionNames[expression->type]);
switch(expression->type){
case EXPR_LITERAL:
return fromLiteral(expression->literal);
case EXPR_BINARY:
return resolveBinary(expression->binary, env);
case EXPR_LOGICAL:
return resolveLogical(expression->logical, env);
case EXPR_NONE:
return nullObject;
case EXPR_VARIABLE:
return resolveVariable(expression->variable, env);
case EXPR_ARRAY:
return resolveArray(expression->arrayExpression, env);
case EXPR_CALL:
return resolveCall(expression->callExpression, env);
case EXPR_REFERENCE:
return resolveReference(expression->referenceExpression, env);
}
}
static void printString(const char *s){
int i = 0, len = strlen(s);
//printf("\nPrinting : %s", s);
while(i < len){
if(s[i] == '\\' && i < (len - 1)){
if(s[i+1] == 'n'){
putchar('\n');
i++;
}
else if(s[i+1] == 't'){
putchar('\t');
i++;
}
else if(s[i+1] == '"'){
putchar('"');
i++;
}
else
putchar('\\');
}
else
putchar(s[i]);
i++;
}
}
static void printLiteral(Literal result){
switch(result.type){
case LIT_NULL:
printf("Null");
break;
case LIT_LOGICAL:
printf("%s", result.lVal == 0 ? "False" : "True");
break;
case LIT_DOUBLE:
printf("%g", result.dVal);
break;
case LIT_INT:
printf("%ld", result.iVal);
break;
case LIT_STRING:
printString(result.sVal);
break;
}
}
static void printObject(Object o){
switch(o.type){
case OBJECT_ARRAY:
printf("<array of %d>", o.arr.count);
break;
case OBJECT_CONTAINER:
printf("<container %s>", o.container.name);
break;
case OBJECT_INSTANCE:
printf("<instance of container %s>", o.instance->name);
break;
case OBJECT_ROUTINE:
printf("<routine %s>", o.routine.name);
break;
case OBJECT_NULL:
printf("Null");
break;
case OBJECT_LITERAL:
printLiteral(o.literal);
break;
}
}
static Object executePrint(Print p, Environment *env){
int i = 0;
while(i < p.argCount){
Object o = resolveExpression(p.expressions[i], env);
printObject(o);
i = i+1;
}
return nullObject;
}
static Object executeStatement(Statement s, Environment *env);
static Object executeBlock(Block b, Environment *env){
//debug("Executing block statement");
int num = 0;
Object retl = nullObject;
while(num < b.numStatements){
retl = executeStatement(b.statements[num], env);
if(brk || ret)
break;
num++;
}
return retl;
}
static Object executeIf(If ifs, Environment *env){
//debug("Executing if statement");
Literal cond = resolveLiteral(ifs.condition, ifs.line, env);
if(cond.type != LIT_LOGICAL){
printf(runtime_error("Not a logical expression as condition!"), ifs.line);
stop();
return nullObject;
}
if(cond.lVal){
return executeBlock(ifs.thenBranch, env);
}
else{
return executeBlock(ifs.elseBranch, env);
}
}
static Object executeWhile(While w, Environment *env){
//debug("Executing while statement");
Literal cond = resolveLiteral(w.condition, w.line, env);
if(cond.type != LIT_LOGICAL){
printf(runtime_error("Not a logical expression as condition!"), w.line);
stop();
return nullObject;
}
Object retl = nullObject;
while(cond.lVal){
retl = executeBlock(w.body, env);
if(brk){
brk = 0;
break;
}
if(ret)
return retl;
cond = resolveLiteral(w.condition, w.line, env);
}
return nullObject;
}
static void write_array(Expression *id, Expression *initializerExpression, Environment *resEnv,
Environment *writeEnv, int line){
Literal index = resolveLiteral(id->arrayExpression.index, line, resEnv);
if(index.type != LIT_INT){
printf(runtime_error("Array index must be an integer!"), line);
stop();
}
Object get = env_get(id->arrayExpression.identifier, line, writeEnv);
if(get.type == OBJECT_LITERAL && get.literal.type == LIT_STRING){
Literal rep = resolveLiteral(initializerExpression, line, resEnv);
if(index.lVal < 1){
printf(runtime_error("String index must be positive!"), line);
stop();
}
if(rep.type != LIT_STRING){
printf(runtime_error("Bad assignment to a string!"), line);
stop();
}
if(strlen(rep.sVal) > 1)
printf(warning("[Line %d] Ignoring extra characters while assignment!"), line);
get.literal.sVal[index.lVal - 1] = rep.sVal[0];
}
else
env_arr_put(id->arrayExpression.identifier, line, index.iVal,
resolveExpression(initializerExpression, resEnv), writeEnv);
}
static void write_ref(Expression *id, Expression *init, Environment *resEnv,
Environment *writeEnv, int line){
Object ref = resolveExpression(id->referenceExpression.containerName, writeEnv);
if(ref.type != OBJECT_INSTANCE){
printf(runtime_error("Referenced item is not an instance of a container!"), line);
stop();
}
Set s;
Expression *mem = id->referenceExpression.member;
if(mem->type == EXPR_ARRAY){
write_array(mem, init, resEnv, (Environment *)ref.instance->environment, line);
}
else if(mem->type == EXPR_VARIABLE){
Object value = resolveExpression(init, resEnv);
env_put(mem->variable.name, s.line, value, (Environment *)ref.instance->environment);
}
else if(mem->type == EXPR_REFERENCE){
write_ref(mem, init, resEnv, (Environment *)ref.instance->environment, line);
}
else{
printf(runtime_error("Bad member access for container type '%s'"),
s.line, ref.instance->name);
stop();
}
}
static Object executeSet(Set s, Environment *env){
//debug("Executing set statement");
int i = 0;
while(i < s.count){
Expression *id = s.initializers[i].identifer;
Expression *init = s.initializers[i].initializerExpression;
if(id->type == EXPR_VARIABLE)
env_put(id->variable.name, s.line, resolveExpression(init, env), env);
else if(id->type == EXPR_ARRAY){
write_array(id, init, env, env, s.line);
}
else if(id->type == EXPR_REFERENCE){
write_ref(id, init, env, env, s.line);
}
else{
printf(runtime_error("Bad assignment target!"), s.line);
stop();
return nullObject;
}
i++;
}
return nullObject;
}
static Object executeArray(ArrayInit ai, Environment *env){
int i = 0;
while(i < ai.count){
Expression *iden = ai.initializers[i];
Literal init = resolveLiteral(iden->arrayExpression.index, ai.line, env);
if(init.type != LIT_INT){
printf(runtime_error("Array dimension must be an integer!"), ai.line);
stop();
return nullObject;
}
env_arr_new(iden->arrayExpression.identifier, ai.line, init.iVal, env);
i++;
}
return nullObject;
}
static Object executeInput(InputStatement is, Environment *env){
int i = 0;
while(i < is.count){
Input in = is.inputs[i];
switch(in.type){
case INPUT_PROMPT:
printString(in.prompt);
break;
case INPUT_IDENTIFER:
{
char *ide = in.identifer;
switch(in.datatype){
case INPUT_ANY:
env_put(ide, is.line, fromLiteral(getString(is.line)), env);
break;
case INPUT_FLOAT:
env_put(ide, is.line, fromLiteral(getFloat(is.line)), env);
break;
case INPUT_INT:
env_put(ide, is.line, fromLiteral(getInt(is.line)), env);
break;
}
}
}
i++;
}
return nullObject;
}
static Object executeBreak(){
//debug("Executing break statement");
brk = 1;
return nullObject;
}
static Object executeEnd(){
//debug("Executing end statement");
memfree_all();
exit(0);
return nullObject;
}
static Object executeBegin(){
//debug("Executing begin statement");
warning("Begin is a no-op!\n");
return nullObject;
}
static Object registerRoutine(Routine r){
env_routine_put(r, r.line, globalEnv);
return nullObject;
}
static Object registerContainer(Container c){
env_container_put(c, c.line, globalEnv);
return nullObject;
}
static Object executeCall(CallStatement cs, Environment *env){
if(cs.callee->type != EXPR_CALL){
printf(runtime_error("Expected call expression!"), cs.line);
stop();
}
else{
Object o = resolveCall(cs.callee->callExpression, env);
if(o.type != OBJECT_NULL)
printf(warning("[Line %d] Ignoring return value!"), cs.line);
if(o.type == OBJECT_INSTANCE){
gc_obj(o);
}
}
return nullObject;
}
static Object executeReturn(ReturnStatement rs, Environment *env){
Object retl = nullObject;
if(rs.value != NULL)
retl = resolveExpression(rs.value, env);
// printf(debug("Returing object of type %d"), retl.type);
if(retl.type == OBJECT_INSTANCE){
retl.instance->fromReturn = 1;
// printf(debug("Incrementing ref to %d of %s#%d\n"), retl.instance->refCount,
// retl.instance->name, retl.instance->insCount);
}
ret = 1;
return retl;
}
static Object executeStatement(Statement s, Environment *env){
switch(s.type){
case STATEMENT_PRINT:
return executePrint(s.printStatement, env);
case STATEMENT_IF:
return executeIf(s.ifStatement, env);
case STATEMENT_WHILE:
return executeWhile(s.whileStatement, env);
case STATEMENT_SET:
return executeSet(s.setStatement, env);
case STATEMENT_ARRAY:
return executeArray(s.arrayStatement, env);
case STATEMENT_INPUT:
return executeInput(s.inputStatement, env);
case STATEMENT_BREAK:
return executeBreak();
case STATEMENT_END:
return executeEnd();
case STATEMENT_BEGIN:
return executeBegin();
// case STATEMENT_DO:
// executeDo(s.)
case STATEMENT_ROUTINE:
return registerRoutine(s.routine);
case STATEMENT_CONTAINER:
return registerContainer(s.container);
case STATEMENT_CALL:
return executeCall(s.callStatement, env);
case STATEMENT_NOOP:
break;
case STATEMENT_RETURN:
return executeReturn(s.returnStatement, env);
default:
break;
}
return nullObject;
}
void interpret(Code c){
int i = 0;
globalEnv = env_new(NULL);
register_native(globalEnv);
while(i < c.count){
executeStatement(c.parts[i], globalEnv);
i++;
}
Call call;
call.argCount = 0;
call.identifer = strdup("Main");
call.arguments = NULL;
call.line = 0;
clock_t start = clock();
resolveCall(call, globalEnv);
clock_t end = clock();
printf(debug("[Interpreter] Execution time : %gms"), (double)(end-start)/CLOCKS_PER_SEC);
unload_all();
env_free(globalEnv);
}
void stop(){
printf("\n");
unload_all();
memfree_all();
exit(1);
}