Fix a bug, add type kind

This commit is contained in:
Sofia 2026-04-13 16:56:33 +03:00
parent cf965dd47a
commit 8ec4e538f5
2 changed files with 26 additions and 4 deletions

View File

@ -62,7 +62,12 @@ namespace AST {
) { ) {
auto expr_ty = this->m_fn_expr->typecheck(state, scope, {}); auto expr_ty = this->m_fn_expr->typecheck(state, scope, {});
// TODO make sure function_ty really is a function type if (expr_ty->m_kind != types::TypeKind::Function) {
state.errors.push_back(CompileError("Tried calling a non-function", this->m_meta));
return std::shared_ptr<types::Type> {
new types::FundamentalType{ types::FundamentalTypeKind::Void }
};
}
auto fn_ty = dynamic_cast<types::FunctionType*>(expr_ty.get()); auto fn_ty = dynamic_cast<types::FunctionType*>(expr_ty.get());
@ -123,6 +128,12 @@ namespace AST {
typecheck::Scope inner{ scope }; typecheck::Scope inner{ scope };
inner.return_ty = return_ty; inner.return_ty = return_ty;
for (auto& param : this->m_params) {
if (param.first) {
inner.symbols[*param.first] = param.second;
}
}
if (this->m_statements) { if (this->m_statements) {
for (auto& statement : *this->m_statements) { for (auto& statement : *this->m_statements) {
statement->typecheck(state, inner); statement->typecheck(state, inner);

View File

@ -18,6 +18,12 @@ namespace types {
int operator_precedence(BinOp& op); int operator_precedence(BinOp& op);
std::string format_operator(BinOp& op); std::string format_operator(BinOp& op);
enum class TypeKind {
Fundamental,
Function,
Pointer,
};
enum FundamentalTypeKind { enum FundamentalTypeKind {
Int, Int,
Bool, Bool,
@ -27,6 +33,8 @@ namespace types {
class Type { class Type {
public: public:
TypeKind m_kind;
Type(TypeKind kind) : m_kind{ kind } {}
virtual ~Type() = default; virtual ~Type() = default;
virtual std::string formatted() = 0; virtual std::string formatted() = 0;
virtual llvm::Type* codegen(codegen::Builder& builder) = 0; virtual llvm::Type* codegen(codegen::Builder& builder) = 0;
@ -42,7 +50,7 @@ namespace types {
private: private:
FundamentalTypeKind m_ty; FundamentalTypeKind m_ty;
public: public:
FundamentalType(FundamentalTypeKind kind) : m_ty{ kind } {} FundamentalType(FundamentalTypeKind kind) : Type(TypeKind::Fundamental), m_ty{ kind } {}
virtual ~FundamentalType() override = default; virtual ~FundamentalType() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
virtual llvm::Type* codegen(codegen::Builder& builder) override; virtual llvm::Type* codegen(codegen::Builder& builder) override;
@ -60,7 +68,10 @@ namespace types {
std::vector<std::shared_ptr<Type>> m_param_tys; std::vector<std::shared_ptr<Type>> m_param_tys;
bool m_vararg; bool m_vararg;
FunctionType(std::shared_ptr<Type> ret_ty, std::vector<std::shared_ptr<Type>> param_tys, bool vararg) 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 } { : Type(TypeKind::Function)
, m_ret_ty{ std::move(ret_ty) }
, m_param_tys{ std::move(param_tys) }
, m_vararg{ vararg } {
} }
virtual ~FunctionType() override = default; virtual ~FunctionType() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
@ -76,7 +87,7 @@ namespace types {
std::shared_ptr<Type> m_inner; std::shared_ptr<Type> m_inner;
public: public:
PointerType(std::shared_ptr<Type> inner) PointerType(std::shared_ptr<Type> inner)
: m_inner{ std::move(inner) } { : Type(TypeKind::Pointer), m_inner{ std::move(inner) } {
} }
virtual ~PointerType() override = default; virtual ~PointerType() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;