Use unique_ptrs everywhere again
This commit is contained in:
parent
04a6ec1ded
commit
31c697a097
25
src/ast.h
25
src/ast.h
@ -8,36 +8,47 @@
|
||||
namespace AST {
|
||||
class Node {};
|
||||
|
||||
class Expression : Node {};
|
||||
class Expression : public Node {};
|
||||
class Type {};
|
||||
class Statement : Node {};
|
||||
class Statement : public Node {};
|
||||
|
||||
class IntLiteralExpression : Expression {
|
||||
class IntLiteralExpression : public Expression {
|
||||
private:
|
||||
int m_value;
|
||||
};
|
||||
|
||||
|
||||
class ReturnStatement : Statement {
|
||||
class ReturnStatement : public Statement {
|
||||
private:
|
||||
std::unique_ptr<Expression> m_expr;
|
||||
};
|
||||
|
||||
class TopLevelStatement : Node {};
|
||||
class TopLevelStatement : public Node {};
|
||||
|
||||
class Function : TopLevelStatement {
|
||||
class Function : public TopLevelStatement {
|
||||
private:
|
||||
std::unique_ptr<Type> m_return_ty;
|
||||
std::vector<std::pair<std::string, std::unique_ptr<Type>>> m_params;
|
||||
std::string m_name;
|
||||
std::vector<std::unique_ptr<Statement>> m_statements;
|
||||
public:
|
||||
Function(
|
||||
Type* return_ty,
|
||||
std::vector<std::pair<std::string, std::unique_ptr<Type>>> params,
|
||||
std::string name,
|
||||
std::vector<std::unique_ptr<Statement>> statements)
|
||||
: m_return_ty{ std::move(return_ty) }
|
||||
, m_params{ std::move(params) }
|
||||
, m_name{ name }
|
||||
, m_statements{ std::move(statements) } {
|
||||
}
|
||||
};
|
||||
|
||||
enum class FundamentalTypeKind {
|
||||
Int,
|
||||
};
|
||||
|
||||
class FundamentalType : Type {
|
||||
class FundamentalType : public Type {
|
||||
private:
|
||||
FundamentalTypeKind m_ty;
|
||||
};
|
||||
|
||||
@ -53,7 +53,7 @@ int main() {
|
||||
|
||||
auto stream = token::TokenStream{ tokens };
|
||||
|
||||
std::vector<AST::TopLevelStatement> statements;
|
||||
std::vector<std::unique_ptr<AST::TopLevelStatement>> statements;
|
||||
auto statement = parsing::parse_top_level_statement(stream);
|
||||
while (statement.ok()) {
|
||||
statements.push_back(statement.unwrap());
|
||||
|
||||
@ -2,7 +2,8 @@
|
||||
|
||||
|
||||
namespace parsing {
|
||||
Result<AST::TopLevelStatement, std::string> parse_top_level_statement(token::TokenStream& stream) {
|
||||
return Result<AST::TopLevelStatement, std::string>{ "test" };
|
||||
Result<std::unique_ptr<AST::TopLevelStatement>, std::string> parse_top_level_statement(token::TokenStream& stream) {
|
||||
auto fun = new AST::Function{ nullptr, {}, "main", {} };
|
||||
return Result<std::unique_ptr<AST::TopLevelStatement>, std::string>{ std::unique_ptr<AST::TopLevelStatement>{ fun } };
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
#include "tokens.h"
|
||||
|
||||
namespace parsing {
|
||||
Result<AST::TopLevelStatement, std::string> parse_top_level_statement(token::TokenStream& stream);
|
||||
Result<std::unique_ptr<AST::TopLevelStatement>, std::string> parse_top_level_statement(token::TokenStream& stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -11,16 +11,16 @@ private:
|
||||
std::unique_ptr<TErr> m_error;
|
||||
|
||||
public:
|
||||
Result(T value) : m_value{ std::make_unique(value) }, m_error{ nullptr } {}
|
||||
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>(error) } {}
|
||||
Result(T value) : m_value{ std::make_unique<T>() }, m_error{ nullptr } {}
|
||||
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>() } {}
|
||||
|
||||
bool ok() {
|
||||
return m_error == nullptr;
|
||||
return !m_error;
|
||||
}
|
||||
|
||||
T unwrap() {
|
||||
if (m_value) {
|
||||
return *m_value;
|
||||
return std::move(*m_value);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Tried to unwrap " + *m_error + "!");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user