From 902610aaea83d00f35248e05cee5d1f5d0f9317f Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 13 Apr 2026 00:54:14 +0300 Subject: [PATCH] Add statement typecheckings --- src/typechecker.cpp | 34 +++++++++++++++++++++++++++++----- src/typechecker.h | 5 +++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/typechecker.cpp b/src/typechecker.cpp index c4aed66..e46fcb3 100644 --- a/src/typechecker.cpp +++ b/src/typechecker.cpp @@ -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{ 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> 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{ function_ty }; + + typecheck::Scope inner{ scope }; + + if (this->m_statements) { + for (auto& statement : *this->m_statements) { + statement->typecheck(state, inner); + } + } } } \ No newline at end of file diff --git a/src/typechecker.h b/src/typechecker.h index a32e230..6256338 100644 --- a/src/typechecker.h +++ b/src/typechecker.h @@ -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 symbols; + std::map> symbols; + std::optional> return_ty; }; struct State {