forked from KPGPMC/furlang
refactor(AST): use pointers instead of results inside of AST nodes
This commit is contained in:
@@ -108,7 +108,7 @@ public:
|
||||
* @param body Body of the function.
|
||||
*/
|
||||
template <typename T>
|
||||
function_definition_node(struct location location, T&& name, body_r&& body)
|
||||
function_definition_node(struct location location, T&& name, body&& body)
|
||||
: function_declaration_node(location, std::forward<T>(name)), m_body(std::move(body)) {}
|
||||
public:
|
||||
/**
|
||||
@@ -123,7 +123,7 @@ public:
|
||||
*
|
||||
* @return Body of the function.
|
||||
*/
|
||||
const body_r& body() const { return m_body; }
|
||||
const body& body() const { return m_body; }
|
||||
public:
|
||||
void accept(visitor& visitor) const override;
|
||||
|
||||
@@ -131,7 +131,7 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
body_r m_body;
|
||||
struct body m_body;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -59,8 +59,6 @@ protected:
|
||||
* @brief Var read expression AST node.
|
||||
*/
|
||||
class var_read_expression_node final : public expression_node {
|
||||
public:
|
||||
using name_type = furlang::result<std::string_view, error>; /**< Name type alias. */
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new var read expression node object from a name handle.
|
||||
@@ -68,22 +66,23 @@ public:
|
||||
* @param location Node location.
|
||||
* @param name Handle to the name.
|
||||
*/
|
||||
var_read_expression_node(struct location location, name_type&& name)
|
||||
: expression_node(location), m_name(std::move(name)) {}
|
||||
template <typename T>
|
||||
var_read_expression_node(struct location location, T&& name)
|
||||
: expression_node(location), m_name(std::forward<T>(name)) {}
|
||||
|
||||
/**
|
||||
* @brief Returns the variable's name.
|
||||
*
|
||||
* @return Name of the variable.
|
||||
*/
|
||||
const name_type& get_name() const { return m_name; }
|
||||
const std::string& get_name() const { return m_name; }
|
||||
|
||||
/**
|
||||
* @brief Returns the variable's name.
|
||||
*
|
||||
* @return Name of the variable.
|
||||
*/
|
||||
name_type&& move_name() { return std::move(m_name); }
|
||||
std::string&& move_name() { return std::move(m_name); }
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's expression type.
|
||||
@@ -98,7 +97,7 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
name_type m_name;
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -117,8 +116,6 @@ enum class unaryop_expression_node_t {
|
||||
* @brief Unary operation expression AST node.
|
||||
*/
|
||||
class unaryop_expression_node final : public expression_node {
|
||||
public:
|
||||
using value_type = std::optional<expression_node_r>; /**< Value type. */
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new unaryop expression node object from type and expression node handle.
|
||||
@@ -127,7 +124,7 @@ public:
|
||||
* @param type Operation type.
|
||||
* @param node Handle to the inner expression node.
|
||||
*/
|
||||
unaryop_expression_node(struct location location, unaryop_expression_node_t type, expression_node_r&& node)
|
||||
unaryop_expression_node(struct location location, unaryop_expression_node_t type, expression_node_p&& node)
|
||||
: expression_node(location), m_type(type), m_node(std::move(node)) {}
|
||||
|
||||
/**
|
||||
@@ -135,7 +132,7 @@ public:
|
||||
*
|
||||
* @param node New node handle.
|
||||
*/
|
||||
void set_node(expression_node_r&& node) { m_node = std::move(node); }
|
||||
void set_node(expression_node_p&& node) { m_node = std::move(node); }
|
||||
|
||||
/**
|
||||
* @brief Returns the type of this node's operation.
|
||||
@@ -149,21 +146,21 @@ public:
|
||||
*
|
||||
* @return The inner expression.
|
||||
*/
|
||||
const value_type& get_node() const { return m_node; }
|
||||
const expression_node_p& get_node() const { return m_node; }
|
||||
|
||||
/**
|
||||
* @brief Returns this node's inner expression.
|
||||
*
|
||||
* @return The inner expression.
|
||||
*/
|
||||
value_type& get_node() { return m_node; }
|
||||
expression_node_p& get_node() { return m_node; }
|
||||
|
||||
/**
|
||||
* @brief Moves this node's inner expression.
|
||||
*
|
||||
* @return The moved inner expression.
|
||||
*/
|
||||
value_type&& move_node() { return std::move(m_node); }
|
||||
expression_node_p&& move_node() { return std::move(m_node); }
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's expression type.
|
||||
@@ -179,7 +176,7 @@ protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
unaryop_expression_node_t m_type;
|
||||
value_type m_node; /**< The inner expression. */
|
||||
expression_node_p m_node; /**< The inner expression. */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -216,8 +213,8 @@ public:
|
||||
*/
|
||||
binop_expression_node(struct location location,
|
||||
binop_expression_node_t type,
|
||||
expression_node_r&& lhs,
|
||||
expression_node_r&& rhs)
|
||||
expression_node_p&& lhs,
|
||||
expression_node_p&& rhs)
|
||||
: expression_node(location), m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
|
||||
/**
|
||||
@@ -232,42 +229,42 @@ public:
|
||||
*
|
||||
* @return The left-hand-side expression.
|
||||
*/
|
||||
const expression_node_r& lhs() const { return m_lhs; };
|
||||
const expression_node_p& lhs() const { return m_lhs; };
|
||||
|
||||
/**
|
||||
* @brief Returns this node's left-hand-side expression.
|
||||
*
|
||||
* @return The left-hand-side expression.
|
||||
*/
|
||||
expression_node_r& lhs() { return m_lhs; };
|
||||
expression_node_p& lhs() { return m_lhs; };
|
||||
|
||||
/**
|
||||
* @brief Moves this node's left-hand-side expression.
|
||||
*
|
||||
* @return The moved left-hand-side expression.
|
||||
*/
|
||||
expression_node_r&& move_lhs() { return std::move(m_lhs); };
|
||||
expression_node_p&& move_lhs() { return std::move(m_lhs); };
|
||||
|
||||
/**
|
||||
* @brief Returns this node's right-hand-side expression.
|
||||
*
|
||||
* @return The right-hand-side expression.
|
||||
*/
|
||||
const expression_node_r& rhs() const { return m_rhs; };
|
||||
const expression_node_p& rhs() const { return m_rhs; };
|
||||
|
||||
/**
|
||||
* @brief Returns this node's right-hand-side expression.
|
||||
*
|
||||
* @return The right-hand-side expression.
|
||||
*/
|
||||
expression_node_r& rhs() { return m_rhs; };
|
||||
expression_node_p& rhs() { return m_rhs; };
|
||||
|
||||
/**
|
||||
* @brief Moves this node's right-hand-side expression.
|
||||
*
|
||||
* @return The moved right-hand-side expression.
|
||||
*/
|
||||
expression_node_r&& move_rhs() { return std::move(m_rhs); };
|
||||
expression_node_p&& move_rhs() { return std::move(m_rhs); };
|
||||
public:
|
||||
expression_node_t expression_type() const override { return expression_node_t::Binop; }
|
||||
public:
|
||||
@@ -278,8 +275,8 @@ protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
binop_expression_node_t m_type;
|
||||
expression_node_r m_lhs;
|
||||
expression_node_r m_rhs;
|
||||
expression_node_p m_lhs;
|
||||
expression_node_p m_rhs;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -294,7 +291,7 @@ public:
|
||||
* @param lhs Left-hand-side expression handle.
|
||||
* @param rhs Right-hand-side expression handle.
|
||||
*/
|
||||
var_assign_expression_node(struct location location, expression_node_r&& lhs, expression_node_r&& rhs)
|
||||
var_assign_expression_node(struct location location, expression_node_p&& lhs, expression_node_p&& rhs)
|
||||
: expression_node(location),
|
||||
m_compound(binop_expression_node_t::None),
|
||||
m_lhs(std::move(lhs)),
|
||||
@@ -310,8 +307,8 @@ public:
|
||||
*/
|
||||
var_assign_expression_node(struct location location,
|
||||
binop_expression_node_t compound,
|
||||
expression_node_r&& lhs,
|
||||
expression_node_r&& rhs)
|
||||
expression_node_p&& lhs,
|
||||
expression_node_p&& rhs)
|
||||
: expression_node(location), m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
|
||||
/**
|
||||
@@ -326,14 +323,14 @@ public:
|
||||
*
|
||||
* @return The left-hand-side expression.
|
||||
*/
|
||||
const expression_node_r& lhs() const { return m_lhs; }
|
||||
const expression_node_p& lhs() const { return m_lhs; }
|
||||
|
||||
/**
|
||||
* @brief Returns this node's right-hand-side expression.
|
||||
*
|
||||
* @return The right-hand-side expression.
|
||||
*/
|
||||
const expression_node_r& rhs() const { return m_rhs; }
|
||||
const expression_node_p& rhs() const { return m_rhs; }
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's expression type.
|
||||
@@ -349,8 +346,8 @@ protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
binop_expression_node_t m_compound;
|
||||
expression_node_r m_lhs;
|
||||
expression_node_r m_rhs;
|
||||
expression_node_p m_lhs;
|
||||
expression_node_p m_rhs;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef FURC_AST_PROGRAM_HPP
|
||||
#define FURC_AST_PROGRAM_HPP
|
||||
|
||||
#include "furc/ast/declaration.hpp"
|
||||
#include "furc/ast/node.hpp"
|
||||
|
||||
#include <vector>
|
||||
@@ -29,14 +28,14 @@ public:
|
||||
*
|
||||
* @param declaration Declaration to add.
|
||||
*/
|
||||
void push(node_r<declaration_node>&& declaration) { m_declarations.push_back(std::move(declaration)); }
|
||||
void push(declaration_node_p&& declaration) { m_declarations.push_back(std::move(declaration)); }
|
||||
|
||||
/**
|
||||
* @brief Returns a list of declarations of this program.
|
||||
*
|
||||
* @return The list of this program's declarations.
|
||||
*/
|
||||
const std::vector<node_r<declaration_node>>& declarations() const { return m_declarations; }
|
||||
const std::vector<declaration_node_p>& declarations() const { return m_declarations; }
|
||||
public:
|
||||
void accept(visitor& visitor) const override;
|
||||
|
||||
@@ -44,7 +43,7 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
std::vector<node_r<declaration_node>> m_declarations;
|
||||
std::vector<declaration_node_p> m_declarations;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -46,7 +46,7 @@ protected:
|
||||
*/
|
||||
class return_statement_node final : public statement_node, public abstract_node {
|
||||
public:
|
||||
using value_type = std::optional<expression_node_r>; /**< Value type. */
|
||||
using value_type = std::optional<expression_node_p>; /**< Value type. */
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new return statement AST node.
|
||||
@@ -60,7 +60,7 @@ public:
|
||||
* @param location Node location.
|
||||
* @param value Return value handle.
|
||||
*/
|
||||
return_statement_node(struct location location, expression_node_r&& value)
|
||||
return_statement_node(struct location location, expression_node_p&& value)
|
||||
: abstract_node(location), m_value(std::move(value)) {}
|
||||
public:
|
||||
/**
|
||||
@@ -98,7 +98,7 @@ public:
|
||||
* @param cond Condition expression handle.
|
||||
* @param then Then statement handle.
|
||||
*/
|
||||
if_statement_node(struct location location, expression_node_r&& cond, statement_node_r&& then)
|
||||
if_statement_node(struct location location, expression_node_p&& cond, statement_node_p&& then)
|
||||
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)) {}
|
||||
|
||||
/**
|
||||
@@ -110,9 +110,9 @@ public:
|
||||
* @param elze Else statement handle.
|
||||
*/
|
||||
if_statement_node(struct location location,
|
||||
expression_node_r&& cond,
|
||||
statement_node_r&& then,
|
||||
statement_node_r&& elze)
|
||||
expression_node_p&& cond,
|
||||
statement_node_p&& then,
|
||||
statement_node_p&& elze)
|
||||
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
|
||||
public:
|
||||
/**
|
||||
@@ -120,21 +120,21 @@ public:
|
||||
*
|
||||
* @return The condition expression handle.
|
||||
*/
|
||||
expression_node_r cond() const { return m_cond; }
|
||||
expression_node_p cond() const { return m_cond; }
|
||||
|
||||
/**
|
||||
* @brief Returns this node's then statement handle.
|
||||
*
|
||||
* @return The then statement handle.
|
||||
*/
|
||||
const statement_node_r& then() const { return m_then; }
|
||||
const statement_node_p& then() const { return m_then; }
|
||||
|
||||
/**
|
||||
* @brief Returns this node's else statement handle.
|
||||
*
|
||||
* @return The else statement handle.
|
||||
*/
|
||||
const std::optional<statement_node_r>& elze() const { return m_else; }
|
||||
const std::optional<statement_node_p>& elze() const { return m_else; }
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's statement type.
|
||||
@@ -149,9 +149,9 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
expression_node_r m_cond; /**< The condition expression handle */
|
||||
statement_node_r m_then; /**< The then statement handle */
|
||||
std::optional<statement_node_r> m_else; /**< The else statement handle */
|
||||
expression_node_p m_cond; /**< The condition expression handle */
|
||||
statement_node_p m_then; /**< The then statement handle */
|
||||
std::optional<statement_node_p> m_else; /**< The else statement handle */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -165,7 +165,7 @@ public:
|
||||
* @param location Node location.
|
||||
* @param body Body handle.
|
||||
*/
|
||||
compound_statement_node(struct location location, body_r&& body)
|
||||
compound_statement_node(struct location location, body&& body)
|
||||
: abstract_node(location), m_body(std::move(body)) {}
|
||||
public:
|
||||
/**
|
||||
@@ -173,7 +173,7 @@ public:
|
||||
*
|
||||
* @return The body handle.
|
||||
*/
|
||||
const body_r& body() const { return m_body; }
|
||||
const body& body() const { return m_body; }
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's statement type.
|
||||
@@ -188,7 +188,7 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
body_r m_body; /**< The body handle. */
|
||||
struct body m_body; /**< The body handle. */
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -62,7 +62,7 @@ private:
|
||||
|
||||
ast::expression_node_r parse_expression_primary();
|
||||
ast::expression_node_r parse_expression_unary(std::uint32_t precedence);
|
||||
ast::expression_node_r parse_expression_rhs(ast::expression_node_r&& init, std::uint32_t precedence);
|
||||
ast::expression_node_r parse_expression_rhs(ast::expression_node_p&& init, std::uint32_t precedence);
|
||||
|
||||
ast::body_r parse_body();
|
||||
private:
|
||||
|
||||
+5
-13
@@ -21,8 +21,7 @@ void var_read_expression_node::accept(visitor& visitor) const {
|
||||
}
|
||||
|
||||
std::ostream& var_read_expression_node::print(std::ostream& os) const {
|
||||
if (m_name.has_error()) return os << m_name.error();
|
||||
return os << *m_name;
|
||||
return os << m_name;
|
||||
}
|
||||
|
||||
bool var_read_expression_node::equal(const node& rhsNode) const {
|
||||
@@ -47,7 +46,7 @@ void unaryop_expression_node::accept(visitor& visitor) const {
|
||||
}
|
||||
|
||||
std::ostream& unaryop_expression_node::print(std::ostream& os) const {
|
||||
if (!m_node.has_value()) return os;
|
||||
if (m_node == nullptr) return os;
|
||||
switch (m_type) {
|
||||
case unaryop_expression_node_t::Positive:
|
||||
case unaryop_expression_node_t::Negative:
|
||||
@@ -132,12 +131,9 @@ void function_definition_node::accept(visitor& visitor) const {
|
||||
std::ostream& function_definition_node::print(std::ostream& os) const {
|
||||
function_declaration_node::print(os);
|
||||
os << ":\n";
|
||||
if (m_body.has_value()) {
|
||||
for (const auto& entry : m_body->statements)
|
||||
for (const auto& entry : m_body.statements)
|
||||
os << entry << '\n';
|
||||
return os << m_body->end << ": " << p_name << " end";
|
||||
}
|
||||
return os << m_body.error();
|
||||
return os << m_body.end << ": " << p_name << " end";
|
||||
}
|
||||
|
||||
bool function_definition_node::equal(const node& rhs) const {
|
||||
@@ -192,11 +188,7 @@ bool compound_statement_node::equal(const node& rhs) const {
|
||||
|
||||
void program_node::accept(visitor& visitor) const {
|
||||
for (const auto& decl : m_declarations) {
|
||||
if (decl.has_error()) {
|
||||
visitor.visit_error(decl.error());
|
||||
} else {
|
||||
decl.value()->accept(visitor);
|
||||
}
|
||||
decl->accept(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
namespace furc::front {
|
||||
|
||||
@@ -18,28 +17,18 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
|
||||
m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()));
|
||||
|
||||
push_block();
|
||||
if (funcDef.body().has_error()) {
|
||||
std::cerr << funcDef.body().error() << '\n';
|
||||
return;
|
||||
}
|
||||
for (const auto& stmt : funcDef.body()->statements) {
|
||||
for (const auto& stmt : funcDef.body().statements) {
|
||||
stmt.value()->accept(*this);
|
||||
}
|
||||
|
||||
m_currentBlock->emplace<ir::return_instruction>();
|
||||
|
||||
m_module.push(std::move(m_currentFunction));
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::return_statement_node& returnStmt) {
|
||||
if (!returnStmt.value().has_value()) return;
|
||||
auto value = returnStmt.value().value();
|
||||
if (value.has_error()) {
|
||||
std::cerr << value.error() << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.has_value()) {
|
||||
value.value()->accept(*this);
|
||||
if (returnStmt.value().has_value()) {
|
||||
returnStmt.value().value()->accept(*this);
|
||||
push<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
|
||||
} else {
|
||||
push<ir::return_instruction>();
|
||||
@@ -47,19 +36,19 @@ void ir_generator::visit(const ast::return_statement_node& returnStmt) {
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::if_statement_node& node) {
|
||||
node.cond().value()->accept(*this);
|
||||
node.cond()->accept(*this);
|
||||
ir_register cond = m_registerCounter - 1;
|
||||
push<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
|
||||
m_currentFunction->blocks().size(),
|
||||
m_currentFunction->blocks().size() + 1);
|
||||
|
||||
push_block(); // then block
|
||||
node.then().value()->accept(*this);
|
||||
node.then()->accept(*this);
|
||||
if (node.elze().has_value()) {
|
||||
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
|
||||
|
||||
push_block(); // else block
|
||||
node.elze().value().value()->accept(*this);
|
||||
node.elze().value()->accept(*this);
|
||||
}
|
||||
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size());
|
||||
|
||||
@@ -67,7 +56,7 @@ void ir_generator::visit(const ast::if_statement_node& node) {
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::compound_statement_node& node) {
|
||||
for (const auto& stmt : node.body()->statements) {
|
||||
for (const auto& stmt : node.body().statements) {
|
||||
stmt.value()->accept(*this);
|
||||
}
|
||||
}
|
||||
@@ -83,7 +72,7 @@ void ir_generator::visit(const ast::integer_literal_node& node) {
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::var_read_expression_node& node) {
|
||||
if (auto it = m_variables.find(*node.get_name()); it != m_variables.end()) {
|
||||
if (auto it = m_variables.find(node.get_name()); it != m_variables.end()) {
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_reg(it->second),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
} else {
|
||||
@@ -114,9 +103,9 @@ static inline furlang::ir::binary_op_instruction_t binary_op_instruction_t(ast::
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::binop_expression_node& node) {
|
||||
node.lhs().value()->accept(*this);
|
||||
node.lhs()->accept(*this);
|
||||
ir_register lhs = m_registerCounter - 1;
|
||||
node.rhs().value()->accept(*this);
|
||||
node.rhs()->accept(*this);
|
||||
ir_register rhs = m_registerCounter - 1;
|
||||
ir_register dst = m_registerCounter++;
|
||||
push<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
|
||||
@@ -126,16 +115,16 @@ void ir_generator::visit(const ast::binop_expression_node& node) {
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::var_assign_expression_node& node) {
|
||||
node.rhs().value()->accept(*this);
|
||||
node.rhs()->accept(*this);
|
||||
ir_register rhs = m_registerCounter - 1;
|
||||
assert(node.lhs().value()->expression_type() == ast::expression_node_t::VarRead);
|
||||
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs().value());
|
||||
assert(node.lhs()->expression_type() == ast::expression_node_t::VarRead);
|
||||
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs());
|
||||
|
||||
ir_register reg = 0;
|
||||
if (auto it = m_variables.find(*lhs->get_name()); it != m_variables.end()) {
|
||||
if (auto it = m_variables.find(lhs->get_name()); it != m_variables.end()) {
|
||||
reg = it->second;
|
||||
} else {
|
||||
m_variables[*lhs->get_name()] = reg = m_registerCounter++;
|
||||
m_variables[lhs->get_name()] = reg = m_registerCounter++;
|
||||
}
|
||||
|
||||
auto compound = node.compound();
|
||||
|
||||
+55
-20
@@ -34,10 +34,15 @@ ast::program_node_r parser::parse() & {
|
||||
auto program = m_arena.allocate_shared<ast::program_node>(location{ m_filename });
|
||||
|
||||
while (peek_token().has_value()) {
|
||||
program->push(std::move(parse_declaration()));
|
||||
auto decl = parse_declaration();
|
||||
if (decl.has_error()) {
|
||||
program = nullptr;
|
||||
} else if (program != nullptr) {
|
||||
program->push(std::move(decl.value()));
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(program);
|
||||
return program != nullptr ? std::move(program) : ast::program_node_r(ast::error{ location{ m_filename } });
|
||||
}
|
||||
|
||||
ast::declaration_node_r parser::parse_declaration() {
|
||||
@@ -63,7 +68,7 @@ ast::declaration_node_r parser::parse_declaration() {
|
||||
if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location });
|
||||
return m_arena.allocate_shared<ast::function_definition_node>(first->location,
|
||||
name->value.string,
|
||||
std::move(body));
|
||||
std::move(body.value()));
|
||||
}
|
||||
case token_t::Semicolon: {
|
||||
m_peekBuffer.clear();
|
||||
@@ -109,7 +114,7 @@ ast::statement_node_r parser::parse_statement() {
|
||||
auto value = parse_expression();
|
||||
auto err = eat_token(token_t::Semicolon);
|
||||
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
|
||||
return m_arena.allocate_shared<ast::return_statement_node>(location, std::move(value));
|
||||
return m_arena.allocate_shared<ast::return_statement_node>(location, std::move(value.value()));
|
||||
}
|
||||
case keyword_token::If: {
|
||||
auto tok = next_token();
|
||||
@@ -128,20 +133,28 @@ ast::statement_node_r parser::parse_statement() {
|
||||
peek_token()->value.keyword == keyword_token::Else) {
|
||||
next_token();
|
||||
|
||||
auto elseBody = parse_statement();
|
||||
if (elseBody.has_error()) return ast::statement_node_r(ast::error{ elseBody.error().location });
|
||||
return m_arena.allocate_shared<ast::if_statement_node>(location,
|
||||
std::move(cond),
|
||||
std::move(then),
|
||||
std::move(parse_statement()));
|
||||
std::move(cond.value()),
|
||||
std::move(then.value()),
|
||||
std::move(elseBody.value()));
|
||||
}
|
||||
|
||||
return m_arena.allocate_shared<ast::if_statement_node>(location, std::move(cond), std::move(then));
|
||||
return m_arena.allocate_shared<ast::if_statement_node>(location,
|
||||
std::move(cond.value()),
|
||||
std::move(then.value()));
|
||||
}
|
||||
case keyword_token::None:
|
||||
case keyword_token::Func:
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
case token_t::LBrace: return m_arena.allocate_shared<ast::compound_statement_node>(location, parse_body());
|
||||
case token_t::LBrace: {
|
||||
auto body = parse_body();
|
||||
if (body.has_error()) return ast::statement_node_r(ast::error{ body.error().location });
|
||||
return m_arena.allocate_shared<ast::compound_statement_node>(location, std::move(body.value()));
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
@@ -159,7 +172,11 @@ ast::statement_node_r parser::parse_statement() {
|
||||
}
|
||||
|
||||
ast::expression_node_r parser::parse_expression(std::uint32_t precedence) {
|
||||
return parse_expression_rhs(parse_expression_unary(precedence), precedence);
|
||||
auto expr = parse_expression_unary(precedence);
|
||||
if (expr.has_error()) {
|
||||
return ast::expression_node_r(ast::error{ expr.error().location });
|
||||
}
|
||||
return parse_expression_rhs(std::move(expr.value()), precedence);
|
||||
}
|
||||
|
||||
ast::expression_node_r parser::parse_expression_primary() {
|
||||
@@ -214,12 +231,16 @@ ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence)
|
||||
if (current.precedence >= precedence) break;
|
||||
auto token = next_token();
|
||||
|
||||
ast::expression_node_r expression;
|
||||
ast::expression_node_p expression;
|
||||
|
||||
auto nextIt = s_prefixes.find(peek_token()->type);
|
||||
if (nextIt != s_prefixes.end()) {
|
||||
auto next = nextIt->second;
|
||||
expression = parse_expression_unary(current.precedence + 1);
|
||||
auto expr = parse_expression_unary(current.precedence + 1);
|
||||
if (expr.has_error()) {
|
||||
return ast::expression_node_r(ast::error{ expr.error().location });
|
||||
}
|
||||
expression = std::move(std::move(expr.value()));
|
||||
}
|
||||
|
||||
result =
|
||||
@@ -227,7 +248,13 @@ ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence)
|
||||
}
|
||||
|
||||
if (result == nullptr) return parse_expression_primary();
|
||||
if (result->get_node().has_value()) result->set_node(parse_expression_primary());
|
||||
if (result->get_node() == nullptr) {
|
||||
auto expr = parse_expression_primary();
|
||||
if (expr.has_error()) {
|
||||
return ast::expression_node_r(ast::error{ expr.error().location });
|
||||
}
|
||||
result->set_node(std::move(std::move(expr.value())));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -282,7 +309,7 @@ struct rhsop_info {
|
||||
}
|
||||
};
|
||||
|
||||
ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& init, std::uint32_t precedence) {
|
||||
ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_p&& init, std::uint32_t precedence) {
|
||||
static std::unordered_map<token_t, rhsop_info> s_rhsops = {
|
||||
{ token_t::Plus, rhsop_info::create(ast::binop_expression_node_t::Add, 5, associativity::Left) },
|
||||
{ token_t::Minus, rhsop_info::create(ast::binop_expression_node_t::Sub, 5, associativity::Left) },
|
||||
@@ -306,7 +333,7 @@ ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& ini
|
||||
{ token_t::GreaterEq, rhsop_info::create(ast::binop_expression_node_t::GreaterEqual, 9, associativity::Left) },
|
||||
};
|
||||
|
||||
ast::expression_node_r lhs = std::move(init);
|
||||
ast::expression_node_p lhs = std::move(init);
|
||||
while (peek_token().has_value()) {
|
||||
auto it = s_rhsops.find(peek_token()->type);
|
||||
if (it == s_rhsops.end()) return lhs;
|
||||
@@ -315,20 +342,28 @@ ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& ini
|
||||
if (current.precedence >= precedence) return lhs;
|
||||
auto opToken = next_token();
|
||||
|
||||
ast::expression_node_r rhs;
|
||||
ast::expression_node_p rhs;
|
||||
if (current.type != rhsop_info_t::Unaryop) {
|
||||
rhs = parse_expression_unary(current.precedence + 1); // unary prefix is always right-associative
|
||||
auto expr = parse_expression_unary(current.precedence + 1); // unary prefix is always right-associative
|
||||
if (expr.has_error()) {
|
||||
return ast::expression_node_r(ast::error{ expr.error().location });
|
||||
}
|
||||
rhs = std::move(expr.value());
|
||||
}
|
||||
|
||||
auto nextIt = s_rhsops.find(peek_token()->type);
|
||||
if (nextIt != s_rhsops.end()) {
|
||||
rhsop_info next = nextIt->second;
|
||||
|
||||
if (current.type != rhsop_info_t::Unaryop) {
|
||||
rhs = std::move(parse_expression_rhs(std::move(rhs),
|
||||
auto expr = std::move(parse_expression_rhs(std::move(rhs),
|
||||
current.precedence + static_cast<std::uint32_t>(current.associativity == associativity::Right)));
|
||||
if (expr.has_error()) {
|
||||
return ast::expression_node_r(ast::error{ expr.error().location });
|
||||
}
|
||||
if (current.type != rhsop_info_t::Unaryop) {
|
||||
rhs = std::move(expr.value());
|
||||
} else {
|
||||
lhs = std::move(parse_expression_rhs(std::move(lhs), current.precedence));
|
||||
lhs = std::move(expr.value());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user