diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-22 13:18:47 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-22 13:18:47 -0500 |
| commit | f9df7afb8d6fb74f2d113c5f6ceed07fdd96daca (patch) | |
| tree | 932fc6ed2e221655f40d7535a6ce716d05fccff9 | |
| parent | bfdb2da9e12775a0caa8de35f1d114181918529a (diff) | |
Fix comparisons
| -rw-r--r-- | compile.c | 74 |
1 files changed, 41 insertions, 33 deletions
@@ -179,139 +179,147 @@ CORD compile(env_t *env, ast_t *ast) CORD lhs = compile(env, binop->lhs); CORD rhs = compile(env, binop->rhs); - type_t *result_t = get_type(env, ast); + type_t *lhs_t = get_type(env, binop->lhs); + type_t *rhs_t = get_type(env, binop->rhs); + type_t *operand_t; + if (can_promote(rhs_t, lhs_t)) + operand_t = lhs_t; + else if (can_promote(lhs_t, rhs_t)) + operand_t = rhs_t; + else + code_err(ast, "I can't do binary operations between %T and %T", lhs_t, rhs_t); switch (binop->op) { case BINOP_POWER: { - if (result_t->tag != NumType) - code_err(ast, "Exponentiation is only supported for Num types"); - if (Match(result_t, NumType)->bits == 32) + if (operand_t->tag != NumType && operand_t->tag != IntType) + code_err(ast, "Exponentiation is only supported for numeric types"); + if (operand_t->tag == NumType && Match(operand_t, NumType)->bits == 32) return CORD_all("powf(", lhs, ", ", rhs, ")"); else return CORD_all("pow(", lhs, ", ", rhs, ")"); } case BINOP_MULT: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("(%r * %r)", lhs, rhs); } case BINOP_DIVIDE: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("(%r / %r)", lhs, rhs); } case BINOP_MOD: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("mod(%r, %r)", lhs, rhs); } case BINOP_MOD1: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("mod1(%r, %r)", lhs, rhs); } case BINOP_PLUS: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("(%r + %r)", lhs, rhs); } case BINOP_MINUS: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("(%r - %r)", lhs, rhs); } case BINOP_LSHIFT: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("(%r << %r)", lhs, rhs); } case BINOP_RSHIFT: { - if (result_t->tag != IntType && result_t->tag != NumType) + if (operand_t->tag != IntType && operand_t->tag != NumType) code_err(ast, "Math operations are only supported for numeric types"); return CORD_asprintf("(%r >> %r)", lhs, rhs); } case BINOP_EQ: { - switch (result_t->tag) { + switch (operand_t->tag) { case BoolType: case IntType: case NumType: case PointerType: case FunctionType: return CORD_asprintf("(%r == %r)", lhs, rhs); default: - return CORD_asprintf("generic_equal($stack(%r), $stack(%r), %r)", lhs, rhs, compile_type_info(env, result_t)); + return CORD_asprintf("generic_equal($stack(%r), $stack(%r), %r)", lhs, rhs, compile_type_info(env, operand_t)); } } case BINOP_NE: { - switch (result_t->tag) { + switch (operand_t->tag) { case BoolType: case IntType: case NumType: case PointerType: case FunctionType: return CORD_asprintf("(%r != %r)", lhs, rhs); default: - return CORD_asprintf("!generic_equal($stack(%r), $stack(%r), %r)", lhs, rhs, compile_type_info(env, result_t)); + return CORD_asprintf("!generic_equal($stack(%r), $stack(%r), %r)", lhs, rhs, compile_type_info(env, operand_t)); } } case BINOP_LT: { - switch (result_t->tag) { + switch (operand_t->tag) { case BoolType: case IntType: case NumType: case PointerType: case FunctionType: return CORD_asprintf("(%r < %r)", lhs, rhs); default: - return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) < 0)", lhs, rhs, compile_type_info(env, result_t)); + return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) < 0)", lhs, rhs, compile_type_info(env, operand_t)); } } case BINOP_LE: { - switch (result_t->tag) { + switch (operand_t->tag) { case BoolType: case IntType: case NumType: case PointerType: case FunctionType: return CORD_asprintf("(%r <= %r)", lhs, rhs); default: - return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) <= 0)", lhs, rhs, compile_type_info(env, result_t)); + return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) <= 0)", lhs, rhs, compile_type_info(env, operand_t)); } } case BINOP_GT: { - switch (result_t->tag) { + switch (operand_t->tag) { case BoolType: case IntType: case NumType: case PointerType: case FunctionType: return CORD_asprintf("(%r > %r)", lhs, rhs); default: - return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) > 0)", lhs, rhs, compile_type_info(env, result_t)); + return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) > 0)", lhs, rhs, compile_type_info(env, operand_t)); } } case BINOP_GE: { - switch (result_t->tag) { + switch (operand_t->tag) { case BoolType: case IntType: case NumType: case PointerType: case FunctionType: return CORD_asprintf("(%r >= %r)", lhs, rhs); default: - return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) >= 0)", lhs, rhs, compile_type_info(env, result_t)); + return CORD_asprintf("(generic_compare($stack(%r), $stack(%r), %r) >= 0)", lhs, rhs, compile_type_info(env, operand_t)); } } case BINOP_AND: { - if (result_t->tag == BoolType) + if (operand_t->tag == BoolType) return CORD_asprintf("(%r && %r)", lhs, rhs); - else if (result_t->tag == IntType) + else if (operand_t->tag == IntType) return CORD_asprintf("(%r & %r)", lhs, rhs); else code_err(ast, "Boolean operators are only supported for Bool and integer types"); } case BINOP_OR: { - if (result_t->tag == BoolType) + if (operand_t->tag == BoolType) return CORD_asprintf("(%r || %r)", lhs, rhs); - else if (result_t->tag == IntType) + else if (operand_t->tag == IntType) return CORD_asprintf("(%r | %r)", lhs, rhs); else code_err(ast, "Boolean operators are only supported for Bool and integer types"); } case BINOP_XOR: { - if (result_t->tag == BoolType || result_t->tag == IntType) + if (operand_t->tag == BoolType || operand_t->tag == IntType) return CORD_asprintf("(%r ^ %r)", lhs, rhs); else code_err(ast, "Boolean operators are only supported for Bool and integer types"); } case BINOP_CONCAT: { - switch (result_t->tag) { + switch (operand_t->tag) { case StringType: { return CORD_all("CORD_cat(", lhs, ", ", rhs, ")"); } case ArrayType: { return CORD_all("({ array_t $joined = ", lhs, ", $rhs = ", rhs, ";\n" - "Array__insert_all(&$joined, $rhs, 0, ", compile_type_info(env, result_t), ");\n" + "Array__insert_all(&$joined, $rhs, 0, ", compile_type_info(env, operand_t), ");\n" "$joined; })"); } default: - code_err(ast, "Concatenation isn't supported for %T types", result_t); + code_err(ast, "Concatenation isn't supported for %T types", operand_t); } } default: break; @@ -888,7 +896,7 @@ CORD compile(env_t *env, ast_t *ast) CORD compile_type_info(env_t *env, type_t *t) { switch (t->tag) { - case BoolType: return "&Bool_type.type"; + case BoolType: return "&Bool.type"; case IntType: return CORD_asprintf("&Int%ld.type", Match(t, IntType)->bits); case NumType: return CORD_asprintf("&Num%ld.type", Match(t, NumType)->bits); case StringType: return CORD_all("&", Match(t, StringType)->dsl ? Match(t, StringType)->dsl : "Str", ".type"); |
