Implicitly add cast if implicit cast is found

This commit is contained in:
Sofia 2026-04-13 21:36:44 +03:00
parent ee0e30934a
commit e0f2a1620e

View File

@ -5,19 +5,24 @@
#include "result.h"
namespace {
enum class TypecheckRes {
enum class TypecheckResKind {
Ok,
Castable,
};
struct TypecheckRes {
TypecheckResKind kind;
std::shared_ptr<types::Type> result;
};
Result<TypecheckRes, std::string> check_type(typecheck::State& state, std::shared_ptr<types::Type> checked, std::shared_ptr<types::Type> target) {
auto potential_cast = types::find_cast(state.casts, checked, target);
if (types::types_equal(checked, target)) {
return TypecheckRes::Ok;
return TypecheckRes{ TypecheckResKind::Ok, target };
}
else if (potential_cast.has_value() && potential_cast->allow_implicit) {
return TypecheckRes::Castable;
return TypecheckRes{ TypecheckResKind::Castable, target };
}
return std::string{ "Types " + checked->formatted() + " and " + target->formatted() + " incompatible" };
@ -29,12 +34,13 @@ namespace {
typecheck::State& state) {
if (res.ok()) {
auto result = res.unwrap();
if (result == TypecheckRes::Ok) {
if (result.kind == TypecheckResKind::Ok) {
return expr;
}
else {
state.errors.push_back(CompileError("Casting not yet implemented", expr->m_meta));
return expr;
return std::unique_ptr<AST::Expression> {
new AST::CastExpression{ expr->m_meta, result.result, std::move(expr) }
};
}
}
else {