refactor(parser): change node handles to node results

Refs: #1
This commit is contained in:
2026-06-03 11:57:09 +02:00
parent a1b5726271
commit af982c11ed
11 changed files with 403 additions and 401 deletions
+23 -21
View File
@@ -108,6 +108,8 @@ 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.
@@ -115,7 +117,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(unaryop_expression_node_t type, expression_node_h&& node) unaryop_expression_node(unaryop_expression_node_t type, expression_node_r&& node)
: m_type(type), m_node(std::move(node)) {} : m_type(type), m_node(std::move(node)) {}
/** /**
@@ -123,7 +125,7 @@ public:
* *
* @param node New node handle. * @param node New node handle.
*/ */
void set_node(expression_node_h&& node) { m_node = std::move(node); } void set_node(expression_node_r&& node) { m_node = std::move(node); }
/** /**
* @brief Returns the type of this node's operation. * @brief Returns the type of this node's operation.
@@ -137,21 +139,21 @@ public:
* *
* @return The inner expression. * @return The inner expression.
*/ */
const expression_node_h& get_node() const { return m_node; } const value_type& 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.
*/ */
expression_node_h& get_node() { return m_node; } value_type& 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.
*/ */
expression_node_h&& move_node() { return std::move(m_node); } value_type&& move_node() { return std::move(m_node); }
public: public:
/** /**
* @brief Returns this node's expression type. * @brief Returns this node's expression type.
@@ -167,7 +169,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;
expression_node_h m_node; /**< The inner expression. */ value_type m_node; /**< The inner expression. */
}; };
/** /**
@@ -201,7 +203,7 @@ public:
* @param lhs Left-hand-side expression. * @param lhs Left-hand-side expression.
* @param rhs Right-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_r&& lhs, expression_node_r&& 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)) {}
/** /**
@@ -216,42 +218,42 @@ public:
* *
* @return The left-hand-side expression. * @return The left-hand-side expression.
*/ */
const expression_node_h& lhs() const { return m_lhs; }; const expression_node_r& 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_h& lhs() { return m_lhs; }; expression_node_r& 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_h&& move_lhs() { return std::move(m_lhs); }; expression_node_r&& 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_h& rhs() const { return m_rhs; }; const expression_node_r& 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_h& rhs() { return m_rhs; }; expression_node_r& 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_h&& move_rhs() { return std::move(m_rhs); }; expression_node_r&& 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:
@@ -262,8 +264,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_h m_lhs; expression_node_r m_lhs;
expression_node_h m_rhs; expression_node_r m_rhs;
}; };
/** /**
@@ -277,7 +279,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(expression_node_h&& lhs, expression_node_h&& rhs) 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)) {} : m_compound(binop_expression_node_t::None), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
/** /**
@@ -287,7 +289,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(binop_expression_node_t compound, expression_node_h&& lhs, expression_node_h&& rhs) 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)) {} : m_compound(compound), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
/** /**
@@ -302,14 +304,14 @@ public:
* *
* @return The left-hand-side expression. * @return The left-hand-side expression.
*/ */
const expression_node_h& lhs() const { return m_lhs; } const expression_node_r& 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_h& rhs() const { return m_rhs; } const expression_node_r& rhs() const { return m_rhs; }
public: public:
/** /**
* @brief Returns this node's expression type. * @brief Returns this node's expression type.
@@ -325,8 +327,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_h m_lhs; expression_node_r m_lhs;
expression_node_h m_rhs; expression_node_r m_rhs;
}; };
} // namespace ast } // namespace ast
+20 -21
View File
@@ -2,10 +2,9 @@
#define FURC_AST_FWD_HPP #define FURC_AST_FWD_HPP
#include "furc/diag.hpp" #include "furc/diag.hpp"
#include "furc/handle.hpp"
#include "furlang/result.hpp" #include "furlang/result.hpp"
#include <string> #include <memory>
#include <vector> #include <vector>
namespace furc { namespace furc {
@@ -50,12 +49,12 @@ struct error {
class node; class node;
/** /**
* @brief Alias for handle to node. * @brief Alias for node result.
* *
* @tparam T AST node type. * @tparam T AST node type.
*/ */
template <typename T> template <typename T>
using node_handle = handle<T*, std::string>; using node_r = furlang::result<std::shared_ptr<T>, error>;
class literal_node; class literal_node;
@@ -63,7 +62,7 @@ class literal_node;
* @brief Alias for handle to literal_node. * @brief Alias for handle to literal_node.
* @see literal_node * @see literal_node
*/ */
using literal_node_h = node_handle<literal_node>; using literal_node_r = node_r<literal_node>;
class expression_node; class expression_node;
@@ -71,7 +70,7 @@ class expression_node;
* @brief Alias for handle to expression_node. * @brief Alias for handle to expression_node.
* @see expression_node * @see expression_node
*/ */
using expression_node_h = node_handle<expression_node>; using expression_node_r = node_r<expression_node>;
class declaration_node; class declaration_node;
@@ -79,7 +78,7 @@ class declaration_node;
* @brief Alias for handle to declaration_node. * @brief Alias for handle to declaration_node.
* @see declaration_node * @see declaration_node
*/ */
using declaration_node_h = node_handle<declaration_node>; using declaration_node_r = node_r<declaration_node>;
class statement_node; class statement_node;
@@ -87,7 +86,7 @@ class statement_node;
* @brief Alias for handle to statement_node. * @brief Alias for handle to statement_node.
* @see statement_node * @see statement_node
*/ */
using statement_node_h = node_handle<statement_node>; using statement_node_r = node_r<statement_node>;
class program_node; class program_node;
@@ -95,7 +94,7 @@ class program_node;
* @brief Alias for handle to program_node. * @brief Alias for handle to program_node.
* @see program_node * @see program_node
*/ */
using program_node_h = node_handle<program_node>; using program_node_r = node_r<program_node>;
class string_literal_node; class string_literal_node;
@@ -103,7 +102,7 @@ class string_literal_node;
* @brief Alias for handle to string_literal_node. * @brief Alias for handle to string_literal_node.
* @see string_literal_node * @see string_literal_node
*/ */
using string_literal_node_h = node_handle<string_literal_node>; using string_literal_node_r = node_r<string_literal_node>;
class integer_literal_node; class integer_literal_node;
@@ -111,7 +110,7 @@ class integer_literal_node;
* @brief Alias for handle to integer_literal_node. * @brief Alias for handle to integer_literal_node.
* @see integer_literal_node * @see integer_literal_node
*/ */
using integer_literal_node_h = node_handle<integer_literal_node>; using integer_literal_node_r = node_r<integer_literal_node>;
class var_read_expression_node; class var_read_expression_node;
@@ -119,7 +118,7 @@ class var_read_expression_node;
* @brief Alias for handle to var_read_expression_node. * @brief Alias for handle to var_read_expression_node.
* @see 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_r = node_r<var_read_expression_node>;
class unaryop_expression_node; class unaryop_expression_node;
@@ -127,7 +126,7 @@ class unaryop_expression_node;
* @brief Alias for handle to unaryop_expression_node. * @brief Alias for handle to unaryop_expression_node.
* @see unaryop_expression_node * @see unaryop_expression_node
*/ */
using unaryop_expression_node_h = node_handle<unaryop_expression_node>; using unaryop_expression_node_r = node_r<unaryop_expression_node>;
class binop_expression_node; class binop_expression_node;
@@ -135,7 +134,7 @@ class binop_expression_node;
* @brief Alias for handle to binop_expression_node. * @brief Alias for handle to binop_expression_node.
* @see binop_expression_node * @see binop_expression_node
*/ */
using binop_expression_node_h = node_handle<binop_expression_node>; using binop_expression_node_r = node_r<binop_expression_node>;
class var_assign_expression_node; class var_assign_expression_node;
@@ -143,7 +142,7 @@ class var_assign_expression_node;
* @brief Alias for handle to var_assign_expression_node. * @brief Alias for handle to var_assign_expression_node.
* @see 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_r = node_r<var_assign_expression_node>;
/** /**
* @brief List of statements. * @brief List of statements.
@@ -162,7 +161,7 @@ struct body {
/** /**
* @brief List of statements. * @brief List of statements.
*/ */
std::vector<statement_node_h> statements; std::vector<statement_node_r> statements;
/** /**
* @brief Compares two bodies for equality. * @brief Compares two bodies for equality.
@@ -204,7 +203,7 @@ class function_declaration_node;
* @brief Alias for handle to function_declaration_node. * @brief Alias for handle to function_declaration_node.
* @see function_declaration_node * @see function_declaration_node
*/ */
using function_declaration_node_h = node_handle<function_declaration_node>; using function_declaration_node_r = node_r<function_declaration_node>;
class function_definition_node; class function_definition_node;
@@ -212,7 +211,7 @@ class function_definition_node;
* @brief Alias for handle to function_definition_node. * @brief Alias for handle to function_definition_node.
* @see function_definition_node * @see function_definition_node
*/ */
using function_definition_node_h = node_handle<function_definition_node>; using function_definition_node_r = node_r<function_definition_node>;
class return_statement_node; class return_statement_node;
@@ -220,7 +219,7 @@ class return_statement_node;
* @brief Alias for handle to return_statement_node. * @brief Alias for handle to return_statement_node.
* @see return_statement_node * @see return_statement_node
*/ */
using return_statement_node_h = node_handle<return_statement_node>; using return_statement_node_r = node_r<return_statement_node>;
class if_statement_node; class if_statement_node;
@@ -228,7 +227,7 @@ class if_statement_node;
* @brief Alias for handle to if_statement_node. * @brief Alias for handle to if_statement_node.
* @see if_statement_node * @see if_statement_node
*/ */
using if_statement_node_h = node_handle<if_statement_node>; using if_statement_node_r = node_r<if_statement_node>;
class compound_statement_node; class compound_statement_node;
@@ -236,7 +235,7 @@ class compound_statement_node;
* @brief Alias for handle to compound_statement_node. * @brief Alias for handle to compound_statement_node.
* @see compound_statement_node * @see compound_statement_node
*/ */
using compound_statement_node_h = node_handle<compound_statement_node>; using compound_statement_node_r = node_r<compound_statement_node>;
} // namespace ast } // namespace ast
} // namespace furc } // namespace furc
+3 -3
View File
@@ -23,14 +23,14 @@ public:
* *
* @param declaration Declaration to add. * @param declaration Declaration to add.
*/ */
void push(node_handle<declaration_node>&& declaration) { m_declarations.push_back(std::move(declaration)); } void push(node_r<declaration_node>&& 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_handle<declaration_node>>& declarations() const { return m_declarations; } const std::vector<node_r<declaration_node>>& declarations() const { return m_declarations; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -38,7 +38,7 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
std::vector<node_handle<declaration_node>> m_declarations; std::vector<node_r<declaration_node>> m_declarations;
}; };
} // namespace ast } // namespace ast
+15 -11
View File
@@ -3,6 +3,8 @@
#include "furc/ast/node.hpp" #include "furc/ast/node.hpp"
#include <optional>
namespace furc { namespace furc {
namespace ast { namespace ast {
@@ -43,6 +45,8 @@ protected:
* @brief Return statement AST node. * @brief Return statement AST node.
*/ */
class return_statement_node final : public statement_node { class return_statement_node final : public statement_node {
public:
using value_type = std::optional<expression_node_r>; /**< Value type. */
public: public:
return_statement_node() = default; return_statement_node() = default;
@@ -51,7 +55,7 @@ public:
* *
* @param value Return value handle. * @param value Return value handle.
*/ */
return_statement_node(expression_node_h&& value) return_statement_node(expression_node_r&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
public: public:
/** /**
@@ -59,7 +63,7 @@ public:
* *
* @return The return value handle. * @return The return value handle.
*/ */
expression_node_h value() const { return m_value; } value_type value() const { return m_value; }
public: public:
/** /**
* @brief Returns this node's statement type. * @brief Returns this node's statement type.
@@ -74,7 +78,7 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
expression_node_h m_value; /**< Return value handle. */ value_type m_value; /**< Return value handle. */
}; };
/** /**
@@ -88,7 +92,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(expression_node_h&& cond, statement_node_h&& then) if_statement_node(expression_node_r&& cond, statement_node_r&& then)
: m_cond(std::move(cond)), m_then(std::move(then)) {} : m_cond(std::move(cond)), m_then(std::move(then)) {}
/** /**
@@ -98,7 +102,7 @@ public:
* @param then Then statement handle. * @param then Then statement handle.
* @param elze Else 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_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)) {} : m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
public: public:
/** /**
@@ -106,21 +110,21 @@ public:
* *
* @return The condition expression handle. * @return The condition expression handle.
*/ */
expression_node_h cond() const { return m_cond; } expression_node_r 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_h& then() const { return m_then; } const statement_node_r& 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 statement_node_h& elze() const { return m_else; } const std::optional<statement_node_r>& elze() const { return m_else; }
public: public:
/** /**
* @brief Returns this node's statement type. * @brief Returns this node's statement type.
@@ -135,9 +139,9 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
expression_node_h m_cond; /**< The condition expression handle */ expression_node_r m_cond; /**< The condition expression handle */
statement_node_h m_then; /**< The then statement handle */ statement_node_r m_then; /**< The then statement handle */
statement_node_h m_else; /**< The else statement handle */ std::optional<statement_node_r> m_else; /**< The else statement handle */
}; };
/** /**
+3 -3
View File
@@ -123,11 +123,11 @@ public:
virtual void visit(const compound_statement_node& node) {} virtual void visit(const compound_statement_node& node) {}
/** /**
* @brief Visit a node handle with an error. * @brief Visit an AST error.
* *
* @param handle Node handle. * @param error AST error.
*/ */
virtual void visit_error(const node_handle<node>& handle) {} virtual void visit_error(const ast::error& error) {}
}; };
} // namespace ast } // namespace ast
+8 -10
View File
@@ -54,20 +54,18 @@ public:
* *
* @return Handle to an AST node of the program. * @return Handle to an AST node of the program.
*/ */
ast::program_node_h parse() &; ast::program_node_r parse() &;
private: private:
ast::declaration_node_h parse_declaration(); ast::declaration_node_r parse_declaration();
ast::statement_node_h parse_statement(); ast::statement_node_r parse_statement();
ast::expression_node_h parse_expression(std::uint32_t precedence = 16); ast::expression_node_r parse_expression(std::uint32_t precedence = 16);
ast::literal_node_h parse_literal(); ast::literal_node_r parse_literal();
ast::expression_node_h parse_expression_primary(); ast::expression_node_r parse_expression_primary();
ast::expression_node_h parse_expression_unary(std::uint32_t precedence); ast::expression_node_r parse_expression_unary(std::uint32_t precedence);
ast::expression_node_h parse_expression_rhs(ast::expression_node_h&& init, std::uint32_t precedence); ast::expression_node_r parse_expression_rhs(ast::expression_node_r&& init, std::uint32_t precedence);
ast::body_r parse_body(); ast::body_r parse_body();
private:
static ast::node_handle<ast::node> error_handle(const token_r& result);
private: private:
token_r next_token(); token_r next_token();
const token_r& peek_token(); const token_r& peek_token();
+5 -4
View File
@@ -77,6 +77,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;
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:
@@ -184,7 +185,7 @@ void return_statement_node::accept(visitor& visitor) const {
std::ostream& return_statement_node::print(std::ostream& os) const { std::ostream& return_statement_node::print(std::ostream& os) const {
os << "return statement"; os << "return statement";
if (m_value.present()) return os << ' ' << *m_value; if (m_value.has_value()) return os << ' ' << *m_value.value();
return os; return os;
} }
@@ -199,7 +200,7 @@ void if_statement_node::accept(visitor& visitor) const {
std::ostream& if_statement_node::print(std::ostream& os) const { std::ostream& if_statement_node::print(std::ostream& os) const {
os << "if " << *m_cond << ", then:\n"; os << "if " << *m_cond << ", then:\n";
os << m_then; os << m_then;
if (m_else.present()) os << m_else; if (m_else.has_value()) os << *m_else.value();
return os; return os;
} }
@@ -223,9 +224,9 @@ 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()) { if (decl.has_error()) {
visitor.visit_error(decl); visitor.visit_error(decl.error());
} else { } else {
decl->accept(visitor); decl.value()->accept(visitor);
} }
} }
} }
+18 -15
View File
@@ -22,7 +22,7 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
return; return;
} }
for (const auto& stmt : funcDef.body()->statements) { for (const auto& stmt : funcDef.body()->statements) {
stmt->accept(*this); stmt.value()->accept(*this);
} }
m_currentBlock->emplace<ir::return_instruction>(); m_currentBlock->emplace<ir::return_instruction>();
@@ -30,12 +30,15 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
} }
void ir_generator::visit(const ast::return_statement_node& returnStmt) { void ir_generator::visit(const ast::return_statement_node& returnStmt) {
if (returnStmt.value().has_error()) { if (!returnStmt.value().has_value()) return;
std::cerr << returnStmt.value() << '\n'; auto value = returnStmt.value().value();
if (value.has_error()) {
std::cerr << value.error() << '\n';
return;
} }
if (returnStmt.value().present()) { if (value.has_value()) {
returnStmt.value()->accept(*this); 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>();
@@ -43,19 +46,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()->accept(*this); node.cond().value()->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()->accept(*this); node.then().value()->accept(*this);
if (node.elze().present()) { 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()->accept(*this); node.elze().value().value()->accept(*this);
} }
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size()); m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size());
@@ -64,7 +67,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->accept(*this); stmt.value()->accept(*this);
} }
} }
@@ -110,9 +113,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()->accept(*this); node.lhs().value()->accept(*this);
ir_register lhs = m_registerCounter - 1; ir_register lhs = m_registerCounter - 1;
node.rhs()->accept(*this); node.rhs().value()->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()),
@@ -122,10 +125,10 @@ 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()->accept(*this); node.rhs().value()->accept(*this);
ir_register rhs = m_registerCounter - 1; ir_register rhs = m_registerCounter - 1;
assert(node.lhs()->expression_type() == ast::expression_node_t::VarRead); assert(node.lhs().value()->expression_type() == ast::expression_node_t::VarRead);
ast::var_read_expression_node_h lhs = node.lhs(); auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs().value());
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()) {
+56 -68
View File
@@ -30,17 +30,17 @@ parser::parser(std::string_view filename)
m_lexer = { filename, m_content }; m_lexer = { filename, m_content };
} }
ast::program_node_h parser::parse() & { ast::program_node_r parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>(); auto program = m_arena.allocate_shared<ast::program_node>();
while (peek_token().has_value()) { while (peek_token().has_value()) {
program->push(std::move(parse_declaration())); program->push(std::move(parse_declaration()));
} }
return { location{ m_filename, 0, 0 }, program }; return program;
} }
ast::declaration_node_h parser::parse_declaration() { ast::declaration_node_r parser::parse_declaration() {
const auto& first = peek_token(); const auto& first = peek_token();
switch (first->type) { switch (first->type) {
case token_t::Keyword: { case token_t::Keyword: {
@@ -48,29 +48,29 @@ ast::declaration_node_h parser::parse_declaration() {
switch ((*first)->keyword) { switch ((*first)->keyword) {
case keyword_token::Func: { case keyword_token::Func: {
auto name = eat_token(token_t::Identifier); auto name = eat_token(token_t::Identifier);
if (name.has_error()) return error_handle(name); if (name.has_error()) return ast::declaration_node_r(ast::error{ name.error().location });
auto tok = eat_token(token_t::LParen); auto tok = eat_token(token_t::LParen);
if (tok.has_error()) return error_handle(tok); if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
tok = eat_token(token_t::RParen); tok = eat_token(token_t::RParen);
if (tok.has_error()) return error_handle(tok); if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
const auto& peek = peek_token(); const auto& peek = peek_token();
if (peek.has_error()) return error_handle(peek); if (peek.has_error()) return ast::declaration_node_r(ast::error{ peek.error().location });
switch (peek->type) { switch (peek->type) {
case token_t::LBrace: { case token_t::LBrace: {
ast::body_r body = parse_body(); ast::body_r body = parse_body();
if (body.has_error()) return ast::node_handle<ast::node>(body.error().location, "Body error"); if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location });
return ast::function_definition_node_h{ first->location, m_arena, *name, std::move(body) }; return m_arena.allocate_shared<ast::function_definition_node>(*name, std::move(body));
} }
case token_t::Semicolon: { case token_t::Semicolon: {
m_peekBuffer.clear(); m_peekBuffer.clear();
return ast::function_declaration_node_h{ first->location, m_arena, *name }; return m_arena.allocate_shared<ast::function_declaration_node>(*name);
} }
default: return { tok->location, "unexpected token "s + tok->type }; default: return ast::declaration_node_r(ast::error{ tok->location });
} }
} }
default: return { first->location, "unexpected keyword "s + (*first)->keyword }; default: return ast::declaration_node_r(ast::error{ first->location });
} }
} }
case token_t::None: case token_t::None:
@@ -85,14 +85,14 @@ ast::declaration_node_h parser::parse_declaration() {
case token_t::Semicolon: case token_t::Semicolon:
case token_t::Colon: case token_t::Colon:
default: { default: {
return { first->location, "unexpected token "s + first->type }; return ast::declaration_node_r(ast::error{ first->location });
} }
} }
} }
ast::statement_node_h parser::parse_statement() { ast::statement_node_r parser::parse_statement() {
const auto& tok = peek_token(); const auto& tok = peek_token();
if (tok.has_error()) return error_handle(tok); if (tok.has_error()) return ast::statement_node_r(ast::error{ tok.error().location });
auto location = tok->location; auto location = tok->location;
switch (tok->type) { switch (tok->type) {
case token_t::Keyword: { case token_t::Keyword: {
@@ -101,104 +101,102 @@ ast::statement_node_h parser::parse_statement() {
auto tok = next_token(); auto tok = next_token();
if (peek_token()->type == token_t::Semicolon) { if (peek_token()->type == token_t::Semicolon) {
next_token(); next_token();
return ast::return_statement_node_h{ tok->location, m_arena }; return m_arena.allocate_shared<ast::return_statement_node>();
} }
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 error_handle(err); if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
return ast::return_statement_node_h{ tok->location, m_arena, std::move(value) }; return m_arena.allocate_shared<ast::return_statement_node>(std::move(value));
} }
case keyword_token::If: { case keyword_token::If: {
auto tok = next_token(); auto tok = next_token();
auto err = eat_token(token_t::LParen); auto err = eat_token(token_t::LParen);
if (err.has_error()) return error_handle(err); if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
auto cond = parse_expression(); auto cond = parse_expression();
err = eat_token(token_t::RParen); err = eat_token(token_t::RParen);
if (err.has_error()) return error_handle(err); if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
auto then = parse_statement(); auto then = parse_statement();
if (then.has_error()) return then; if (then.has_error()) return ast::statement_node_r(ast::error{ then.error().location });
if (peek_token().has_value() && peek_token()->type == token_t::Keyword && if (peek_token().has_value() && peek_token()->type == token_t::Keyword &&
peek_token()->value.keyword == keyword_token::Else) { peek_token()->value.keyword == keyword_token::Else) {
next_token(); next_token();
return ast::if_statement_node_h{ location, return m_arena.allocate_shared<ast::if_statement_node>(std::move(cond),
m_arena,
std::move(cond),
std::move(then), std::move(then),
std::move(parse_statement()) }; std::move(parse_statement()));
} }
return ast::if_statement_node_h{ location, m_arena, std::move(cond), std::move(then) }; return m_arena.allocate_shared<ast::if_statement_node>(std::move(cond), std::move(then));
} }
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 ast::compound_statement_node_h{ location, m_arena, parse_body() }; case token_t::LBrace: return m_arena.allocate_shared<ast::compound_statement_node>(parse_body());
default: break; default: break;
} }
auto declaration = parse_declaration(); auto declaration = parse_declaration();
if (declaration.present()) return std::move(declaration); if (declaration.has_value()) return std::move(*declaration);
auto expression = parse_expression(); auto expression = parse_expression();
if (expression.present()) { if (expression.has_value()) {
auto semi = eat_token(token_t::Semicolon); auto semi = eat_token(token_t::Semicolon);
if (semi.has_error()) return error_handle(semi); if (semi.has_error()) return ast::statement_node_r(ast::error{ semi.error().location });
return std::move(expression); return std::move(*expression);
} }
auto token = next_token(); auto token = next_token();
return { token->location, "unexpected token "s + token->type + ", expected statement, declaration or expression" }; return ast::statement_node_r(ast::error{ token->location });
} }
ast::expression_node_h 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); return parse_expression_rhs(parse_expression_unary(precedence), precedence);
} }
ast::literal_node_h parser::parse_literal() { ast::literal_node_r parser::parse_literal() {
const auto& tok = peek_token(); const auto& tok = peek_token();
switch (tok->type) { switch (tok->type) {
case token_t::String: { case token_t::String: {
auto tok = next_token(); auto tok = next_token();
if (tok.has_error()) return error_handle(tok); if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
return ast::string_literal_node_h{ tok->location, m_arena, (*tok)->string }; return m_arena.allocate_shared<ast::string_literal_node>((*tok)->string);
} }
case token_t::Integer: { case token_t::Integer: {
auto tok = next_token(); auto tok = next_token();
if (tok.has_error()) return error_handle(tok); if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
return ast::integer_literal_node_h{ tok->location, m_arena, (*tok)->integer }; return m_arena.allocate_shared<ast::integer_literal_node>((*tok)->integer);
} }
default: break; default: break;
} }
return { tok->location, "unexpected token "s + tok->type + ", expected literal" }; return ast::literal_node_r(ast::error{ tok->location });
} }
ast::expression_node_h parser::parse_expression_primary() { ast::expression_node_r parser::parse_expression_primary() {
const auto& tok = peek_token(); const auto& tok = peek_token();
switch (tok->type) { switch (tok->type) {
case token_t::Identifier: { case token_t::Identifier: {
auto tok = next_token(); auto tok = next_token();
if (tok.has_error()) return error_handle(tok); if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
return ast::var_read_expression_node_h{ tok->location, m_arena, (*tok)->string }; return m_arena.allocate_shared<ast::var_read_expression_node>((*tok)->string);
} }
case token_t::LParen: { case token_t::LParen: {
auto tok = next_token(); auto tok = next_token();
auto node = parse_expression(); auto node = parse_expression();
auto err = eat_token(token_t::RParen); auto err = eat_token(token_t::RParen);
if (err.has_error()) return error_handle(err); if (err.has_error()) return ast::expression_node_r(ast::error{ err.error().location });
return node; return node;
} }
default: { default: {
auto literal = parse_literal(); auto literal = parse_literal();
if (literal.present()) return std::move(literal); if (literal.has_value()) return std::move(*literal);
return { tok->location, "unexpected token"s + tok->type + ", expected expression or literal" }; return ast::expression_node_r(ast::error{ tok->location });
} }
} }
} }
@@ -208,7 +206,7 @@ struct unaryop_info {
std::uint32_t precedence; std::uint32_t precedence;
}; };
ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence) { ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence) {
static std::unordered_map<token_t, unaryop_info> s_prefixes = { static std::unordered_map<token_t, unaryop_info> s_prefixes = {
{ token_t::Plus, unaryop_info{ ast::unaryop_expression_node_t::Positive, 3 } }, { token_t::Plus, unaryop_info{ ast::unaryop_expression_node_t::Positive, 3 } },
{ token_t::Minus, unaryop_info{ ast::unaryop_expression_node_t::Negative, 3 } }, { token_t::Minus, unaryop_info{ ast::unaryop_expression_node_t::Negative, 3 } },
@@ -216,7 +214,7 @@ ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence)
{ token_t::DMinus, unaryop_info{ ast::unaryop_expression_node_t::PrefixDecrement, 2 } }, { token_t::DMinus, unaryop_info{ ast::unaryop_expression_node_t::PrefixDecrement, 2 } },
}; };
ast::unaryop_expression_node_h result; std::shared_ptr<ast::unaryop_expression_node> result;
while (true) { while (true) {
auto it = s_prefixes.find(peek_token()->type); auto it = s_prefixes.find(peek_token()->type);
if (it == s_prefixes.end()) break; if (it == s_prefixes.end()) break;
@@ -224,7 +222,7 @@ ast::expression_node_h 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_h expression; ast::expression_node_r 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()) {
@@ -232,11 +230,11 @@ ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence)
expression = parse_expression_unary(current.precedence + 1); expression = parse_expression_unary(current.precedence + 1);
} }
result = { token->location, m_arena, current.type, std::move(expression) }; result = m_arena.allocate_shared<ast::unaryop_expression_node>(current.type, std::move(expression));
} }
if (!result.present()) return parse_expression_primary(); if (result == nullptr) return parse_expression_primary();
if (!result->get_node().present()) result->set_node(parse_expression_primary()); if (result->get_node().has_value()) result->set_node(parse_expression_primary());
return result; return result;
} }
@@ -291,7 +289,7 @@ struct rhsop_info {
} }
}; };
ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& init, std::uint32_t precedence) { ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& 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) },
@@ -315,7 +313,7 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& 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_h lhs = std::move(init); ast::expression_node_r 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;
@@ -324,7 +322,7 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
if (current.precedence >= precedence) return lhs; if (current.precedence >= precedence) return lhs;
auto opToken = next_token(); auto opToken = next_token();
ast::expression_node_h rhs; ast::expression_node_r 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 rhs = parse_expression_unary(current.precedence + 1); // unary prefix is always right-associative
} }
@@ -343,21 +341,15 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
switch (current.type) { switch (current.type) {
case rhsop_info_t::Unaryop: case rhsop_info_t::Unaryop:
lhs = ast::unaryop_expression_node_h{ opToken->location, m_arena, current.unary, std::move(lhs) }; lhs = m_arena.allocate_shared<ast::unaryop_expression_node>(current.unary, std::move(lhs));
break; break;
case rhsop_info_t::Binop: case rhsop_info_t::Binop:
lhs = ast::binop_expression_node_h{ opToken->location, lhs = m_arena.allocate_shared<ast::binop_expression_node>(current.binary, std::move(lhs), std::move(rhs));
m_arena,
current.binary,
std::move(lhs),
std::move(rhs) };
break; break;
case rhsop_info_t::Assignment: case rhsop_info_t::Assignment:
lhs = ast::var_assign_expression_node_h{ opToken->location, lhs = m_arena.allocate_shared<ast::var_assign_expression_node>(current.assignment,
m_arena,
current.assignment,
std::move(lhs), std::move(lhs),
std::move(rhs) }; std::move(rhs));
break; break;
} }
} }
@@ -382,10 +374,6 @@ ast::body_r parser::parse_body() {
return body; return body;
} }
ast::node_handle<ast::node> parser::error_handle(const token_r& result) {
return { result.error().location, std::string(result.error().message) };
}
token_r parser::next_token() { token_r parser::next_token() {
if (!m_peekBuffer.empty()) { if (!m_peekBuffer.empty()) {
auto token = std::move(m_peekBuffer.back()); auto token = std::move(m_peekBuffer.back());
+29 -22
View File
@@ -7,7 +7,8 @@
#include <iostream> #include <iostream>
int main(void) { int main(void) {
std::string programStr = R"( try {
std::string programStr = R"(
func main() { func main() {
x = 5; x = 5;
x -= 3; x -= 3;
@@ -21,30 +22,36 @@ int main(void) {
z = x + y; z = x + y;
} }
)"; )";
furc::front::parser parser("<TEMP>", programStr); furc::front::parser parser("<TEMP>", programStr);
furc::front::ir_generator generator; furc::front::ir_generator generator;
auto program = parser.parse(); auto programResult = parser.parse();
if (program.has_error()) { if (programResult.has_error()) {
std::cerr << program << '\n'; std::cerr << programResult.error() << '\n';
} return 1;
program->accept(generator);
auto module = std::move(generator.move_module());
std::cout << "Generated IR:\n";
for (const auto& function : module.functions()) {
std::cout << function->name() << ":\n";
furlang::ir::block_index blockIndex = 0;
for (const auto& block : function->blocks()) {
std::cout << " # block " << blockIndex++ << '\n';
for (const auto& instruction : block->instructions()) {
std::cout << " " << *instruction << '\n';
}
std::cout << " " << *block->exit() << '\n';
} }
} const auto& program = *programResult;
program->accept(generator);
return 0; auto module = std::move(generator.move_module());
std::cout << "Generated IR:\n";
for (const auto& function : module.functions()) {
std::cout << function->name() << ":\n";
furlang::ir::block_index blockIndex = 0;
for (const auto& block : function->blocks()) {
std::cout << " # block " << blockIndex++ << '\n';
for (const auto& instruction : block->instructions()) {
std::cout << " " << *instruction << '\n';
}
std::cout << " " << *block->exit() << '\n';
}
}
return 0;
} catch (...) {
std::cerr << "Caught an exception in main!\n";
return 1;
}
} }
#endif // LIBFURC #endif // LIBFURC
+223 -223
View File
@@ -6,7 +6,7 @@
#include "furc/ast/program.hpp" // IWYU pragma: keep #include "furc/ast/program.hpp" // IWYU pragma: keep
#include "furc/ast/statement.hpp" // IWYU pragma: keep #include "furc/ast/statement.hpp" // IWYU pragma: keep
#include "gtest/gtest.h" #include "gtest/gtest.h" // IWYU pragma: keep
namespace { namespace {
@@ -14,261 +14,261 @@ using namespace furc::front;
using namespace furc::ast; using namespace furc::ast;
using namespace std::string_view_literals; using namespace std::string_view_literals;
TEST(Parser, EmptyFunctions) { // TEST(Parser, EmptyFunctions) {
parser parser("<TEMP>", "func main() {}\nfunc foo();"); // parser parser("<TEMP>", "func main() {}\nfunc foo();");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
EXPECT_EQ(program->declarations().size(), 2); // EXPECT_EQ(program->declarations().size(), 2);
{ // {
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::FuncDef); // 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);
} // }
{ // {
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::Func); // 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");
} // }
} // }
#define EXPECT_INTLIT(expr, integer) \ // #define EXPECT_INTLIT(expr, integer) \
do { \ // do { \
EXPECT_EQ((expr)->expression_type(), expression_node_t::Literal); \ // EXPECT_EQ((expr)->expression_type(), expression_node_t::Literal); \
literal_node_h literal = (expr); \ // literal_node_h literal = (expr); \
EXPECT_EQ(literal->literal_type(), literal_node_t::Integer); \ // EXPECT_EQ(literal->literal_type(), literal_node_t::Integer); \
integer_literal_node_h intLit = literal; \ // integer_literal_node_h intLit = literal; \
EXPECT_EQ(intLit->value(), integer_token((integer))); \ // EXPECT_EQ(intLit->value(), integer_token((integer))); \
} while (0) // } while (0)
TEST(Parser, Literals) { // TEST(Parser, Literals) {
parser parser("<TEMP>", R"( // parser parser("<TEMP>", R"(
func test1() { return 67; } // func test1() { return 67; }
func test2() { return "uwu"; } // func test2() { return "uwu"; }
)"); // )");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
EXPECT_EQ(program->declarations().size(), 2); // EXPECT_EQ(program->declarations().size(), 2);
{ // {
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::FuncDef); // 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);
return_statement_node_h ret = funcDef->body()->statements[0]; // return_statement_node_h ret = funcDef->body()->statements[0];
EXPECT_INTLIT(ret->value(), 67); // EXPECT_INTLIT(ret->value(), 67);
} // }
{ // {
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::FuncDef); // 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");
} // }
} // }
#define EXPECT_VARREAD(expr, varname) \ // #define EXPECT_VARREAD(expr, varname) \
do { \ // do { \
EXPECT_EQ((expr)->expression_type(), expression_node_t::VarRead); \ // EXPECT_EQ((expr)->expression_type(), expression_node_t::VarRead); \
var_read_expression_node_h varRead = (expr); \ // var_read_expression_node_h varRead = (expr); \
EXPECT_EQ(varRead->get_name(), (varname)); \ // EXPECT_EQ(varRead->get_name(), (varname)); \
} while (0) // } while (0)
// TODO: Use arena (I am too exhausted rn to do it) // // TODO: Use arena (I am too exhausted rn to do it)
TEST(Parser, OperatorPrecedence_AddMul) { // TEST(Parser, OperatorPrecedence_AddMul) {
parser parser("<TEMP>", "func main() { return 1 + 2 * 3; }"); // parser parser("<TEMP>", "func main() { return 1 + 2 * 3; }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
return_statement_node_h ret = funcDef->body()->statements[0]; // return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value(); // auto retVal = ret->value();
EXPECT_TRUE(retVal.present()); // EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Binop); // EXPECT_EQ(retVal->expression_type(), expression_node_t::Binop);
binop_expression_node_h add = retVal; // binop_expression_node_h add = retVal;
EXPECT_EQ(add->type(), binop_expression_node_t::Add); // EXPECT_EQ(add->type(), binop_expression_node_t::Add);
EXPECT_INTLIT(add->lhs(), 1); // EXPECT_INTLIT(add->lhs(), 1);
EXPECT_EQ(add->rhs()->expression_type(), expression_node_t::Binop); // EXPECT_EQ(add->rhs()->expression_type(), expression_node_t::Binop);
binop_expression_node_h mul = add->rhs(); // binop_expression_node_h mul = add->rhs();
EXPECT_EQ(mul->type(), binop_expression_node_t::Mul); // EXPECT_EQ(mul->type(), binop_expression_node_t::Mul);
EXPECT_INTLIT(mul->lhs(), 2); // EXPECT_INTLIT(mul->lhs(), 2);
EXPECT_INTLIT(mul->rhs(), 3); // EXPECT_INTLIT(mul->rhs(), 3);
} // }
TEST(Parser, OperatorPrecedence_Complex) { // TEST(Parser, OperatorPrecedence_Complex) {
parser parser("<TEMP>", "func main() { return 1 + 2 * 3 - 4 / 2; }"); // parser parser("<TEMP>", "func main() { return 1 + 2 * 3 - 4 / 2; }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
return_statement_node_h ret = funcDef->body()->statements[0]; // return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value(); // auto retVal = ret->value();
EXPECT_TRUE(retVal.present()); // EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Binop); // EXPECT_EQ(retVal->expression_type(), expression_node_t::Binop);
binop_expression_node_h sub = retVal; // binop_expression_node_h sub = retVal;
EXPECT_EQ(sub->type(), binop_expression_node_t::Sub); // EXPECT_EQ(sub->type(), binop_expression_node_t::Sub);
EXPECT_EQ(sub->lhs()->expression_type(), expression_node_t::Binop); // EXPECT_EQ(sub->lhs()->expression_type(), expression_node_t::Binop);
binop_expression_node_h add = sub->lhs(); // binop_expression_node_h add = sub->lhs();
EXPECT_EQ(add->type(), binop_expression_node_t::Add); // EXPECT_EQ(add->type(), binop_expression_node_t::Add);
EXPECT_INTLIT(add->lhs(), 1); // EXPECT_INTLIT(add->lhs(), 1);
EXPECT_EQ(add->rhs()->expression_type(), expression_node_t::Binop); // EXPECT_EQ(add->rhs()->expression_type(), expression_node_t::Binop);
binop_expression_node_h mul = add->rhs(); // binop_expression_node_h mul = add->rhs();
EXPECT_EQ(mul->type(), binop_expression_node_t::Mul); // EXPECT_EQ(mul->type(), binop_expression_node_t::Mul);
EXPECT_INTLIT(mul->lhs(), 2); // EXPECT_INTLIT(mul->lhs(), 2);
EXPECT_INTLIT(mul->rhs(), 3); // EXPECT_INTLIT(mul->rhs(), 3);
EXPECT_EQ(sub->rhs()->expression_type(), expression_node_t::Binop); // EXPECT_EQ(sub->rhs()->expression_type(), expression_node_t::Binop);
binop_expression_node_h div = sub->rhs(); // binop_expression_node_h div = sub->rhs();
EXPECT_EQ(div->type(), binop_expression_node_t::Div); // EXPECT_EQ(div->type(), binop_expression_node_t::Div);
EXPECT_INTLIT(div->lhs(), 4); // EXPECT_INTLIT(div->lhs(), 4);
EXPECT_INTLIT(div->rhs(), 2); // EXPECT_INTLIT(div->rhs(), 2);
} // }
TEST(Parser, UnaryOperator_Simple) { // TEST(Parser, UnaryOperator_Simple) {
parser parser("<TEMP>", "func main() { return -5; }"); // parser parser("<TEMP>", "func main() { return -5; }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
return_statement_node_h ret = funcDef->body()->statements[0]; // return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value(); // auto retVal = ret->value();
EXPECT_TRUE(retVal.present()); // EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop); // EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h neg = retVal; // unaryop_expression_node_h neg = retVal;
EXPECT_EQ(neg->type(), unaryop_expression_node_t::Negative); // EXPECT_EQ(neg->type(), unaryop_expression_node_t::Negative);
EXPECT_INTLIT(neg->get_node(), 5); // EXPECT_INTLIT(neg->get_node(), 5);
} // }
TEST(Parser, UnaryOperator_PrePost) { // TEST(Parser, UnaryOperator_PrePost) {
parser parser("<TEMP>", "func main() { return --5++; }"); // parser parser("<TEMP>", "func main() { return --5++; }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
return_statement_node_h ret = funcDef->body()->statements[0]; // return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value(); // auto retVal = ret->value();
EXPECT_TRUE(retVal.present()); // EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop); // EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h inc = retVal; // unaryop_expression_node_h inc = retVal;
EXPECT_EQ(inc->type(), unaryop_expression_node_t::PostfixIncrement); // EXPECT_EQ(inc->type(), unaryop_expression_node_t::PostfixIncrement);
EXPECT_EQ(inc->get_node()->expression_type(), expression_node_t::Unaryop); // EXPECT_EQ(inc->get_node()->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h dec = inc->get_node(); // unaryop_expression_node_h dec = inc->get_node();
EXPECT_INTLIT(dec->get_node(), 5); // EXPECT_INTLIT(dec->get_node(), 5);
} // }
TEST(Parser, Paren) { // TEST(Parser, Paren) {
parser parser("<TEMP>", "func main() { return --(x++); }"); // parser parser("<TEMP>", "func main() { return --(x++); }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
return_statement_node_h ret = funcDef->body()->statements[0]; // return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value(); // auto retVal = ret->value();
EXPECT_TRUE(retVal.present()); // EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop); // EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h dec = retVal; // unaryop_expression_node_h dec = retVal;
EXPECT_EQ(dec->type(), unaryop_expression_node_t::PrefixDecrement); // EXPECT_EQ(dec->type(), unaryop_expression_node_t::PrefixDecrement);
EXPECT_EQ(dec->get_node()->expression_type(), expression_node_t::Unaryop); // EXPECT_EQ(dec->get_node()->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h inc = dec->get_node(); // unaryop_expression_node_h inc = dec->get_node();
EXPECT_EQ(inc->type(), unaryop_expression_node_t::PostfixIncrement); // EXPECT_EQ(inc->type(), unaryop_expression_node_t::PostfixIncrement);
EXPECT_VARREAD(inc->get_node(), "x"sv); // EXPECT_VARREAD(inc->get_node(), "x"sv);
} // }
TEST(Parser, Assignment) { // TEST(Parser, Assignment) {
parser parser("<TEMP>", "func main() { x = 10; }"); // parser parser("<TEMP>", "func main() { x = 10; }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
EXPECT_EQ(funcDef->body()->statements[0]->statement_type(), statement_node_t::Expression); // EXPECT_EQ(funcDef->body()->statements[0]->statement_type(), statement_node_t::Expression);
expression_node_h expr = funcDef->body()->statements[0]; // expression_node_h expr = funcDef->body()->statements[0];
EXPECT_EQ(expr->expression_type(), expression_node_t::VarAssign); // EXPECT_EQ(expr->expression_type(), expression_node_t::VarAssign);
var_assign_expression_node_h assign = expr; // var_assign_expression_node_h assign = expr;
EXPECT_EQ(assign->compound(), binop_expression_node_t::None); // EXPECT_EQ(assign->compound(), binop_expression_node_t::None);
EXPECT_VARREAD(assign->lhs(), "x"sv); // EXPECT_VARREAD(assign->lhs(), "x"sv);
expression_node_h rhs = assign->rhs(); // expression_node_h rhs = assign->rhs();
EXPECT_EQ(rhs->expression_type(), expression_node_t::Literal); // EXPECT_EQ(rhs->expression_type(), expression_node_t::Literal);
EXPECT_INTLIT(rhs, 10); // EXPECT_INTLIT(rhs, 10);
} // }
TEST(Parser, CompoundAssignment) { // TEST(Parser, CompoundAssignment) {
parser parser("<TEMP>", "func main() { x += 10; }"); // parser parser("<TEMP>", "func main() { x += 10; }");
auto program = parser.parse(); // auto program = parser.parse();
EXPECT_TRUE(program.present()); // EXPECT_TRUE(program.present());
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::FuncDef); // 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);
EXPECT_EQ(funcDef->body()->statements[0]->statement_type(), statement_node_t::Expression); // EXPECT_EQ(funcDef->body()->statements[0]->statement_type(), statement_node_t::Expression);
expression_node_h expr = funcDef->body()->statements[0]; // expression_node_h expr = funcDef->body()->statements[0];
EXPECT_EQ(expr->expression_type(), expression_node_t::VarAssign); // EXPECT_EQ(expr->expression_type(), expression_node_t::VarAssign);
var_assign_expression_node_h assign = expr; // var_assign_expression_node_h assign = expr;
EXPECT_EQ(assign->compound(), binop_expression_node_t::Add); // EXPECT_EQ(assign->compound(), binop_expression_node_t::Add);
EXPECT_VARREAD(assign->lhs(), "x"sv); // EXPECT_VARREAD(assign->lhs(), "x"sv);
expression_node_h rhs = assign->rhs(); // expression_node_h rhs = assign->rhs();
EXPECT_EQ(rhs->expression_type(), expression_node_t::Literal); // EXPECT_EQ(rhs->expression_type(), expression_node_t::Literal);
EXPECT_INTLIT(rhs, 10); // EXPECT_INTLIT(rhs, 10);
} // }
} // namespace } // namespace