Fix passing around results

This commit is contained in:
Sofia 2026-04-02 16:09:42 +03:00
parent e19349beb6
commit ab406df610
6 changed files with 40 additions and 13 deletions

View File

@ -15,7 +15,7 @@ separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST}) add_definitions(${LLVM_DEFINITIONS_LIST})
# Executable # 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) target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
# Find the libraries that correspond to the LLVM components # Find the libraries that correspond to the LLVM components

15
src/ast.cpp Normal file
View File

@ -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";
}
}

View File

@ -6,7 +6,10 @@
#include <memory> #include <memory>
namespace AST { namespace AST {
class Node {}; class Node {
public:
virtual std::string formatted() = 0;
};
class Expression : public Node {}; class Expression : public Node {};
class Type {}; class Type {};
@ -17,6 +20,7 @@ namespace AST {
int m_value; int m_value;
public: public:
IntLiteralExpression(int value) : m_value{ value } {} IntLiteralExpression(int value) : m_value{ value } {}
virtual std::string formatted() override;
}; };
@ -25,6 +29,7 @@ namespace AST {
std::unique_ptr<Expression> m_expr; std::unique_ptr<Expression> m_expr;
public: public:
ReturnStatement(std::unique_ptr<Expression> expr) : m_expr{ std::move(expr) } {} ReturnStatement(std::unique_ptr<Expression> expr) : m_expr{ std::move(expr) } {}
virtual std::string formatted() override;
}; };
class TopLevelStatement : public Node {}; class TopLevelStatement : public Node {};
@ -46,6 +51,7 @@ namespace AST {
, m_name{ name } , m_name{ name }
, m_statements{ std::move(statements) } { , m_statements{ std::move(statements) } {
} }
virtual std::string formatted() override;
}; };
enum class FundamentalTypeKind { enum class FundamentalTypeKind {

View File

@ -56,11 +56,16 @@ int main() {
std::vector<std::unique_ptr<AST::TopLevelStatement>> statements; std::vector<std::unique_ptr<AST::TopLevelStatement>> statements;
auto statement = parsing::parse_top_level_statement(stream); auto statement = parsing::parse_top_level_statement(stream);
while (statement.ok()) { while (statement.ok()) {
statements.push_back(statement.unwrap()); statements.push_back(std::move(statement.unwrap()));
statement = parsing::parse_top_level_statement(stream); statement = parsing::parse_top_level_statement(stream);
} }
stream.expect(token::Type::Eof); 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
// llvm_hello_world(); // llvm_hello_world();

View File

@ -11,10 +11,10 @@ namespace parsing {
stream.m_position = inner.m_position; stream.m_position = inner.m_position;
auto ty = new AST::FundamentalType{ AST::FundamentalTypeKind::Int }; auto ty = new AST::FundamentalType{ AST::FundamentalTypeKind::Int };
return std::unique_ptr<AST::Type> {ty}; return new std::unique_ptr<AST::Type>{ ty };
} }
catch (std::runtime_error error) { 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; stream.m_position = inner.m_position;
auto expr = new AST::IntLiteralExpression{ 5 }; auto expr = new AST::IntLiteralExpression{ 5 };
return std::unique_ptr<AST::Expression>{expr}; return new std::unique_ptr<AST::Expression>{ expr };
} }
else { else {
throw std::runtime_error("Expected expression"); throw std::runtime_error("Expected expression");
} }
} }
catch (std::runtime_error error) { 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; stream.m_position = inner.m_position;
auto ret = new AST::ReturnStatement{ std::move(expression) }; auto ret = new AST::ReturnStatement{ std::move(expression) };
return std::unique_ptr<AST::Statement>{ ret }; return new std::unique_ptr<AST::Statement>{ ret };
} }
else { else {
throw std::runtime_error("Expected return-keyword"); throw std::runtime_error("Expected return-keyword");
@ -56,7 +56,7 @@ namespace parsing {
} }
catch (std::runtime_error error) { 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; stream.m_position = inner.m_position;
auto fun = new AST::Function{ std::move(type), {}, "main", std::move(statements) }; auto fun = new AST::Function{ std::move(type), {}, "main", std::move(statements) };
return std::unique_ptr<AST::TopLevelStatement>{ fun }; return new std::unique_ptr<AST::TopLevelStatement>{ fun };
} }
catch (std::runtime_error error) { catch (std::runtime_error error) {
return std::string(error.what()); return new std::string(error.what());
} }
} }
} }

View File

@ -3,6 +3,7 @@
#define RESULT_H #define RESULT_H
#include <memory> #include <memory>
#include <iostream>
template<typename T, typename TErr> template<typename T, typename TErr>
class Result { class Result {
@ -11,8 +12,8 @@ private:
std::unique_ptr<TErr> m_error; std::unique_ptr<TErr> m_error;
public: public:
Result(T value) : m_value{ std::make_unique<T>() }, m_error{ nullptr } {} Result(T* value) : m_value{ std::unique_ptr<T>{value} }, m_error{ nullptr } {}
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>() } {} Result(TErr* error) : m_value{ nullptr }, m_error{ std::unique_ptr<TErr>{error} } {}
bool ok() { bool ok() {
return !m_error; return !m_error;