fix: solve unary operation bug!

This commit is contained in:
NunoSempere 2023-04-30 12:35:00 -04:00
parent 6d4850e38b
commit a75fb677ab
2 changed files with 12 additions and 6 deletions

BIN
mumble

Binary file not shown.

View File

@ -37,20 +37,20 @@ void print_lispval(lispval l){
case LISPVAL_ERR: case LISPVAL_ERR:
switch(l.err){ switch(l.err){
case LISPERR_BAD_OP: case LISPERR_BAD_OP:
printf("Error: Invalid operator"); printf("\nError: Invalid operator");
break; break;
case LISPERR_BAD_NUM: case LISPERR_BAD_NUM:
printf("Error: Invalid number"); printf("\nError: Invalid number");
break; break;
case LISPERR_DIV_ZERO: case LISPERR_DIV_ZERO:
printf("Error: Division by zero"); printf("\nError: Division by zero");
break; break;
default: default:
printf("Error: Unknown error"); printf("\nError: Unknown error");
} }
break; break;
default: default:
printf("Unknown lispval type"); printf("\nUnknown lispval type");
} }
} }
@ -137,7 +137,7 @@ lispval evaluate_ast(mpc_ast_t* t)
lispval x; lispval x;
char* operation; char* operation;
// Case #4: Unary operations case // Case #4: Top level unary operation
if (t->children_num == 3 && is_ignorable(t->children[0]) && strstr(t->children[1]->tag, "operator")) { if (t->children_num == 3 && is_ignorable(t->children[0]) && strstr(t->children[1]->tag, "operator")) {
operation = t->children[1]->contents; operation = t->children[1]->contents;
if (VERBOSE) if (VERBOSE)
@ -153,12 +153,18 @@ lispval evaluate_ast(mpc_ast_t* t)
printf("\nCase #5, %s", operation); printf("\nCase #5, %s", operation);
x = evaluate_ast(t->children[2]); x = evaluate_ast(t->children[2]);
int i = 3; int i = 3;
int is_unary = 1;
while ((i < t->children_num) && strstr(t->children[i]->tag, "expr")) { while ((i < t->children_num) && strstr(t->children[i]->tag, "expr")) {
// note that when reaching a closing parenthesis, ^ returns false // note that when reaching a closing parenthesis, ^ returns false
lispval y = evaluate_ast(t->children[i]); lispval y = evaluate_ast(t->children[i]);
x = evaluate_operation(operation, x, y); x = evaluate_operation(operation, x, y);
i++; i++;
is_unary = 0;
} }
if(is_unary){
printf("\nCase #5.b, unary operation %s", operation);
x = evaluate_unary_operation(operation, x);
}
} }
return x; return x;