llm
Download
132 lines 5.2 KiBCModified 2026-08-30 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
// This file defines how to compile comparisons

#include "../ast.h"
#include "../config.h"
#include "../environment.h"
#include "../stdlib/text.h"
#include "../typecheck.h"
#include "compilation.h"
#include "optionals.h"

static CONSTFUNC const char *comparison_operator(ast_e tag) {
    switch (tag) {
    case Equals: return "==";
    case NotEquals: return "!=";
    case LessThan: return "<";
    case LessThanOrEquals: return "<=";
    case GreaterThan: return ">";
    case GreaterThanOrEquals: return ">=";
    default: return NULL;
    }
}

Text_t compile_comparison(env_t *env, ast_t *ast) {
    switch (ast->tag) {
    case Equals:
    case NotEquals: {
        binary_operands_t binop = BINARY_OPERANDS(ast);

        type_t *lhs_t = get_type(env, binop.lhs);
        type_t *rhs_t = get_type(with_enum_scope(env, lhs_t), binop.rhs);
        type_t *operand_t;
        if (type_eq(lhs_t, rhs_t)) {
            operand_t = lhs_t;
        } else if (is_int_literal(binop.lhs, NULL) && is_numeric_type(rhs_t)) {
            operand_t = rhs_t;
        } else if (is_int_literal(binop.rhs, NULL) && is_numeric_type(lhs_t)) {
            operand_t = lhs_t;
        } else if (binop.lhs->tag == Num && is_numeric_type(non_optional(rhs_t))) {
            operand_t = rhs_t;
        } else if (binop.rhs->tag == Num && is_numeric_type(non_optional(lhs_t))) {
            operand_t = lhs_t;
        } else if (can_compile_to_type(with_enum_scope(env, lhs_t), binop.rhs, lhs_t)) {
            operand_t = lhs_t;
        } else if (can_compile_to_type(env, binop.lhs, rhs_t)) {
            operand_t = rhs_t;
        } else {
            code_err(ast, "I can't do comparisons between ", type_to_text(lhs_t), " and ", type_to_text(rhs_t));
        }
        assert(operand_t);

        // Fast path: comparing an optional against the `none` literal is just
        // an inline none-check on the other operand, with no need for a full
        // type-dispatched generic_equal() (e.g. `ptr == none` -> `ptr == NULL`).
        if (operand_t->tag == OptionalType) {
            ast_t *other = binop.lhs->tag == None ? binop.rhs : (binop.rhs->tag == None ? binop.lhs : NULL);
            if (other != NULL) {
                if (other->tag == None) // `none == none`
                    return ast->tag == Equals ? Text("yes") : Text("no");
                Text_t check = check_none(operand_t, compile_to_type(env, other, operand_t));
                return ast->tag == Equals ? check : Texts("!", check);
            }
        }

        Text_t lhs, rhs;
        lhs = compile_to_type(env, binop.lhs, operand_t);
        rhs = compile_to_type(env, binop.rhs, operand_t);

        switch (operand_t->tag) {
        case BigIntType:
            return Texts(ast->tag == Equals ? EMPTY_TEXT : Text("!"), "Int$equal_value(", lhs, ", ", rhs, ")");
        case BoolType:
        case ByteType:
        case IntType:
        case FloatType:
        case PointerType:
        case FunctionType: return Texts("(", lhs, ast->tag == Equals ? " == " : " != ", rhs, ")");
        default:
            return Texts(ast->tag == Equals ? EMPTY_TEXT : Text("!"), "generic_equal(stack(", lhs, "), stack(", rhs,
                         "), ", compile_type_info(operand_t), ")");
        }
    }
    case LessThan:
    case LessThanOrEquals:
    case GreaterThan:
    case GreaterThanOrEquals:
    case Compare: {
        binary_operands_t cmp = BINARY_OPERANDS(ast);

        type_t *lhs_t = get_type(env, cmp.lhs);
        type_t *rhs_t = get_type(env, cmp.rhs);
        type_t *operand_t;
        if (type_eq(lhs_t, rhs_t)) {
            operand_t = lhs_t;
        } else if (is_int_literal(cmp.lhs, NULL) && is_numeric_type(rhs_t)) {
            operand_t = rhs_t;
        } else if (is_int_literal(cmp.rhs, NULL) && is_numeric_type(lhs_t)) {
            operand_t = lhs_t;
        } else if (cmp.lhs->tag == Num && is_numeric_type(non_optional(rhs_t))) {
            operand_t = rhs_t;
        } else if (cmp.rhs->tag == Num && is_numeric_type(non_optional(lhs_t))) {
            operand_t = lhs_t;
        } else if (can_compile_to_type(env, cmp.rhs, lhs_t)) {
            operand_t = lhs_t;
        } else if (can_compile_to_type(env, cmp.lhs, rhs_t)) {
            operand_t = rhs_t;
        } else {
            code_err(ast, "I can't do comparisons between ", type_to_text(lhs_t), " and ", type_to_text(rhs_t));
        }

        Text_t lhs = compile_to_type(env, cmp.lhs, operand_t);
        Text_t rhs = compile_to_type(env, cmp.rhs, operand_t);

        if (ast->tag == Compare)
            return Texts("generic_compare(stack(", lhs, "), stack(", rhs, "), ", compile_type_info(operand_t), ")");

        const char *op = comparison_operator(ast->tag);
        switch (operand_t->tag) {
        case BigIntType: return Texts("(Int$compare_value(", lhs, ", ", rhs, ") ", op, " 0)");
        case BoolType:
        case ByteType:
        case IntType:
        case FloatType:
        case PointerType:
        case FunctionType: return Texts("(", lhs, " ", op, " ", rhs, ")");
        default:
            return Texts("(generic_compare(stack(", lhs, "), stack(", rhs, "), ", compile_type_info(operand_t), ") ",
                         op, " 0)");
        }
    }
    default: code_err(ast, "This is not a comparison!");
    }
}