Add doxygen documentation #4

Merged
CHatingPython merged 10 commits from docs into master 2026-06-02 12:01:39 +00:00
10 changed files with 819 additions and 95 deletions
Showing only changes of commit dd6344aab6 - Show all commits
+69 -8
View File
@@ -8,30 +8,69 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Declaration node type.
*/
enum class declaration_node_t { enum class declaration_node_t {
FunctionDeclaration, Func, /**< Function declaration. */
FunctionDefinition, FuncDef, /**< Function definition. */
}; };
/**
* @brief Declaration AST node interface.
*/
class declaration_node : public statement_node { class declaration_node : public statement_node {
public: public:
/**
* @brief Returns this node's category.
*
* @return node_t::Declaration.
*/
node_t category() const override { return node_t::Declaration; } node_t category() const override { return node_t::Declaration; }
statement_node_t statement_type() const override { return statement_node_t::Declaration; } /**
* @brief Returns this node's statement type.
*
* @return statement_node_t::Declaration.
*/
statement_node_t statement_type() const final { return statement_node_t::Declaration; }
/**
* @brief Returns this node's declaration type.
*
* @return The declaration type.
*/
virtual declaration_node_t declaration_type() const = 0; virtual declaration_node_t declaration_type() const = 0;
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
}; };
/**
* @brief Function declaration AST node.
*/
class function_declaration_node : public declaration_node { class function_declaration_node : public declaration_node {
public: public:
/**
* @brief Construct a new function declaration node object from name token.
*
* @param name Name of the function.
*/
function_declaration_node(front::token name) function_declaration_node(front::token name)
: m_name(name) {} : p_name(name) {}
public: public:
declaration_node_t declaration_type() const override { return declaration_node_t::FunctionDeclaration; } /**
* @brief Returns this node's declaration type.
*
* @return declaration_node_t::FunctionDeclaration.
*/
declaration_node_t declaration_type() const override { return declaration_node_t::Func; }
front::token name() const { return m_name; } /**
* @brief Returns function's name.
*
* @return Name of the function.
*/
front::token name() const { return p_name; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -39,16 +78,38 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
protected: protected:
front::token m_name; /**
* @brief Name of the function.
*/
front::token p_name;
}; };
/**
* @brief Function definition AST node.
*/
class function_definition_node final : public function_declaration_node { class function_definition_node final : public function_declaration_node {
public: public:
/**
* @brief Construct a new function definition node object from name and body.
*
* @param name Name of the function.
* @param body Body of the function.
*/
function_definition_node(front::token name, body_h&& body) function_definition_node(front::token name, body_h&& body)
: function_declaration_node(name), m_body(std::move(body)) {} : function_declaration_node(name), m_body(std::move(body)) {}
public: public:
declaration_node_t declaration_type() const override { return declaration_node_t::FunctionDefinition; } /**
* @brief Returns this node's declaration type.
*
* @return declaration_node_t::FunctionDefinition.
*/
declaration_node_t declaration_type() const override { return declaration_node_t::FuncDef; }
/**
* @brief Returns function's body.
*
* @return Body of the function.
*/
const body_h& body() const { return m_body; } const body_h& body() const { return m_body; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
+206 -25
View File
@@ -7,34 +7,78 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Expression node type.
*/
enum class expression_node_t { enum class expression_node_t {
Literal, Literal, /**< Literal */
VarRead, VarRead, /**< Variable read expression */
VarAssign, Unaryop, /**< Unary operation expression */
Unaryop, Binop, /**< Binary operation expression */
Binop, VarAssign, /**< Variable assignment expression */
Paren,
}; };
/**
* @brief Expression AST node.
*/
class expression_node : public statement_node { class expression_node : public statement_node {
public: public:
/**
* @brief Returns this node's category.
*
* @return node_t::Expression.
*/
node_t category() const override { return node_t::Expression; } node_t category() const override { return node_t::Expression; }
/**
* @brief Returns this node's statement type.
*
* @return statement_node_t::Expression.
*/
statement_node_t statement_type() const override { return statement_node_t::Expression; } statement_node_t statement_type() const override { return statement_node_t::Expression; }
/**
* @brief Returns this node's expression type.
*
* @return The expression type.
*/
virtual expression_node_t expression_type() const = 0; virtual expression_node_t expression_type() const = 0;
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
}; };
/**
* @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: public:
/**
* @brief Construct a new var read expression node object from a name handle.
*
* @param name Handle to the name.
*/
var_read_expression_node(handle<std::string_view>&& name) var_read_expression_node(handle<std::string_view>&& name)
: m_name(std::move(name)) {} : m_name(std::move(name)) {}
/**
* @brief Returns the variable's name.
*
* @return Name of the variable.
*/
const handle<std::string_view>& get_name() const { return m_name; } const handle<std::string_view>& get_name() const { return m_name; }
/**
* @brief Returns the variable's name.
*
* @return Name of the variable.
*/
handle<std::string_view>&& move_name() { return std::move(m_name); } handle<std::string_view>&& move_name() { return std::move(m_name); }
public: public:
/**
* @brief Returns this node's expression type.
*
* @return expression_node_t::VarRead.
*/
expression_node_t expression_type() const override { return expression_node_t::VarRead; } expression_node_t expression_type() const override { return expression_node_t::VarRead; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -46,27 +90,72 @@ private:
handle<std::string_view> m_name; handle<std::string_view> m_name;
}; };
/**
* @brief Unary operation node type.
*/
enum class unaryop_expression_node_t { enum class unaryop_expression_node_t {
Positive, Positive, /**< Positive (unary plus) */
Negative, Negative, /**< Negative (unary minus) */
PrefixIncrement, PrefixIncrement, /**< Prefix increment */
PostfixIncrement, PostfixIncrement, /**< Postfix increment */
PrefixDecrement, PrefixDecrement, /**< Prefix decrement */
PostfixDecrement, PostfixDecrement, /**< Postfix decrement */
}; };
/**
* @brief Unary operation expression AST node.
*/
class unaryop_expression_node final : public expression_node { class unaryop_expression_node final : public expression_node {
public: public:
/**
* @brief Construct a new unaryop expression node object from type and expression node handle.
*
* @param type Operation type.
* @param node Handle to the inner expression node.
*/
unaryop_expression_node(unaryop_expression_node_t type, expression_node_h&& node) unaryop_expression_node(unaryop_expression_node_t type, expression_node_h&& node)
: m_type(type), m_node(std::move(node)) {} : m_type(type), m_node(std::move(node)) {}
/**
* @brief Sets this node's inner expression.
*
* @param node New node handle.
*/
void set_node(expression_node_h&& node) { m_node = std::move(node); } void set_node(expression_node_h&& node) { m_node = std::move(node); }
/**
* @brief Returns the type of this node's operation.
*
* @return The operation type.
*/
unaryop_expression_node_t type() const { return m_type; } unaryop_expression_node_t type() const { return m_type; }
/**
* @brief Returns this node's inner expression.
*
* @return The inner expression.
*/
const expression_node_h& get_node() const { return m_node; } const expression_node_h& get_node() const { return m_node; }
/**
* @brief Returns this node's inner expression.
*
* @return The inner expression.
*/
expression_node_h& get_node() { return m_node; } expression_node_h& get_node() { return m_node; }
/**
* @brief Moves this node's inner expression.
*
* @return The moved inner expression.
*/
expression_node_h&& move_node() { return std::move(m_node); } expression_node_h&& move_node() { return std::move(m_node); }
public: public:
/**
* @brief Returns this node's expression type.
*
* @return expression_node_t::Unaryop.
*/
expression_node_t expression_type() const override { return expression_node_t::Unaryop; } expression_node_t expression_type() const override { return expression_node_t::Unaryop; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -76,36 +165,90 @@ 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;
expression_node_h m_node; expression_node_h m_node; /**< The inner expression. */
}; };
/**
* @brief Binary operation expression node type.
*/
enum class binop_expression_node_t { enum class binop_expression_node_t {
None = 0, None = 0, /**< None */
Add, Add, /**< Addition */
Sub, Sub, /**< Subtraction */
Mul, Mul, /**< Multiplication */
Div, Div, /**< Division */
Mod, Mod, /**< Modulo */
Equal, Equal, /**< Equality */
NotEqual, NotEqual, /**< Inequality */
LessThan, LessThan, /**< Less */
GreaterThan, GreaterThan, /**< Greater */
LessEqual, LessEqual, /**< Less or equal */
GreaterEqual, GreaterEqual, /**< Greater or equal */
}; };
/**
* @brief Binary operation expression AST node.
*/
class binop_expression_node final : public expression_node { class binop_expression_node final : public expression_node {
public: public:
/**
* @brief Construct a new binary operation expression AST node.
*
* @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_h&& lhs, expression_node_h&& rhs) binop_expression_node(binop_expression_node_t type, expression_node_h&& lhs, expression_node_h&& rhs)
: m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {} : m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
/**
* @brief Returns this node's binary operation type.
*
* @return The binary operation type.
*/
binop_expression_node_t type() const { return m_type; }; binop_expression_node_t type() const { return m_type; };
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
const expression_node_h& lhs() const { return m_lhs; }; const expression_node_h& lhs() const { return m_lhs; };
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
expression_node_h& lhs() { return m_lhs; }; expression_node_h& lhs() { return m_lhs; };
/**
* @brief Moves this node's left-hand-side expression.
*
* @return The moved left-hand-side expression.
*/
expression_node_h&& move_lhs() { return std::move(m_lhs); }; expression_node_h&& 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_h& rhs() const { return m_rhs; }; const expression_node_h& rhs() const { return m_rhs; };
/**
* @brief Returns this node's right-hand-side expression.
*
* @return The right-hand-side expression.
*/
expression_node_h& rhs() { return m_rhs; }; expression_node_h& rhs() { return m_rhs; };
/**
* @brief Moves this node's right-hand-side expression.
*
* @return The moved right-hand-side expression.
*/
expression_node_h&& move_rhs() { return std::move(m_rhs); }; expression_node_h&& 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; }
@@ -121,18 +264,56 @@ private:
expression_node_h m_rhs; expression_node_h m_rhs;
}; };
/**
* @brief Variable assignment expression AST node.
*/
class var_assign_expression_node final : public expression_node { class var_assign_expression_node final : public expression_node {
public: public:
/**
* @brief Construct a new variable assignment expression AST node.
*
* @param lhs Left-hand-side expression handle.
* @param rhs Right-hand-side expression handle.
*/
var_assign_expression_node(expression_node_h&& lhs, expression_node_h&& rhs) var_assign_expression_node(expression_node_h&& lhs, expression_node_h&& rhs)
: m_compound(binop_expression_node_t::None), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {} : 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 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_h&& lhs, expression_node_h&& rhs) var_assign_expression_node(binop_expression_node_t compound, expression_node_h&& lhs, expression_node_h&& rhs)
: m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {} : m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
/**
* @brief Returns this node's compound operation type.
*
* @return The compound operation type.
*/
binop_expression_node_t compound() const { return m_compound; } binop_expression_node_t compound() const { return m_compound; }
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
const expression_node_h& lhs() const { return m_lhs; } const expression_node_h& lhs() const { return m_lhs; }
/**
* @brief Returns this node's right-hand-side expression.
*
* @return The right-hand-side expression.
*/
const expression_node_h& rhs() const { return m_rhs; } const expression_node_h& rhs() const { return m_rhs; }
public: public:
/**
* @brief Returns this node's expression type.
*
* @return expression_node_t::VarAssign.
*/
expression_node_t expression_type() const override { return expression_node_t::VarAssign; } expression_node_t expression_type() const override { return expression_node_t::VarAssign; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
+140 -2
View File
@@ -7,63 +7,201 @@
#include <vector> #include <vector>
namespace furc { namespace furc {
/**
* @brief Abstract Syntax Tree definitions.
*/
namespace ast { namespace ast {
class node; class node;
/**
* @brief Alias for handle to node.
*
* @tparam T AST node type.
*/
template <typename T> template <typename T>
using node_handle = handle<T*, std::string>; using node_handle = handle<T*, std::string>;
class literal_node; class literal_node;
/**
* @brief Alias for handle to literal_node.
* @see literal_node
*/
using literal_node_h = node_handle<literal_node>; using literal_node_h = node_handle<literal_node>;
class expression_node; class expression_node;
/**
* @brief Alias for handle to expression_node.
* @see expression_node
*/
using expression_node_h = node_handle<expression_node>; using expression_node_h = node_handle<expression_node>;
class declaration_node; class declaration_node;
/**
* @brief Alias for handle to declaration_node.
* @see declaration_node
*/
using declaration_node_h = node_handle<declaration_node>; using declaration_node_h = node_handle<declaration_node>;
class statement_node; class statement_node;
/**
* @brief Alias for handle to statement_node.
* @see statement_node
*/
using statement_node_h = node_handle<statement_node>; using statement_node_h = node_handle<statement_node>;
class program_node; class program_node;
/**
* @brief Alias for handle to program_node.
* @see program_node
*/
using program_node_h = node_handle<program_node>; using program_node_h = node_handle<program_node>;
class string_literal_node; class string_literal_node;
/**
* @brief Alias for handle to string_literal_node.
* @see string_literal_node
*/
using string_literal_node_h = node_handle<string_literal_node>; using string_literal_node_h = node_handle<string_literal_node>;
class integer_literal_node; class integer_literal_node;
/**
* @brief Alias for handle to integer_literal_node.
* @see integer_literal_node
*/
using integer_literal_node_h = node_handle<integer_literal_node>; using integer_literal_node_h = node_handle<integer_literal_node>;
class var_read_expression_node; class var_read_expression_node;
/**
* @brief Alias for handle to var_read_expression_node.
* @see var_read_expression_node
*/
using var_read_expression_node_h = node_handle<var_read_expression_node>; using var_read_expression_node_h = node_handle<var_read_expression_node>;
class unaryop_expression_node; class unaryop_expression_node;
/**
* @brief Alias for handle to unaryop_expression_node.
* @see unaryop_expression_node
*/
using unaryop_expression_node_h = node_handle<unaryop_expression_node>; using unaryop_expression_node_h = node_handle<unaryop_expression_node>;
class binop_expression_node; class binop_expression_node;
/**
* @brief Alias for handle to binop_expression_node.
* @see binop_expression_node
*/
using binop_expression_node_h = node_handle<binop_expression_node>; using binop_expression_node_h = node_handle<binop_expression_node>;
class var_assign_expression_node; class var_assign_expression_node;
/**
* @brief Alias for handle to var_assign_expression_node.
* @see var_assign_expression_node
*/
using var_assign_expression_node_h = node_handle<var_assign_expression_node>; using var_assign_expression_node_h = node_handle<var_assign_expression_node>;
/**
* @brief List of statements.
*/
struct body { struct body {
location begin, end; /**
* @brief Location of the opening curly.
*/
location begin;
/**
* @brief Location of the closing curly.
*/
location end;
/**
* @brief List of statements.
*/
std::vector<statement_node_h> statements; std::vector<statement_node_h> statements;
/**
* @brief Compares two bodies for equality.
*
* @param rhs Body to compare against.
* @return true if the bodies are equal.
*/
bool operator==(const body& rhs) const { bool operator==(const body& rhs) const {
return begin == rhs.begin && end == rhs.end && statements == rhs.statements; return begin == rhs.begin && end == rhs.end && statements == rhs.statements;
} }
/**
* @brief Compares two bodies for inequality.
*
* @param rhs Body to compare against.
* @return true if the bodies are not equal.
*/
bool operator!=(const body& rhs) const { return !this->operator==(rhs); } bool operator!=(const body& rhs) const { return !this->operator==(rhs); }
friend std::ostream& operator<<(std::ostream&, const body&); /**
* @brief Prints a body to an output stream.
*
* @param os Output stream.
* @param body Body to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const body& body);
}; };
/**
* @brief Alias for handle to body.
* @see body
*/
using body_h = handle<body>; using body_h = handle<body>;
class function_declaration_node; class function_declaration_node;
/**
* @brief Alias for handle to function_declaration_node.
* @see function_declaration_node
*/
using function_declaration_node_h = node_handle<function_declaration_node>; using function_declaration_node_h = node_handle<function_declaration_node>;
class function_definition_node; class function_definition_node;
/**
* @brief Alias for handle to function_definition_node.
* @see function_definition_node
*/
using function_definition_node_h = node_handle<function_definition_node>; using function_definition_node_h = node_handle<function_definition_node>;
class return_statement_node; class return_statement_node;
/**
* @brief Alias for handle to return_statement_node.
* @see return_statement_node
*/
using return_statement_node_h = node_handle<return_statement_node>; using return_statement_node_h = node_handle<return_statement_node>;
class if_statement_node; class if_statement_node;
/**
* @brief Alias for handle to if_statement_node.
* @see if_statement_node
*/
using if_statement_node_h = node_handle<if_statement_node>; using if_statement_node_h = node_handle<if_statement_node>;
class compound_statement_node; class compound_statement_node;
/**
* @brief Alias for handle to compound_statement_node.
* @see compound_statement_node
*/
using compound_statement_node_h = node_handle<compound_statement_node>; using compound_statement_node_h = node_handle<compound_statement_node>;
} // namespace ast } // namespace ast
+65 -2
View File
@@ -8,29 +8,68 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Literal node type.
*/
enum class literal_node_t { enum class literal_node_t {
String, String, /**< String literal. */
Integer Integer, /**< Integer literal. */
}; };
/**
* @brief Literal AST node.
*/
class literal_node : public expression_node { class literal_node : public expression_node {
public: public:
/**
* @brief Returns this node's category.
*
* @return node_t::Literal.
*/
node_t category() const override { return node_t::Literal; } node_t category() const override { return node_t::Literal; }
/**
* @brief Returns this node's expression type.
*
* @return expression_node_t::Literal.
*/
expression_node_t expression_type() const override { return expression_node_t::Literal; } expression_node_t expression_type() const override { return expression_node_t::Literal; }
/**
* @brief Returns this node's literal type.
*
* @return The literal type.
*/
virtual literal_node_t literal_type() const = 0; virtual literal_node_t literal_type() const = 0;
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
}; };
/**
* @brief String literal AST node.
*/
class string_literal_node final : public literal_node { class string_literal_node final : public literal_node {
public: public:
/**
* @brief Construct a new string literal node object from a handle.
*
* @param value A handle to value.
*/
string_literal_node(handle<std::string_view>&& value) string_literal_node(handle<std::string_view>&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
public: public:
/**
* @brief Returns this node's literal type.
*
* @return literal_node_t::String.
*/
literal_node_t literal_type() const override { return literal_node_t::String; } literal_node_t literal_type() const override { return literal_node_t::String; }
/**
* @brief Returns this node's value.
*
* @return A handle to the value.
*/
const handle<std::string_view>& value() const { return m_value; } const handle<std::string_view>& value() const { return m_value; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -42,15 +81,39 @@ private:
handle<std::string_view> m_value; handle<std::string_view> m_value;
}; };
/**
* @brief Integer literal AST node.
*/
class integer_literal_node final : public literal_node { class integer_literal_node final : public literal_node {
public: public:
/**
* @brief Construct a new integer literal node object from a handle.
*
* @param value A handle to the value.
*/
integer_literal_node(handle<front::integer_token>&& value) integer_literal_node(handle<front::integer_token>&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
public: public:
/**
* @brief Returns this node's literal type.
*
* @return literal_node_t::Integer.
*/
literal_node_t literal_type() const override { return literal_node_t::Integer; } literal_node_t literal_type() const override { return literal_node_t::Integer; }
/**
* @brief Returns this node's value.
*
* @return A handle to the value.
*/
const handle<front::integer_token>& value() const { return m_value; } const handle<front::integer_token>& value() const { return m_value; }
/**
* @brief Compares this node with an integer token for equality.
*
* @param integer Integer token to compare against.
* @return true if the integer token is equal to this node.
*/
bool operator==(front::integer_token integer) const { return m_value == integer; } bool operator==(front::integer_token integer) const { return m_value == integer; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
+86 -7
View File
@@ -7,14 +7,24 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Node category.
*/
enum class node_t { enum class node_t {
Literal, Literal, /**< Literal. */
Expression, Expression, /**< Expression. */
Statement, Statement, /**< Statement. */
Declaration, Declaration, /**< Declaration. */
Program, Program, /**< Program. */
}; };
/**
* @brief Prints a node type (category) to an output stream.
*
* @param os Output stream.
* @param type Type to print.
* @return The output stream.
*/
static inline std::ostream& operator<<(std::ostream& os, node_t type) { static inline std::ostream& operator<<(std::ostream& os, node_t type) {
switch (type) { switch (type) {
case node_t::Literal: return os << "literal"; case node_t::Literal: return os << "literal";
@@ -23,29 +33,98 @@ static inline std::ostream& operator<<(std::ostream& os, node_t type) {
case node_t::Declaration: return os << "declaration"; case node_t::Declaration: return os << "declaration";
case node_t::Program: return os << "program"; case node_t::Program: return os << "program";
} }
return os;
} }
/**
* @brief AST node interface.
*/
class node { class node {
public: public:
node() = default; node() = default;
virtual ~node() = default; virtual ~node() = default;
node(node&&) = default; /**
* @brief Move constructor.
*
* Constructs a node by transferring the state of another node.
*
* @param other Node to move from.
*/
node(node&& other) = default;
node(const node&) = delete; node(const node&) = delete;
node& operator=(node&&) = default;
/**
* @brief Move constructor.
*
* Constructs a node by transferring the state of another node.
*
* @param other Node to move from.
*/
node& operator=(node&& other) = default;
node& operator=(const node&) = delete; node& operator=(const node&) = delete;
public: public:
/**
* @brief Returns the category of this AST node.
* @see node_t
*
* @return The node category.
*/
virtual node_t category() const = 0; virtual node_t category() const = 0;
public: public:
/**
* @brief Compares two nodes for equality.
*
* Nodes are equal if they have the same category and
* their derived-class-specific contents are equal.
*
* @param rhs Node to compare against.
* @return true if the nodes are equal.
*/
bool operator==(const node& rhs) const { return category() == rhs.category() && equal(rhs); } bool operator==(const node& rhs) const { return category() == rhs.category() && equal(rhs); }
/**
* @brief Compares two nodes for inequality.
*
* @param rhs Node to compare against.
* @return true if the nodes are not equal.
*/
bool operator!=(const node& rhs) const { return !this->operator==(rhs); } bool operator!=(const node& rhs) const { return !this->operator==(rhs); }
public: public:
/**
* @brief Accepts a visitor.
*
* Dispatches to the visitor overload corresponding to the concrete node type.
*
* @param visitor Visitor instance.
*/
virtual void accept(visitor& visitor) const = 0; virtual void accept(visitor& visitor) const = 0;
/**
* @brief Prints a node to an output stream.
*
* @param os Output stream.
* @return The output stream.
*/
virtual std::ostream& print(std::ostream& os) const = 0; virtual std::ostream& print(std::ostream& os) const = 0;
/**
* @brief Prints a node to an output stream.
*
* Equivalent to calling node.print(os).
*
* @param os Output stream.
* @param node Node to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const node& node) { return node.print(os); } friend std::ostream& operator<<(std::ostream& os, const node& node) { return node.print(os); }
protected: protected:
/**
* @brief Compares two nodes for equality.
*
* @param rhs Node to compare against.
* @return true if nodes are equal.
*/
virtual bool equal(const node& rhs) const = 0; virtual bool equal(const node& rhs) const = 0;
}; };
+13
View File
@@ -9,14 +9,27 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Program AST node.
*/
class program_node final : public node { class program_node final : public node {
public: public:
program_node() = default; program_node() = default;
node_t category() const override { return node_t::Program; } node_t category() const override { return node_t::Program; }
public: public:
/**
* @brief Adds a declaration to this program.
*
* @param declaration Declaration to add.
*/
void push(node_handle<declaration_node>&& declaration) { m_declarations.push_back(std::move(declaration)); } void push(node_handle<declaration_node>&& 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_handle<declaration_node>>& declarations() const { return m_declarations; } const std::vector<node_handle<declaration_node>>& declarations() const { return m_declarations; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
+100 -10
View File
@@ -6,32 +6,66 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Statement node type.
*/
enum class statement_node_t { enum class statement_node_t {
Expression, Expression, /**< Expression */
Declaration, Declaration, /**< Declaration */
Return, Return, /**< Return statement */
If, If, /**< If statement */
Compound, Compound, /**< Compound statement */
}; };
/**
* @brief Statement AST node.
*/
class statement_node : public node { class statement_node : public node {
public: public:
/**
* @brief Returns this node's category.
*
* @return node_t::Statement.
*/
node_t category() const override { return node_t::Statement; } node_t category() const override { return node_t::Statement; }
/**
* @brief Returns this node's statement type.
*
* @return The statement type.
*/
virtual statement_node_t statement_type() const = 0; virtual statement_node_t statement_type() const = 0;
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
}; };
/**
* @brief Return statement AST node.
*/
class return_statement_node final : public statement_node { class return_statement_node final : public statement_node {
public: public:
return_statement_node() = default; return_statement_node() = default;
/**
* @brief Construct a new return statement AST node.
*
* @param value Return value handle.
*/
return_statement_node(expression_node_h&& value) return_statement_node(expression_node_h&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
public: public:
/**
* @brief Returns this node's return value handle.
*
* @return The return value handle.
*/
expression_node_h value() const { return m_value; } expression_node_h value() const { return m_value; }
public: public:
/**
* @brief Returns this node's statement type.
*
* @return statement_node_t::Return.
*/
statement_node_t statement_type() const override { return statement_node_t::Return; } statement_node_t statement_type() const override { return statement_node_t::Return; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -40,21 +74,59 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
expression_node_h m_value; expression_node_h m_value; /**< Return value handle. */
}; };
/**
* @brief If statement AST node.
*/
class if_statement_node final : public statement_node { class if_statement_node final : public statement_node {
public: public:
/**
* @brief Construct a new if statement AST node.
*
* @param cond Condition expression handle.
* @param then Then statement handle.
*/
if_statement_node(expression_node_h&& cond, statement_node_h&& then) if_statement_node(expression_node_h&& cond, statement_node_h&& then)
: m_cond(std::move(cond)), m_then(std::move(then)) {} : m_cond(std::move(cond)), m_then(std::move(then)) {}
/**
* @brief Construct a new if statement AST node.
*
* @param cond Condition expression handle.
* @param then Then statement handle.
* @param elze Else statement handle.
*/
if_statement_node(expression_node_h&& cond, statement_node_h&& then, statement_node_h&& elze) if_statement_node(expression_node_h&& cond, statement_node_h&& then, statement_node_h&& elze)
: m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {} : m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
public: public:
/**
* @brief Returns this node's condition expression handle.
*
* @return The condition expression handle.
*/
expression_node_h cond() const { return m_cond; } expression_node_h cond() const { return m_cond; }
/**
* @brief Returns this node's then statement handle.
*
* @return The then statement handle.
*/
const statement_node_h& then() const { return m_then; } const statement_node_h& then() const { return m_then; }
/**
* @brief Returns this node's else statement handle.
*
* @return The else statement handle.
*/
const statement_node_h& elze() const { return m_else; } const statement_node_h& elze() const { return m_else; }
public: public:
/**
* @brief Returns this node's statement type.
*
* @return statement_node_t::If.
*/
statement_node_t statement_type() const override { return statement_node_t::If; } statement_node_t statement_type() const override { return statement_node_t::If; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -63,18 +135,36 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
expression_node_h m_cond; expression_node_h m_cond; /**< The condition expression handle */
statement_node_h m_then; statement_node_h m_then; /**< The then statement handle */
statement_node_h m_else; statement_node_h m_else; /**< The else statement handle */
}; };
/**
* @brief Compound statement AST node.
*/
class compound_statement_node final : public statement_node { class compound_statement_node final : public statement_node {
public: public:
/**
* @brief Construct a new compound statement AST node.
*
* @param body Body handle.
*/
compound_statement_node(body_h&& body) compound_statement_node(body_h&& body)
: m_body(std::move(body)) {} : m_body(std::move(body)) {}
public: public:
/**
* @brief Returns this node's body handle.
*
* @return The body handle.
*/
const body_h& body() const { return m_body; } const body_h& body() const { return m_body; }
public: public:
/**
* @brief Returns this node's statement type.
*
* @return statement_node_t::Compound.
*/
statement_node_t statement_type() const override { return statement_node_t::Compound; } statement_node_t statement_type() const override { return statement_node_t::Compound; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -83,7 +173,7 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
body_h m_body; body_h m_body; /**< The body handle. */
}; };
} // namespace ast } // namespace ast
+110 -11
View File
@@ -6,28 +6,127 @@
namespace furc { namespace furc {
namespace ast { namespace ast {
/**
* @brief Visitor pattern class for AST nodes.
*/
class visitor { class visitor {
public: public:
visitor() = default; visitor() = default;
virtual ~visitor() = default; virtual ~visitor() = default;
/**
* @brief Move constructor.
*/
visitor(visitor&&) = default; visitor(visitor&&) = default;
/**
* @brief Move constructor.
*/
visitor& operator=(visitor&&) = default; visitor& operator=(visitor&&) = default;
/**
* @brief Copy constructor.
*/
visitor(const visitor&) = default; visitor(const visitor&) = default;
/**
* @brief Copy constructor.
*/
visitor& operator=(const visitor&) = default; visitor& operator=(const visitor&) = default;
public: public:
virtual void visit(const string_literal_node&) {} /**
virtual void visit(const integer_literal_node&) {} * @brief Visit a string_literal_node.
virtual void visit(const var_read_expression_node&) {} * @see string_literal_node
virtual void visit(const unaryop_expression_node&) {} *
virtual void visit(const binop_expression_node&) {} * @param node Node.
virtual void visit(const var_assign_expression_node&) {} */
virtual void visit(const function_declaration_node&) {} virtual void visit(const string_literal_node& node) {}
virtual void visit(const function_definition_node&) {}
virtual void visit(const return_statement_node&) {}
virtual void visit(const if_statement_node&) {}
virtual void visit(const compound_statement_node&) {}
/**
* @brief Visit a integer_literal_node.
* @see integer_literal_node
*
* @param node Node.
*/
virtual void visit(const integer_literal_node& node) {}
/**
* @brief Visit a var_read_expression_node.
* @see var_read_expression_node
*
* @param node Node.
*/
virtual void visit(const var_read_expression_node& node) {}
/**
* @brief Visit a unaryop_expression_node.
* @see unaryop_expression_node
*
* @param node Node.
*/
virtual void visit(const unaryop_expression_node& node) {}
/**
* @brief Visit a binop_expression_node.
* @see binop_expression_node
*
* @param node Node.
*/
virtual void visit(const binop_expression_node& node) {}
/**
* @brief Visit a var_assign_expression_node.
* @see var_assign_expression_node
*
* @param node Node.
*/
virtual void visit(const var_assign_expression_node& node) {}
/**
* @brief Visit a function_declaration_node.
* @see function_declaration_node
*
* @param node Node.
*/
virtual void visit(const function_declaration_node& node) {}
/**
* @brief Visit a function_definition_node.
* @see function_definition_node
*
* @param node Node.
*/
virtual void visit(const function_definition_node& node) {}
/**
* @brief Visit a return_statement_node.
* @see return_statement_node
*
* @param node Node.
*/
virtual void visit(const return_statement_node& node) {}
/**
* @brief Visit a if_statement_node.
* @see if_statement_node
*
* @param node Node.
*/
virtual void visit(const if_statement_node& node) {}
/**
* @brief Visit a compound_statement_node.
* @see compound_statement_node
*
* @param node Node.
*/
virtual void visit(const compound_statement_node& node) {}
/**
* @brief Visit a node handle with an error.
*
* @param handle Node handle.
*/
virtual void visit_error(const node_handle<node>& handle) {} virtual void visit_error(const node_handle<node>& handle) {}
}; };
+3 -3
View File
@@ -143,11 +143,11 @@ void function_declaration_node::accept(visitor& visitor) const {
} }
std::ostream& function_declaration_node::print(std::ostream& os) const { std::ostream& function_declaration_node::print(std::ostream& os) const {
return os << "function " << m_name->string << " declaration"; return os << "function " << p_name->string << " declaration";
} }
bool function_declaration_node::equal(const node& rhs) const { bool function_declaration_node::equal(const node& rhs) const {
return declaration_node::equal(rhs) && m_name == reinterpret_cast<const function_declaration_node&>(rhs).m_name; return declaration_node::equal(rhs) && p_name == reinterpret_cast<const function_declaration_node&>(rhs).p_name;
} }
void function_definition_node::accept(visitor& visitor) const { void function_definition_node::accept(visitor& visitor) const {
@@ -160,7 +160,7 @@ std::ostream& function_definition_node::print(std::ostream& os) const {
if (m_body.present()) { if (m_body.present()) {
for (const auto& entry : m_body->statements) for (const auto& entry : m_body->statements)
os << '\n' << entry; os << '\n' << entry;
return os << '\n' << m_body->end << ": " << m_name->string << " end"; return os << '\n' << m_body->end << ": " << p_name->string << " end";
} }
return os << m_body.error(); // error return os << m_body.error(); // error
} }
+11 -11
View File
@@ -22,7 +22,7 @@ TEST(Parser, EmptyFunctions) {
{ {
auto first = program->declarations()[0]; auto first = program->declarations()[0];
EXPECT_TRUE(first.present()); EXPECT_TRUE(first.present());
EXPECT_EQ(first->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(first->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = first; function_definition_node_h funcDef = first;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 0); EXPECT_EQ(funcDef->body()->statements.size(), 0);
@@ -30,7 +30,7 @@ TEST(Parser, EmptyFunctions) {
{ {
auto second = program->declarations()[1]; auto second = program->declarations()[1];
EXPECT_TRUE(second.present()); EXPECT_TRUE(second.present());
EXPECT_EQ(second->declaration_type(), declaration_node_t::FunctionDeclaration); EXPECT_EQ(second->declaration_type(), declaration_node_t::Func);
function_declaration_node_h funcDecl = second; function_declaration_node_h funcDecl = second;
EXPECT_EQ(funcDecl->name()->string, "foo"); EXPECT_EQ(funcDecl->name()->string, "foo");
} }
@@ -47,7 +47,7 @@ TEST(Parser, Literals) {
{ {
auto test1 = program->declarations()[0]; auto test1 = program->declarations()[0];
EXPECT_TRUE(test1.present()); EXPECT_TRUE(test1.present());
EXPECT_EQ(test1->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(test1->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = test1; function_definition_node_h funcDef = test1;
EXPECT_EQ(funcDef->name()->string, "test1"); EXPECT_EQ(funcDef->name()->string, "test1");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -57,7 +57,7 @@ TEST(Parser, Literals) {
{ {
auto test2 = program->declarations()[1]; auto test2 = program->declarations()[1];
EXPECT_TRUE(test2.present()); EXPECT_TRUE(test2.present());
EXPECT_EQ(test2->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(test2->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDecl = test2; function_definition_node_h funcDecl = test2;
EXPECT_EQ(funcDecl->name()->string, "test2"); EXPECT_EQ(funcDecl->name()->string, "test2");
} }
@@ -87,7 +87,7 @@ TEST(Parser, OperatorPrecedence_AddMul) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -115,7 +115,7 @@ TEST(Parser, OperatorPrecedence_Complex) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -153,7 +153,7 @@ TEST(Parser, UnaryOperator_Simple) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -175,7 +175,7 @@ TEST(Parser, UnaryOperator_PrePost) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -200,7 +200,7 @@ TEST(Parser, Paren) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -226,7 +226,7 @@ TEST(Parser, Assignment) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -255,7 +255,7 @@ TEST(Parser, CompoundAssignment) {
EXPECT_EQ(program->declarations().size(), 1); EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0]; auto func = program->declarations()[0];
EXPECT_TRUE(func.present()); EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); EXPECT_EQ(func->declaration_type(), declaration_node_t::FuncDef);
function_definition_node_h funcDef = func; function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1); EXPECT_EQ(funcDef->body()->statements.size(), 1);