Add statement typecheckings
This commit is contained in:
parent
44c9e31556
commit
902610aaea
@ -45,22 +45,46 @@ namespace AST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9,7 +9,7 @@
|
|||||||
namespace typecheck {
|
namespace typecheck {
|
||||||
enum class TypecheckResult {
|
enum class TypecheckResult {
|
||||||
/// @brief The types match perfectly (enough)
|
/// @brief The types match perfectly (enough)
|
||||||
Suitable,
|
Ok,
|
||||||
/// @brief The original type can be implicitly cast into the given type
|
/// @brief The original type can be implicitly cast into the given type
|
||||||
Castable,
|
Castable,
|
||||||
/// @brief The types are totally incompatible
|
/// @brief The types are totally incompatible
|
||||||
@ -17,7 +17,8 @@ namespace typecheck {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Scope {
|
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 {
|
struct State {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user