Fix passing around results
This commit is contained in:
parent
e19349beb6
commit
ab406df610
@ -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
|
||||
|
||||
15
src/ast.cpp
Normal file
15
src/ast.cpp
Normal 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";
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,10 @@
|
||||
#include <memory>
|
||||
|
||||
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<Expression> m_expr;
|
||||
public:
|
||||
ReturnStatement(std::unique_ptr<Expression> 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 {
|
||||
|
||||
@ -56,11 +56,16 @@ int main() {
|
||||
std::vector<std::unique_ptr<AST::TopLevelStatement>> 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();
|
||||
|
||||
|
||||
@ -11,10 +11,10 @@ namespace parsing {
|
||||
stream.m_position = inner.m_position;
|
||||
|
||||
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) {
|
||||
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<AST::Expression>{expr};
|
||||
return new std::unique_ptr<AST::Expression>{ 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<AST::Statement>{ ret };
|
||||
return new std::unique_ptr<AST::Statement>{ 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<AST::TopLevelStatement>{ fun };
|
||||
return new std::unique_ptr<AST::TopLevelStatement>{ fun };
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
return std::string(error.what());
|
||||
return new std::string(error.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@
|
||||
#define RESULT_H
|
||||
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
template<typename T, typename TErr>
|
||||
class Result {
|
||||
@ -11,8 +12,8 @@ private:
|
||||
std::unique_ptr<TErr> m_error;
|
||||
|
||||
public:
|
||||
Result(T value) : m_value{ std::make_unique<T>() }, m_error{ nullptr } {}
|
||||
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>() } {}
|
||||
Result(T* value) : m_value{ std::unique_ptr<T>{value} }, m_error{ nullptr } {}
|
||||
Result(TErr* error) : m_value{ nullptr }, m_error{ std::unique_ptr<TErr>{error} } {}
|
||||
|
||||
bool ok() {
|
||||
return !m_error;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user