From e0ca664381066668073270ce1fe3e0813b25bfb1 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 26 Mar 2024 13:35:27 -0400 Subject: More bool ops --- compiler.c | 81 ++++++++++++++++++++++++++----------------- tests/test_conditional_4.tnsl | 16 +++++++++ tests/test_conditional_5.tnsl | 20 +++++++++++ tests/test_conditional_6.tnsl | 8 +++++ 4 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 tests/test_conditional_4.tnsl create mode 100644 tests/test_conditional_5.tnsl create mode 100644 tests/test_conditional_6.tnsl diff --git a/compiler.c b/compiler.c index da2bd70..ef86ae7 100644 --- a/compiler.c +++ b/compiler.c @@ -1725,10 +1725,20 @@ void var_op_not(CompData *out, Variable *base) { } char *not_store = _var_get_store(out, base); - - vect_push_string(&out->text, "\tnot "); - vect_push_free_string(&out->text, not_store); - vect_push_string(&out->text, " ; Complete not\n"); + if (base->type != NULL && strcmp(base->type->name, "bool") == 0) { + // boolean not + vect_push_string(&out->text, "\tnot "); + vect_push_string(&out->text, not_store); + vect_push_string(&out->text, "\n"); + vect_push_string(&out->text, "\tand "); + vect_push_free_string(&out->text, not_store); + vect_push_string(&out->text, ", 1 ; Complete and\n"); + } else { + // normal not + vect_push_string(&out->text, "\tnot "); + vect_push_free_string(&out->text, not_store); + vect_push_string(&out->text, " ; Complete not\n"); + } } // test base with itself @@ -1749,7 +1759,7 @@ void var_op_test(CompData *out, Variable *base) { vect_push_free_string(&out->text, test_store); vect_push_string(&out->text, ", "); vect_push_free_string(&out->text, test_from); - vect_push_string(&out->text, " ; Complete not\n"); + vect_push_string(&out->text, " ; Complete test\n"); } // bit shift base left by "bsl" @@ -1918,26 +1928,6 @@ void var_op_ne(CompData *out, Variable *base, Variable *cmp) { *base = tmp; } -// Boolean and -void var_op_band(CompData *out, Scope *s) { - // Just parsed the left side, short circuit if zero - vect_push_string(&out->text, "\tjz "); - vect_push_free_string(&out->text, scope_gen_bool_label(s)); - vect_push_string(&out->text, "false ; bool and\n\n"); -} - -// Boolean or -void var_op_bor(CompData *out, Scope *s) { - // Just parsed the left side, short circuit if zero - vect_push_string(&out->text, "\tjnz "); - vect_push_free_string(&out->text, scope_gen_bool_label(s)); - vect_push_string(&out->text, "true ; bool or\n\n"); -} - -void var_op_bxor(CompData *out, Variable *lhs, Variable *rhs) { - // Nothing for now -} - void var_op_inc(CompData *out, Variable *lhs) { if (lhs->location == LOC_LITL) { lhs->offset++; @@ -4808,6 +4798,7 @@ Variable _eval_literal(Scope *s, CompData *data, Vector *tokens, size_t literal) vect_push_string(&data->text, "\ttest rax, rax ; literal bool\n\n"); out.offset = 0; } + out.location = LOC_LITL; } else { out.location = LOC_LITL; if (t->data[0] == '\'') @@ -4843,9 +4834,11 @@ Variable _eval(Scope *s, CompData *data, Vector *tokens, size_t start, size_t en } else { i = dcl; } - } else if (t->type == TT_AUGMENT && op_order(t) > op) { - op = op_order(t); - op_pos = i; + } else if (t->type == TT_AUGMENT) { + if (op_order(t) > op || (op_order(t) == op && op == 9)) { + op = op_order(t); + op_pos = i; + } } } @@ -4919,9 +4912,35 @@ Variable _eval(Scope *s, CompData *data, Vector *tokens, size_t start, size_t en // if boolean, eval left to right, not right to left if (op == 9) { - printf("ERROR: TO IMPL BOOL\n"); - p2_error = true; - return out; + Variable lhs = _eval(s, data, tokens, start, op_pos); + + char chk = op_token->data[0]; + if (op_token->data[0] == '!') { + printf("WARN: TO IMPL BOOLEAN NOT\n"); + chk = op_token->data[1]; + } + + Variable rhs; + if (chk == '&') { + vect_push_string(&data->text, "\tjz "); + vect_push_free_string(&data->text, scope_gen_bool_label(s)); + vect_push_string(&data->text, " ; boolean and\n"); + rhs = _eval(s, data, tokens, op_pos + 1, end); + } else if (chk == '|') { + vect_push_string(&data->text, "\tjnz "); + vect_push_free_string(&data->text, scope_gen_bool_label(s)); + vect_push_string(&data->text, " ; boolean or\n"); + rhs = _eval(s, data, tokens, op_pos + 1, end); + } else if (chk == '^') { + + } + + vect_push_free_string(&data->text, scope_gen_bool_label(s)); + vect_push_string(&data->text, ": ; boolean end\n"); + scope_adv_bool_label(s); + + var_end(&rhs); + return lhs; } if (op_pos == start) { diff --git a/tests/test_conditional_4.tnsl b/tests/test_conditional_4.tnsl new file mode 100644 index 0000000..d31944a --- /dev/null +++ b/tests/test_conditional_4.tnsl @@ -0,0 +1,16 @@ + +/; main [int] + int a = 0 + int b = 69 + int c = 70 + + /; if (a < b && b > c) + return c + ;; else if (b > c && a < b) + return a + ;; else if (a > b && b > c) + return 1 + ;/ + + return b +;/ diff --git a/tests/test_conditional_5.tnsl b/tests/test_conditional_5.tnsl new file mode 100644 index 0000000..2c68c17 --- /dev/null +++ b/tests/test_conditional_5.tnsl @@ -0,0 +1,20 @@ + +/; main [int] + + /; if (true && false) + return 0 + ;; else if (false && true) + return 1 + ;; else if (false && false) + return 2 + ;/ + + /; if (false || false) + return 3 + ;; else if (true || false) + return 69 + ;/ + + return 4 + +;/ diff --git a/tests/test_conditional_6.tnsl b/tests/test_conditional_6.tnsl new file mode 100644 index 0000000..051b0f3 --- /dev/null +++ b/tests/test_conditional_6.tnsl @@ -0,0 +1,8 @@ +/; main [int] + bool a = false + /; if (false || !a && true) + return 69 + ;/ + + return 0 +;/ -- cgit v1.2.3