From ab406df61005d2dbcde4c83d26aee2b114fb5afd Mon Sep 17 00:00:00 2001 From: Sofia Date: Thu, 2 Apr 2026 16:09:42 +0300 Subject: [PATCH] Fix passing around results --- CMakeLists.txt | 2 +- src/ast.cpp | 15 +++++++++++++++ src/ast.h | 8 +++++++- src/main.cpp | 7 ++++++- src/parsing.cpp | 16 ++++++++-------- src/result.h | 5 +++-- 6 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/ast.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 92a2e8b..340b6b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS_LIST}) # Executable -add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp) +add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp src/ast.cpp) target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) # Find the libraries that correspond to the LLVM components diff --git a/src/ast.cpp b/src/ast.cpp new file mode 100644 index 0000000..d5b434d --- /dev/null +++ b/src/ast.cpp @@ -0,0 +1,15 @@ +#include "ast.h" + +namespace AST { + std::string ReturnStatement::formatted() { + return "ReturnStatement"; + } + + std::string IntLiteralExpression::formatted() { + return "IntLiteral"; + } + + std::string Function::formatted() { + return "Function"; + } +} \ No newline at end of file diff --git a/src/ast.h b/src/ast.h index f27cbf3..6733f7a 100644 --- a/src/ast.h +++ b/src/ast.h @@ -6,7 +6,10 @@ #include namespace AST { - class Node {}; + class Node { + public: + virtual std::string formatted() = 0; + }; class Expression : public Node {}; class Type {}; @@ -17,6 +20,7 @@ namespace AST { int m_value; public: IntLiteralExpression(int value) : m_value{ value } {} + virtual std::string formatted() override; }; @@ -25,6 +29,7 @@ namespace AST { std::unique_ptr m_expr; public: ReturnStatement(std::unique_ptr expr) : m_expr{ std::move(expr) } {} + virtual std::string formatted() override; }; class TopLevelStatement : public Node {}; @@ -46,6 +51,7 @@ namespace AST { , m_name{ name } , m_statements{ std::move(statements) } { } + virtual std::string formatted() override; }; enum class FundamentalTypeKind { diff --git a/src/main.cpp b/src/main.cpp index 1e52ed6..79615ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,11 +56,16 @@ int main() { std::vector> statements; auto statement = parsing::parse_top_level_statement(stream); while (statement.ok()) { - statements.push_back(statement.unwrap()); + statements.push_back(std::move(statement.unwrap())); statement = parsing::parse_top_level_statement(stream); } stream.expect(token::Type::Eof); + for (int i = 0; i < statements.size(); i++) { + if (statements[i]) + std::cout << statements[i]->formatted() << std::endl; + } + // LLVM Hello World // llvm_hello_world(); diff --git a/src/parsing.cpp b/src/parsing.cpp index 773bd4d..04794bf 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -11,10 +11,10 @@ namespace parsing { stream.m_position = inner.m_position; auto ty = new AST::FundamentalType{ AST::FundamentalTypeKind::Int }; - return std::unique_ptr {ty}; + return new std::unique_ptr{ ty }; } catch (std::runtime_error error) { - return std::string{ error.what() }; + return new std::string{ error.what() }; } } @@ -26,14 +26,14 @@ namespace parsing { stream.m_position = inner.m_position; auto expr = new AST::IntLiteralExpression{ 5 }; - return std::unique_ptr{expr}; + return new std::unique_ptr{ expr }; } else { throw std::runtime_error("Expected expression"); } } catch (std::runtime_error error) { - return std::string{ error.what() }; + return new std::string{ error.what() }; } } @@ -48,7 +48,7 @@ namespace parsing { stream.m_position = inner.m_position; auto ret = new AST::ReturnStatement{ std::move(expression) }; - return std::unique_ptr{ ret }; + return new std::unique_ptr{ ret }; } else { throw std::runtime_error("Expected return-keyword"); @@ -56,7 +56,7 @@ namespace parsing { } catch (std::runtime_error error) { - return std::string{ error.what() }; + return new std::string{ error.what() }; } } } @@ -84,10 +84,10 @@ namespace parsing { stream.m_position = inner.m_position; auto fun = new AST::Function{ std::move(type), {}, "main", std::move(statements) }; - return std::unique_ptr{ fun }; + return new std::unique_ptr{ fun }; } catch (std::runtime_error error) { - return std::string(error.what()); + return new std::string(error.what()); } } } \ No newline at end of file diff --git a/src/result.h b/src/result.h index 54de95a..cbaa9a1 100644 --- a/src/result.h +++ b/src/result.h @@ -3,6 +3,7 @@ #define RESULT_H #include +#include template class Result { @@ -11,8 +12,8 @@ private: std::unique_ptr m_error; public: - Result(T value) : m_value{ std::make_unique() }, m_error{ nullptr } {} - Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique() } {} + Result(T* value) : m_value{ std::unique_ptr{value} }, m_error{ nullptr } {} + Result(TErr* error) : m_value{ nullptr }, m_error{ std::unique_ptr{error} } {} bool ok() { return !m_error;