@@ -93,40 +93,16 @@ int evalMain(Interpret *i) {
93
93
}
94
94
95
95
int interpretFunc (Stack * stack , Node * node ) {
96
-
97
- // if is node
98
- Stack * localStack = NULL ;
99
- SymbolTable * localTable = NULL ;
100
-
101
- Node * mainFn = table_lookup (symTableGlob , "Main.run" );
102
96
Function * f = node -> data .function ;
103
97
104
- (void ) mainFn ;
105
- (void ) localStack ;
106
-
107
- // if(strcmp(node->symbol, mainFn->symbol)){
108
- // dPrintf("%s", "Creating new local stack.\n");
109
- // localStack = createLocalStack(stack);
110
- // }
111
- // else{
112
- // dPrintf("%s", "Setting reference of: local stack to global stack\n");
113
- // localStack = GlobalStack;
114
- // }
115
-
116
- dPrintf ("%s" , "Creating new local table.\n" );
117
- localTable = createSymbolTable ();
98
+ SymbolTable * localTable = createSymbolTable ();
118
99
ht_insert (& alloc_tab , localTable );
119
-
120
- // assert(localStack != NULL);
121
- assert (localTable != NULL );
100
+ dPrintf ("%s" , "Creating new local table.\n" );
122
101
123
102
for (Command * c = f -> body .head ; c != NULL ; c = c -> next ) {
124
103
evalCommand (localTable , stack , c , getClassName (f -> name ));
125
104
}
126
105
127
- // Node *n = table_lookup_either(symTableGlob, localTable, getClassName(f->name), "b");
128
- // printValue(n->data.value);
129
-
130
106
return 0 ;
131
107
}
132
108
@@ -155,19 +131,12 @@ Value *evalCommand(SymbolTable *symTable, Stack *stack, Command *cmd, char *clas
155
131
breakFlag = FALSE;
156
132
returnFlag = FALSE;
157
133
158
- // printf("SWITCH CMD:\n");
159
- // printCommand(cmd);
160
- // printf("\n");
161
134
switch (cmd -> type ){
162
135
case (C_DECLARE ):
163
- // insert declaration into table
164
136
table_insert_dummy (symTable , cmd -> data .declare );
165
137
break ;
166
138
167
139
case (C_DEFINE ):
168
- // printf("\ncommand: ");
169
- // printCommand(cmd);
170
- // printf("\n");
171
140
val = evalExpression (symTable , stack , className , cmd -> data .define .expr );
172
141
val = coerceTo (cmd -> data .define .declaration .type , val );
173
142
table_insert (symTable , createValueNode (cmd -> data .define .declaration .name , val ));
@@ -329,99 +298,82 @@ int builtInFunc(SymbolTable *symTable, Stack *stack, Function *fn){
329
298
char * str = fn -> name ;
330
299
331
300
if (!strcmp (str , "ifj16.print" )){
332
- Value * term = popFromStack (stack );
333
-
334
- print (term );
335
-
301
+ Value * v = coerceTo (T_STRING , popFromStack (stack ));
302
+ print (v );
336
303
return 0 ;
337
304
}
338
305
else if (!strcmp (str , "ifj16.readInt" ) ){
339
306
Value * val = createValue (T_INTEGER );
340
307
I (val ) = readInt ();
341
308
342
309
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
343
-
344
310
return 0 ;
345
311
}
346
312
else if (!strcmp (str , "ifj16.readDouble" ) ){
347
313
Value * val = createValue (T_DOUBLE );
348
314
D (val ) = readDouble ();
349
315
350
316
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
351
-
352
317
return 0 ;
353
318
}
354
319
else if (!strcmp (str , "ifj16.readString" ) ){
355
320
Value * val = createValue (T_STRING );
356
321
S (val ) = readString ();
357
322
358
323
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
359
-
360
324
return 0 ;
361
325
}
362
326
else if (!strcmp (str , "ifj16.length" ) ){
327
+ Value * v = coerceTo (T_STRING , popFromStack (stack ));
363
328
364
- char * s = popFromStack (stack )-> data .str ;
365
329
Value * val = createValue (T_INTEGER );
366
- I (val ) = length (s );
330
+ I (val ) = length (S ( v ) );
367
331
368
332
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
369
-
370
333
return 0 ;
371
334
}
372
335
else if (!strcmp (str , "ifj16.substr" ) ){
373
- int n = popFromStack (stack )-> data . integer ;
374
- int i = popFromStack (stack )-> data . integer ;
375
- char * s = popFromStack (stack )-> data . str ;
336
+ Value * n = coerceTo ( T_INTEGER , popFromStack (stack )) ;
337
+ Value * i = coerceTo ( T_INTEGER , popFromStack (stack )) ;
338
+ Value * s = coerceTo ( T_STRING , popFromStack (stack )) ;
376
339
377
340
Value * val = createValue (T_STRING );
378
- S (val ) = substr (s , i , n );
341
+ S (val ) = substr (S ( s ), I ( i ), I ( n ) );
379
342
380
343
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
381
-
382
344
return 0 ;
383
345
}
384
346
else if (!strcmp (str , "ifj16.compare" )) {
385
- char * s2 = popFromStack (stack )-> data .str ;
386
- char * s1 = popFromStack (stack )-> data .str ;
387
-
388
- dPrintf ("s1: '%s', s2: '%s'" , s1 , s2 );
347
+ Value * s2 = coerceTo (T_STRING , popFromStack (stack ));
348
+ Value * s1 = coerceTo (T_STRING , popFromStack (stack ));
389
349
390
350
Value * val = createValue (T_INTEGER );
391
- I (val ) = compare (s1 , s2 );
351
+ I (val ) = compare (S ( s1 ), S ( s2 ) );
392
352
393
353
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
394
-
395
354
return 0 ;
396
355
}
397
356
else if (!strcmp (str , "ifj16.sort" ) ){
357
+ Value * s = coerceTo (T_STRING , popFromStack (stack ));
398
358
399
- Value * val = popFromStack (stack );
400
- char * s = val -> data .str ;
401
-
402
- val -> type = T_STRING ;
403
- S (val ) = sort (s );
359
+ Value * val = createValue (T_STRING );
360
+ S (val ) = sort (S (s ));
404
361
405
362
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
406
-
407
363
return 0 ;
408
364
}
409
365
else if (!strcmp (str , "ifj16.find" ) ){
366
+ Value * s2 = coerceTo (T_STRING , popFromStack (stack ));
367
+ Value * s1 = coerceTo (T_STRING , popFromStack (stack ));
410
368
411
- Value * val = popFromStack (stack );
412
- char * s2 = val -> data .str ;
413
-
414
- val = popFromStack (stack );
415
- char * s1 = val -> data .str ;
416
-
417
- val -> type = T_INTEGER ;
418
- I (val ) = find (s1 , s2 );
369
+ Value * val = createValue (T_INTEGER );
370
+ I (val ) = find (S (s1 ), S (s2 ));
419
371
420
372
stack -> prev != NULL ? pushToStack (stack -> prev , val ) : pushToStack (stack , val );
421
373
return 0 ;
422
374
}
423
- else
424
- return -1 ;
375
+
376
+ return -1 ;
425
377
}
426
378
427
379
int pushParamToStack (SymbolTable * symTable , Stack * stack , char * funcName , Expression * e ) {
@@ -458,6 +410,7 @@ Value *evalBinaryExpression(BinaryOperation op, Value *left, Value *right) {
458
410
S (result ) = malloc (sizeof (char ) * (strlen (l ) + strlen (r ) + 1 ));
459
411
strcpy (S (result ), l );
460
412
strcat (S (result ), r );
413
+ return result ;
461
414
}
462
415
463
416
if (left -> type == T_BOOLEAN && right -> type == T_BOOLEAN ) {
@@ -581,7 +534,7 @@ Value *evalStaticExpression(Expression *e) {
581
534
582
535
Value * evalExpression (SymbolTable * symTable , Stack * stack , char * className , Expression * e ) {
583
536
584
- if (e == NULL ){
537
+ if (e == NULL ) {
585
538
MERROR (ERR_INTERNAL , "evalExpression: Expression je null" );
586
539
return NULL ;
587
540
}
@@ -594,16 +547,11 @@ Value *evalExpression(SymbolTable *symTable, Stack *stack, char *className, Expr
594
547
595
548
switch (e -> type ) {
596
549
case E_FUNCALL :
597
- //printf("%s\n", "Som fcia");
598
550
localStack = createLocalStack (GlobalStack );
599
551
localSymTable = createSymbolTable ();
600
552
601
553
exp = e -> data .funcall .argHead ;
602
- // printf("Expression:\n");
603
- // printExpression(exp);
604
- // printf("\n" );
605
554
while (exp != NULL ) {
606
- // printf("exp type: %s\n", showExpressionType(exp->type));
607
555
if ((exp -> type == E_VALUE ) || (exp -> type == E_REFERENCE )){
608
556
node = table_lookup_either (symTableGlob , symTable , className , exp -> data .reference );
609
557
}
@@ -631,7 +579,6 @@ Value *evalExpression(SymbolTable *symTable, Stack *stack, char *className, Expr
631
579
return val ;
632
580
633
581
case E_REFERENCE :
634
- //printf("\nclassName:\n%s\n\nreference:%s\n",className,e->data.reference);
635
582
node = table_lookup_either (symTableGlob , symTable , className , e -> data .reference );
636
583
return node -> data .value ;
637
584
0 commit comments