Skip to content

Commit

Permalink
fix: Do not eval bot cond-expr yes/no vals when cond is not eval yet
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Dec 7, 2024
1 parent 59a724b commit 0d921e1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,18 @@ apply_result *cond_apply(parser_node *node, context *ctx)
node_cond *cond = (node_cond *)node->data;
apply_result *cond_res = cond->cond->apply(cond->cond, ctx);
char *rega_cond = reg_a(cond_res->type, ctx);
apply_result *yes_val = cond->true_val->apply(cond->true_val, ctx);
apply_result *no_val = cond->false_val->apply(cond->false_val, ctx);
char *rega_val = reg_a(yes_val->type, ctx);

char *l1 = new_label(ctx);
char *l2 = new_label(ctx);
add_text(ctx, "mov %s, %s", rega_cond, cond_res->code);
add_text(ctx, "cmp %s, 0", rega_cond);
add_text(ctx, "je %s", l1);
apply_result *yes_val = cond->true_val->apply(cond->true_val, ctx);
char *rega_val = reg_a(yes_val->type, ctx);
add_text(ctx, "mov %s, %s", rega_val, yes_val->code);
add_text(ctx, "jmp %s", l2);
add_text(ctx, "%s:", l1);
apply_result *no_val = cond->false_val->apply(cond->false_val, ctx);
add_text(ctx, "mov %s, %s", rega_val, no_val->code);
add_text(ctx, "%s:", l2);
symbol *sym = new_temp_symbol(ctx, yes_val->type);
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"./examples/inp_arg_macro.c": [],
"./examples/inp_extern.c": [],
}
C_PROGRAM_NAME = "./a.out"
C_PROGRAM_NAME = "./30cc"
OUTPUT_FOLDER = "tests/output"
TEMP_FOLDER = "temp_snapshots"

Expand Down

0 comments on commit 0d921e1

Please sign in to comment.