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
SortIncludes: true
ReflowComments: false
ReflowComments: Always
KeepEmptyLinesAtTheStartOfBlocks: true
DerivePointerAlignment: false
+13
View File
@@ -6,5 +6,18 @@ enable_testing()
find_package(GTest REQUIRED)
find_package(Doxygen)
add_subdirectory(furlang)
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 ast {
/**
* @brief Declaration node type.
*/
enum class declaration_node_t {
FunctionDeclaration,
FunctionDefinition,
Func, /**< Function declaration. */
FuncDef, /**< Function definition. */
};
/**
* @brief Declaration AST node interface.
*/
class declaration_node : public statement_node {
public:
/**
* @brief Returns this node's category.
*
* @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;
protected:
bool equal(const node& rhs) const override;
};
/**
* @brief Function declaration AST node.
*/
class function_declaration_node : public declaration_node {
public:
/**
* @brief Construct a new function declaration node object from name token.
*
* @param name Name of the function.
*/
function_declaration_node(front::token name)
: m_name(name) {}
: p_name(name) {}
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:
void accept(visitor& visitor) const override;
@@ -39,16 +78,38 @@ public:
protected:
bool equal(const node& rhs) const override;
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 {
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_declaration_node(name), m_body(std::move(body)) {}
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; }
public:
void accept(visitor& visitor) const override;
+206 -25
View File
@@ -7,34 +7,78 @@
namespace furc {
namespace ast {
/**
* @brief Expression node type.
*/
enum class expression_node_t {
Literal,
VarRead,
VarAssign,
Unaryop,
Binop,
Paren,
Literal, /**< Literal */
VarRead, /**< Variable read expression */
Unaryop, /**< Unary operation expression */
Binop, /**< Binary operation expression */
VarAssign, /**< Variable assignment expression */
};
/**
* @brief Expression AST node.
*/
class expression_node : public statement_node {
public:
/**
* @brief Returns this node's category.
*
* @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; }
/**
* @brief Returns this node's expression type.
*
* @return The expression type.
*/
virtual expression_node_t expression_type() const = 0;
protected:
bool equal(const node& rhs) const override;
};
/**
* @brief Var read expression AST node.
*/
class var_read_expression_node final : public expression_node {
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)
: 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; }
/**
* @brief Returns the variable's name.
*
* @return Name of the variable.
*/
handle<std::string_view>&& move_name() { return std::move(m_name); }
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; }
public:
void accept(visitor& visitor) const override;
@@ -46,27 +90,72 @@ private:
handle<std::string_view> m_name;
};
/**
* @brief Unary operation node type.
*/
enum class unaryop_expression_node_t {
Positive,
Negative,
PrefixIncrement,
PostfixIncrement,
PrefixDecrement,
PostfixDecrement,
Positive, /**< Positive (unary plus) */
Negative, /**< Negative (unary minus) */
PrefixIncrement, /**< Prefix increment */
PostfixIncrement, /**< Postfix increment */
PrefixDecrement, /**< Prefix decrement */
PostfixDecrement, /**< Postfix decrement */
};
/**
* @brief Unary operation expression AST node.
*/
class unaryop_expression_node final : public expression_node {
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)
: 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); }
/**
* @brief Returns the type of this node's operation.
*
* @return The operation 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; }
/**
* @brief Returns this node's inner expression.
*
* @return The inner expression.
*/
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); }
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; }
public:
void accept(visitor& visitor) const override;
@@ -76,36 +165,90 @@ protected:
bool equal(const node& rhs) const override;
private:
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 {
None = 0,
Add,
Sub,
Mul,
Div,
Mod,
None = 0, /**< None */
Add, /**< Addition */
Sub, /**< Subtraction */
Mul, /**< Multiplication */
Div, /**< Division */
Mod, /**< Modulo */
Equal,
NotEqual,
LessThan,
GreaterThan,
LessEqual,
GreaterEqual,
Equal, /**< Equality */
NotEqual, /**< Inequality */
LessThan, /**< Less */
GreaterThan, /**< Greater */
LessEqual, /**< Less or equal */
GreaterEqual, /**< Greater or equal */
};
/**
* @brief Binary operation expression AST node.
*/
class binop_expression_node final : public expression_node {
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)
: 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; };
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
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; };
/**
* @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); };
/**
* @brief Returns this node's right-hand-side expression.
*
* @return The right-hand-side expression.
*/
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; };
/**
* @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); };
public:
expression_node_t expression_type() const override { return expression_node_t::Binop; }
@@ -121,18 +264,56 @@ private:
expression_node_h m_rhs;
};
/**
* @brief Variable assignment expression AST node.
*/
class var_assign_expression_node final : public expression_node {
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)
: 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)
: 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; }
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
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; }
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; }
public:
void accept(visitor& visitor) const override;
+140 -2
View File
@@ -7,63 +7,201 @@
#include <vector>
namespace furc {
/**
* @brief Abstract Syntax Tree definitions.
*/
namespace ast {
class node;
/**
* @brief Alias for handle to node.
*
* @tparam T AST node type.
*/
template <typename T>
using node_handle = handle<T*, std::string>;
class literal_node;
/**
* @brief Alias for handle to literal_node.
* @see literal_node
*/
using literal_node_h = node_handle<literal_node>;
class expression_node;
/**
* @brief Alias for handle to expression_node.
* @see expression_node
*/
using expression_node_h = node_handle<expression_node>;
class declaration_node;
/**
* @brief Alias for handle to declaration_node.
* @see declaration_node
*/
using declaration_node_h = node_handle<declaration_node>;
class statement_node;
/**
* @brief Alias for handle to statement_node.
* @see statement_node
*/
using statement_node_h = node_handle<statement_node>;
class program_node;
/**
* @brief Alias for handle to program_node.
* @see program_node
*/
using program_node_h = node_handle<program_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>;
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>;
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>;
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>;
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>;
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>;
/**
* @brief List of statements.
*/
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;
/**
* @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 {
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); }
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>;
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>;
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>;
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>;
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>;
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>;
} // namespace ast
+65 -2
View File
@@ -8,29 +8,68 @@
namespace furc {
namespace ast {
/**
* @brief Literal node type.
*/
enum class literal_node_t {
String,
Integer
String, /**< String literal. */
Integer, /**< Integer literal. */
};
/**
* @brief Literal AST node.
*/
class literal_node : public expression_node {
public:
/**
* @brief Returns this node's category.
*
* @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; }
/**
* @brief Returns this node's literal type.
*
* @return The literal type.
*/
virtual literal_node_t literal_type() const = 0;
protected:
bool equal(const node& rhs) const override;
};
/**
* @brief String literal AST node.
*/
class string_literal_node final : public literal_node {
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)
: m_value(std::move(value)) {}
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; }
/**
* @brief Returns this node's value.
*
* @return A handle to the value.
*/
const handle<std::string_view>& value() const { return m_value; }
public:
void accept(visitor& visitor) const override;
@@ -42,15 +81,39 @@ private:
handle<std::string_view> m_value;
};
/**
* @brief Integer literal AST node.
*/
class integer_literal_node final : public literal_node {
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)
: m_value(std::move(value)) {}
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; }
/**
* @brief Returns this node's value.
*
* @return A handle to the 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; }
public:
void accept(visitor& visitor) const override;
+86 -7
View File
@@ -7,14 +7,24 @@
namespace furc {
namespace ast {
/**
* @brief Node category.
*/
enum class node_t {
Literal,
Expression,
Statement,
Declaration,
Program,
Literal, /**< Literal. */
Expression, /**< Expression. */
Statement, /**< Statement. */
Declaration, /**< Declaration. */
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) {
switch (type) {
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::Program: return os << "program";
}
return os;
}
/**
* @brief AST node interface.
*/
class node {
public:
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& 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;
public:
/**
* @brief Returns the category of this AST node.
* @see node_t
*
* @return The node category.
*/
virtual node_t category() const = 0;
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); }
/**
* @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); }
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;
/**
* @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;
/**
* @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); }
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;
};
+13
View File
@@ -9,14 +9,27 @@
namespace furc {
namespace ast {
/**
* @brief Program AST node.
*/
class program_node final : public node {
public:
program_node() = default;
node_t category() const override { return node_t::Program; }
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)); }
/**
* @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; }
public:
void accept(visitor& visitor) const override;
+100 -10
View File
@@ -6,32 +6,66 @@
namespace furc {
namespace ast {
/**
* @brief Statement node type.
*/
enum class statement_node_t {
Expression,
Declaration,
Return,
If,
Compound,
Expression, /**< Expression */
Declaration, /**< Declaration */
Return, /**< Return statement */
If, /**< If statement */
Compound, /**< Compound statement */
};
/**
* @brief Statement AST node.
*/
class statement_node : public node {
public:
/**
* @brief Returns this node's category.
*
* @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;
protected:
bool equal(const node& rhs) const override;
};
/**
* @brief Return statement AST node.
*/
class return_statement_node final : public statement_node {
public:
return_statement_node() = default;
/**
* @brief Construct a new return statement AST node.
*
* @param value Return value handle.
*/
return_statement_node(expression_node_h&& value)
: m_value(std::move(value)) {}
public:
/**
* @brief Returns this node's return value handle.
*
* @return The return value handle.
*/
expression_node_h value() const { return m_value; }
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; }
public:
void accept(visitor& visitor) const override;
@@ -40,21 +74,59 @@ public:
protected:
bool equal(const node& rhs) const override;
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 {
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)
: 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)
: m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
public:
/**
* @brief Returns this node's condition expression handle.
*
* @return The condition expression handle.
*/
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; }
/**
* @brief Returns this node's else statement handle.
*
* @return The else statement handle.
*/
const statement_node_h& elze() const { return m_else; }
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; }
public:
void accept(visitor& visitor) const override;
@@ -63,18 +135,36 @@ public:
protected:
bool equal(const node& rhs) const override;
private:
expression_node_h m_cond;
statement_node_h m_then;
statement_node_h m_else;
expression_node_h m_cond; /**< The condition expression handle */
statement_node_h m_then; /**< The then statement handle */
statement_node_h m_else; /**< The else statement handle */
};
/**
* @brief Compound statement AST node.
*/
class compound_statement_node final : public statement_node {
public:
/**
* @brief Construct a new compound statement AST node.
*
* @param body Body handle.
*/
compound_statement_node(body_h&& body)
: m_body(std::move(body)) {}
public:
/**
* @brief Returns this node's body handle.
*
* @return The body handle.
*/
const body_h& body() const { return m_body; }
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; }
public:
void accept(visitor& visitor) const override;
@@ -83,7 +173,7 @@ public:
protected:
bool equal(const node& rhs) const override;
private:
body_h m_body;
body_h m_body; /**< The body handle. */
};
} // namespace ast
+110 -11
View File
@@ -6,28 +6,127 @@
namespace furc {
namespace ast {
/**
* @brief Visitor pattern class for AST nodes.
*/
class visitor {
public:
visitor() = default;
virtual ~visitor() = default;
/**
* @brief Move constructor.
*/
visitor(visitor&&) = default;
/**
* @brief Move constructor.
*/
visitor& operator=(visitor&&) = default;
/**
* @brief Copy constructor.
*/
visitor(const visitor&) = default;
/**
* @brief Copy constructor.
*/
visitor& operator=(const visitor&) = default;
public:
virtual void visit(const string_literal_node&) {}
virtual void visit(const integer_literal_node&) {}
virtual void visit(const var_read_expression_node&) {}
virtual void visit(const unaryop_expression_node&) {}
virtual void visit(const binop_expression_node&) {}
virtual void visit(const var_assign_expression_node&) {}
virtual void visit(const function_declaration_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 string_literal_node.
* @see string_literal_node
*
* @param node Node.
*/
virtual void visit(const string_literal_node& 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) {}
};
+18 -3
View File
@@ -6,15 +6,30 @@
namespace furc {
/**
* @brief A location in file.
*/
struct location {
std::string_view filename;
std::size_t line = 0;
std::size_t column = 0;
std::string_view filename; /**< File's name */
std::size_t line = 0; /**< Line */
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 {
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); }
};
+31
View File
@@ -6,19 +6,50 @@
namespace furc {
namespace front {
/**
* @brief Lexer.
*
* Furlang's lazy tokenizer.
*/
class lexer {
public:
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() = default;
/**
* @brief Move constructor.
*/
lexer(lexer&&) = default;
lexer(const lexer&) = delete;
/**
* @brief Move constructor.
*/
lexer& operator=(lexer&&) = default;
lexer& operator=(const lexer&) = delete;
public:
/**
* @brief Returns a handle to next token.
*
* @return The token handle.
*/
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(); }
private:
void next();
+34 -2
View File
@@ -10,18 +10,50 @@
namespace furc {
namespace front {
class parser {
/**
* @brief Parser.
*
* Furlang's parser.
*/
class parser final {
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);
/**
* @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() = default;
/**
* @brief Move constructor.
*/
parser(parser&&) = default;
parser(const parser&) = delete;
/**
* @brief Move constructor.
*/
parser& operator=(parser&&) = default;
parser& operator=(const parser&) = delete;
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() &;
private:
ast::declaration_node_h parse_declaration();
+139 -45
View File
@@ -10,44 +10,48 @@
namespace furc {
namespace front {
/**
* @brief Token type.
*/
enum class token_t {
None,
Identifier,
String,
Keyword,
Integer,
LParen,
RParen,
LBrace,
RBrace,
LBracket,
RBracket,
Semicolon,
Colon,
Comma,
Dot,
None, /**< None */
Identifier, /**< Identifier */
String, /**< String */
Keyword, /**< Keyword */
Integer, /**< Integer */
Plus,
Minus,
Star,
Slash,
Percent,
DPlus,
DMinus,
LParen, /**< `(` */
RParen, /**< `)` */
LBrace, /**< `{` */
RBrace, /**< `}` */
LBracket, /**< `[` */
RBracket, /**< `]` */
Semicolon, /**< `;` */
Colon, /**< `:` */
Comma, /**< `,` */
Dot, /**< `.` */
Eq,
PlusEq,
MinusEq,
StarEq,
SlashEq,
PercentEq,
Plus, /**< `+` */
Minus, /**< `-` */
Star, /**< `*` */
Slash, /**< `/` */
Percent, /**< `%` */
DPlus, /**< `++` */
DMinus, /**< `--` */
DEq,
NotEq,
LessThan,
GreaterThan,
LessEq,
GreaterEq,
Eq, /**< `=` */
PlusEq, /**< `+=` */
MinusEq, /**< `-=` */
StarEq, /**< `*=` */
SlashEq, /**< `/=` */
PercentEq, /**< `%=` */
DEq, /**< `==` */
NotEq, /**< `!=` */
LessThan, /**< `<` */
GreaterThan, /**< `>` */
LessEq, /**< `<=` */
GreaterEq, /**< `>=` */
};
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;
}
/**
* @brief Keyword token.
*/
enum class keyword_token {
None,
Func,
Return,
If,
Else,
None, /**< None */
Func, /**< `func` */
Return, /**< `return` */
If, /**< `if` */
Else, /**< `else` */
};
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;
}
using integer_token = std::uint64_t;
using integer_token = std::uint64_t; /**< Integer token. */
/**
* @brief Token.
*/
struct token {
token_t type = token_t::None;
token_t type = token_t::None; /**< Token type. */
/**
* @brief Token's 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;
/**
* @brief String value. For token_t::Identifier and token_t::String.
* @see token_t::Identifier
* @see token_t::String
*/
std::string_view string;
/**
* @brief Keyword value. For token_t::Keyword.
* @see token_t::Keyword.
*/
keyword_token keyword;
/**
* @brief Integer value. For token_t::Integer.
* @see token_t::Integer
*/
integer_token integer;
/**
* @brief Construct a new null value.
*
* @param value Null value.
*/
value(std::nullptr_t value = nullptr)
: null(value) {}
/**
* @brief Construct a new string value.
*
* @param value The string.
*/
value(std::string_view value)
: string(value) {}
/**
* @brief Construct a new keyword value.
*
* @param value The keyword.
*/
value(keyword_token value)
: keyword(value) {}
/**
* @brief Construct a new integer value.
*
* @param value The integer.
*/
value(integer_token value)
: integer(value) {}
} value;
} value; /**< Token value. */
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) {}
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) {}
/**
* @brief Construct a new integer token.
*
* @param integer Integer value.
*/
token(integer_token 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; }
/**
* @brief Returns constant pointer to this token's value.
*
* @return Pointer to the token's 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 {
if (type != rhs.type) return false;
switch (type) {
@@ -220,7 +314,7 @@ static inline std::ostream& operator<<(std::ostream& os, const token& token) {
}
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 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 {
return os << "function " << m_name->string << " declaration";
return os << "function " << p_name->string << " declaration";
}
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 {
@@ -160,7 +160,7 @@ std::ostream& function_definition_node::print(std::ostream& os) const {
if (m_body.present()) {
for (const auto& entry : m_body->statements)
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
}
+1 -1
View File
@@ -94,7 +94,7 @@ token_handle<> lexer::next_token() {
{ "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 };
}
+11 -11
View File
@@ -22,7 +22,7 @@ TEST(Parser, EmptyFunctions) {
{
auto first = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 0);
@@ -30,7 +30,7 @@ TEST(Parser, EmptyFunctions) {
{
auto second = program->declarations()[1];
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;
EXPECT_EQ(funcDecl->name()->string, "foo");
}
@@ -47,7 +47,7 @@ TEST(Parser, Literals) {
{
auto test1 = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "test1");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -57,7 +57,7 @@ TEST(Parser, Literals) {
{
auto test2 = program->declarations()[1];
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;
EXPECT_EQ(funcDecl->name()->string, "test2");
}
@@ -87,7 +87,7 @@ TEST(Parser, OperatorPrecedence_AddMul) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -115,7 +115,7 @@ TEST(Parser, OperatorPrecedence_Complex) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -153,7 +153,7 @@ TEST(Parser, UnaryOperator_Simple) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -175,7 +175,7 @@ TEST(Parser, UnaryOperator_PrePost) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -200,7 +200,7 @@ TEST(Parser, Paren) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -226,7 +226,7 @@ TEST(Parser, Assignment) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
@@ -255,7 +255,7 @@ TEST(Parser, CompoundAssignment) {
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
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;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
+44
View File
@@ -8,6 +8,9 @@
namespace furlang {
/**
* @brief An arena (region) allocator implementation.
*/
class arena {
private:
struct region {
@@ -23,14 +26,35 @@ private:
std::size_t free() const { return capacity - used; }
};
public:
/**
* @brief Construct a new arena.
*
* @param minCapacity Minimal capacity of a single region in words.
*/
arena(std::size_t minCapacity = 4 * 1024ULL);
~arena();
/**
* @brief Move constructor
*/
arena(arena&& other) noexcept;
arena(const arena&) = delete;
/**
* @brief Move constructor
*/
arena& operator=(arena&& other) noexcept;
arena& operator=(const arena&) = delete;
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>>>
T* allocate(std::size_t count) {
T* allocated = reinterpret_cast<T*>(allocate(sizeof(T), count));
@@ -40,6 +64,13 @@ public:
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...>>>
T* allocate(Args&&... args) {
T* allocated = reinterpret_cast<T*>(allocate(sizeof(T), 1));
@@ -47,12 +78,25 @@ public:
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>
std::shared_ptr<T> allocate_shared(Args&&... args) {
T* allocated = allocate<T>(std::forward<Args>(args)...);
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();
private:
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 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 {
public:
using value_type = std::unique_ptr<instruction>;
using value_type = std::unique_ptr<instruction>; /**< Value type */
public:
block() = default;
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>>>
bool emplace(Args&&... args) {
if (has_exit()) return false;
@@ -29,11 +42,39 @@ public:
return true;
}
/**
* @brief Returns this block's instructions.
*
* @return The 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; }
/**
* @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; }
/**
* @brief Returns this block's exit instruction.
*
* @return The exit instruction.
*/
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; }
private:
std::vector<value_type> m_instructions;
+32 -1
View File
@@ -9,20 +9,51 @@
namespace furlang {
namespace ir {
/**
* @brief IR function.
*
* Consists of a name and blocks.
* @see block
*/
class function {
public:
using value_type = std::shared_ptr<block>;
using value_type = std::shared_ptr<block>; /**< Value type. */
public:
/**
* @brief Construct a new IR function.
*
* @param name Name to copy.
*/
function(const std::string& name)
: m_name(name) {}
/**
* @brief Construct a new IR function.
*
* @param name Name to move.
*/
function(std::string&& name)
: m_name(std::move(name)) {}
public:
/**
* @brief Returns this function's name.
*
* @return The 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>()); }
/**
* @brief Returns this function's IR blocks.
*
* @return The IR blocks.
*/
const std::vector<value_type>& blocks() const { return m_blocks; }
private:
std::string m_name;
+262 -28
View File
@@ -10,16 +10,25 @@
namespace furlang {
namespace ir {
/**
* @brief IR instruction type.
*/
enum class instruction_t {
Alloca,
Assign,
BinaryOp,
Call,
Branch,
BranchCond,
Return,
Alloca, /**< Unused */
Assign, /**< Assign */
BinaryOp, /**< Binary operation */
Call, /**< Call */
Branch, /**< Branch */
BranchCond, /**< Conditional branch */
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) {
switch (type) {
case instruction_t::Branch:
@@ -29,54 +38,137 @@ static inline bool is_exit_instruction(instruction_t type) {
}
}
/**
* @brief IR instruction
*/
class instruction {
public:
instruction() = default;
virtual ~instruction() = default;
/**
* @brief Move constructor
*/
instruction(instruction&&) = default;
/**
* @brief Move constructor
*/
instruction& operator=(instruction&&) = default;
instruction(const instruction&) = delete;
instruction& operator=(const instruction&) = delete;
public:
/**
* @brief Returns this instruction's type.
*
* @return The type.
*/
virtual instruction_t type() const = 0;
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); }
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;
};
/**
* @brief Alloca instruction
*/
class alloca_instruction final : public instruction {
public:
alloca_instruction() {}
~alloca_instruction() override = default;
/**
* @brief Move constructor
*/
alloca_instruction(alloca_instruction&&) = default;
/**
* @brief Move constructor
*/
alloca_instruction& operator=(alloca_instruction&&) = default;
alloca_instruction(const alloca_instruction&) = delete;
alloca_instruction& operator=(const alloca_instruction&) = delete;
public:
/**
* @brief Returns this instruction's type.
*
* @return instruction_t::Alloca.
*/
instruction_t type() const override { return instruction_t::Alloca; }
protected:
std::ostream& print(std::ostream& os) const override { return os << "alloca"; }
};
/**
* @brief Assign instruction
*/
class assign_instruction final : public instruction {
public:
/**
* @brief Construct a new assign instruction.
*
* @param src Source operand.
* @param dst Destination operand.
*/
assign_instruction(operand&& src, operand&& dst)
: m_source(std::move(src)), m_destination(std::move(dst)) {}
~assign_instruction() override = default;
/**
* @brief Move constructor
*/
assign_instruction(assign_instruction&&) = default;
/**
* @brief Move constructor
*/
assign_instruction& operator=(assign_instruction&&) = default;
assign_instruction(const assign_instruction&) = delete;
assign_instruction& operator=(const assign_instruction&) = delete;
public:
/**
* @brief Returns this instruction's type.
*
* @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; }
/**
* @brief Returns this instruction's destination.
*
* @return The destination.
*/
const operand& destination() const { return m_destination; }
private:
operand m_source;
@@ -87,19 +179,22 @@ protected:
}
};
/**
* @brief Binary operation instruction type
*/
enum class binary_op_instruction_t {
Add,
Sub,
Mul,
Div,
Mod,
Add, /**< Addition */
Sub, /**< Subtraction */
Mul, /**< Multiplication */
Div, /**< Division */
Mod, /**< Modulo */
Eq,
NotEq,
LessThan,
GreaterThan,
LessEq,
GreaterEq,
Eq, /**< Equal */
NotEq, /**< Not equal */
LessThan, /**< Less than */
GreaterThan, /**< Greater than */
LessEq, /**< Less or equal */
GreaterEq, /**< Greater or equal */
};
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;
}
/**
* @brief Binary operation instruction
*/
class binary_op_instruction final : public instruction {
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)
: m_type(type), m_lhs(std::move(lhs)), m_rhs(std::move(rhs)), m_dst(std::move(dst)) {}
~binary_op_instruction() override = default;
/**
* @brief Move constructor
*/
binary_op_instruction(binary_op_instruction&&) = default;
/**
* @brief Move constructor
*/
binary_op_instruction& operator=(binary_op_instruction&&) = default;
binary_op_instruction(const binary_op_instruction&) = delete;
binary_op_instruction& operator=(const binary_op_instruction&) = delete;
public:
/**
* @brief Returns this instruction's type.
*
* @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; }
/**
* @brief Returns this instruction's left-hand-side operand.
*
* @return The operand.
*/
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; }
/**
* @brief Returns this instruction's destination operand.
*
* @return The operand.
*/
const operand& dst() const { return m_dst; }
private:
binary_op_instruction_t m_type;
operand m_lhs;
operand m_rhs;
operand m_dst;
operand m_lhs /**< Left-hand-side operand */;
operand m_rhs /**< Right-hand-side operand */;
operand m_dst /**< Destination operand */;
protected:
std::ostream& print(std::ostream& os) const override {
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 {
public:
/**
* @brief Construct a new branch instruction.
*
* @param block Destination block index.
*/
branch_instruction(block_index block)
: m_block(block) {}
~branch_instruction() override = default;
/**
* @brief Move constructor.
*/
branch_instruction(branch_instruction&&) = default;
/**
* @brief Move constructor.
*/
branch_instruction& operator=(branch_instruction&&) = default;
branch_instruction(const branch_instruction&) = delete;
branch_instruction& operator=(const branch_instruction&) = delete;
public:
/**
* @brief Returns this instruction's type.
*
* @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; }
private:
block_index m_block;
block_index m_block; /**< Destination block index. */
protected:
std::ostream& print(std::ostream& os) const override { return os << "branch #" << m_block; }
};
/**
* @brief Conditional branch instruction
*/
class branch_cond_instruction final : public instruction {
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)
: m_condition(std::move(condition)), m_ifBlock(ifBlock), m_elseBlock(elseBlock) {}
~branch_cond_instruction() override = default;
/**
* @brief Move constructor.
*/
branch_cond_instruction(branch_cond_instruction&&) = default;
/**
* @brief Move constructor.
*/
branch_cond_instruction& operator=(branch_cond_instruction&&) = default;
branch_cond_instruction(const branch_cond_instruction&) = delete;
branch_cond_instruction& operator=(const branch_cond_instruction&) = delete;
public:
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; }
/**
* @brief Returns this instruction's destination block index.
*
* @return The destination block index.
*/
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; }
private:
operand m_condition;
block_index m_ifBlock;
block_index m_elseBlock;
operand m_condition /**< Condition operand. */;
block_index m_ifBlock /**< Destination block index. */;
block_index m_elseBlock /**< Else block index. */;
protected:
std::ostream& print(std::ostream& os) const override {
return os << "branch_cond " << m_condition << ", #" << m_ifBlock << ", #" << m_elseBlock;
}
};
/**
* @brief Return instruction
*/
class return_instruction final : public instruction {
public:
return_instruction() {}
return_instruction() = default;
/**
* @brief Construct a new return instruction.
*
* @param value Return value operand.
*/
return_instruction(operand&& value)
: m_value(std::move(value)) {}
~return_instruction() override = default;
/**
* @brief Move constructor.
*/
return_instruction(return_instruction&&) = default;
/**
* @brief Move constructor.
*/
return_instruction& operator=(return_instruction&&) = default;
return_instruction(const return_instruction&) = delete;
return_instruction& operator=(const return_instruction&) = delete;
public:
/**
* @brief Returns this instruction's type.
*
* @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; }
private:
std::optional<operand> m_value;
std::optional<operand> m_value; /**< The return value operand. */
protected:
std::ostream& print(std::ostream& os) const override {
os << "return";
+21 -1
View File
@@ -9,18 +9,38 @@
namespace furlang {
namespace ir {
/**
* @brief IR module
*/
class module {
public:
using value_type = std::unique_ptr<function>;
using value_type = std::unique_ptr<function>; /**< Value type. */
public:
module() = default;
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>
value_type& push(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; }
/**
* @brief Returns this module's functions.
*
* @return The functions.
*/
const std::vector<value_type>& functions() const { return m_functions; }
private:
std::vector<value_type> m_functions;
+80 -5
View File
@@ -8,19 +8,41 @@
namespace furlang {
namespace ir {
/**
* @brief Operand type
*/
enum class operand_t {
None,
Register,
Variable,
Integer,
String,
None, /**< None */
Register, /**< Register */
Variable, /**< Variable */
Integer, /**< Integer */
String, /**< String */
};
/**
* @brief Register operand alias.
* @see operand_t::Register
*/
using register_operand = std::uint32_t;
/**
* @brief Variable operand alias.
* @see operand_t::Variable
*/
using variable_operand = std::string;
/**
* @brief Integer operand alias.
* @see operand_t::Integer
*/
using integer_operand = std::uint64_t;
/**
* @brief String operand alias.
* @see operand_t::String
*/
using string_operand = std::string;
/**
* @brief IR operand
*/
class operand {
public:
~operand() {
@@ -29,6 +51,9 @@ public:
}
}
/**
* @brief Move constructor.
*/
operand(operand&& other) noexcept
: m_type(other.m_type) {
switch (m_type) {
@@ -49,6 +74,9 @@ public:
other.m_value.destroy(other.m_type);
}
/**
* @brief Move constructor.
*/
operand& operator=(operand&& other) noexcept {
if (this == &other) return *this;
m_type = other.m_type;
@@ -74,6 +102,12 @@ public:
operand(const operand&) = delete;
operand& operator=(const operand&) = delete;
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) {
operand operand;
operand.m_type = operand_t::Register;
@@ -81,6 +115,12 @@ public:
return operand;
}
/**
* @brief Construct a new variable operand.
*
* @param value Value of the new variable operand.
* @return The variable operand.
*/
template <typename T>
static operand new_variable(T&& value) {
operand operand;
@@ -89,6 +129,12 @@ public:
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) {
operand operand;
operand.m_type = operand_t::Integer;
@@ -96,6 +142,12 @@ public:
return operand;
}
/**
* @brief Construct a new string operand.
*
* @param value Value of the new string operand.
* @return The string operand.
*/
template <typename T>
static operand new_string(T&& value) {
operand operand;
@@ -104,11 +156,34 @@ public:
return operand;
}
public:
/**
* @brief Returns this operand's type.
*
* @return The operand 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; }
/**
* @brief Returns this operand's integer value.
*
* @return The integer value.
*/
integer_operand integer() const { return m_value.integer; }
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) {
switch (operand.m_type) {
case operand_t::None: return os << "none";
+172 -8
View File
@@ -6,45 +6,101 @@
namespace furlang {
/**
* @brief Bad result access exception.
*/
class bad_result_access : public std::exception {
public:
bad_result_access() = default;
~bad_result_access() override = default;
/**
* @brief Move constructor.
*/
bad_result_access(bad_result_access&&) noexcept = default;
/**
* @brief Move constructor.
*/
bad_result_access& operator=(bad_result_access&&) noexcept = default;
/**
* @brief Copy constructor.
*/
bad_result_access(const bad_result_access&) = default;
/**
* @brief Copy constructor.
*/
bad_result_access& operator=(const bad_result_access&) = default;
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"; }
};
/**
* @brief Result.
*
* Result stores either value or error.
*
* @tparam R Value type.
* @tparam E Error type.
*/
template <typename R, typename E>
class result {
public:
using value_type = std::remove_reference_t<R>;
using value_reference = value_type&;
using value_const_reference = const value_type&;
using value_pointer = value_type*;
using value_const_pointer = const value_type*;
using error_type = std::remove_reference_t<E>;
using error_reference = error_type&;
using error_const_reference = const error_type&;
using value_type = std::remove_reference_t<R>; /**< Value type. */
using value_reference = value_type&; /**< Value reference type. */
using value_const_reference = const value_type&; /**< Value const reference type. */
using value_pointer = value_type*; /**< Value pointer type. */
using value_const_pointer = const value_type*; /**< Value const pointer type. */
using error_type = std::remove_reference_t<E>; /**< Error type. */
using error_reference = error_type&; /**< Error reference type. */
using error_const_reference = const error_type&; /**< Error const reference type. */
public:
/**
* @brief Construct a new result.
*
* @param value Value to copy.
*/
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)); }
/**
* @brief Construct a new result.
*
* @param args Variadic arguments to construct the value with.
*/
template <typename... Args>
result(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)
: m_error(true) {
new (&m_value.error) error_type(error);
}
/**
* @brief Construct a new error result.
*
* @param error Error to move.
*/
explicit result(error_type&& error)
: m_error(true) {
new (&m_value.error) error_type(std::move(error));
@@ -58,6 +114,9 @@ public:
}
}
/**
* @brief Move constructor.
*/
result(result&& other) noexcept
: m_error(other.m_error) {
if (m_error) {
@@ -67,6 +126,9 @@ public:
}
}
/**
* @brief Move constructor.
*/
result& operator=(result&& other) noexcept {
if (this == &other) return *this;
m_error = other.m_error;
@@ -78,6 +140,9 @@ public:
return *this;
}
/**
* @brief Copy constructor.
*/
result(const result& other)
: m_error(other.m_error) {
if (m_error) {
@@ -87,6 +152,9 @@ public:
}
}
/**
* @brief Copy constructor.
*/
result& operator=(const result& other) {
if (this == &other) return *this;
m_error = other.m_error;
@@ -98,60 +166,156 @@ public:
return *this;
}
public:
/**
* @brief Checks if this result contains a value.
*
* @return true if contains a value.
*/
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; }
/**
* @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 {
return m_error == rhs.m_error && m_error ? m_value.error == rhs.m_value.error
: 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); }
/**
* @brief Returns a reference to value.
*
* @return Reference to the 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(); }
/**
* @brief Returns a pointer to value.
*
* @return Pointer to the 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(); }
public:
/**
* @brief Checks if this result has a value.
*
* @return true if has a value.
*/
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; }
/**
* @brief Returns a reference to value.
*
* @return Reference to the value.
*/
value_reference value() {
if (m_error) throw bad_result_access();
return m_value.result;
}
/**
* @brief Returns a reference to value.
*
* @return Const reference to the value.
*/
value_const_reference value() const {
if (m_error) throw bad_result_access();
return m_value.result;
}
/**
* @brief Returns a reference to error.
*
* @return Reference to the error.
*/
error_reference error() {
if (!m_error) throw bad_result_access();
return m_value.error;
}
/**
* @brief Returns a reference to error.
*
* @return Const reference to the error.
*/
error_const_reference error() const {
if (!m_error) throw bad_result_access();
return m_value.error;
}
public:
/**
* @brief Sets this results value.
*
* @param value Value to copy.
*/
void set_value(const value_type& value) {
if (m_error) m_value.error.~error_type();
new (&m_value.result) value_type(value);
}
/**
* @brief Sets this results value.
*
* @param value Value to move.
*/
void set_value(value_type&& value) {
if (m_error) m_value.error.~error_type();
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) {
if (!m_error) m_value.result.~value_type();
new (&m_value.error) error_type(error);
}
/**
* @brief Sets this results error.
*
* @param error Error to move.
*/
void set_error(error_type&& error) {
if (!m_error) m_value.result.~value_type();
new (&m_value.error) error_type(std::move(error));