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 {
|
namespace AST {
|
||||||
class Node {};
|
class Node {};
|
||||||
|
|
||||||
class Expression : Node {};
|
class Expression : public Node {};
|
||||||
class Type {};
|
class Type {};
|
||||||
class Statement : Node {};
|
class Statement : public Node {};
|
||||||
|
|
||||||
class IntLiteralExpression : Expression {
|
class IntLiteralExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
int m_value;
|
int m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class ReturnStatement : Statement {
|
class ReturnStatement : public Statement {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Expression> m_expr;
|
std::unique_ptr<Expression> m_expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TopLevelStatement : Node {};
|
class TopLevelStatement : public Node {};
|
||||||
|
|
||||||
class Function : TopLevelStatement {
|
class Function : public TopLevelStatement {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Type> m_return_ty;
|
std::unique_ptr<Type> m_return_ty;
|
||||||
std::vector<std::pair<std::string, std::unique_ptr<Type>>> m_params;
|
std::vector<std::pair<std::string, std::unique_ptr<Type>>> m_params;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::vector<std::unique_ptr<Statement>> m_statements;
|
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 {
|
enum class FundamentalTypeKind {
|
||||||
Int,
|
Int,
|
||||||
};
|
};
|
||||||
|
|
||||||
class FundamentalType : Type {
|
class FundamentalType : public Type {
|
||||||
private:
|
private:
|
||||||
FundamentalTypeKind m_ty;
|
FundamentalTypeKind m_ty;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -53,7 +53,7 @@ int main() {
|
|||||||
|
|
||||||
auto stream = token::TokenStream{ tokens };
|
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);
|
auto statement = parsing::parse_top_level_statement(stream);
|
||||||
while (statement.ok()) {
|
while (statement.ok()) {
|
||||||
statements.push_back(statement.unwrap());
|
statements.push_back(statement.unwrap());
|
||||||
|
|||||||
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
|
|
||||||
namespace parsing {
|
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) {
|
||||||
return Result<AST::TopLevelStatement, std::string>{ "test" };
|
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"
|
#include "tokens.h"
|
||||||
|
|
||||||
namespace parsing {
|
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
|
#endif
|
||||||
@ -11,16 +11,16 @@ private:
|
|||||||
std::unique_ptr<TErr> m_error;
|
std::unique_ptr<TErr> m_error;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Result(T value) : m_value{ std::make_unique(value) }, m_error{ nullptr } {}
|
Result(T value) : m_value{ std::make_unique<T>() }, m_error{ nullptr } {}
|
||||||
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>(error) } {}
|
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>() } {}
|
||||||
|
|
||||||
bool ok() {
|
bool ok() {
|
||||||
return m_error == nullptr;
|
return !m_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
T unwrap() {
|
T unwrap() {
|
||||||
if (m_value) {
|
if (m_value) {
|
||||||
return *m_value;
|
return std::move(*m_value);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw std::runtime_error("Tried to unwrap " + *m_error + "!");
|
throw std::runtime_error("Tried to unwrap " + *m_error + "!");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user