Skip to content

Commit 486a807

Browse files
committed
coerceTo in builtins, fix evalBinaryExpression, fix parseReturn.
1 parent f54cedf commit 486a807

File tree

3 files changed

+25
-92
lines changed

3 files changed

+25
-92
lines changed

interpret.c

Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,16 @@ int evalMain(Interpret *i) {
9393
}
9494

9595
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");
10296
Function *f = node->data.function;
10397

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();
11899
ht_insert (&alloc_tab, localTable);
119-
120-
// assert(localStack != NULL);
121-
assert(localTable != NULL);
100+
dPrintf("%s", "Creating new local table.\n");
122101

123102
for (Command *c = f->body.head; c != NULL; c = c->next) {
124103
evalCommand(localTable, stack, c, getClassName(f->name));
125104
}
126105

127-
// Node *n = table_lookup_either(symTableGlob, localTable, getClassName(f->name), "b");
128-
// printValue(n->data.value);
129-
130106
return 0;
131107
}
132108

@@ -155,19 +131,12 @@ Value *evalCommand(SymbolTable *symTable, Stack *stack, Command *cmd, char *clas
155131
breakFlag = FALSE;
156132
returnFlag = FALSE;
157133

158-
// printf("SWITCH CMD:\n");
159-
// printCommand(cmd);
160-
// printf("\n");
161134
switch(cmd->type){
162135
case(C_DECLARE):
163-
// insert declaration into table
164136
table_insert_dummy(symTable, cmd->data.declare);
165137
break;
166138

167139
case(C_DEFINE):
168-
// printf("\ncommand: ");
169-
// printCommand(cmd);
170-
// printf("\n");
171140
val = evalExpression(symTable, stack, className, cmd->data.define.expr);
172141
val = coerceTo(cmd->data.define.declaration.type, val);
173142
table_insert(symTable, createValueNode(cmd->data.define.declaration.name, val));
@@ -329,99 +298,82 @@ int builtInFunc(SymbolTable *symTable, Stack *stack, Function *fn){
329298
char *str = fn->name;
330299

331300
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);
336303
return 0;
337304
}
338305
else if(!strcmp(str, "ifj16.readInt") ){
339306
Value *val = createValue(T_INTEGER);
340307
I(val) = readInt();
341308

342309
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
343-
344310
return 0;
345311
}
346312
else if(!strcmp(str, "ifj16.readDouble") ){
347313
Value *val = createValue(T_DOUBLE);
348314
D(val) = readDouble();
349315

350316
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
351-
352317
return 0;
353318
}
354319
else if(!strcmp(str, "ifj16.readString") ){
355320
Value *val = createValue(T_STRING);
356321
S(val) = readString();
357322

358323
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
359-
360324
return 0;
361325
}
362326
else if(!strcmp(str, "ifj16.length") ){
327+
Value *v = coerceTo(T_STRING, popFromStack(stack));
363328

364-
char *s = popFromStack(stack)->data.str;
365329
Value *val = createValue(T_INTEGER);
366-
I(val) = length(s);
330+
I(val) = length(S(v));
367331

368332
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
369-
370333
return 0;
371334
}
372335
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));
376339

377340
Value *val = createValue(T_STRING);
378-
S(val) = substr(s, i, n);
341+
S(val) = substr(S(s), I(i), I(n));
379342

380343
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
381-
382344
return 0;
383345
}
384346
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));
389349

390350
Value *val = createValue(T_INTEGER);
391-
I(val) = compare(s1, s2);
351+
I(val) = compare(S(s1), S(s2));
392352

393353
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
394-
395354
return 0;
396355
}
397356
else if(!strcmp(str, "ifj16.sort") ){
357+
Value *s = coerceTo(T_STRING, popFromStack(stack));
398358

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));
404361

405362
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
406-
407363
return 0;
408364
}
409365
else if(!strcmp(str, "ifj16.find") ){
366+
Value *s2 = coerceTo(T_STRING, popFromStack(stack));
367+
Value *s1 = coerceTo(T_STRING, popFromStack(stack));
410368

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));
419371

420372
stack->prev != NULL ? pushToStack(stack->prev, val) : pushToStack(stack, val);
421373
return 0;
422374
}
423-
else
424-
return -1;
375+
376+
return -1;
425377
}
426378

427379
int pushParamToStack(SymbolTable *symTable, Stack *stack, char* funcName, Expression *e) {
@@ -458,6 +410,7 @@ Value *evalBinaryExpression(BinaryOperation op, Value *left, Value *right) {
458410
S(result) = malloc(sizeof(char) * (strlen(l) + strlen(r) + 1));
459411
strcpy(S(result), l);
460412
strcat(S(result), r);
413+
return result;
461414
}
462415

463416
if (left->type == T_BOOLEAN && right->type == T_BOOLEAN) {
@@ -581,7 +534,7 @@ Value *evalStaticExpression(Expression *e) {
581534

582535
Value *evalExpression(SymbolTable *symTable, Stack *stack, char *className, Expression *e) {
583536

584-
if(e == NULL){
537+
if (e == NULL) {
585538
MERROR(ERR_INTERNAL, "evalExpression: Expression je null");
586539
return NULL;
587540
}
@@ -594,16 +547,11 @@ Value *evalExpression(SymbolTable *symTable, Stack *stack, char *className, Expr
594547

595548
switch (e->type) {
596549
case E_FUNCALL:
597-
//printf("%s\n", "Som fcia");
598550
localStack = createLocalStack(GlobalStack);
599551
localSymTable = createSymbolTable();
600552

601553
exp = e->data.funcall.argHead;
602-
// printf("Expression:\n");
603-
// printExpression(exp);
604-
// printf("\n" );
605554
while (exp != NULL) {
606-
// printf("exp type: %s\n", showExpressionType(exp->type));
607555
if ((exp->type == E_VALUE) || (exp->type == E_REFERENCE)){
608556
node = table_lookup_either(symTableGlob, symTable, className, exp->data.reference);
609557
}
@@ -631,7 +579,6 @@ Value *evalExpression(SymbolTable *symTable, Stack *stack, char *className, Expr
631579
return val;
632580

633581
case E_REFERENCE:
634-
//printf("\nclassName:\n%s\n\nreference:%s\n",className,e->data.reference);
635582
node = table_lookup_either(symTableGlob, symTable, className, e->data.reference);
636583
return node->data.value;
637584

parser.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ bool parseBreak(Lexer *l, Block *b) {
334334
bool parseReturn(Lexer *l, Block *b) {
335335
tryReserved(l, RES_RETURN, false);
336336
if (isSymbol(l, SYM_SEMI)) {
337+
nextToken(l);
337338
appendToBlock(b, createCommandReturn(NULL));
338339
return true;
339340
}

stringology.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,7 @@ char *substr(char *s,int i, int n) {
173173
}
174174

175175
int compare(char *s1, char *s2) {
176-
//unsigned int s1len = strlen(s1);
177-
//unsigned int s2len = strlen(s2);
178-
179176
return strcmp(s1,s2);
180-
/*if (s1len > s2len)
181-
return 1;
182-
if (s1len < s2len)
183-
return -1;
184-
185-
for (unsigned int i = 0; i < s1len; i++) {
186-
if (s1[i] > s2[i])
187-
return 1;
188-
if (s1[i] < s2[i])
189-
return -1;
190-
}
191-
return 0; */
192177
}
193178

194179
void print(Value *term){

0 commit comments

Comments
 (0)