Fill typechecking methods

This commit is contained in:
Sofia 2026-04-13 01:08:34 +03:00
parent d11da4d1e6
commit 85dbce112f
2 changed files with 48 additions and 17 deletions

View File

@ -5,43 +5,75 @@
namespace AST {
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
typecheck::State&,
typecheck::Scope&,
std::optional<std::shared_ptr<types::Type>>
) {
throw CompileError("TODO", {});
return std::shared_ptr<types::Type>{
new types::FundamentalType{ types::FundamentalTypeKind::Int }
};
}
std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
typecheck::State&,
typecheck::Scope&,
std::optional<std::shared_ptr<types::Type>>
) {
throw CompileError("TODO", {});
auto char_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ types::FundamentalTypeKind::Char }
};
auto ptr_ty = new types::PointerType{ char_ty };
return std::shared_ptr<types::Type>{ptr_ty};
}
std::shared_ptr<types::Type> ValueReferenceExpression::typecheck(
typecheck::State& state,
typecheck::State&,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
std::optional<std::shared_ptr<types::Type>>
) {
throw CompileError("TODO", {});
return scope.symbols[this->m_name];
}
std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
std::optional<std::shared_ptr<types::Type>>
) {
throw CompileError("TODO", {});
auto lhs_ty = this->m_lhs->typecheck(state, scope, {});
auto rhs_ty = this->m_rhs->typecheck(state, scope, {});
// TODO actually check binop types properly
return lhs_ty;
}
std::shared_ptr<types::Type> FunctionCallExpression::typecheck(
typecheck::State& state,
typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty
std::optional<std::shared_ptr<types::Type>>
) {
throw CompileError("TODO", {});
auto expr_ty = this->m_fn_expr->typecheck(state, scope, {});
// TODO make sure function_ty really is a function type
auto fn_ty = dynamic_cast<types::FunctionType*>(expr_ty.get());
if (this->m_args.size() < fn_ty->m_param_tys.size()) {
state.errors.push_back(CompileError("too few arguments", this->m_meta));
}
else if (this->m_args.size() > fn_ty->m_param_tys.size() && !fn_ty->m_vararg) {
state.errors.push_back(CompileError("too many arguments", this->m_meta));
}
else {
for (int i = 0; i < static_cast<int>(fn_ty->m_param_tys.size()); i++) {
auto expected_param_ty = fn_ty->m_param_tys[i];
auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty);
// TODO make sure types actually match
}
}
return fn_ty->m_ret_ty;
}
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {

View File

@ -55,11 +55,10 @@ namespace types {
class FunctionType : public Type {
private:
public:
std::shared_ptr<Type> m_ret_ty;
std::vector<std::shared_ptr<Type>> m_param_tys;
bool m_vararg;
public:
FunctionType(std::shared_ptr<Type> ret_ty, std::vector<std::shared_ptr<Type>> param_tys, bool vararg)
: m_ret_ty{ std::move(ret_ty) }, m_param_tys{ std::move(param_tys) }, m_vararg{ vararg } {
}