feat: Add implementation of conjunction and disjunction

This commit is contained in:
overflowerror 2024-03-02 19:48:09 +01:00
parent 35aa317fde
commit 88dd752e2d
5 changed files with 117 additions and 0 deletions

View file

@ -357,6 +357,71 @@ region_t* codegen_not_equals(FILE* out, scope_t* scope, region_t* op1, region_t*
return op1;
}
region_t* codegen_conjunction(FILE* out, scope_t* scope, region_t* op1, region_t* op2) {
if (!op1->is_temp) {
op1 = clone(op1);
}
if (!op2->is_temp) {
op2 = clone(op2);
}
region_t* tmp = scope_add_tmp(scope, 1);
move_to(tmp); reset();
move_to(op1);
loop({
move_to(op2);
loop({
move_to(tmp); inc();
move_to(op2); reset();
});
move_to(op1); reset();
});
scope_remove(scope, op1);
scope_remove(scope, op2);
return tmp;
}
region_t* codegen_disjunction(FILE* out, scope_t* scope, region_t* op1, region_t* op2) {
if (!op1->is_temp) {
op1 = clone(op1);
}
if (!op2->is_temp) {
op2 = clone(op2);
}
region_t* tmp = scope_add_tmp(scope, 1);
move_to(tmp); reset();
move_to(op1);
loop({
move_to(tmp); inc();
move_to(op1); reset();
});
move_to(op2);
loop({
move_to(tmp); inc();
move_to(op2); reset();
});
move_to(tmp);
loop({
move_to(op1); inc();
move_to(tmp); reset();
})
scope_remove(scope, op2);
scope_remove(scope, tmp);
return op1;
}
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);
@ -385,6 +450,12 @@ region_t* codegen_calc_expr(FILE* out, scope_t* scope, struct calc_expression ex
case NOT_EQUALS:
result = codegen_not_equals(out, scope, operand1, operand2);
break;
case CONJUNCTION:
result = codegen_conjunction(out, scope, operand1, operand2);
break;
case DISJUNCTION:
result = codegen_disjunction(out, scope, operand1, operand2);
break;
default:
fprintf(stderr, "unknown operator: %d\n", expr.operator);
panic("unknown operator");

View file

@ -0,0 +1,15 @@
if (0 && 0) {
print "no 00\n";
}
if (0 && 1) {
print "no 01\n";
}
if (1 && 0) {
print "no 10\n";
}
if (1 && 1) {
print "yes 11\n";
}

View file

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

View file

@ -0,0 +1,15 @@
if (0 || 0) {
print "no 00\n";
}
if (0 || 1) {
print "yes 01\n";
}
if (1 || 0) {
print "yes 10\n";
}
if (1 || 1) {
print "yes 11\n";
}

View file

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