refactor(AST): node location
Add location function to node and introduce a new abstract_node class. Refs: #1
This commit is contained in:
@@ -19,7 +19,15 @@ enum class declaration_node_t {
|
||||
/**
|
||||
* @brief Declaration AST node interface.
|
||||
*/
|
||||
class declaration_node : public statement_node {
|
||||
class declaration_node : public statement_node, public abstract_node {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new declaration AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
*/
|
||||
declaration_node(struct location location)
|
||||
: abstract_node(location) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's category.
|
||||
@@ -53,10 +61,11 @@ public:
|
||||
/**
|
||||
* @brief Construct a new function declaration node object from name token.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param name Name of the function.
|
||||
*/
|
||||
function_declaration_node(front::token name)
|
||||
: p_name(name) {}
|
||||
function_declaration_node(struct location location, front::token name)
|
||||
: declaration_node(location), p_name(name) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's declaration type.
|
||||
@@ -92,11 +101,12 @@ public:
|
||||
/**
|
||||
* @brief Construct a new function definition node object from name and body.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param name Name of the function.
|
||||
* @param body Body of the function.
|
||||
*/
|
||||
function_definition_node(front::token name, body_r&& body)
|
||||
: function_declaration_node(name), m_body(std::move(body)) {}
|
||||
function_definition_node(struct location location, front::token name, body_r&& body)
|
||||
: function_declaration_node(location, name), m_body(std::move(body)) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's declaration type.
|
||||
|
||||
@@ -21,7 +21,15 @@ enum class expression_node_t {
|
||||
/**
|
||||
* @brief Expression AST node.
|
||||
*/
|
||||
class expression_node : public statement_node {
|
||||
class expression_node : public statement_node, public abstract_node {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new expression AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
*/
|
||||
expression_node(struct location location)
|
||||
: abstract_node(location) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's category.
|
||||
@@ -57,10 +65,11 @@ public:
|
||||
/**
|
||||
* @brief Construct a new var read expression node object from a name handle.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param name Handle to the name.
|
||||
*/
|
||||
var_read_expression_node(name_type&& name)
|
||||
: m_name(std::move(name)) {}
|
||||
var_read_expression_node(struct location location, name_type&& name)
|
||||
: expression_node(location), m_name(std::move(name)) {}
|
||||
|
||||
/**
|
||||
* @brief Returns the variable's name.
|
||||
@@ -114,11 +123,12 @@ public:
|
||||
/**
|
||||
* @brief Construct a new unaryop expression node object from type and expression node handle.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param type Operation type.
|
||||
* @param node Handle to the inner expression node.
|
||||
*/
|
||||
unaryop_expression_node(unaryop_expression_node_t type, expression_node_r&& node)
|
||||
: m_type(type), m_node(std::move(node)) {}
|
||||
unaryop_expression_node(struct location location, unaryop_expression_node_t type, expression_node_r&& node)
|
||||
: expression_node(location), m_type(type), m_node(std::move(node)) {}
|
||||
|
||||
/**
|
||||
* @brief Sets this node's inner expression.
|
||||
@@ -199,12 +209,16 @@ public:
|
||||
/**
|
||||
* @brief Construct a new binary operation expression AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param type Binary operation type.
|
||||
* @param lhs Left-hand-side expression.
|
||||
* @param rhs Right-hand-side expression.
|
||||
*/
|
||||
binop_expression_node(binop_expression_node_t type, expression_node_r&& lhs, expression_node_r&& rhs)
|
||||
: m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
binop_expression_node(struct location location,
|
||||
binop_expression_node_t type,
|
||||
expression_node_r&& lhs,
|
||||
expression_node_r&& rhs)
|
||||
: expression_node(location), m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
|
||||
/**
|
||||
* @brief Returns this node's binary operation type.
|
||||
@@ -276,21 +290,29 @@ public:
|
||||
/**
|
||||
* @brief Construct a new variable assignment expression AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param lhs Left-hand-side expression handle.
|
||||
* @param rhs Right-hand-side expression handle.
|
||||
*/
|
||||
var_assign_expression_node(expression_node_r&& lhs, expression_node_r&& rhs)
|
||||
: m_compound(binop_expression_node_t::None), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
var_assign_expression_node(struct location location, expression_node_r&& lhs, expression_node_r&& rhs)
|
||||
: expression_node(location),
|
||||
m_compound(binop_expression_node_t::None),
|
||||
m_lhs(std::move(lhs)),
|
||||
m_rhs(std::move(rhs)) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a new compound variable assignment expression AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param compound Compound operation type.
|
||||
* @param lhs Left-hand-side expression handle.
|
||||
* @param rhs Right-hand-side expression handle.
|
||||
*/
|
||||
var_assign_expression_node(binop_expression_node_t compound, expression_node_r&& lhs, expression_node_r&& rhs)
|
||||
: m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
var_assign_expression_node(struct location location,
|
||||
binop_expression_node_t compound,
|
||||
expression_node_r&& lhs,
|
||||
expression_node_r&& rhs)
|
||||
: expression_node(location), m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
|
||||
|
||||
/**
|
||||
* @brief Returns this node's compound operation type.
|
||||
|
||||
@@ -20,6 +20,14 @@ enum class literal_node_t {
|
||||
* @brief Literal AST node.
|
||||
*/
|
||||
class literal_node : public expression_node {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new literal AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
*/
|
||||
literal_node(struct location location)
|
||||
: expression_node(location) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's category.
|
||||
@@ -55,10 +63,11 @@ public:
|
||||
/**
|
||||
* @brief Construct a new string literal node object from a handle.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param value A string view result.
|
||||
*/
|
||||
string_literal_node(value_type&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
string_literal_node(struct location location, value_type&& value)
|
||||
: literal_node(location), m_value(std::move(value)) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's literal type.
|
||||
@@ -93,10 +102,11 @@ public:
|
||||
/**
|
||||
* @brief Construct a new integer literal node object from a handle.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param value An integer result.
|
||||
*/
|
||||
integer_literal_node(value_type&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
integer_literal_node(struct location location, value_type&& value)
|
||||
: literal_node(location), m_value(std::move(value)) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's literal type.
|
||||
|
||||
@@ -71,6 +71,14 @@ public:
|
||||
* @return The node category.
|
||||
*/
|
||||
virtual node_t category() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Returns the location of this AST node.
|
||||
* @see locaiton
|
||||
*
|
||||
* @return The location.
|
||||
*/
|
||||
virtual location location() const = 0;
|
||||
public:
|
||||
/**
|
||||
* @brief Compares two nodes for equality.
|
||||
@@ -128,6 +136,28 @@ protected:
|
||||
virtual bool equal(const node& rhs) const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief An abstract AST node.
|
||||
* @see node
|
||||
*
|
||||
* Implements location().
|
||||
*/
|
||||
class abstract_node : public virtual node {
|
||||
public:
|
||||
abstract_node(struct location location)
|
||||
: p_location(location) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns the location of this AST node.
|
||||
* @see locaiton
|
||||
*
|
||||
* @return The location.
|
||||
*/
|
||||
struct location location() const override { return p_location; }
|
||||
protected:
|
||||
struct location p_location; /**< Node location. */
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
} // namespace furc
|
||||
|
||||
|
||||
@@ -12,9 +12,15 @@ namespace ast {
|
||||
/**
|
||||
* @brief Program AST node.
|
||||
*/
|
||||
class program_node final : public node {
|
||||
class program_node final : public abstract_node {
|
||||
public:
|
||||
program_node() = default;
|
||||
/**
|
||||
* @brief Construct a new program AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
*/
|
||||
program_node(struct location location)
|
||||
: abstract_node(location) {}
|
||||
|
||||
node_t category() const override { return node_t::Program; }
|
||||
public:
|
||||
|
||||
@@ -22,7 +22,7 @@ enum class statement_node_t {
|
||||
/**
|
||||
* @brief Statement AST node.
|
||||
*/
|
||||
class statement_node : public node {
|
||||
class statement_node : public virtual node {
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's category.
|
||||
@@ -44,19 +44,24 @@ protected:
|
||||
/**
|
||||
* @brief Return statement AST node.
|
||||
*/
|
||||
class return_statement_node final : public statement_node {
|
||||
class return_statement_node final : public statement_node, public abstract_node {
|
||||
public:
|
||||
using value_type = std::optional<expression_node_r>; /**< Value type. */
|
||||
public:
|
||||
return_statement_node() = default;
|
||||
/**
|
||||
* @brief Construct a new return statement AST node.
|
||||
*/
|
||||
return_statement_node(struct location location)
|
||||
: abstract_node(location) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a new return statement AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param value Return value handle.
|
||||
*/
|
||||
return_statement_node(expression_node_r&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
return_statement_node(struct location location, expression_node_r&& value)
|
||||
: abstract_node(location), m_value(std::move(value)) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's return value handle.
|
||||
@@ -84,26 +89,31 @@ private:
|
||||
/**
|
||||
* @brief If statement AST node.
|
||||
*/
|
||||
class if_statement_node final : public statement_node {
|
||||
class if_statement_node final : public statement_node, public abstract_node {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new if statement AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param cond Condition expression handle.
|
||||
* @param then Then statement handle.
|
||||
*/
|
||||
if_statement_node(expression_node_r&& cond, statement_node_r&& then)
|
||||
: m_cond(std::move(cond)), m_then(std::move(then)) {}
|
||||
if_statement_node(struct location location, expression_node_r&& cond, statement_node_r&& then)
|
||||
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a new if statement AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param cond Condition expression handle.
|
||||
* @param then Then statement handle.
|
||||
* @param elze Else statement handle.
|
||||
*/
|
||||
if_statement_node(expression_node_r&& cond, statement_node_r&& then, statement_node_r&& elze)
|
||||
: m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
|
||||
if_statement_node(struct location location,
|
||||
expression_node_r&& cond,
|
||||
statement_node_r&& then,
|
||||
statement_node_r&& elze)
|
||||
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's condition expression handle.
|
||||
@@ -147,15 +157,16 @@ private:
|
||||
/**
|
||||
* @brief Compound statement AST node.
|
||||
*/
|
||||
class compound_statement_node final : public statement_node {
|
||||
class compound_statement_node final : public statement_node, public abstract_node {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new compound statement AST node.
|
||||
*
|
||||
* @param location Node location.
|
||||
* @param body Body handle.
|
||||
*/
|
||||
compound_statement_node(body_r&& body)
|
||||
: m_body(std::move(body)) {}
|
||||
compound_statement_node(struct location location, body_r&& body)
|
||||
: abstract_node(location), m_body(std::move(body)) {}
|
||||
public:
|
||||
/**
|
||||
* @brief Returns this node's body handle.
|
||||
|
||||
Reference in New Issue
Block a user