-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.c
737 lines (629 loc) · 23 KB
/
interpret.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
/*
* Project: IFJ16, a programming language interpreter
* FIT VUT Brno
* Authors: xzaryb00 - Zarybnický Jakub
* xtamas01 - Tamaškovič Marek
* xvasko12 - Vaško Martin
* xvasko14 - Vaško Michal
* xzales12 - Záleský Jiří
*/
#include "interpret.h"
bool jirkaDouble = false;
static SymbolTable *symTableGlob = NULL;
static Stack *GlobalStack = NULL;
// cycle helpers.
static bool continueFlag = false;
static bool breakFlag = false;
static bool returnFlag = false;
#define FN(i, ret, fnName, count, arg) do { \
char *name = strdup_("ifj16." #fnName); \
Function *f = createFunction(name, ret, count, arg); \
f->builtin = true; \
table_insert_function(&i->symTable, f); \
} while (0);
Interpret *createInterpret(void) {
Interpret *i = malloc_c(sizeof(Interpret));
i->symTable.root = NULL;
Declaration *printArg = createDeclaration(T_STRING, strdup_("x"));
Declaration *sortArg = createDeclaration(T_STRING, strdup_("x"));
Declaration *lengthArg = createDeclaration(T_STRING, strdup_("x"));
Declaration *findArg1 = createDeclaration(T_STRING, strdup_("x"));
Declaration *findArg2 = createDeclaration(T_STRING, strdup_("y"));
findArg2->next = findArg1;
Declaration *compareArg1 = createDeclaration(T_STRING, strdup_("x"));
Declaration *compareArg2 = createDeclaration(T_STRING, strdup_("y"));
compareArg2->next = compareArg1;
Declaration *substrArg1 = createDeclaration(T_STRING, strdup_("x"));
Declaration *substrArg2 = createDeclaration(T_INTEGER, strdup_("n"));
Declaration *substrArg3 = createDeclaration(T_INTEGER, strdup_("l"));
substrArg2->next = substrArg1;
substrArg3->next = substrArg2;
table_insert(&i->symTable, createClassNode(strdup_("ifj16")));
FN(i, T_INTEGER, readInt, 0, NULL);
FN(i, T_DOUBLE, readDouble, 0, NULL);
FN(i, T_STRING, readString, 0, NULL);
FN(i, T_VOID, print, 1, printArg);
FN(i, T_STRING, sort, 1, sortArg);
FN(i, T_INTEGER, length, 1, lengthArg);
FN(i, T_INTEGER, find, 2, findArg2);
FN(i, T_INTEGER, compare, 2, compareArg2);
FN(i, T_STRING, substr, 3, substrArg3);
return i;
}
// Free symbolTable
int freeInterpret(Interpret *i) {
if (i == NULL)
return 1;
if (i->symTable.root != NULL)
freeNode(i->symTable.root);
free_c(i);
return 0;
}
int evalMain(Interpret *i) {
assert(i != NULL);
symTableGlob = &(i->symTable);
GlobalStack = createLocalStack(NULL);
Node *mainFn = table_lookup(&i->symTable, "Main.run");
assert(mainFn != NULL);
assert(mainFn->type == N_FUNCTION);
interpretFunc(GlobalStack, mainFn);
free_c(GlobalStack);
return 0;
}
int interpretFunc(Stack *stack, Node *node) {
Function *f = node->data.function;
SymbolTable *localTable = createSymbolTable();
dPrintf("%s", "Creating new local table.\n");
for (Command *c = f->body.head; c != NULL; c = c->next) {
char *className=getClassName(f->name);
evalCommand(localTable, stack, c, className);
free_c(className);
}
if (localTable != NULL)
freeSymbolTable(localTable);
return 0;
}
// A core of every cycle is basicaly the same -> why not use some macros?
#define CYCLE_INNER(symTable, stack, funcName, body, end) \
do { \
for (Command *c = body.head; c != NULL; c = c->next) { \
evalCommand(symTable, stack, c, funcName); \
if (breakFlag) { \
breakFlag = false; \
goto end; \
} \
if (continueFlag) { \
continueFlag = false; \
break; \
} \
} \
} while (0);
Value *evalCommand(SymbolTable *symTable, Stack *stack, Command *cmd, char *className){
Node *node;
Value *val = NULL;
// Initialize cycle helpers
continueFlag = false;
breakFlag = false;
returnFlag = false;
//switch for command type
dPrintf("CMD TYPE:%s",showCommandType(cmd->type));
switch(cmd->type){
case C_DECLARE:
table_insert_dummy(symTable, cmd->data.declare);
break;
case C_DEFINE:
val = coerceTo(cmd->data.define.declaration.type,
evalExpression(symTable, stack, className, cmd->data.define.expr, U_ID));
table_insert(symTable, createValueNode(cmd->data.define.declaration.name, val));
break;
case C_ASSIGN:
node = table_lookup_either(symTableGlob, symTable, className, cmd->data.assign.name);
if(node == NULL)
MERROR(ERR_INTERNAL, "Interpret: CMD: Assign: Variable not found in local or global symbol table.");
val = evalExpression(symTable, stack, className, cmd->data.assign.expr, U_ID);
if (val == NULL)
MERROR(ERR_INTERNAL, "Interpret: CMD: Assign: Evaluation of value was not successful.")
node->data.value = coerceTo(node->data.value->type, val);
break;
case C_BLOCK:
evalBlock(symTable, stack, &(cmd->data.block), className);
break;
case C_IF:
if (evalCondition(symTable, stack, className, cmd->data.ifC.cond)) {
evalBlock(symTable, stack, &(cmd->data.ifC.thenBlock), className);
} else {
evalBlock(symTable, stack, &(cmd->data.ifC.elseBlock), className);
}
break;
case C_EXPRESSION:
val = evalExpression(symTable, stack, className, cmd->data.expr, U_ID);
break;
case C_RETURN:
if(cmd->data.expr != NULL){
val = evalExpression(symTable, stack, className, cmd->data.expr, U_ID);
pushToStack(GlobalStack, val);
}
returnFlag = true;
break;
case C_CONTINUE:
continueFlag = true;
break;
case C_BREAK:
breakFlag = true;
break;
case C_FOR:
if (cmd->data.forC.initial != NULL) {
val = evalExpression(symTable, stack, className, cmd->data.forC.initial, U_ID);
val = coerceTo(cmd->data.forC.var.type, val);
} else {
val = createValue(cmd->data.forC.var.type);
val->undefined = true;
}
node = createValueNode(cmd->data.forC.var.name, val);
table_insert(symTable, node);
while (evalCondition(symTable, stack, className, cmd->data.forC.cond)){
CYCLE_INNER(symTable, stack, className, cmd->data.forC.bodyBlock, for_end);
val = evalCommand(symTable, stack, cmd->data.forC.iter, className);
node->data.value = coerceTo(cmd->data.forC.var.type, val);
}
for_end:
table_remove(symTable, cmd->data.forC.var.name);
break;
case C_WHILE:
while (evalCondition(symTable, stack, className, cmd->data.whileC.cond)) {
CYCLE_INNER(symTable, stack, className, cmd->data.whileC.bodyBlock, while_end);
}
while_end:
break;
case C_DO_WHILE:
do {
CYCLE_INNER(symTable, stack, className, cmd->data.doWhileC.bodyBlock, doWhile_end);
} while (evalCondition(symTable, stack, className, cmd->data.doWhileC.cond));
doWhile_end:
break;
}
return val;
}
bool evalCondition(SymbolTable *symTable, Stack *stack, char *funcName, Expression *cond) {
Value *val = evalExpression(symTable, stack, funcName, cond, U_ID);
if (val == NULL)
MERROR(ERR_RUNTIME_MISC, "Interpret: CMD: evalCondition: Evaluation was unsuccessful.");
if (val->type != T_BOOLEAN)
MERROR(ERR_RUNTIME_MISC, "Interpret: CMD: evalCondition: Condition is not a boolean expression");
return B(val);
}
int evalBlock(SymbolTable *symTable, Stack *stack, Block *block, char *className){
Command *current = block->head;
while (current != NULL) {
if (continueFlag)
continueFlag = false;
evalCommand(symTable, stack, current,className);
if(continueFlag){
current = block->head;
break;
}
else if(breakFlag){
break;
}
else if(returnFlag){
break;
}
current = current->next;
}
return 0;
}
void evalFunction(Stack *localStack, SymbolTable* localSymTable, Function *fn, char *className) {
#ifdef DEBUG
printf("evalFunction: %s\n", fn->name);
#endif
// First check for builtins
if (fn->builtin) {
builtInFunc(localStack, fn);
}
// Else push params to local sym table
Declaration *d = fn->argHead;
while (localStack->size > -1) {
Value *val = popFromStack(localStack);
table_insert(localSymTable, createValueNode(d->name, val));
d = d->next;
}
evalBlock(localSymTable, localStack, &(fn->body), className);
}
/**
* Look for builtin functions
*/
int builtInFunc(Stack *stack, Function *fn){
returnFlag = true;
jirkaDouble = true;
char *str = fn->name;
if(!strcmp(str, "ifj16.print")){
Value *v = coerceTo(T_STRING, popFromStack(stack));
printf("%s", v->data.str);
returnFlag = false;
return 0;
}
else if(!strcmp(str, "ifj16.readInt") ){
Value *val = createValue(T_INTEGER);
I(val) = readInt();
pushToStack(GlobalStack, val);
return 0;
}
else if(!strcmp(str, "ifj16.readDouble") ){
Value *val = createValue(T_DOUBLE);
D(val) = readDouble();
pushToStack(GlobalStack, val);
return 0;
}
else if(!strcmp(str, "ifj16.readString") ){
Value *val = createValue(T_STRING);
S(val) = readString();
pushToStack(GlobalStack, val);
return 0;
}
else if(!strcmp(str, "ifj16.length") ){
Value *v = coerceTo(T_STRING, popFromStack(stack));
Value *val = createValue(T_INTEGER);
I(val) = strlen(S(v));
pushToStack(GlobalStack, val);
return 0;
}
else if(!strcmp(str, "ifj16.substr") ){
Value *n = coerceTo(T_INTEGER, popFromStack(stack));
Value *i = coerceTo(T_INTEGER, popFromStack(stack));
Value *s = coerceTo(T_STRING, popFromStack(stack));
Value *val = createValue(T_STRING);
S(val) = substr(S(s), I(i), I(n));
pushToStack(GlobalStack, val);
return 0;
}
else if (!strcmp(str, "ifj16.compare")) {
Value *s2 = coerceTo(T_STRING, popFromStack(stack));
Value *s1 = coerceTo(T_STRING, popFromStack(stack));
Value *val = createValue(T_INTEGER);
int i = strcmp(S(s1), S(s2));
I(val) =
i < 0 ? -1 :
i > 0 ? 1 :
0;
pushToStack(GlobalStack, val);
return 0;
}
else if(!strcmp(str, "ifj16.sort") ){
Value *s = coerceTo(T_STRING, popFromStack(stack));
Value *val = createValue(T_STRING);
S(val) = sort(S(s));
pushToStack(GlobalStack, val);
return 0;
}
else if(!strcmp(str, "ifj16.find") ){
Value *s2 = coerceTo(T_STRING, popFromStack(stack));
Value *s1 = coerceTo(T_STRING, popFromStack(stack));
Value *val = createValue(T_INTEGER);
I(val) = find(S(s1), S(s2));
pushToStack(GlobalStack, val);
return 0;
}
return -1;
}
int pushParamToStack(SymbolTable *symTable, Stack *stack, char* funcName, Expression *e) {
assert(e != NULL);
pushToStack(stack, evalExpression(symTable, stack, funcName, e, U_ID));
return 0;
}
Value *evalUnaryExpression(UnaryOperation op, Value *v) {
if (v->undefined) {
ERROR(ERR_RUNTIME_UNINITIALIZED);
}
if (v->type == T_BOOLEAN && op == U_NOT) {
Value *r = createValue(T_BOOLEAN);
B(r) = !B(v);
return r;
}
if (v->type != T_DOUBLE && v->type != T_INTEGER) {
Value *r = createValue(T_VOID);
r->undefined = true;
return r;
}
switch (op) {
case U_ID:
case U_PREINC:
case U_POSTINC:
case U_PREDEC:
case U_POSTDEC:
return v; //FIXME: increment while evaluating the reference
case U_NEG:
if (v->type == T_INTEGER) {
Value *r = createValue(T_INTEGER);
I(r) = -I(v);
return r;
} else if (v->type == T_DOUBLE) {
Value *r = createValue(T_DOUBLE);
D(r) = -D(v);
return r;
}
break;
case U_NOT:
if (v->type == T_BOOLEAN) {
Value *r = createValue(T_BOOLEAN);
B(r) = !B(v);
return r;
}
break;
}
//...the compiler can't do enum switch path analysis
Value *r = createValue(T_VOID);
r->undefined = true;
return r;
}
Value *evalBinaryExpression(BinaryOperation op, Value *left, Value *right) {
if (left->undefined || right->undefined) {
ERROR(ERR_RUNTIME_UNINITIALIZED);
}
if (left->type == T_VOID || right->type == T_VOID) {
Value *result = createValue(T_VOID);
result->undefined = true;
return result;
}
if (left->type == T_STRING || right->type == T_STRING) {
if (op != EB_ADD) {
Value *result = createValue(T_VOID);
result->undefined = true;
return result;
}
char *l = ( left->type == T_STRING) ? S( left) : S(coerceTo(T_STRING, left));
char *r = (right->type == T_STRING) ? S(right) : S(coerceTo(T_STRING, right));
Value *result = createValue(T_STRING);
S(result) = malloc_c(sizeof(char) * (strlen(l) + strlen(r) + 1));
strcpy(S(result), l);
strcat(S(result), r);
return result;
}
if (left->type == T_BOOLEAN && right->type == T_BOOLEAN) {
Value *result = createValue(T_BOOLEAN);
switch(op) {
case EB_EQUAL:
B(result) = B(left) == B(right);
return result;
case EB_NOT_EQUAL:
B(result) = B(left) != B(right);
return result;
case EB_AND:
B(result) = B(left) && B(right);
return result;
case EB_OR:
B(result) = B(left) || B(right);
return result;
default:
return evalBinaryExpression(op, coerceTo(T_STRING, left), coerceTo(T_STRING, right));
}
}
if (left->type == T_INTEGER && right->type == T_INTEGER) {
Value *result = createValue(T_BOOLEAN);
switch(op) {
case EB_EQUAL:
B(result) = I(left) == I(right);
return result;
case EB_NOT_EQUAL:
B(result) = I(left) != I(right);
return result;
case EB_LESS:
B(result) = I(left) < I(right);
return result;
case EB_LESS_EQUAL:
B(result) = I(left) <= I(right);
return result;
case EB_GREATER:
B(result) = I(left) > I(right);
return result;
case EB_GREATER_EQUAL:
B(result) = I(left) >= I(right);
return result;
case EB_ADD:
result->type = T_INTEGER;
I(result) = I(left) + I(right);
return result;
case EB_SUBTRACT:
result->type = T_INTEGER;
I(result) = I(left) - I(right);
return result;
case EB_MULTIPLY:
result->type = T_INTEGER;
I(result) = I(left) * I(right);
return result;
case EB_DIVIDE:
if (I(right) == 0)
ERROR(ERR_RUNTIME_DIV_BY_ZERO);
result->type = T_INTEGER;
I(result) = I(left) / I(right);
return result;
default:
return evalBinaryExpression(op, coerceTo(T_STRING, left), coerceTo(T_STRING, right));
}
}
if (( left->type == T_DOUBLE || left->type == T_INTEGER) &&
(right->type == T_DOUBLE || right->type == T_INTEGER)) {
Value *result = createValue(T_BOOLEAN);
switch(op) {
case EB_EQUAL:
B(result) = DVAL(left) == DVAL(right);
return result;
case EB_NOT_EQUAL:
B(result) = DVAL(left) != DVAL(right);
return result;
case EB_LESS:
B(result) = DVAL(left) < DVAL(right);
return result;
case EB_LESS_EQUAL:
B(result) = DVAL(left) <= DVAL(right);
return result;
case EB_GREATER:
B(result) = DVAL(left) > DVAL(right);
return result;
case EB_GREATER_EQUAL:
B(result) = DVAL(left) >= DVAL(right);
return result;
case EB_ADD:
result->type = T_DOUBLE;
D(result) = DVAL(left) + DVAL(right);
return result;
case EB_SUBTRACT:
result->type = T_DOUBLE;
D(result) = DVAL(left) - DVAL(right);
return result;
case EB_MULTIPLY:
result->type = T_DOUBLE;
D(result) = DVAL(left) * DVAL(right);
return result;
case EB_DIVIDE:
if (fabs(DVAL(right)) < 10e-7)
ERROR(ERR_RUNTIME_DIV_BY_ZERO);
result->type = T_DOUBLE;
D(result) = DVAL(left) / DVAL(right);
return result;
case EB_AND:
case EB_OR:
return evalBinaryExpression(op, coerceTo(T_STRING, left), coerceTo(T_STRING, right));
}
}
Value *result = createValue(T_VOID);
result->undefined = true;
return result;
}
Value *evalStaticExpression(Expression *e) {
switch (e->type) {
case E_FUNCALL:
case E_REFERENCE:
MERROR(ERR_SEM_MISC, "Cannot use dynamic expressions during static variable initialization!");
case E_VALUE:
return e->data.value;
case E_UNARY:
return evalUnaryExpression(e->data.unary.op,
evalStaticExpression(e->data.unary.e));
case E_BINARY:
return evalBinaryExpression(e->data.binary.op,
evalStaticExpression(e->data.binary.left),
evalStaticExpression(e->data.binary.right));
}
return NULL; //Just to pacify the compiler...
}
Value *evalExpression(SymbolTable *symTable, Stack *stack, char *className, Expression *e, UnaryOperation op) {
if (e == NULL) {
MERROR(ERR_INTERNAL, "evalExpression: Expression je null");
}
Value *val = NULL;
Stack *localStack;
SymbolTable *localSymTable;
Expression *exp;
static Value one = { .type = T_INTEGER, .data = { .integer = 1 } };
switch (e->type) {
case E_FUNCALL:
if (op > U_ID && op < U_NOT) {
MERROR(ERR_INTERNAL, "Trying to pre/postinc/dec a non-reference");
}
localStack = createLocalStack(GlobalStack);
localSymTable = createSymbolTable();
exp = e->data.funcall.argHead;
while (exp != NULL) {
pushToStack(localStack, evalExpression(symTable, localStack, className, exp, U_ID));
exp = exp->next;
}
if (strchr(e->data.funcall.name, '.') != NULL) {
className = getClassName(e->data.funcall.name);
}
Node *node = table_lookup_either(symTableGlob, NULL, className, e->data.funcall.name);
Function *f = node->data.function;
evalFunction(localStack, localSymTable, f, className);
free_c(localStack);
if (returnFlag) {
returnFlag = false;
return coerceTo(f->returnType, popFromStack(GlobalStack));
}
return NULL;
case E_REFERENCE:
node = table_lookup_either(symTableGlob, symTable, className, e->data.reference);
val = node->data.value;
switch (op) {
case U_ID:
case U_NOT:
case U_NEG:
break;
case U_PREINC:
node->data.value = evalBinaryExpression(EB_ADD, node->data.value, &one);
break;
case U_PREDEC:
node->data.value = evalBinaryExpression(EB_SUBTRACT, node->data.value, &one);
break;
case U_POSTINC:
val = copyValue(val);
node->data.value = evalBinaryExpression(EB_ADD, node->data.value, &one);
break;
case U_POSTDEC:
val = copyValue(val);
node->data.value = evalBinaryExpression(EB_SUBTRACT, node->data.value, &one);
break;
}
return val;
case E_VALUE:
if (op > U_ID && op < U_NOT) {
MERROR(ERR_INTERNAL, "Trying to pre/postinc/dec a non-reference");
}
if (e->data.value->undefined) {
MERROR(ERR_RUNTIME_UNINITIALIZED, "Trying to work with uninitialized value");
}
return e->data.value;
case E_BINARY:
if (op > U_ID && op < U_NOT) {
MERROR(ERR_INTERNAL, "Trying to pre/postinc/dec a non-reference");
}
return evalBinaryExpression(e->data.binary.op,
evalExpression(symTable, stack, className, e->data.binary.left, U_ID),
evalExpression(symTable, stack, className, e->data.binary.right, U_ID));
case E_UNARY:
if (op > U_ID && op < U_NOT) {
MERROR(ERR_INTERNAL, "Trying to pre/postinc/dec a non-reference");
}
return evalUnaryExpression(e->data.unary.op,
evalExpression(symTable, stack, className, e->data.unary.e, e->data.unary.op));
}
return NULL; //Just to pacify the compiler...
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~Function handling~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void printStack(Stack *stack){
if(stack == NULL){
printf("Stack is not initialized\n");
}
printf("size: %d cap: %d\n", stack->size, stack->cap);
printf("|-----------------|\n ");
for (int i = stack->size; i >= 0; i--) {
printValue(stack->data[i]);
printf("\n|-----------------|\n ");
}
}
Stack *createLocalStack(Stack *stack) {
//allocate space for 5 params.
Stack *tmp = malloc_c(sizeof(Stack) + 5 * sizeof(Value *));
tmp->prev = stack;
tmp->data[0] = NULL;
tmp->size = -1;
tmp->cap = 5;
return tmp;
}
int pushToStack(Stack *stack, Value *val){
assert(stack != NULL);
assert(val != NULL);
stack->size++;
//reallocate whole stack if there is no space in stack;
if (stack->size == stack->cap) {
stack->cap *= 2;
stack = realloc_c(stack, sizeof(stack) + stack->cap);
}
stack->data[stack->size] = val;
return 0;
}
Value *popFromStack(Stack *stack){
assert(stack != NULL);
if (stack->size == -1) {
return NULL;
}
return stack->data[stack->size--];
}