Merge pull request #4 from CHatingPython/docs

Add doxygen documentation
This commit was merged in pull request #4.
This commit is contained in:
Aleksander Krajewski
2026-06-02 14:01:39 +02:00
committed by GitHub
25 changed files with 1772 additions and 243 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ BreakArrays: false
IncludeBlocks: Regroup IncludeBlocks: Regroup
SortIncludes: true SortIncludes: true
ReflowComments: false ReflowComments: Always
KeepEmptyLinesAtTheStartOfBlocks: true KeepEmptyLinesAtTheStartOfBlocks: true
DerivePointerAlignment: false DerivePointerAlignment: false
+13
View File
@@ -6,5 +6,18 @@ enable_testing()
find_package(GTest REQUIRED) find_package(GTest REQUIRED)
find_package(Doxygen)
add_subdirectory(furlang) add_subdirectory(furlang)
add_subdirectory(furc) add_subdirectory(furc)
if(DOXYGEN_FOUND)
set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/docs)
set(DOXYGEN_SHOW_ENUM_VALUES YES)
set(DOXYGEN_HIDE_UNDOC_NAMESPACES NO)
set(DOXYGEN_HIDE_UNDOC_RELATIONS NO)
set(DOXYGEN_FILE_PATTERNS "*.hpp")
doxygen_add_docs(doxygen_doc ${CMAKE_SOURCE_DIR}/furc/ ${CMAKE_SOURCE_DIR}/furlang/
ALL WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen")
endif()
+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) {}
}; };
+18 -3
View File
@@ -6,15 +6,30 @@
namespace furc { namespace furc {
/**
* @brief A location in file.
*/
struct location { struct location {
std::string_view filename; std::string_view filename; /**< File's name */
std::size_t line = 0; std::size_t line = 0; /**< Line */
std::size_t column = 0; std::size_t column = 0; /**< Column */
/**
* @brief Compare two locations for equality.
*
* @param rhs Location to compare against.
* @return true if the locations are equal.
*/
bool operator==(const location& rhs) const { bool operator==(const location& rhs) const {
return filename == rhs.filename && line == rhs.line && column == rhs.column; return filename == rhs.filename && line == rhs.line && column == rhs.column;
} }
/**
* @brief Compare two locations for inequality.
*
* @param rhs Location to compare against.
* @return true if the locations are not equal.
*/
bool operator!=(const location& rhs) const { return !this->operator==(rhs); } bool operator!=(const location& rhs) const { return !this->operator==(rhs); }
}; };
+31
View File
@@ -6,19 +6,50 @@
namespace furc { namespace furc {
namespace front { namespace front {
/**
* @brief Lexer.
*
* Furlang's lazy tokenizer.
*/
class lexer { class lexer {
public: public:
lexer() = default; lexer() = default;
/**
* @brief Construct a new lexer.
*
* @param filename Filename for debugging.
* @param content Content.
*/
lexer(std::string_view filename, std::string_view content); lexer(std::string_view filename, std::string_view content);
~lexer() = default; ~lexer() = default;
/**
* @brief Move constructor.
*/
lexer(lexer&&) = default; lexer(lexer&&) = default;
lexer(const lexer&) = delete; lexer(const lexer&) = delete;
/**
* @brief Move constructor.
*/
lexer& operator=(lexer&&) = default; lexer& operator=(lexer&&) = default;
lexer& operator=(const lexer&) = delete; lexer& operator=(const lexer&) = delete;
public: public:
/**
* @brief Returns a handle to next token.
*
* @return The token handle.
*/
token_handle<> next_token(); token_handle<> next_token();
/**
* @brief Checks whether the cursor is at the EOF.
*
* @return true if the cursor is at the EOF.
*/
bool empty() const { return m_cursor >= m_content.size(); } bool empty() const { return m_cursor >= m_content.size(); }
private: private:
void next(); void next();
+34 -2
View File
@@ -10,18 +10,50 @@
namespace furc { namespace furc {
namespace front { namespace front {
class parser { /**
* @brief Parser.
*
* Furlang's parser.
*/
class parser final {
public: public:
/**
* @brief Construct a new parser from content.
*
* @param filename Filename for debugging.
* @param content Content.
*/
parser(std::string_view filename, std::string_view content); parser(std::string_view filename, std::string_view content);
/**
* @brief Construct a new parser from file.
*
* Constructs a lexer with content read from file passed through \p filename.
*
* @param filename Name of the file.
*/
parser(std::string_view filename); parser(std::string_view filename);
~parser() = default; ~parser() = default;
/**
* @brief Move constructor.
*/
parser(parser&&) = default; parser(parser&&) = default;
parser(const parser&) = delete; parser(const parser&) = delete;
/**
* @brief Move constructor.
*/
parser& operator=(parser&&) = default; parser& operator=(parser&&) = default;
parser& operator=(const parser&) = delete; parser& operator=(const parser&) = delete;
public: public:
// parser owns the arena :3c /**
* @brief Returns a parsed program.
*
* @return Handle to an AST node of the program.
*/
ast::program_node_h parse() &; ast::program_node_h parse() &;
private: private:
ast::declaration_node_h parse_declaration(); ast::declaration_node_h parse_declaration();
+139 -45
View File
@@ -10,44 +10,48 @@
namespace furc { namespace furc {
namespace front { namespace front {
/**
* @brief Token type.
*/
enum class token_t { enum class token_t {
None, None, /**< None */
Identifier, Identifier, /**< Identifier */
String, String, /**< String */
Keyword, Keyword, /**< Keyword */
Integer, Integer, /**< Integer */
LParen,
RParen,
LBrace,
RBrace,
LBracket,
RBracket,
Semicolon,
Colon,
Comma,
Dot,
Plus, LParen, /**< `(` */
Minus, RParen, /**< `)` */
Star, LBrace, /**< `{` */
Slash, RBrace, /**< `}` */
Percent, LBracket, /**< `[` */
DPlus, RBracket, /**< `]` */
DMinus, Semicolon, /**< `;` */
Colon, /**< `:` */
Comma, /**< `,` */
Dot, /**< `.` */
Eq, Plus, /**< `+` */
PlusEq, Minus, /**< `-` */
MinusEq, Star, /**< `*` */
StarEq, Slash, /**< `/` */
SlashEq, Percent, /**< `%` */
PercentEq, DPlus, /**< `++` */
DMinus, /**< `--` */
DEq, Eq, /**< `=` */
NotEq, PlusEq, /**< `+=` */
LessThan, MinusEq, /**< `-=` */
GreaterThan, StarEq, /**< `*=` */
LessEq, SlashEq, /**< `/=` */
GreaterEq, PercentEq, /**< `%=` */
DEq, /**< `==` */
NotEq, /**< `!=` */
LessThan, /**< `<` */
GreaterThan, /**< `>` */
LessEq, /**< `<=` */
GreaterEq, /**< `>=` */
}; };
static inline std::ostream& operator<<(std::ostream& os, token_t type) { static inline std::ostream& operator<<(std::ostream& os, token_t type) {
@@ -130,12 +134,15 @@ static inline std::string operator+(const std::string& str, token_t type) {
return str; return str;
} }
/**
* @brief Keyword token.
*/
enum class keyword_token { enum class keyword_token {
None, None, /**< None */
Func, Func, /**< `func` */
Return, Return, /**< `return` */
If, If, /**< `if` */
Else, Else, /**< `else` */
}; };
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) { static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
@@ -160,43 +167,130 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
return str; return str;
} }
using integer_token = std::uint64_t; using integer_token = std::uint64_t; /**< Integer token. */
/**
* @brief Token.
*/
struct token { struct token {
token_t type = token_t::None; token_t type = token_t::None; /**< Token type. */
/**
* @brief Token's value.
*/
union value { union value {
/**
* @brief Null value. For token_t::None, token_t::Plus, token_t::Minus, etc.
* @see token_t::None
*/
std::nullptr_t null; std::nullptr_t null;
/**
* @brief String value. For token_t::Identifier and token_t::String.
* @see token_t::Identifier
* @see token_t::String
*/
std::string_view string; std::string_view string;
/**
* @brief Keyword value. For token_t::Keyword.
* @see token_t::Keyword.
*/
keyword_token keyword; keyword_token keyword;
/**
* @brief Integer value. For token_t::Integer.
* @see token_t::Integer
*/
integer_token integer; integer_token integer;
/**
* @brief Construct a new null value.
*
* @param value Null value.
*/
value(std::nullptr_t value = nullptr) value(std::nullptr_t value = nullptr)
: null(value) {} : null(value) {}
/**
* @brief Construct a new string value.
*
* @param value The string.
*/
value(std::string_view value) value(std::string_view value)
: string(value) {} : string(value) {}
/**
* @brief Construct a new keyword value.
*
* @param value The keyword.
*/
value(keyword_token value) value(keyword_token value)
: keyword(value) {} : keyword(value) {}
/**
* @brief Construct a new integer value.
*
* @param value The integer.
*/
value(integer_token value) value(integer_token value)
: integer(value) {} : integer(value) {}
} value; } value; /**< Token value. */
token() = default; token() = default;
token(token_t type, std::string_view value = {}) /**
* @brief Construct a new null token.
*
* @param type Token's type.
*/
token(token_t type)
: type(type) {}
/**
* @brief Construct a new string token.
*
* @param type Token's type.
* @param value String value.
*/
token(token_t type, std::string_view value)
: type(type), value(value) {} : type(type), value(value) {}
token(keyword_token keyword, std::string_view value = {}) /**
* @brief Construct a new keyword token.
*
* @param keyword Keyword value.
*/
token(keyword_token keyword)
: type(token_t::Keyword), value(keyword) {} : type(token_t::Keyword), value(keyword) {}
/**
* @brief Construct a new integer token.
*
* @param integer Integer value.
*/
token(integer_token integer) token(integer_token integer)
: type(token_t::Integer), value(integer) {} : type(token_t::Integer), value(integer) {}
/**
* @brief Returns pointer to this token's value.
*
* @return Pointer to the token's value.
*/
union value* operator->() { return &value; } union value* operator->() { return &value; }
/**
* @brief Returns constant pointer to this token's value.
*
* @return Pointer to the token's value.
*/
const union value* operator->() const { return &value; } const union value* operator->() const { return &value; }
/**
* @brief Compares two tokens for equality.
*
* @param rhs Token to compare against.
* @return true if the tokens are equal.
*/
bool operator==(const token& rhs) const { bool operator==(const token& rhs) const {
if (type != rhs.type) return false; if (type != rhs.type) return false;
switch (type) { switch (type) {
@@ -220,7 +314,7 @@ static inline std::ostream& operator<<(std::ostream& os, const token& token) {
} }
template <typename Error = std::string> template <typename Error = std::string>
using token_handle = handle<token, Error>; using token_handle = handle<token, Error>; /**< Alias to handle of token. */
} // namespace front } // namespace front
} // namespace furc } // namespace furc
+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
} }
+1 -1
View File
@@ -94,7 +94,7 @@ token_handle<> lexer::next_token() {
{ "else", keyword_token::Else }, { "else", keyword_token::Else },
}; };
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second, value }; if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second };
return { location, token_t::Identifier, value }; return { location, token_t::Identifier, value };
} }
+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);
+44
View File
@@ -8,6 +8,9 @@
namespace furlang { namespace furlang {
/**
* @brief An arena (region) allocator implementation.
*/
class arena { class arena {
private: private:
struct region { struct region {
@@ -23,14 +26,35 @@ private:
std::size_t free() const { return capacity - used; } std::size_t free() const { return capacity - used; }
}; };
public: public:
/**
* @brief Construct a new arena.
*
* @param minCapacity Minimal capacity of a single region in words.
*/
arena(std::size_t minCapacity = 4 * 1024ULL); arena(std::size_t minCapacity = 4 * 1024ULL);
~arena(); ~arena();
/**
* @brief Move constructor
*/
arena(arena&& other) noexcept; arena(arena&& other) noexcept;
arena(const arena&) = delete; arena(const arena&) = delete;
/**
* @brief Move constructor
*/
arena& operator=(arena&& other) noexcept; arena& operator=(arena&& other) noexcept;
arena& operator=(const arena&) = delete; arena& operator=(const arena&) = delete;
public: public:
/**
* @brief Allocates and default constructs objects.
*
* @tparam T Type of the objects.
* @param count How many objects to allocate.
* @return A pointer to the allocated objects.
*/
template <typename T, typename = std::enable_if_t<std::is_default_constructible_v<T>>> template <typename T, typename = std::enable_if_t<std::is_default_constructible_v<T>>>
T* allocate(std::size_t count) { T* allocate(std::size_t count) {
T* allocated = reinterpret_cast<T*>(allocate(sizeof(T), count)); T* allocated = reinterpret_cast<T*>(allocate(sizeof(T), count));
@@ -40,6 +64,13 @@ public:
return allocated; return allocated;
} }
/**
* @brief Allocates and constructs an object.
*
* @tparam T Type of the object.
* @param args Arguments passed to the object's constructor.
* @return A pointer to the allocated object.
*/
template <typename T, typename... Args, typename = std::enable_if_t<std::is_constructible_v<T, Args...>>> template <typename T, typename... Args, typename = std::enable_if_t<std::is_constructible_v<T, Args...>>>
T* allocate(Args&&... args) { T* allocate(Args&&... args) {
T* allocated = reinterpret_cast<T*>(allocate(sizeof(T), 1)); T* allocated = reinterpret_cast<T*>(allocate(sizeof(T), 1));
@@ -47,12 +78,25 @@ public:
return allocated; return allocated;
} }
/**
* @brief Allocates and constructs an object.
*
* @tparam T Type of the object.
* @param args Arguments passed to the object's constructor.
* @return A shared pointer to the allocated object.
*/
template <typename T, typename... Args> template <typename T, typename... Args>
std::shared_ptr<T> allocate_shared(Args&&... args) { std::shared_ptr<T> allocate_shared(Args&&... args) {
T* allocated = allocate<T>(std::forward<Args>(args)...); T* allocated = allocate<T>(std::forward<Args>(args)...);
return std::shared_ptr<T>(allocated, [](T* object) { object->~T(); }); return std::shared_ptr<T>(allocated, [](T* object) { object->~T(); });
} }
/**
* @brief Resets the arena.
*
* Resets occupied size of regions. Using previously allocated pointers after calling this function is
* undefined-behaviour.
*/
void reset(); void reset();
private: private:
void* allocate(std::size_t size, std::size_t count); void* allocate(std::size_t size, std::size_t count);
+11
View File
@@ -0,0 +1,11 @@
#ifndef FURLANG_HPP
#define FURLANG_HPP
#include "furlang/result.hpp" // IWYU pragma: export
/**
* @brief The common furlang library.
*/
namespace furlang {}
#endif // FURLANG_HPP
+43 -2
View File
@@ -10,13 +10,26 @@
namespace furlang { namespace furlang {
namespace ir { namespace ir {
// https://en.wikipedia.org/wiki/Basic_block /**
* @brief Basic block.
*
* A basic block of IR instructions. https://en.wikipedia.org/wiki/Basic_block
*/
class block { class block {
public: public:
using value_type = std::unique_ptr<instruction>; using value_type = std::unique_ptr<instruction>; /**< Value type */
public: public:
block() = default; block() = default;
public: public:
/**
* @brief Emplaces a new instruction.
*
* Emplaces a new instruction, if exit instruction hasn't been emplaced in this block yet.
*
* @tparam T Type of the instruction to emplace.
* @param args Arguments to call the constructor with.
* @return true if the instruction has been emplaced successfully.
*/
template <typename T, typename... Args, typename = std::enable_if_t<std::is_base_of_v<instruction, T>>> template <typename T, typename... Args, typename = std::enable_if_t<std::is_base_of_v<instruction, T>>>
bool emplace(Args&&... args) { bool emplace(Args&&... args) {
if (has_exit()) return false; if (has_exit()) return false;
@@ -29,11 +42,39 @@ public:
return true; return true;
} }
/**
* @brief Returns this block's instructions.
*
* @return The instructions.
*/
std::vector<value_type>& instructions() { return m_instructions; } std::vector<value_type>& instructions() { return m_instructions; }
/**
* @brief Returns this block's instructions.
*
* @return The instructions.
*/
const std::vector<value_type>& instructions() const { return m_instructions; } const std::vector<value_type>& instructions() const { return m_instructions; }
/**
* @brief Checks whether an exit instruction has been emplaced in this block yet.
*
* @return true if the exit instruction has been emplaced.
*/
bool has_exit() const { return m_exit != nullptr; } bool has_exit() const { return m_exit != nullptr; }
/**
* @brief Returns this block's exit instruction.
*
* @return The exit instruction.
*/
value_type& exit() { return m_exit; } value_type& exit() { return m_exit; }
/**
* @brief Returns this block's exit instruction.
*
* @return The exit instruction.
*/
const value_type& exit() const { return m_exit; } const value_type& exit() const { return m_exit; }
private: private:
std::vector<value_type> m_instructions; std::vector<value_type> m_instructions;
+32 -1
View File
@@ -9,20 +9,51 @@
namespace furlang { namespace furlang {
namespace ir { namespace ir {
/**
* @brief IR function.
*
* Consists of a name and blocks.
* @see block
*/
class function { class function {
public: public:
using value_type = std::shared_ptr<block>; using value_type = std::shared_ptr<block>; /**< Value type. */
public: public:
/**
* @brief Construct a new IR function.
*
* @param name Name to copy.
*/
function(const std::string& name) function(const std::string& name)
: m_name(name) {} : m_name(name) {}
/**
* @brief Construct a new IR function.
*
* @param name Name to move.
*/
function(std::string&& name) function(std::string&& name)
: m_name(std::move(name)) {} : m_name(std::move(name)) {}
public: public:
/**
* @brief Returns this function's name.
*
* @return The name.
*/
const std::string& name() const { return m_name; } const std::string& name() const { return m_name; }
/**
* @brief Pushes and returns a new IR block.
*
* @return The new IR block.
*/
value_type push() { return m_blocks.emplace_back(std::make_shared<block>()); } value_type push() { return m_blocks.emplace_back(std::make_shared<block>()); }
/**
* @brief Returns this function's IR blocks.
*
* @return The IR blocks.
*/
const std::vector<value_type>& blocks() const { return m_blocks; } const std::vector<value_type>& blocks() const { return m_blocks; }
private: private:
std::string m_name; std::string m_name;
+262 -28
View File
@@ -10,16 +10,25 @@
namespace furlang { namespace furlang {
namespace ir { namespace ir {
/**
* @brief IR instruction type.
*/
enum class instruction_t { enum class instruction_t {
Alloca, Alloca, /**< Unused */
Assign, Assign, /**< Assign */
BinaryOp, BinaryOp, /**< Binary operation */
Call, Call, /**< Call */
Branch, Branch, /**< Branch */
BranchCond, BranchCond, /**< Conditional branch */
Return, Return, /**< Return */
}; };
/**
* @brief Checks if an instruction type exits.
*
* @param type Instruction type.
* @return true if the instruction type exits.
*/
static inline bool is_exit_instruction(instruction_t type) { static inline bool is_exit_instruction(instruction_t type) {
switch (type) { switch (type) {
case instruction_t::Branch: case instruction_t::Branch:
@@ -29,54 +38,137 @@ static inline bool is_exit_instruction(instruction_t type) {
} }
} }
/**
* @brief IR instruction
*/
class instruction { class instruction {
public: public:
instruction() = default; instruction() = default;
virtual ~instruction() = default; virtual ~instruction() = default;
/**
* @brief Move constructor
*/
instruction(instruction&&) = default; instruction(instruction&&) = default;
/**
* @brief Move constructor
*/
instruction& operator=(instruction&&) = default; instruction& operator=(instruction&&) = default;
instruction(const instruction&) = delete; instruction(const instruction&) = delete;
instruction& operator=(const instruction&) = delete; instruction& operator=(const instruction&) = delete;
public: public:
/**
* @brief Returns this instruction's type.
*
* @return The type.
*/
virtual instruction_t type() const = 0; virtual instruction_t type() const = 0;
public: public:
/**
* @brief Prints an instruction to an output stream.
*
* Equivalent to calling instruction.print(os).
*
* @param os Output stream.
* @param instruction Instruction to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const instruction& instruction) { return instruction.print(os); } friend std::ostream& operator<<(std::ostream& os, const instruction& instruction) { return instruction.print(os); }
protected: protected:
/**
* @brief Prints this instruction 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 Alloca instruction
*/
class alloca_instruction final : public instruction { class alloca_instruction final : public instruction {
public: public:
alloca_instruction() {} alloca_instruction() {}
~alloca_instruction() override = default; ~alloca_instruction() override = default;
/**
* @brief Move constructor
*/
alloca_instruction(alloca_instruction&&) = default; alloca_instruction(alloca_instruction&&) = default;
/**
* @brief Move constructor
*/
alloca_instruction& operator=(alloca_instruction&&) = default; alloca_instruction& operator=(alloca_instruction&&) = default;
alloca_instruction(const alloca_instruction&) = delete; alloca_instruction(const alloca_instruction&) = delete;
alloca_instruction& operator=(const alloca_instruction&) = delete; alloca_instruction& operator=(const alloca_instruction&) = delete;
public: public:
/**
* @brief Returns this instruction's type.
*
* @return instruction_t::Alloca.
*/
instruction_t type() const override { return instruction_t::Alloca; } instruction_t type() const override { return instruction_t::Alloca; }
protected: protected:
std::ostream& print(std::ostream& os) const override { return os << "alloca"; } std::ostream& print(std::ostream& os) const override { return os << "alloca"; }
}; };
/**
* @brief Assign instruction
*/
class assign_instruction final : public instruction { class assign_instruction final : public instruction {
public: public:
/**
* @brief Construct a new assign instruction.
*
* @param src Source operand.
* @param dst Destination operand.
*/
assign_instruction(operand&& src, operand&& dst) assign_instruction(operand&& src, operand&& dst)
: m_source(std::move(src)), m_destination(std::move(dst)) {} : m_source(std::move(src)), m_destination(std::move(dst)) {}
~assign_instruction() override = default; ~assign_instruction() override = default;
/**
* @brief Move constructor
*/
assign_instruction(assign_instruction&&) = default; assign_instruction(assign_instruction&&) = default;
/**
* @brief Move constructor
*/
assign_instruction& operator=(assign_instruction&&) = default; assign_instruction& operator=(assign_instruction&&) = default;
assign_instruction(const assign_instruction&) = delete; assign_instruction(const assign_instruction&) = delete;
assign_instruction& operator=(const assign_instruction&) = delete; assign_instruction& operator=(const assign_instruction&) = delete;
public: public:
/**
* @brief Returns this instruction's type.
*
* @return instruction_t::Assign.
*/
instruction_t type() const override { return instruction_t::Assign; } instruction_t type() const override { return instruction_t::Assign; }
/**
* @brief Returns this instruction's source.
*
* @return The source.
*/
const operand& source() const { return m_source; } const operand& source() const { return m_source; }
/**
* @brief Returns this instruction's destination.
*
* @return The destination.
*/
const operand& destination() const { return m_destination; } const operand& destination() const { return m_destination; }
private: private:
operand m_source; operand m_source;
@@ -87,19 +179,22 @@ protected:
} }
}; };
/**
* @brief Binary operation instruction type
*/
enum class binary_op_instruction_t { enum class binary_op_instruction_t {
Add, Add, /**< Addition */
Sub, Sub, /**< Subtraction */
Mul, Mul, /**< Multiplication */
Div, Div, /**< Division */
Mod, Mod, /**< Modulo */
Eq, Eq, /**< Equal */
NotEq, NotEq, /**< Not equal */
LessThan, LessThan, /**< Less than */
GreaterThan, GreaterThan, /**< Greater than */
LessEq, LessEq, /**< Less or equal */
GreaterEq, GreaterEq, /**< Greater or equal */
}; };
static inline std::ostream& operator<<(std::ostream& os, binary_op_instruction_t type) { static inline std::ostream& operator<<(std::ostream& os, binary_op_instruction_t type) {
@@ -119,103 +214,242 @@ static inline std::ostream& operator<<(std::ostream& os, binary_op_instruction_t
return os; return os;
} }
/**
* @brief Binary operation instruction
*/
class binary_op_instruction final : public instruction { class binary_op_instruction final : public instruction {
public: public:
/**
* @brief Construct a new binary operation instruction.
*
* @param type Operation type.
* @param lhs Left-hand-side operand.
* @param rhs Right-hand-side operand.
* @param dst Destination operand.
*/
binary_op_instruction(binary_op_instruction_t type, operand&& lhs, operand&& rhs, operand&& dst) binary_op_instruction(binary_op_instruction_t type, operand&& lhs, operand&& rhs, operand&& dst)
: m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)), m_dst(std::move(dst)) {} : m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)), m_dst(std::move(dst)) {}
~binary_op_instruction() override = default; ~binary_op_instruction() override = default;
/**
* @brief Move constructor
*/
binary_op_instruction(binary_op_instruction&&) = default; binary_op_instruction(binary_op_instruction&&) = default;
/**
* @brief Move constructor
*/
binary_op_instruction& operator=(binary_op_instruction&&) = default; binary_op_instruction& operator=(binary_op_instruction&&) = default;
binary_op_instruction(const binary_op_instruction&) = delete; binary_op_instruction(const binary_op_instruction&) = delete;
binary_op_instruction& operator=(const binary_op_instruction&) = delete; binary_op_instruction& operator=(const binary_op_instruction&) = delete;
public: public:
/**
* @brief Returns this instruction's type.
*
* @return instruction_t::BinaryOp.
*/
instruction_t type() const override { return instruction_t::BinaryOp; } instruction_t type() const override { return instruction_t::BinaryOp; }
/**
* @brief Returns this instruction's operation type.
*
* @return The operation type.
*/
binary_op_instruction_t op_type() const { return m_type; } binary_op_instruction_t op_type() const { return m_type; }
/**
* @brief Returns this instruction's left-hand-side operand.
*
* @return The operand.
*/
const operand& lhs() const { return m_lhs; } const operand& lhs() const { return m_lhs; }
/**
* @brief Returns this instruction's right-hand-side operand.
*
* @return The operand.
*/
const operand& rhs() const { return m_rhs; } const operand& rhs() const { return m_rhs; }
/**
* @brief Returns this instruction's destination operand.
*
* @return The operand.
*/
const operand& dst() const { return m_dst; } const operand& dst() const { return m_dst; }
private: private:
binary_op_instruction_t m_type; binary_op_instruction_t m_type;
operand m_lhs; operand m_lhs /**< Left-hand-side operand */;
operand m_rhs; operand m_rhs /**< Right-hand-side operand */;
operand m_dst; operand m_dst /**< Destination operand */;
protected: protected:
std::ostream& print(std::ostream& os) const override { std::ostream& print(std::ostream& os) const override {
return os << "binop(" << m_type << ") " << m_lhs << ", " << m_rhs << ", " << m_dst; return os << "binop(" << m_type << ") " << m_lhs << ", " << m_rhs << ", " << m_dst;
} }
}; };
using block_index = std::uint64_t; using block_index = std::uint64_t; /**< IR block index alias */
/**
* @brief Branch instruction
*/
class branch_instruction final : public instruction { class branch_instruction final : public instruction {
public: public:
/**
* @brief Construct a new branch instruction.
*
* @param block Destination block index.
*/
branch_instruction(block_index block) branch_instruction(block_index block)
: m_block(block) {} : m_block(block) {}
~branch_instruction() override = default; ~branch_instruction() override = default;
/**
* @brief Move constructor.
*/
branch_instruction(branch_instruction&&) = default; branch_instruction(branch_instruction&&) = default;
/**
* @brief Move constructor.
*/
branch_instruction& operator=(branch_instruction&&) = default; branch_instruction& operator=(branch_instruction&&) = default;
branch_instruction(const branch_instruction&) = delete; branch_instruction(const branch_instruction&) = delete;
branch_instruction& operator=(const branch_instruction&) = delete; branch_instruction& operator=(const branch_instruction&) = delete;
public: public:
/**
* @brief Returns this instruction's type.
*
* @return instruction_t::Branch.
*/
instruction_t type() const override { return instruction_t::Branch; } instruction_t type() const override { return instruction_t::Branch; }
/**
* @brief Returns this instruction's destination block index.
*
* @return The destination block index.
*/
block_index block() const { return m_block; } block_index block() const { return m_block; }
private: private:
block_index m_block; block_index m_block; /**< Destination block index. */
protected: protected:
std::ostream& print(std::ostream& os) const override { return os << "branch #" << m_block; } std::ostream& print(std::ostream& os) const override { return os << "branch #" << m_block; }
}; };
/**
* @brief Conditional branch instruction
*/
class branch_cond_instruction final : public instruction { class branch_cond_instruction final : public instruction {
public: public:
/**
* @brief Construct a new conditional branch instruction.
*
* @param condition Condition operand.
* @param ifBlock Destination block index.
* @param elseBlock Else block index.
*/
branch_cond_instruction(operand&& condition, block_index ifBlock, block_index elseBlock) branch_cond_instruction(operand&& condition, block_index ifBlock, block_index elseBlock)
: m_condition(std::move(condition)), m_ifBlock(ifBlock), m_elseBlock(elseBlock) {} : m_condition(std::move(condition)), m_ifBlock(ifBlock), m_elseBlock(elseBlock) {}
~branch_cond_instruction() override = default; ~branch_cond_instruction() override = default;
/**
* @brief Move constructor.
*/
branch_cond_instruction(branch_cond_instruction&&) = default; branch_cond_instruction(branch_cond_instruction&&) = default;
/**
* @brief Move constructor.
*/
branch_cond_instruction& operator=(branch_cond_instruction&&) = default; branch_cond_instruction& operator=(branch_cond_instruction&&) = default;
branch_cond_instruction(const branch_cond_instruction&) = delete; branch_cond_instruction(const branch_cond_instruction&) = delete;
branch_cond_instruction& operator=(const branch_cond_instruction&) = delete; branch_cond_instruction& operator=(const branch_cond_instruction&) = delete;
public: public:
instruction_t type() const override { return instruction_t::BranchCond; } instruction_t type() const override { return instruction_t::BranchCond; }
/**
* @brief Returns this instruction's condition operand.
*
* @return The operand.
*/
const operand& condition() const { return m_condition; } const operand& condition() const { return m_condition; }
/**
* @brief Returns this instruction's destination block index.
*
* @return The destination block index.
*/
block_index if_block() const { return m_ifBlock; } block_index if_block() const { return m_ifBlock; }
/**
* @brief Returns this instruction's else block index.
*
* @return The else block index.
*/
block_index else_block() const { return m_elseBlock; } block_index else_block() const { return m_elseBlock; }
private: private:
operand m_condition; operand m_condition /**< Condition operand. */;
block_index m_ifBlock; block_index m_ifBlock /**< Destination block index. */;
block_index m_elseBlock; block_index m_elseBlock /**< Else block index. */;
protected: protected:
std::ostream& print(std::ostream& os) const override { std::ostream& print(std::ostream& os) const override {
return os << "branch_cond " << m_condition << ", #" << m_ifBlock << ", #" << m_elseBlock; return os << "branch_cond " << m_condition << ", #" << m_ifBlock << ", #" << m_elseBlock;
} }
}; };
/**
* @brief Return instruction
*/
class return_instruction final : public instruction { class return_instruction final : public instruction {
public: public:
return_instruction() {} return_instruction() = default;
/**
* @brief Construct a new return instruction.
*
* @param value Return value operand.
*/
return_instruction(operand&& value) return_instruction(operand&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
~return_instruction() override = default; ~return_instruction() override = default;
/**
* @brief Move constructor.
*/
return_instruction(return_instruction&&) = default; return_instruction(return_instruction&&) = default;
/**
* @brief Move constructor.
*/
return_instruction& operator=(return_instruction&&) = default; return_instruction& operator=(return_instruction&&) = default;
return_instruction(const return_instruction&) = delete; return_instruction(const return_instruction&) = delete;
return_instruction& operator=(const return_instruction&) = delete; return_instruction& operator=(const return_instruction&) = delete;
public: public:
/**
* @brief Returns this instruction's type.
*
* @return instruction_t::Return.
*/
instruction_t type() const override { return instruction_t::Return; } instruction_t type() const override { return instruction_t::Return; }
/**
* @brief Returns this instruction's return value operand.
*
* @return The operand.
*/
const std::optional<operand>& value() const { return m_value; } const std::optional<operand>& value() const { return m_value; }
private: private:
std::optional<operand> m_value; std::optional<operand> m_value; /**< The return value operand. */
protected: protected:
std::ostream& print(std::ostream& os) const override { std::ostream& print(std::ostream& os) const override {
os << "return"; os << "return";
+21 -1
View File
@@ -9,18 +9,38 @@
namespace furlang { namespace furlang {
namespace ir { namespace ir {
/**
* @brief IR module
*/
class module { class module {
public: public:
using value_type = std::unique_ptr<function>; using value_type = std::unique_ptr<function>; /**< Value type. */
public: public:
module() = default; module() = default;
public: public:
/**
* @brief Pushes and returns a new IR function.
*
* @param args Arguments to call the constructor with.
* @return The new IR function.
*/
template <typename... Args> template <typename... Args>
value_type& push(Args&&... args) { value_type& push(Args&&... args) {
return m_functions.emplace_back(std::forward<Args>(args)...); return m_functions.emplace_back(std::forward<Args>(args)...);
} }
/**
* @brief Returns this module's functions.
*
* @return The functions.
*/
std::vector<value_type>& functions() { return m_functions; } std::vector<value_type>& functions() { return m_functions; }
/**
* @brief Returns this module's functions.
*
* @return The functions.
*/
const std::vector<value_type>& functions() const { return m_functions; } const std::vector<value_type>& functions() const { return m_functions; }
private: private:
std::vector<value_type> m_functions; std::vector<value_type> m_functions;
+80 -5
View File
@@ -8,19 +8,41 @@
namespace furlang { namespace furlang {
namespace ir { namespace ir {
/**
* @brief Operand type
*/
enum class operand_t { enum class operand_t {
None, None, /**< None */
Register, Register, /**< Register */
Variable, Variable, /**< Variable */
Integer, Integer, /**< Integer */
String, String, /**< String */
}; };
/**
* @brief Register operand alias.
* @see operand_t::Register
*/
using register_operand = std::uint32_t; using register_operand = std::uint32_t;
/**
* @brief Variable operand alias.
* @see operand_t::Variable
*/
using variable_operand = std::string; using variable_operand = std::string;
/**
* @brief Integer operand alias.
* @see operand_t::Integer
*/
using integer_operand = std::uint64_t; using integer_operand = std::uint64_t;
/**
* @brief String operand alias.
* @see operand_t::String
*/
using string_operand = std::string; using string_operand = std::string;
/**
* @brief IR operand
*/
class operand { class operand {
public: public:
~operand() { ~operand() {
@@ -29,6 +51,9 @@ public:
} }
} }
/**
* @brief Move constructor.
*/
operand(operand&& other) noexcept operand(operand&& other) noexcept
: m_type(other.m_type) { : m_type(other.m_type) {
switch (m_type) { switch (m_type) {
@@ -49,6 +74,9 @@ public:
other.m_value.destroy(other.m_type); other.m_value.destroy(other.m_type);
} }
/**
* @brief Move constructor.
*/
operand& operator=(operand&& other) noexcept { operand& operator=(operand&& other) noexcept {
if (this == &other) return *this; if (this == &other) return *this;
m_type = other.m_type; m_type = other.m_type;
@@ -74,6 +102,12 @@ public:
operand(const operand&) = delete; operand(const operand&) = delete;
operand& operator=(const operand&) = delete; operand& operator=(const operand&) = delete;
public: public:
/**
* @brief Construct a new register operand.
*
* @param value Value of the new register operand.
* @return The register operand.
*/
static operand new_reg(register_operand value) { static operand new_reg(register_operand value) {
operand operand; operand operand;
operand.m_type = operand_t::Register; operand.m_type = operand_t::Register;
@@ -81,6 +115,12 @@ public:
return operand; return operand;
} }
/**
* @brief Construct a new variable operand.
*
* @param value Value of the new variable operand.
* @return The variable operand.
*/
template <typename T> template <typename T>
static operand new_variable(T&& value) { static operand new_variable(T&& value) {
operand operand; operand operand;
@@ -89,6 +129,12 @@ public:
return operand; return operand;
} }
/**
* @brief Construct a new integer operand.
*
* @param value Value of the new integer operand.
* @return The integer operand.
*/
static operand new_integer(integer_operand value) { static operand new_integer(integer_operand value) {
operand operand; operand operand;
operand.m_type = operand_t::Integer; operand.m_type = operand_t::Integer;
@@ -96,6 +142,12 @@ public:
return operand; return operand;
} }
/**
* @brief Construct a new string operand.
*
* @param value Value of the new string operand.
* @return The string operand.
*/
template <typename T> template <typename T>
static operand new_string(T&& value) { static operand new_string(T&& value) {
operand operand; operand operand;
@@ -104,11 +156,34 @@ public:
return operand; return operand;
} }
public: public:
/**
* @brief Returns this operand's type.
*
* @return The operand type.
*/
operand_t type() const { return m_type; } operand_t type() const { return m_type; }
/**
* @brief Returns this operand's register value.
*
* @return The register value.
*/
register_operand reg() const { return m_value.reg; } register_operand reg() const { return m_value.reg; }
/**
* @brief Returns this operand's integer value.
*
* @return The integer value.
*/
integer_operand integer() const { return m_value.integer; } integer_operand integer() const { return m_value.integer; }
public: public:
/**
* @brief Prints an operand to an output stream.
*
* @param os Output stream.
* @param operand Operand to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const operand& operand) { friend std::ostream& operator<<(std::ostream& os, const operand& operand) {
switch (operand.m_type) { switch (operand.m_type) {
case operand_t::None: return os << "none"; case operand_t::None: return os << "none";
+172 -8
View File
@@ -6,45 +6,101 @@
namespace furlang { namespace furlang {
/**
* @brief Bad result access exception.
*/
class bad_result_access : public std::exception { class bad_result_access : public std::exception {
public: public:
bad_result_access() = default; bad_result_access() = default;
~bad_result_access() override = default; ~bad_result_access() override = default;
/**
* @brief Move constructor.
*/
bad_result_access(bad_result_access&&) noexcept = default; bad_result_access(bad_result_access&&) noexcept = default;
/**
* @brief Move constructor.
*/
bad_result_access& operator=(bad_result_access&&) noexcept = default; bad_result_access& operator=(bad_result_access&&) noexcept = default;
/**
* @brief Copy constructor.
*/
bad_result_access(const bad_result_access&) = default; bad_result_access(const bad_result_access&) = default;
/**
* @brief Copy constructor.
*/
bad_result_access& operator=(const bad_result_access&) = default; bad_result_access& operator=(const bad_result_access&) = default;
public: public:
/**
* @brief Returns a C-style string describing the cause of the error.
*
* @return The cause of the error.
*/
const char* what() const noexcept override { return "bad result access"; } const char* what() const noexcept override { return "bad result access"; }
}; };
/**
* @brief Result.
*
* Result stores either value or error.
*
* @tparam R Value type.
* @tparam E Error type.
*/
template <typename R, typename E> template <typename R, typename E>
class result { class result {
public: public:
using value_type = std::remove_reference_t<R>; using value_type = std::remove_reference_t<R>; /**< Value type. */
using value_reference = value_type&; using value_reference = value_type&; /**< Value reference type. */
using value_const_reference = const value_type&; using value_const_reference = const value_type&; /**< Value const reference type. */
using value_pointer = value_type*; using value_pointer = value_type*; /**< Value pointer type. */
using value_const_pointer = const value_type*; using value_const_pointer = const value_type*; /**< Value const pointer type. */
using error_type = std::remove_reference_t<E>; using error_type = std::remove_reference_t<E>; /**< Error type. */
using error_reference = error_type&; using error_reference = error_type&; /**< Error reference type. */
using error_const_reference = const error_type&; using error_const_reference = const error_type&; /**< Error const reference type. */
public: public:
/**
* @brief Construct a new result.
*
* @param value Value to copy.
*/
result(const value_type& value) { new (&m_value.result) value_type(value); } result(const value_type& value) { new (&m_value.result) value_type(value); }
/**
* @brief Construct a new result.
*
* @param value Value to move.
*/
result(value_type&& value) { new (&m_value.result) value_type(std::move(value)); } result(value_type&& value) { new (&m_value.result) value_type(std::move(value)); }
/**
* @brief Construct a new result.
*
* @param args Variadic arguments to construct the value with.
*/
template <typename... Args> template <typename... Args>
result(Args&&... args) { result(Args&&... args) {
new (&m_value.result) value_type(std::forward<Args>(args)...); new (&m_value.result) value_type(std::forward<Args>(args)...);
} }
/**
* @brief Construct a new error result.
*
* @param error Error to copy.
*/
explicit result(const error_type& error) explicit result(const error_type& error)
: m_error(true) { : m_error(true) {
new (&m_value.error) error_type(error); new (&m_value.error) error_type(error);
} }
/**
* @brief Construct a new error result.
*
* @param error Error to move.
*/
explicit result(error_type&& error) explicit result(error_type&& error)
: m_error(true) { : m_error(true) {
new (&m_value.error) error_type(std::move(error)); new (&m_value.error) error_type(std::move(error));
@@ -58,6 +114,9 @@ public:
} }
} }
/**
* @brief Move constructor.
*/
result(result&& other) noexcept result(result&& other) noexcept
: m_error(other.m_error) { : m_error(other.m_error) {
if (m_error) { if (m_error) {
@@ -67,6 +126,9 @@ public:
} }
} }
/**
* @brief Move constructor.
*/
result& operator=(result&& other) noexcept { result& operator=(result&& other) noexcept {
if (this == &other) return *this; if (this == &other) return *this;
m_error = other.m_error; m_error = other.m_error;
@@ -78,6 +140,9 @@ public:
return *this; return *this;
} }
/**
* @brief Copy constructor.
*/
result(const result& other) result(const result& other)
: m_error(other.m_error) { : m_error(other.m_error) {
if (m_error) { if (m_error) {
@@ -87,6 +152,9 @@ public:
} }
} }
/**
* @brief Copy constructor.
*/
result& operator=(const result& other) { result& operator=(const result& other) {
if (this == &other) return *this; if (this == &other) return *this;
m_error = other.m_error; m_error = other.m_error;
@@ -98,60 +166,156 @@ public:
return *this; return *this;
} }
public: public:
/**
* @brief Checks if this result contains a value.
*
* @return true if contains a value.
*/
operator bool() const { return !m_error; } operator bool() const { return !m_error; }
/**
* @brief Checks if this result contains an error.
*
* @return true if contains an error.
*/
bool operator!() const { return m_error; } bool operator!() const { return m_error; }
/**
* @brief Compares two results for equality.
*
* @param rhs Result to compare against.
* @return true if the results are equal.
*/
bool operator==(const result& rhs) const { bool operator==(const result& rhs) const {
return m_error == rhs.m_error && m_error ? m_value.error == rhs.m_value.error return m_error == rhs.m_error && m_error ? m_value.error == rhs.m_value.error
: m_value.result == rhs.m_value.result; : m_value.result == rhs.m_value.result;
} }
/**
* @brief Compares two results for inequality.
*
* @param rhs Result to compare against.
* @return true if the results are not equal.
*/
bool operator!=(const result& rhs) const { return !this->operator==(rhs); } bool operator!=(const result& rhs) const { return !this->operator==(rhs); }
/**
* @brief Returns a reference to value.
*
* @return Reference to the value.
*/
value_reference operator*() { return value(); } value_reference operator*() { return value(); }
/**
* @brief Returns a reference to value.
*
* @return Const reference to the value.
*/
value_const_reference operator*() const { return value(); } value_const_reference operator*() const { return value(); }
/**
* @brief Returns a pointer to value.
*
* @return Pointer to the value.
*/
value_pointer operator->() { return &value(); } value_pointer operator->() { return &value(); }
/**
* @brief Returns a pointer to value.
*
* @return Const pointer to the value.
*/
value_const_pointer operator->() const { return &value(); } value_const_pointer operator->() const { return &value(); }
public: public:
/**
* @brief Checks if this result has a value.
*
* @return true if has a value.
*/
bool has_value() const { return !m_error; } bool has_value() const { return !m_error; }
/**
* @brief Checks if this result has an error.
*
* @return true if has an error.
*/
bool has_error() const { return m_error; } bool has_error() const { return m_error; }
/**
* @brief Returns a reference to value.
*
* @return Reference to the value.
*/
value_reference value() { value_reference value() {
if (m_error) throw bad_result_access(); if (m_error) throw bad_result_access();
return m_value.result; return m_value.result;
} }
/**
* @brief Returns a reference to value.
*
* @return Const reference to the value.
*/
value_const_reference value() const { value_const_reference value() const {
if (m_error) throw bad_result_access(); if (m_error) throw bad_result_access();
return m_value.result; return m_value.result;
} }
/**
* @brief Returns a reference to error.
*
* @return Reference to the error.
*/
error_reference error() { error_reference error() {
if (!m_error) throw bad_result_access(); if (!m_error) throw bad_result_access();
return m_value.error; return m_value.error;
} }
/**
* @brief Returns a reference to error.
*
* @return Const reference to the error.
*/
error_const_reference error() const { error_const_reference error() const {
if (!m_error) throw bad_result_access(); if (!m_error) throw bad_result_access();
return m_value.error; return m_value.error;
} }
public: public:
/**
* @brief Sets this results value.
*
* @param value Value to copy.
*/
void set_value(const value_type& value) { void set_value(const value_type& value) {
if (m_error) m_value.error.~error_type(); if (m_error) m_value.error.~error_type();
new (&m_value.result) value_type(value); new (&m_value.result) value_type(value);
} }
/**
* @brief Sets this results value.
*
* @param value Value to move.
*/
void set_value(value_type&& value) { void set_value(value_type&& value) {
if (m_error) m_value.error.~error_type(); if (m_error) m_value.error.~error_type();
new (&m_value.result) value_type(std::move(value)); new (&m_value.result) value_type(std::move(value));
} }
/**
* @brief Sets this results error.
*
* @param error Error to copy.
*/
void set_error(const error_type& error) { void set_error(const error_type& error) {
if (!m_error) m_value.result.~value_type(); if (!m_error) m_value.result.~value_type();
new (&m_value.error) error_type(error); new (&m_value.error) error_type(error);
} }
/**
* @brief Sets this results error.
*
* @param error Error to move.
*/
void set_error(error_type&& error) { void set_error(error_type&& error) {
if (!m_error) m_value.result.~value_type(); if (!m_error) m_value.result.~value_type();
new (&m_value.error) error_type(std::move(error)); new (&m_value.error) error_type(std::move(error));