From d11da4d1e686e051e7dd461ff9ab013db972c54c Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 13 Apr 2026 00:55:38 +0300 Subject: [PATCH] Make expressions return type on typecheck --- src/ast.h | 12 ++++++------ src/typechecker.cpp | 10 +++++----- src/typechecker.h | 9 --------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/ast.h b/src/ast.h index 0e892bd..37917c7 100644 --- a/src/ast.h +++ b/src/ast.h @@ -24,7 +24,7 @@ namespace AST { public: Expression(token::Metadata meta) : Node{ meta } {} virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) = 0; - virtual typecheck::TypecheckResult typecheck( + virtual std::shared_ptr typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -46,7 +46,7 @@ namespace AST { virtual ~IntLiteralExpression() override = default; virtual std::string formatted() override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; - virtual typecheck::TypecheckResult typecheck( + virtual std::shared_ptr typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -61,7 +61,7 @@ namespace AST { virtual ~StringLiteralExpression() override = default; virtual std::string formatted() override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; - virtual typecheck::TypecheckResult typecheck( + virtual std::shared_ptr typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -76,7 +76,7 @@ namespace AST { virtual ~ValueReferenceExpression() override = default; virtual std::string formatted() override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; - virtual typecheck::TypecheckResult typecheck( + virtual std::shared_ptr typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -102,7 +102,7 @@ namespace AST { virtual ~BinaryOperationExpression() override = default; virtual std::string formatted() override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; - virtual typecheck::TypecheckResult typecheck( + virtual std::shared_ptr typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -125,7 +125,7 @@ namespace AST { virtual ~FunctionCallExpression() override = default; virtual std::string formatted() override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; - virtual typecheck::TypecheckResult typecheck( + virtual std::shared_ptr typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty diff --git a/src/typechecker.cpp b/src/typechecker.cpp index e46fcb3..3276c41 100644 --- a/src/typechecker.cpp +++ b/src/typechecker.cpp @@ -4,7 +4,7 @@ #include "errors.h" namespace AST { - typecheck::TypecheckResult IntLiteralExpression::typecheck( + std::shared_ptr IntLiteralExpression::typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -12,7 +12,7 @@ namespace AST { throw CompileError("TODO", {}); } - typecheck::TypecheckResult StringLiteralExpression::typecheck( + std::shared_ptr StringLiteralExpression::typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -20,7 +20,7 @@ namespace AST { throw CompileError("TODO", {}); } - typecheck::TypecheckResult ValueReferenceExpression::typecheck( + std::shared_ptr ValueReferenceExpression::typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -28,7 +28,7 @@ namespace AST { throw CompileError("TODO", {}); } - typecheck::TypecheckResult BinaryOperationExpression::typecheck( + std::shared_ptr BinaryOperationExpression::typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty @@ -36,7 +36,7 @@ namespace AST { throw CompileError("TODO", {}); } - typecheck::TypecheckResult FunctionCallExpression::typecheck( + std::shared_ptr FunctionCallExpression::typecheck( typecheck::State& state, typecheck::Scope& scope, std::optional> expected_ty diff --git a/src/typechecker.h b/src/typechecker.h index 6256338..528c720 100644 --- a/src/typechecker.h +++ b/src/typechecker.h @@ -7,15 +7,6 @@ #include "errors.h" 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 { std::map> symbols; std::optional> return_ty;