feat: Add negation to codegen

This commit is contained in:
overflowerror 2024-03-06 15:57:50 +01:00
parent 7de4815ab8
commit 8126e835a3
3 changed files with 35 additions and 1 deletions

View file

@ -420,11 +420,31 @@ region_t* codegen_disjunction(FILE* out, scope_t* scope, region_t* op1, region_t
return op1;
}
region_t* codegen_negation(FILE* out, scope_t* scope, region_t* op) {
if (!op->is_temp) {
op = clone(op);
}
region_t* result = scope_add_tmp(scope, 1);
move_to(result); reset(); inc();
move_to(op);
loop({
move_to(result); dec();
move_to(op); reset();
});
scope_remove(scope, op);
return result;
}
region_t* codegen_calc_expr(FILE* out, scope_t* scope, struct calc_expression expr) {
region_t* operand1 = codegen_expr(out, scope, expr.operand1);
region_t* operand2 = codegen_expr(out, scope, expr.operand2);
region_t* operand2 = NULL;
if (expr.operand2) {
operand2 = codegen_expr(out, scope, expr.operand2);
}
region_t* result = NULL;
@ -456,6 +476,9 @@ region_t* codegen_calc_expr(FILE* out, scope_t* scope, struct calc_expression ex
case DISJUNCTION:
result = codegen_disjunction(out, scope, operand1, operand2);
break;
case NEGATION:
result = codegen_negation(out, scope, operand1);
break;
default:
fprintf(stderr, "unknown operator: %d\n", expr.operator);
panic("unknown operator");

View file

@ -0,0 +1,7 @@
if (!0) {
print "yes 0\n";
}
if (!1) {
print "no 1\n";
}

View file

@ -0,0 +1,4 @@
[-]>[-]+<[>-<[-]]>[>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<.>.>.>.>.>.
<<<<<<[-]]
<[-]+>[-]+<[>-<[-]]>[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<.>.>.>.>.
<<<<<[-]]