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.
|
* @param body Body of the function.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
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)) {}
|
: function_declaration_node(location, std::forward<T>(name)), m_body(std::move(body)) {}
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@@ -123,7 +123,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @return Body of the function.
|
* @return Body of the function.
|
||||||
*/
|
*/
|
||||||
const body_r& body() const { return m_body; }
|
const body& body() const { return m_body; }
|
||||||
public:
|
public:
|
||||||
void accept(visitor& visitor) const override;
|
void accept(visitor& visitor) const override;
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
body_r m_body;
|
struct body m_body;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
|||||||
@@ -59,8 +59,6 @@ protected:
|
|||||||
* @brief Var read expression AST node.
|
* @brief Var read expression AST node.
|
||||||
*/
|
*/
|
||||||
class var_read_expression_node final : public expression_node {
|
class var_read_expression_node final : public expression_node {
|
||||||
public:
|
|
||||||
using name_type = furlang::result<std::string_view, error>; /**< Name type alias. */
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new var read expression node object from a name handle.
|
* @brief Construct a new var read expression node object from a name handle.
|
||||||
@@ -68,22 +66,23 @@ public:
|
|||||||
* @param location Node location.
|
* @param location Node location.
|
||||||
* @param name Handle to the name.
|
* @param name Handle to the name.
|
||||||
*/
|
*/
|
||||||
var_read_expression_node(struct location location, name_type&& name)
|
template <typename T>
|
||||||
: expression_node(location), m_name(std::move(name)) {}
|
var_read_expression_node(struct location location, T&& name)
|
||||||
|
: expression_node(location), m_name(std::forward<T>(name)) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the variable's name.
|
* @brief Returns the variable's name.
|
||||||
*
|
*
|
||||||
* @return Name of the variable.
|
* @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.
|
* @brief Returns the variable's name.
|
||||||
*
|
*
|
||||||
* @return Name of the variable.
|
* @return Name of the variable.
|
||||||
*/
|
*/
|
||||||
name_type&& move_name() { return std::move(m_name); }
|
std::string&& move_name() { return std::move(m_name); }
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns this node's expression type.
|
* @brief Returns this node's expression type.
|
||||||
@@ -98,7 +97,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
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.
|
* @brief Unary operation expression AST node.
|
||||||
*/
|
*/
|
||||||
class unaryop_expression_node final : public expression_node {
|
class unaryop_expression_node final : public expression_node {
|
||||||
public:
|
|
||||||
using value_type = std::optional<expression_node_r>; /**< Value type. */
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new unaryop expression node object from type and expression node handle.
|
* @brief Construct a new unaryop expression node object from type and expression node handle.
|
||||||
@@ -127,7 +124,7 @@ public:
|
|||||||
* @param type Operation type.
|
* @param type Operation type.
|
||||||
* @param node Handle to the inner expression node.
|
* @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)) {}
|
: expression_node(location), m_type(type), m_node(std::move(node)) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -135,7 +132,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @param node New node handle.
|
* @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.
|
* @brief Returns the type of this node's operation.
|
||||||
@@ -149,21 +146,21 @@ public:
|
|||||||
*
|
*
|
||||||
* @return The inner expression.
|
* @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.
|
* @brief Returns this node's inner expression.
|
||||||
*
|
*
|
||||||
* @return The 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.
|
* @brief Moves this node's inner expression.
|
||||||
*
|
*
|
||||||
* @return The moved 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:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns this node's expression type.
|
* @brief Returns this node's expression type.
|
||||||
@@ -179,7 +176,7 @@ protected:
|
|||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
unaryop_expression_node_t m_type;
|
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(struct location location,
|
||||||
binop_expression_node_t type,
|
binop_expression_node_t type,
|
||||||
expression_node_r&& lhs,
|
expression_node_p&& lhs,
|
||||||
expression_node_r&& rhs)
|
expression_node_p&& rhs)
|
||||||
: expression_node(location), m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(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.
|
* @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.
|
* @brief Returns this node's left-hand-side expression.
|
||||||
*
|
*
|
||||||
* @return The 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.
|
* @brief Moves this node's left-hand-side expression.
|
||||||
*
|
*
|
||||||
* @return The moved 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.
|
* @brief Returns this node's right-hand-side expression.
|
||||||
*
|
*
|
||||||
* @return The 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.
|
* @brief Returns this node's right-hand-side expression.
|
||||||
*
|
*
|
||||||
* @return The 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.
|
* @brief Moves this node's right-hand-side expression.
|
||||||
*
|
*
|
||||||
* @return The moved 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:
|
public:
|
||||||
expression_node_t expression_type() const override { return expression_node_t::Binop; }
|
expression_node_t expression_type() const override { return expression_node_t::Binop; }
|
||||||
public:
|
public:
|
||||||
@@ -278,8 +275,8 @@ protected:
|
|||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
binop_expression_node_t m_type;
|
binop_expression_node_t m_type;
|
||||||
expression_node_r m_lhs;
|
expression_node_p m_lhs;
|
||||||
expression_node_r m_rhs;
|
expression_node_p m_rhs;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,7 +291,7 @@ public:
|
|||||||
* @param lhs Left-hand-side expression handle.
|
* @param lhs Left-hand-side expression handle.
|
||||||
* @param rhs Right-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),
|
: expression_node(location),
|
||||||
m_compound(binop_expression_node_t::None),
|
m_compound(binop_expression_node_t::None),
|
||||||
m_lhs(std::move(lhs)),
|
m_lhs(std::move(lhs)),
|
||||||
@@ -310,8 +307,8 @@ public:
|
|||||||
*/
|
*/
|
||||||
var_assign_expression_node(struct location location,
|
var_assign_expression_node(struct location location,
|
||||||
binop_expression_node_t compound,
|
binop_expression_node_t compound,
|
||||||
expression_node_r&& lhs,
|
expression_node_p&& lhs,
|
||||||
expression_node_r&& rhs)
|
expression_node_p&& rhs)
|
||||||
: expression_node(location), m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(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.
|
* @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.
|
* @brief Returns this node's right-hand-side expression.
|
||||||
*
|
*
|
||||||
* @return The 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:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns this node's expression type.
|
* @brief Returns this node's expression type.
|
||||||
@@ -349,8 +346,8 @@ protected:
|
|||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
binop_expression_node_t m_compound;
|
binop_expression_node_t m_compound;
|
||||||
expression_node_r m_lhs;
|
expression_node_p m_lhs;
|
||||||
expression_node_r m_rhs;
|
expression_node_p m_rhs;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#ifndef FURC_AST_PROGRAM_HPP
|
#ifndef FURC_AST_PROGRAM_HPP
|
||||||
#define FURC_AST_PROGRAM_HPP
|
#define FURC_AST_PROGRAM_HPP
|
||||||
|
|
||||||
#include "furc/ast/declaration.hpp"
|
|
||||||
#include "furc/ast/node.hpp"
|
#include "furc/ast/node.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -29,14 +28,14 @@ public:
|
|||||||
*
|
*
|
||||||
* @param declaration Declaration to add.
|
* @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.
|
* @brief Returns a list of declarations of this program.
|
||||||
*
|
*
|
||||||
* @return The list of this program's declarations.
|
* @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:
|
public:
|
||||||
void accept(visitor& visitor) const override;
|
void accept(visitor& visitor) const override;
|
||||||
|
|
||||||
@@ -44,7 +43,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
std::vector<node_r<declaration_node>> m_declarations;
|
std::vector<declaration_node_p> m_declarations;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ protected:
|
|||||||
*/
|
*/
|
||||||
class return_statement_node final : public statement_node, public abstract_node {
|
class return_statement_node final : public statement_node, public abstract_node {
|
||||||
public:
|
public:
|
||||||
using value_type = std::optional<expression_node_r>; /**< Value type. */
|
using value_type = std::optional<expression_node_p>; /**< Value type. */
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new return statement AST node.
|
* @brief Construct a new return statement AST node.
|
||||||
@@ -60,7 +60,7 @@ public:
|
|||||||
* @param location Node location.
|
* @param location Node location.
|
||||||
* @param value Return value handle.
|
* @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)) {}
|
: abstract_node(location), m_value(std::move(value)) {}
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@@ -98,7 +98,7 @@ public:
|
|||||||
* @param cond Condition expression handle.
|
* @param cond Condition expression handle.
|
||||||
* @param then Then statement 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)) {}
|
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,9 +110,9 @@ public:
|
|||||||
* @param elze Else statement handle.
|
* @param elze Else statement handle.
|
||||||
*/
|
*/
|
||||||
if_statement_node(struct location location,
|
if_statement_node(struct location location,
|
||||||
expression_node_r&& cond,
|
expression_node_p&& cond,
|
||||||
statement_node_r&& then,
|
statement_node_p&& then,
|
||||||
statement_node_r&& elze)
|
statement_node_p&& elze)
|
||||||
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
|
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@@ -120,21 +120,21 @@ public:
|
|||||||
*
|
*
|
||||||
* @return The condition expression handle.
|
* @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.
|
* @brief Returns this node's then statement handle.
|
||||||
*
|
*
|
||||||
* @return The 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.
|
* @brief Returns this node's else statement handle.
|
||||||
*
|
*
|
||||||
* @return The 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:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns this node's statement type.
|
* @brief Returns this node's statement type.
|
||||||
@@ -149,9 +149,9 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
expression_node_r m_cond; /**< The condition expression handle */
|
expression_node_p m_cond; /**< The condition expression handle */
|
||||||
statement_node_r m_then; /**< The then statement handle */
|
statement_node_p m_then; /**< The then statement handle */
|
||||||
std::optional<statement_node_r> m_else; /**< The else statement handle */
|
std::optional<statement_node_p> m_else; /**< The else statement handle */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -165,7 +165,7 @@ public:
|
|||||||
* @param location Node location.
|
* @param location Node location.
|
||||||
* @param body Body handle.
|
* @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)) {}
|
: abstract_node(location), m_body(std::move(body)) {}
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@@ -173,7 +173,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @return The body handle.
|
* @return The body handle.
|
||||||
*/
|
*/
|
||||||
const body_r& body() const { return m_body; }
|
const body& body() const { return m_body; }
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Returns this node's statement type.
|
* @brief Returns this node's statement type.
|
||||||
@@ -188,7 +188,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
private:
|
private:
|
||||||
body_r m_body; /**< The body handle. */
|
struct body m_body; /**< The body handle. */
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ private:
|
|||||||
|
|
||||||
ast::expression_node_r parse_expression_primary();
|
ast::expression_node_r parse_expression_primary();
|
||||||
ast::expression_node_r parse_expression_unary(std::uint32_t precedence);
|
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();
|
ast::body_r parse_body();
|
||||||
private:
|
private:
|
||||||
|
|||||||
+6
-14
@@ -21,8 +21,7 @@ void var_read_expression_node::accept(visitor& visitor) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& var_read_expression_node::print(std::ostream& os) 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 {
|
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 {
|
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) {
|
switch (m_type) {
|
||||||
case unaryop_expression_node_t::Positive:
|
case unaryop_expression_node_t::Positive:
|
||||||
case unaryop_expression_node_t::Negative:
|
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 {
|
std::ostream& function_definition_node::print(std::ostream& os) const {
|
||||||
function_declaration_node::print(os);
|
function_declaration_node::print(os);
|
||||||
os << ":\n";
|
os << ":\n";
|
||||||
if (m_body.has_value()) {
|
for (const auto& entry : m_body.statements)
|
||||||
for (const auto& entry : m_body->statements)
|
os << entry << '\n';
|
||||||
os << entry << '\n';
|
return os << m_body.end << ": " << p_name << " end";
|
||||||
return os << m_body->end << ": " << p_name << " end";
|
|
||||||
}
|
|
||||||
return os << m_body.error();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool function_definition_node::equal(const node& rhs) const {
|
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 {
|
void program_node::accept(visitor& visitor) const {
|
||||||
for (const auto& decl : m_declarations) {
|
for (const auto& decl : m_declarations) {
|
||||||
if (decl.has_error()) {
|
decl->accept(visitor);
|
||||||
visitor.visit_error(decl.error());
|
|
||||||
} else {
|
|
||||||
decl.value()->accept(visitor);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace furc::front {
|
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()));
|
m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()));
|
||||||
|
|
||||||
push_block();
|
push_block();
|
||||||
if (funcDef.body().has_error()) {
|
for (const auto& stmt : funcDef.body().statements) {
|
||||||
std::cerr << funcDef.body().error() << '\n';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (const auto& stmt : funcDef.body()->statements) {
|
|
||||||
stmt.value()->accept(*this);
|
stmt.value()->accept(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_currentBlock->emplace<ir::return_instruction>();
|
m_currentBlock->emplace<ir::return_instruction>();
|
||||||
|
|
||||||
m_module.push(std::move(m_currentFunction));
|
m_module.push(std::move(m_currentFunction));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ir_generator::visit(const ast::return_statement_node& returnStmt) {
|
void ir_generator::visit(const ast::return_statement_node& returnStmt) {
|
||||||
if (!returnStmt.value().has_value()) return;
|
if (returnStmt.value().has_value()) {
|
||||||
auto value = returnStmt.value().value();
|
returnStmt.value().value()->accept(*this);
|
||||||
if (value.has_error()) {
|
|
||||||
std::cerr << value.error() << '\n';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.has_value()) {
|
|
||||||
value.value()->accept(*this);
|
|
||||||
push<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
|
push<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
|
||||||
} else {
|
} else {
|
||||||
push<ir::return_instruction>();
|
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) {
|
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;
|
ir_register cond = m_registerCounter - 1;
|
||||||
push<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
|
push<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
|
||||||
m_currentFunction->blocks().size(),
|
m_currentFunction->blocks().size(),
|
||||||
m_currentFunction->blocks().size() + 1);
|
m_currentFunction->blocks().size() + 1);
|
||||||
|
|
||||||
push_block(); // then block
|
push_block(); // then block
|
||||||
node.then().value()->accept(*this);
|
node.then()->accept(*this);
|
||||||
if (node.elze().has_value()) {
|
if (node.elze().has_value()) {
|
||||||
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
|
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
|
||||||
|
|
||||||
push_block(); // else block
|
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());
|
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) {
|
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);
|
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) {
|
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),
|
push<furlang::ir::assign_instruction>(ir::operand::new_reg(it->second),
|
||||||
ir::operand::new_reg(m_registerCounter++));
|
ir::operand::new_reg(m_registerCounter++));
|
||||||
} else {
|
} 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) {
|
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;
|
ir_register lhs = m_registerCounter - 1;
|
||||||
node.rhs().value()->accept(*this);
|
node.rhs()->accept(*this);
|
||||||
ir_register rhs = m_registerCounter - 1;
|
ir_register rhs = m_registerCounter - 1;
|
||||||
ir_register dst = m_registerCounter++;
|
ir_register dst = m_registerCounter++;
|
||||||
push<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
|
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) {
|
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;
|
ir_register rhs = m_registerCounter - 1;
|
||||||
assert(node.lhs().value()->expression_type() == ast::expression_node_t::VarRead);
|
assert(node.lhs()->expression_type() == ast::expression_node_t::VarRead);
|
||||||
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs().value());
|
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs());
|
||||||
|
|
||||||
ir_register reg = 0;
|
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;
|
reg = it->second;
|
||||||
} else {
|
} else {
|
||||||
m_variables[*lhs->get_name()] = reg = m_registerCounter++;
|
m_variables[lhs->get_name()] = reg = m_registerCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto compound = node.compound();
|
auto compound = node.compound();
|
||||||
|
|||||||
+56
-21
@@ -34,10 +34,15 @@ ast::program_node_r parser::parse() & {
|
|||||||
auto program = m_arena.allocate_shared<ast::program_node>(location{ m_filename });
|
auto program = m_arena.allocate_shared<ast::program_node>(location{ m_filename });
|
||||||
|
|
||||||
while (peek_token().has_value()) {
|
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() {
|
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 });
|
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,
|
return m_arena.allocate_shared<ast::function_definition_node>(first->location,
|
||||||
name->value.string,
|
name->value.string,
|
||||||
std::move(body));
|
std::move(body.value()));
|
||||||
}
|
}
|
||||||
case token_t::Semicolon: {
|
case token_t::Semicolon: {
|
||||||
m_peekBuffer.clear();
|
m_peekBuffer.clear();
|
||||||
@@ -109,7 +114,7 @@ ast::statement_node_r parser::parse_statement() {
|
|||||||
auto value = parse_expression();
|
auto value = parse_expression();
|
||||||
auto err = eat_token(token_t::Semicolon);
|
auto err = eat_token(token_t::Semicolon);
|
||||||
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
|
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: {
|
case keyword_token::If: {
|
||||||
auto tok = next_token();
|
auto tok = next_token();
|
||||||
@@ -128,20 +133,28 @@ ast::statement_node_r parser::parse_statement() {
|
|||||||
peek_token()->value.keyword == keyword_token::Else) {
|
peek_token()->value.keyword == keyword_token::Else) {
|
||||||
next_token();
|
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,
|
return m_arena.allocate_shared<ast::if_statement_node>(location,
|
||||||
std::move(cond),
|
std::move(cond.value()),
|
||||||
std::move(then),
|
std::move(then.value()),
|
||||||
std::move(parse_statement()));
|
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::None:
|
||||||
case keyword_token::Func:
|
case keyword_token::Func:
|
||||||
default: break;
|
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;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +172,11 @@ ast::statement_node_r parser::parse_statement() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ast::expression_node_r parser::parse_expression(std::uint32_t precedence) {
|
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() {
|
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;
|
if (current.precedence >= precedence) break;
|
||||||
auto token = next_token();
|
auto token = next_token();
|
||||||
|
|
||||||
ast::expression_node_r expression;
|
ast::expression_node_p expression;
|
||||||
|
|
||||||
auto nextIt = s_prefixes.find(peek_token()->type);
|
auto nextIt = s_prefixes.find(peek_token()->type);
|
||||||
if (nextIt != s_prefixes.end()) {
|
if (nextIt != s_prefixes.end()) {
|
||||||
auto next = nextIt->second;
|
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 =
|
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 == 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;
|
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 = {
|
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::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) },
|
{ 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) },
|
{ 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()) {
|
while (peek_token().has_value()) {
|
||||||
auto it = s_rhsops.find(peek_token()->type);
|
auto it = s_rhsops.find(peek_token()->type);
|
||||||
if (it == s_rhsops.end()) return lhs;
|
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;
|
if (current.precedence >= precedence) return lhs;
|
||||||
auto opToken = next_token();
|
auto opToken = next_token();
|
||||||
|
|
||||||
ast::expression_node_r rhs;
|
ast::expression_node_p rhs;
|
||||||
if (current.type != rhsop_info_t::Unaryop) {
|
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);
|
auto nextIt = s_rhsops.find(peek_token()->type);
|
||||||
if (nextIt != s_rhsops.end()) {
|
if (nextIt != s_rhsops.end()) {
|
||||||
rhsop_info next = nextIt->second;
|
rhsop_info next = nextIt->second;
|
||||||
|
|
||||||
|
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) {
|
if (current.type != rhsop_info_t::Unaryop) {
|
||||||
rhs = std::move(parse_expression_rhs(std::move(rhs),
|
rhs = std::move(expr.value());
|
||||||
current.precedence + static_cast<std::uint32_t>(current.associativity == associativity::Right)));
|
|
||||||
} else {
|
} else {
|
||||||
lhs = std::move(parse_expression_rhs(std::move(lhs), current.precedence));
|
lhs = std::move(expr.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user