Make expressions return type on typecheck

This commit is contained in:
Sofia 2026-04-13 00:55:38 +03:00
parent 902610aaea
commit d11da4d1e6
3 changed files with 11 additions and 20 deletions

View File

@ -24,7 +24,7 @@ namespace AST {
public: public:
Expression(token::Metadata meta) : Node{ meta } {} Expression(token::Metadata meta) : Node{ meta } {}
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) = 0; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) = 0;
virtual typecheck::TypecheckResult typecheck( virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -46,7 +46,7 @@ namespace AST {
virtual ~IntLiteralExpression() override = default; virtual ~IntLiteralExpression() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual typecheck::TypecheckResult typecheck( virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -61,7 +61,7 @@ namespace AST {
virtual ~StringLiteralExpression() override = default; virtual ~StringLiteralExpression() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual typecheck::TypecheckResult typecheck( virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -76,7 +76,7 @@ namespace AST {
virtual ~ValueReferenceExpression() override = default; virtual ~ValueReferenceExpression() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual typecheck::TypecheckResult typecheck( virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -102,7 +102,7 @@ namespace AST {
virtual ~BinaryOperationExpression() override = default; virtual ~BinaryOperationExpression() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual typecheck::TypecheckResult typecheck( virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -125,7 +125,7 @@ namespace AST {
virtual ~FunctionCallExpression() override = default; virtual ~FunctionCallExpression() override = default;
virtual std::string formatted() override; virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
virtual typecheck::TypecheckResult typecheck( virtual std::shared_ptr<types::Type> typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty

View File

@ -4,7 +4,7 @@
#include "errors.h" #include "errors.h"
namespace AST { namespace AST {
typecheck::TypecheckResult IntLiteralExpression::typecheck( std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -12,7 +12,7 @@ namespace AST {
throw CompileError("TODO", {}); throw CompileError("TODO", {});
} }
typecheck::TypecheckResult StringLiteralExpression::typecheck( std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -20,7 +20,7 @@ namespace AST {
throw CompileError("TODO", {}); throw CompileError("TODO", {});
} }
typecheck::TypecheckResult ValueReferenceExpression::typecheck( std::shared_ptr<types::Type> ValueReferenceExpression::typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -28,7 +28,7 @@ namespace AST {
throw CompileError("TODO", {}); throw CompileError("TODO", {});
} }
typecheck::TypecheckResult BinaryOperationExpression::typecheck( std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty
@ -36,7 +36,7 @@ namespace AST {
throw CompileError("TODO", {}); throw CompileError("TODO", {});
} }
typecheck::TypecheckResult FunctionCallExpression::typecheck( std::shared_ptr<types::Type> FunctionCallExpression::typecheck(
typecheck::State& state, typecheck::State& state,
typecheck::Scope& scope, typecheck::Scope& scope,
std::optional<std::shared_ptr<types::Type>> expected_ty std::optional<std::shared_ptr<types::Type>> expected_ty

View File

@ -7,15 +7,6 @@
#include "errors.h" #include "errors.h"
namespace typecheck { namespace typecheck {
enum class TypecheckResult {
/// @brief The types match perfectly (enough)
Ok,
/// @brief The original type can be implicitly cast into the given type
Castable,
/// @brief The types are totally incompatible
Error,
};
struct Scope { struct Scope {
std::map<std::string, std::shared_ptr<types::Type>> symbols; std::map<std::string, std::shared_ptr<types::Type>> symbols;
std::optional<std::shared_ptr<types::Type>> return_ty; std::optional<std::shared_ptr<types::Type>> return_ty;