From 88dd752e2d163880a5e306d7414a2d99950f27f1 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 2 Mar 2024 19:48:09 +0100 Subject: [PATCH] feat: Add implementation of conjunction and disjunction --- compiler/src/codegen.c | 71 ++++++++++++++++++++++++++ compiler/test/cases/018a-print-and.in | 15 ++++++ compiler/test/cases/018a-print-and.out | 8 +++ compiler/test/cases/018b-print-or.in | 15 ++++++ compiler/test/cases/018b-print-or.out | 8 +++ 5 files changed, 117 insertions(+) create mode 100644 compiler/test/cases/018a-print-and.in create mode 100644 compiler/test/cases/018a-print-and.out create mode 100644 compiler/test/cases/018b-print-or.in create mode 100644 compiler/test/cases/018b-print-or.out diff --git a/compiler/src/codegen.c b/compiler/src/codegen.c index f8d16cb..767c19b 100644 --- a/compiler/src/codegen.c +++ b/compiler/src/codegen.c @@ -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"); diff --git a/compiler/test/cases/018a-print-and.in b/compiler/test/cases/018a-print-and.in new file mode 100644 index 0000000..1089339 --- /dev/null +++ b/compiler/test/cases/018a-print-and.in @@ -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"; +} \ No newline at end of file diff --git a/compiler/test/cases/018a-print-and.out b/compiler/test/cases/018a-print-and.out new file mode 100644 index 0000000..e751bfa --- /dev/null +++ b/compiler/test/cases/018a-print-and.out @@ -0,0 +1,8 @@ +[-]>[-]>[-]<<[>[>+<[-]]<[-]]>>[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<.>.>.>.>.>. +<<<<<<[-]] +<<[-]>[-]+>[-]<<[>[>+<[-]]<[-]]>>[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<.>.>.>.>.>. +<<<<<<[-]] +<<[-]+>[-]>[-]<<[>[>+<[-]]<[-]]>>[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<.>.>.>.>.>. +<<<<<<[-]] +<<[-]+>[-]+>[-]<<[>[>+<[-]]<[-]]>>[>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<<.>.>.>.>.>.>. +<<<<<<<[-]] diff --git a/compiler/test/cases/018b-print-or.in b/compiler/test/cases/018b-print-or.in new file mode 100644 index 0000000..3922446 --- /dev/null +++ b/compiler/test/cases/018b-print-or.in @@ -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"; +} \ No newline at end of file diff --git a/compiler/test/cases/018b-print-or.out b/compiler/test/cases/018b-print-or.out new file mode 100644 index 0000000..0b62506 --- /dev/null +++ b/compiler/test/cases/018b-print-or.out @@ -0,0 +1,8 @@ +[-]>[-]>[-]<<[>>+<<[-]]>[>+<[-]]>[<<+>>[-]]<<[>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<.>.>.>.>.>. +<<<<<<[-]] +[-]>[-]+>[-]<<[>>+<<[-]]>[>+<[-]]>[<<+>>[-]]<<[>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<<.>.>.>.>.>.>. +<<<<<<<[-]] +[-]+>[-]>[-]<<[>>+<<[-]]>[>+<[-]]>[<<+>>[-]]<<[>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<<.>.>.>.>.>.>. +<<<<<<<[-]] +[-]+>[-]+>[-]<<[>>+<<[-]]>[>+<[-]]>[<<+>>[-]]<<[>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<<.>.>.>.>.>.>. +<<<<<<<[-]]