Add statement typecheckings

This commit is contained in:
Sofia 2026-04-13 00:54:14 +03:00
parent 44c9e31556
commit 902610aaea
2 changed files with 32 additions and 7 deletions

View File

@ -45,22 +45,46 @@ namespace AST {
}
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
throw CompileError("TODO", {});
this->m_expr->typecheck(state, scope, scope.return_ty);
}
void InitializationStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
throw CompileError("TODO", {});
if (this->m_expr) {
(*this->m_expr)->typecheck(state, scope, this->m_type);
}
scope.symbols[this->m_name] = this->m_type;
}
void ExpressionStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
throw CompileError("TODO", {});
this->m_expr->typecheck(state, scope, {});
}
void IfStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
throw CompileError("TODO", {});
auto bool_ty_ptr = new types::FundamentalType{ types::FundamentalTypeKind::Bool };
this->m_condition->typecheck(state, scope, std::shared_ptr<types::Type>{ bool_ty_ptr });
this->m_then->typecheck(state, scope);
if (this->m_else) {
(*this->m_else)->typecheck(state, scope);
}
}
void Function::typecheck(typecheck::State& state, typecheck::Scope& scope) {
throw CompileError("TODO", {});
auto return_ty = this->m_return_ty;
std::vector<std::shared_ptr<types::Type>> param_tys{};
for (auto& param : this->m_params) {
param_tys.push_back(param.second);
}
auto function_ty = new types::FunctionType{ return_ty, param_tys, this->m_is_vararg };
scope.symbols[this->m_name] = std::shared_ptr<types::Type>{ function_ty };
typecheck::Scope inner{ scope };
if (this->m_statements) {
for (auto& statement : *this->m_statements) {
statement->typecheck(state, inner);
}
}
}
}

View File

@ -9,7 +9,7 @@
namespace typecheck {
enum class TypecheckResult {
/// @brief The types match perfectly (enough)
Suitable,
Ok,
/// @brief The original type can be implicitly cast into the given type
Castable,
/// @brief The types are totally incompatible
@ -17,7 +17,8 @@ namespace typecheck {
};
struct Scope {
std::map<std::string, types::Type> symbols;
std::map<std::string, std::shared_ptr<types::Type>> symbols;
std::optional<std::shared_ptr<types::Type>> return_ty;
};
struct State {