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:
|
||||
|
||||
Reference in New Issue
Block a user