refactor: remove furc for later remake

This commit is contained in:
2026-08-04 12:51:47 +02:00
parent 549af109b9
commit 224468446d
31 changed files with 95 additions and 4989 deletions
-248
View File
@@ -1,248 +0,0 @@
#ifndef FURC_AST_DECLARATION_HPP
#define FURC_AST_DECLARATION_HPP
#include "furc/ast/node.hpp"
#include "furc/ast/statement.hpp"
#include <optional>
#include <string>
#include <type_traits>
#include <vector>
namespace furc {
namespace ast {
/**
* @brief Type class for AST declarations.
*/
class type {
public:
template <typename NameFwd, typename = std::enable_if_t<std::is_constructible_v<std::string, NameFwd>>>
type(NameFwd&& name)
: m_name(std::forward<NameFwd>(name)) {}
public:
const std::string& name() const { return m_name; }
private:
std::string m_name;
};
enum class declaration_access_t {
Implicit = 0, /**< Implicit access. */
Public, /**< Public access. */
Private, /**< Private access. */
};
static inline bool same_access(declaration_access_t lhs, declaration_access_t rhs) {
return lhs == declaration_access_t::Implicit || lhs == rhs;
}
/**
* @brief Declaration node type.
*/
enum class declaration_node_t {
Func, /**< Function declaration. */
FuncDef, /**< Function definition. */
};
/**
* @brief Declaration AST node interface.
*/
class declaration_node : public statement_node, public abstract_node {
public:
/**
* @brief Construct a new declaration AST node.
*
* @param location Node location.
* @param access Declaration access.
*/
declaration_node(struct location location, declaration_access_t access)
: abstract_node(location), p_access(access) {}
public:
/**
* @brief Returns this node's category.
*
* @return node_t::Declaration.
*/
node_t category() const override { return 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;
/**
* @brief Returns the declaration's access.
*
* @return Access.
*/
declaration_access_t access() const { return p_access; }
protected:
bool equal(const node& rhs) const override;
protected:
declaration_access_t p_access;
};
/**
* @brief Parameter of function declaration AST node.
*/
struct function_declaration_param {
std::string name;
type type;
};
enum class function_declaration_node_t {
Normal = 0,
Import,
Native,
};
/**
* @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 location Node location.
* @param name Name of the function.
* @param type Return type of the function.
*/
template <typename T, typename ParamsFwd>
function_declaration_node(struct location location,
declaration_access_t access,
T&& name,
std::optional<type>&& returnType,
ParamsFwd&& params,
function_declaration_node_t type = function_declaration_node_t::Normal)
: declaration_node(location, access),
p_name(std::forward<T>(name)),
p_returnType(std::move(returnType)),
p_params(std::forward<ParamsFwd>(params)),
p_type(type) {}
public:
/**
* @brief Returns this node's declaration type.
*
* @return declaration_node_t::FunctionDeclaration.
*/
declaration_node_t declaration_type() const override { return declaration_node_t::Func; }
/**
* @brief Returns function's name.
*
* @return Name of the function.
*/
std::string name() const { return p_name; }
/**
* @brief Returns function's return type.
*
* @return Function's return type.
*/
const std::optional<type>& return_type() const { return p_returnType; }
/**
* @brief Returns function's parameters.
*
* @return Function's parameters.
*/
const std::vector<function_declaration_param>& params() const { return p_params; }
/**
* @brief Returns function's type.
*
* @return Function's type.
*/
function_declaration_node_t type() const { return p_type; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
protected:
/**
* @brief Name of the function.
*/
std::string p_name;
/**
* @brief Return type of the function.
*/
std::optional<class type> p_returnType;
/**
* @brief Parameters of the function.
*/
std::vector<function_declaration_param> p_params;
/**
* @brief Type of the function declaration.
*/
function_declaration_node_t p_type;
};
/**
* @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 location Node location.
* @param name Name of the function.
* @param type Return type of the function.
* @param body Body of the function.
*/
template <typename T, typename ParamsFwd>
function_definition_node(struct location location,
declaration_access_t access,
T&& name,
std::optional<class type>&& type,
ParamsFwd&& params,
body&& body)
: function_declaration_node(location,
access,
std::forward<T>(name),
std::move(type),
std::forward<ParamsFwd>(params)),
m_body(std::move(body)) {}
public:
/**
* @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& body() const { return m_body; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
struct body m_body;
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_DECLARATION_HPP
-409
View File
@@ -1,409 +0,0 @@
#ifndef FURC_AST_EXPRESSION_HPP
#define FURC_AST_EXPRESSION_HPP
#include "furc/ast/node.hpp"
#include "furc/ast/statement.hpp"
#include <vector>
namespace furc {
namespace ast {
/**
* @brief Expression node type.
*/
enum class expression_node_t {
Literal, /**< Literal */
VarRead, /**< Variable read expression */
Unaryop, /**< Unary operation expression */
Binop, /**< Binary operation expression */
VarAssign, /**< Variable assignment expression */
FuncCall, /**< Function call expression. */
};
/**
* @brief Expression AST node.
*/
class expression_node : public statement_node, public abstract_node {
public:
/**
* @brief Construct a new expression AST node.
*
* @param location Node location.
*/
expression_node(struct location location)
: abstract_node(location) {}
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 location Node location.
* @param name Handle to the name.
*/
template <typename T>
var_read_expression_node(struct location location, T&& name)
: expression_node(location), m_name(std::forward<T>(name)) {}
/**
* @brief Returns the variable's name.
*
* @return Name of the variable.
*/
const std::string& get_name() const { return m_name; }
/**
* @brief Returns the variable's name.
*
* @return Name of the variable.
*/
std::string&& 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;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
std::string m_name;
};
/**
* @brief Unary operation node type.
*/
enum class unaryop_expression_node_t {
Positive, /**< Positive (unary plus) */
Negative, /**< Negative (unary minus) */
PrefixIncrement, /**< Prefix increment */
PostfixIncrement, /**< Postfix increment */
PrefixDecrement, /**< Prefix decrement */
PostfixDecrement, /**< Postfix decrement */
Pointerof, /**< Pointerof */
Sizeof, /**< Sizeof */
};
/**
* @brief Unary operation expression AST node.
*/
class unary_op_expression_node final : public expression_node {
public:
/**
* @brief Construct a new unaryop expression node object from type and expression node handle.
*
* @param location Node location.
* @param type Operation type.
* @param node Handle to the inner expression node.
*/
unary_op_expression_node(struct location location, unaryop_expression_node_t type, expression_node_p&& node)
: expression_node(location), m_type(type), m_node(std::move(node)) {}
/**
* @brief Sets this node's inner expression.
*
* @param node New node handle.
*/
void set_node(expression_node_p&& 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_p& get_node() const { return m_node; }
/**
* @brief Returns this node's inner expression.
*
* @return The inner expression.
*/
expression_node_p& get_node() { return m_node; }
/**
* @brief Moves this node's inner expression.
*
* @return The moved inner expression.
*/
expression_node_p&& 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;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
unaryop_expression_node_t m_type;
expression_node_p m_node; /**< The inner expression. */
};
/**
* @brief Binary operation expression node type.
*/
enum class binop_expression_node_t {
None = 0, /**< None */
Add, /**< Addition */
Sub, /**< Subtraction */
Mul, /**< Multiplication */
Div, /**< Division */
Mod, /**< Modulo */
Equal, /**< Equality */
NotEqual, /**< Inequality */
LessThan, /**< Less */
GreaterThan, /**< Greater */
LessEqual, /**< Less or equal */
GreaterEqual, /**< Greater or equal */
};
/**
* @brief Binary operation expression AST node.
*/
class binary_op_expression_node final : public expression_node {
public:
/**
* @brief Construct a new binary operation expression AST node.
*
* @param location Node location.
* @param type Binary operation type.
* @param lhs Left-hand-side expression.
* @param rhs Right-hand-side expression.
*/
binary_op_expression_node(struct location location,
binop_expression_node_t type,
expression_node_p&& lhs,
expression_node_p&& rhs)
: expression_node(location), 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_p& lhs() const { return m_lhs; };
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
expression_node_p& lhs() { return m_lhs; };
/**
* @brief Moves this node's left-hand-side expression.
*
* @return The moved left-hand-side expression.
*/
expression_node_p&& 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_p& rhs() const { return m_rhs; };
/**
* @brief Returns this node's right-hand-side expression.
*
* @return The right-hand-side expression.
*/
expression_node_p& rhs() { return m_rhs; };
/**
* @brief Moves this node's right-hand-side expression.
*
* @return The moved right-hand-side expression.
*/
expression_node_p&& move_rhs() { return std::move(m_rhs); };
public:
expression_node_t expression_type() const override { return expression_node_t::Binop; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
binop_expression_node_t m_type;
expression_node_p m_lhs;
expression_node_p 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 location Node location.
* @param lhs Left-hand-side expression handle.
* @param rhs Right-hand-side expression handle.
*/
var_assign_expression_node(struct location location, expression_node_p&& lhs, expression_node_p&& rhs)
: expression_node(location),
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 location Node location.
* @param compound Compound operation type.
* @param lhs Left-hand-side expression handle.
* @param rhs Right-hand-side expression handle.
*/
var_assign_expression_node(struct location location,
binop_expression_node_t compound,
expression_node_p&& lhs,
expression_node_p&& rhs)
: expression_node(location), 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_p& lhs() const { return m_lhs; }
/**
* @brief Returns this node's right-hand-side expression.
*
* @return The right-hand-side expression.
*/
const expression_node_p& 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;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
binop_expression_node_t m_compound;
expression_node_p m_lhs;
expression_node_p m_rhs;
};
/**
* @brief Function call expression AST node.
*/
class function_call_expression_node final : public expression_node {
public:
/**
* @brief Construct a new function call expression AST node.
*
* @param location Node location.
* @param func Left-hand-side expression.
* @param args Function arguments.
*/
function_call_expression_node(struct location location,
expression_node_p&& func,
std::vector<expression_node_p>&& args)
: expression_node(location), m_func(std::move(func)), m_args(std::move(args)) {}
/**
* @brief Returns this node's left-hand-side expression.
*
* @return The left-hand-side expression.
*/
const expression_node_p& func() const { return m_func; }
/**
* @brief Returns this node's argument expressions.
*
* @return The argument expressions.
*/
const std::vector<expression_node_p>& args() const { return m_args; }
public:
/**
* @brief Returns this node's expression type.
*
* @return expression_node_t::FuncCall.
*/
expression_node_t expression_type() const override { return expression_node_t::FuncCall; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
expression_node_p m_func;
std::vector<expression_node_p> m_args;
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_EXPRESSION_HPP
-259
View File
@@ -1,259 +0,0 @@
#ifndef FURC_AST_FWD_HPP
#define FURC_AST_FWD_HPP
#include "furc/diag.hpp"
#include "furlang/result.hpp"
#include <cstdint>
#include <memory>
#include <vector>
namespace furc {
/**
* @brief Abstract Syntax Tree definitions.
*/
namespace ast {
/**
* @brief AST error.
*/
struct error {
location location; /**< Location of the error. */
/**
* @brief Compares two AST errors for equality.
*
* @param other Error to compare against.
* @return true if the errors are equal.
*/
bool operator==(const error& other) const { return location == other.location; }
/**
* @brief Compares two AST errors for inequality.
*
* @param other Error to compare against.
* @return true if the errors are not equal.
*/
bool operator!=(const error& other) const { return !this->operator==(other); }
/**
* @brief Prints an AST error to output stream.
*
* @param os Output stream.
* @param error AST error to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const error& error);
};
class node;
/**
* @brief Alias for a shared pointer to node.
*
* @tparam T AST node type.
*/
template <typename T = node>
using node_p = std::shared_ptr<T>;
/**
* @brief Alias for node result.
*
* @tparam T AST node type.
*/
template <typename T = node>
using node_r = furlang::result<node_p<T>, error>;
class expression_node;
using expression_node_p = node_p<expression_node>; /**< Alias for a shared pointer to expression_node. */
using expression_node_r = node_r<expression_node>; /**< Alias for expression_node result */
class type;
using type_r = furlang::result<type, error>; /**< Alias for AST type result */
class declaration_node;
using declaration_node_p = node_p<declaration_node>; /**< Alias for a shared pointer to declaration_node. */
using declaration_node_r = node_r<declaration_node>; /**< Alias for declaration_node result */
class statement_node;
using statement_node_p = node_p<statement_node>; /**< Alias for a shared pointer to statement_node. */
using statement_node_r = node_r<statement_node>; /**< Alias for statement_node result */
class program_node;
using program_node_p = node_p<program_node>; /**< Alias for a shared pointer to program_node. */
using program_node_r = node_r<program_node>; /**< Alias for program_node result */
/**
* @brief Literal node type.
*/
enum class literal_node_t {
String, /**< String literal. */
Integer, /**< Integer literal. */
};
template <typename, literal_node_t>
class literal_node;
/**
* @brief String literal AST node.
*/
using string_literal_node = literal_node<std::string, literal_node_t::String>;
using string_literal_node_p = node_p<string_literal_node>; /**< Alias for a shared pointer to string_literal_node */
using string_literal_node_r = node_r<string_literal_node>; /**< Alias for string_literal_node result */
/**
* @brief Integer literal AST node.
*/
using integer_literal_node = literal_node<std::uint64_t, literal_node_t::Integer>;
using integer_literal_node_p = node_p<integer_literal_node>; /**< Alias for a shared pointer to integer_literal_node */
using integer_literal_node_r = node_r<integer_literal_node>; /**< Alias for integer_literal_node result */
class var_read_expression_node;
using var_read_expression_node_p =
node_p<var_read_expression_node>; /**< Alias for a shared pointer to var_read_expression_node. */
using var_read_expression_node_r = node_r<var_read_expression_node>; /**< Alias for var_read_expression_node result */
class unary_op_expression_node;
using unary_op_expression_node_p =
node_p<unary_op_expression_node>; /**< Alias for a shared pointer to unaryop_expression_node. */
using unary_op_expression_node_r = node_r<unary_op_expression_node>; /**< Alias for unaryop_expression_node result */
class binary_op_expression_node;
using binary_op_expression_node_p =
node_p<binary_op_expression_node>; /**< Alias for a shared pointer to binop_expression_node. */
using binary_op_expression_node_r = node_r<binary_op_expression_node>; /**< Alias for binop_expression_node result */
class var_assign_expression_node;
using var_assign_expression_node_p =
node_p<var_assign_expression_node>; /**< Alias for a shared pointer to var_assign_expression_node. */
using var_assign_expression_node_r =
node_r<var_assign_expression_node>; /**< Alias for var_assign_expression_node result */
class function_call_expression_node;
using function_call_expression_node_p =
node_p<function_call_expression_node>; /**< Alias for a shared pointer to function_call_expression_node. */
using function_call_expression_node_r =
node_r<function_call_expression_node>; /**< Alias for function_call_expression_node result. */
/**
* @brief List of statements.
*/
struct body {
/**
* @brief Location of the opening curly.
*/
location begin;
/**
* @brief Location of the closing curly.
*/
location end;
/**
* @brief List of statements.
*/
std::vector<statement_node_r> 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); }
/**
* @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 body result.
* @see body
*/
using body_r = furlang::result<body, error>;
class function_declaration_node;
using function_declaration_node_p =
node_p<function_declaration_node>; /**< Alias for a shared pointer to function_declaration_node. */
using function_declaration_node_r =
node_r<function_declaration_node>; /**< Alias for function_declaration_node result */
class function_definition_node;
using function_definition_node_p =
node_p<function_definition_node>; /**< Alias for a shared pointer to function_definition_node. */
using function_definition_node_r = node_r<function_definition_node>; /**< Alias for function_definition_node result */
class return_statement_node;
using return_statement_node_p =
node_p<return_statement_node>; /**< Alias for a shared pointer to return_statement_node. */
using return_statement_node_r = node_r<return_statement_node>; /**< Alias for return_statement_node result */
class if_statement_node;
using if_statement_node_p = node_p<if_statement_node>; /**< Alias for a shared pointer to if_statement_node. */
using if_statement_node_r = node_r<if_statement_node>; /**< Alias for if_statement_node result */
class compound_statement_node;
using compound_statement_node_p =
node_p<compound_statement_node>; /**< Alias for a shared pointer to compound_statement_node. */
using compound_statement_node_r = node_r<compound_statement_node>; /**< Alias for compound_statement_node result */
class while_statement_node;
using while_statement_node_p = node_p<while_statement_node>; /**< Alias for a shared pointer to while_statement_node. */
using while_statement_node_r = node_r<while_statement_node>; /**< Alias for while_statement_node result */
} // namespace ast
} // namespace furc
#endif // FURC_AST_FWD_HPP
-102
View File
@@ -1,102 +0,0 @@
// NOLINTBEGIN(portability-template-virtual-member-function)
#ifndef FURC_AST_LITERAL_HPP
#define FURC_AST_LITERAL_HPP
#include "furc/ast/expression.hpp"
#include "furc/ast/node.hpp"
namespace furc {
namespace ast {
/**
* @brief Literal AST node.
*/
template <typename ValueType, literal_node_t LiteralType>
class literal_node : public expression_node {
public:
using value_type = std::remove_reference_t<ValueType>; /**< Value type. */
public:
/**
* @brief Construct a new literal AST node.
*
* @param location Node location.
*/
template <typename = std::enable_if_t<std::is_default_constructible_v<ValueType>>>
literal_node(struct location location)
: expression_node(location) {}
/**
* @brief Construct a new literal AST node.
*
* @param location Node location.
* @param value Node value to copy.
*/
literal_node(struct location location, const value_type& value)
: expression_node(location), p_value(value) {}
/**
* @brief Construct a new literal AST node.
*
* @param location Node location.
* @param value Node value to move.
*/
literal_node(struct location location, value_type&& value)
: expression_node(location), p_value(std::move(value)) {}
/**
* @brief Construct a new literal AST node.
*
* @param location Node location.
* @param args Arguments to call value constructor with.
*/
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<ValueType, Args...>>>
literal_node(struct location location, Args&&... args)
: expression_node(location), p_value(std::forward<Args>(args)...) {}
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.
*/
literal_node_t literal_type() const { return LiteralType; }
/**
* @brief Returns this node's value.
*
* @return A string view result.
*/
const value_type& value() const { return p_value; }
public:
void accept(visitor& visitor) const override { visitor.visit(*this); }
std::ostream& print(std::ostream& os) const override { return os << p_value; }
protected:
bool equal(const node& rhsNode) const override {
const auto& rhs = dynamic_cast<const literal_node&>(rhsNode);
return literal_type() == rhs.literal_type() && p_value == rhs.p_value;
}
protected:
value_type p_value; /**< Node value. */
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_LITERAL_HPP
// NOLINTEND(portability-template-virtual-member-function)
-166
View File
@@ -1,166 +0,0 @@
#ifndef FURC_AST_NODE_HPP
#define FURC_AST_NODE_HPP
#include "furc/ast/fwd.hpp"
#include "furc/ast/visitor.hpp"
namespace furc {
namespace ast {
/**
* @brief Node category.
*/
enum class node_t {
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";
case node_t::Expression: return os << "expression";
case node_t::Statement: return os << "statement";
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;
/**
* @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;
/**
* @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;
/**
* @brief Returns the location of this AST node.
* @see locaiton
*
* @return The location.
*/
virtual location location() 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 << node.location() << ": ");
}
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;
};
/**
* @brief An abstract AST node.
* @see node
*
* Implements location().
*/
class abstract_node : public virtual node {
public:
abstract_node(struct location location)
: p_location(location) {}
public:
/**
* @brief Returns the location of this AST node.
* @see locaiton
*
* @return The location.
*/
struct location location() const override { return p_location; }
protected:
struct location p_location; /**< Node location. */
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_NODE_HPP
-52
View File
@@ -1,52 +0,0 @@
#ifndef FURC_AST_PROGRAM_HPP
#define FURC_AST_PROGRAM_HPP
#include "furc/ast/node.hpp"
#include <vector>
namespace furc {
namespace ast {
/**
* @brief Program AST node.
*/
class program_node final : public abstract_node {
public:
/**
* @brief Construct a new program AST node.
*
* @param location Node location.
*/
program_node(struct location location)
: abstract_node(location) {}
node_t category() const override { return node_t::Program; }
public:
/**
* @brief Adds a declaration to this program.
*
* @param declaration Declaration to add.
*/
void push(declaration_node_p&& 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<declaration_node_p>& declarations() const { return m_declarations; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
std::vector<declaration_node_p> m_declarations;
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_PROGRAM_HPP
-244
View File
@@ -1,244 +0,0 @@
#ifndef FURC_AST_STATEMENT_HPP
#define FURC_AST_STATEMENT_HPP
#include "furc/ast/fwd.hpp"
#include "furc/ast/node.hpp"
#include <optional>
namespace furc {
namespace ast {
/**
* @brief Statement node type.
*/
enum class statement_node_t {
Expression, /**< Expression */
Declaration, /**< Declaration */
Return, /**< Return statement */
If, /**< If statement */
Compound, /**< Compound statement */
While, /**< While loop statement. */
};
/**
* @brief Statement AST node.
*/
class statement_node : public virtual 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 abstract_node {
public:
using value_type = std::optional<expression_node_p>; /**< Value type. */
public:
/**
* @brief Construct a new return statement AST node.
*/
return_statement_node(struct location location)
: abstract_node(location) {}
/**
* @brief Construct a new return statement AST node.
*
* @param location Node location.
* @param value Return value handle.
*/
return_statement_node(struct location location, expression_node_p&& value)
: abstract_node(location), m_value(std::move(value)) {}
public:
/**
* @brief Returns this node's return value handle.
*
* @return The return value handle.
*/
value_type 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;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
value_type m_value; /**< Return value handle. */
};
/**
* @brief If statement AST node.
*/
class if_statement_node final : public statement_node, public abstract_node {
public:
/**
* @brief Construct a new if statement AST node.
*
* @param location Node location.
* @param cond Condition expression handle.
* @param then Then statement handle.
*/
if_statement_node(struct location location, expression_node_p&& cond, statement_node_p&& then)
: abstract_node(location), m_cond(std::move(cond)), m_then(std::move(then)) {}
/**
* @brief Construct a new if statement AST node.
*
* @param location Node location.
* @param cond Condition expression handle.
* @param then Then statement handle.
* @param elze Else statement handle.
*/
if_statement_node(struct location location,
expression_node_p&& cond,
statement_node_p&& then,
statement_node_p&& elze)
: abstract_node(location), 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_p cond() const { return m_cond; }
/**
* @brief Returns this node's then statement handle.
*
* @return The then statement handle.
*/
const statement_node_p& then() const { return m_then; }
/**
* @brief Returns this node's else statement handle.
*
* @return The else statement handle.
*/
const std::optional<statement_node_p>& 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;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
expression_node_p m_cond; /**< The condition expression handle */
statement_node_p m_then; /**< The then statement handle */
std::optional<statement_node_p> m_else; /**< The else statement handle */
};
/**
* @brief Compound statement AST node.
*/
class compound_statement_node final : public statement_node, public abstract_node {
public:
/**
* @brief Construct a new compound statement AST node.
*
* @param location Node location.
* @param body Body handle.
*/
compound_statement_node(struct location location, body&& body)
: abstract_node(location), m_body(std::move(body)) {}
public:
/**
* @brief Returns this node's body handle.
*
* @return The body handle.
*/
const body& 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;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
struct body m_body; /**< The body handle. */
};
/**
* @brief while statement AST node.
*/
class while_statement_node final : public statement_node, public abstract_node {
public:
/**
* @brief Construct a new while statement AST node.
*
* @param location Node location.
* @param body Body handle.
*/
while_statement_node(struct location location, expression_node_p&& cond, statement_node_p&& body)
: abstract_node(location), m_cond(std::move(cond)), m_body(std::move(body)) {}
public:
/**
* @brief Returns this node's condition expression.
*
* @return The condition expression.
*/
const expression_node_p& condition() const { return m_cond; }
/**
* @brief Returns this node's body handle.
*
* @return The body handle.
*/
const statement_node_p& body() const { return m_body; }
public:
/**
* @brief Returns this node's statement type.
*
* @return statement_node_t::while.
*/
statement_node_t statement_type() const override { return statement_node_t::While; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
expression_node_p m_cond; /**< The condition expression. */
statement_node_p m_body; /**< The body handle. */
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_STATEMENT_HPP
-152
View File
@@ -1,152 +0,0 @@
#ifndef FURC_AST_VISITOR_HPP
#define FURC_AST_VISITOR_HPP
#include "furc/ast/fwd.hpp"
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:
/**
* @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 unary_op_expression_node& node) {}
/**
* @brief Visit a binop_expression_node.
* @see binop_expression_node
*
* @param node Node.
*/
virtual void visit(const binary_op_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_call_expression_node.
* @see function_call_expression_node
*
* @param node Node.
*/
virtual void visit(const function_call_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 while_statement_node.
* @see while_statement_node
*
* @param node Node.
*/
virtual void visit(const while_statement_node& node) {}
/**
* @brief Visit an AST error.
*
* @param error AST error.
*/
virtual void visit_error(const ast::error& error) {}
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_VISITOR_HPP
-43
View File
@@ -1,43 +0,0 @@
#ifndef FURC_BACK_FURVM_HPP
#define FURC_BACK_FURVM_HPP
#include "furlang/ir/operand.hpp"
#include <cstddef>
#include <furlang/ir/function.hpp>
#include <furlang/ir/instruction.hpp>
#include <furlang/ir/module.hpp>
#include <furvm/fwd.hpp>
#include <furvm/module.hpp>
#include <unordered_map>
#include <vector>
namespace furc {
namespace back {
class furvm_generator {
public:
furvm_generator() = default;
public:
static furvm::mod generate(furlang::ir::mod& mod);
private:
static void generate_function(furvm::mod& mod, const furlang::ir::function& function);
struct function_context {
std::unordered_map<furlang::ir::block_index, std::size_t> blockOffsets;
std::unordered_map<furlang::ir::block_index, std::vector<std::size_t>> incompleteJumps;
std::unordered_map<furlang::ir::register_operand, furvm::variable_t> variables;
furvm::variable_t variableCounter{ 0 };
};
static void generate_instruction(furvm::mod& mod, function_context& ctx, const furlang::ir::instruction& instr);
static void generate_operand(furvm::mod& mod, function_context& ctx, const furlang::ir::operand& operand);
static void generate_jump(furvm::mod& mod, function_context& ctx, furlang::ir::block_index block, bool conditional);
};
} // namespace back
} // namespace furc
#endif // FURC_BACK_FURVM_HPP
-42
View File
@@ -1,42 +0,0 @@
#ifndef FURC_DIAG_HPP
#define FURC_DIAG_HPP
#include <ostream>
#include <string_view>
namespace furc {
/**
* @brief A location in file.
*/
struct location {
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); }
};
static inline std::ostream& operator<<(std::ostream& os, const location& location) {
return os << location.filename << ':' << location.line + 1 << ':' << location.column + 1;
}
} // namespace furc
#endif // FURC_DIAG_HPP
-61
View File
@@ -1,61 +0,0 @@
#ifndef FURC_FRONT_IR_GENERATOR_HPP
#define FURC_FRONT_IR_GENERATOR_HPP
#include "furc/ast/fwd.hpp"
#include "furc/ast/visitor.hpp"
#include "furlang/ir/module.hpp"
#include <unordered_map>
namespace furc {
namespace front {
using ir_register = std::uint32_t;
class ir_generator final : public ast::visitor {
public:
ir_generator() = default;
~ir_generator() override = default;
ir_generator(ir_generator&&) = default;
ir_generator& operator=(ir_generator&&) = default;
ir_generator(const ir_generator&) = delete;
ir_generator& operator=(const ir_generator&) = delete;
public:
furlang::ir::mod&& move_module() { return std::move(m_module); }
public:
void visit(const ast::function_definition_node& funcDef) override;
void visit(const ast::function_declaration_node& funcDecl) override;
void visit(const ast::return_statement_node& returnStmt) override;
void visit(const ast::if_statement_node& node) override;
void visit(const ast::while_statement_node& node) override;
void visit(const ast::compound_statement_node& node) override;
void visit(const ast::string_literal_node& node) override;
void visit(const ast::integer_literal_node& node) override;
void visit(const ast::var_read_expression_node& node) override;
void visit(const ast::unary_op_expression_node& node) override;
void visit(const ast::binary_op_expression_node& node) override;
void visit(const ast::var_assign_expression_node& node) override;
void visit(const ast::function_call_expression_node& node) override;
private:
template <typename T, typename... Args>
void push(Args&&... args) {
if (!m_currentBlock->emplace<T>(std::forward<Args>(args)...)) {
throw std::runtime_error("block exited too soon");
}
}
furlang::ir::block_index push_block(bool validate = true);
private:
furlang::ir::mod m_module;
std::unique_ptr<furlang::ir::function> m_currentFunction;
std::shared_ptr<furlang::ir::block> m_currentBlock;
ir_register m_registerCounter = 0;
std::unordered_map<std::string_view, ir_register> m_variables;
};
} // namespace front
} // namespace furc
#endif // FURC_FRONT_IR_GENERATOR_HPP
-70
View File
@@ -1,70 +0,0 @@
#ifndef FURC_FRONT_LEXER_HPP
#define FURC_FRONT_LEXER_HPP
#include "furc/front/token.hpp"
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_r 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();
char get(std::size_t offset = 0) const;
void skip_spaces();
location current_location();
private:
std::string_view m_filename;
std::string_view m_content;
std::size_t m_cursor = 0;
std::size_t m_row = 0;
std::size_t m_lineStart = 0;
};
} // namespace front
} // namespace furc
#endif // FURC_FRONT_LEXER_HPP
-86
View File
@@ -1,86 +0,0 @@
#ifndef FURC_FRONT_PARSER_HPP
#define FURC_FRONT_PARSER_HPP
#include "furc/ast/declaration.hpp"
#include "furc/ast/fwd.hpp"
#include "furc/front/lexer.hpp"
#include "furlang/arena.hpp"
#include <vector>
namespace furc {
namespace front {
/**
* @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(furlang::arena& arena, 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(furlang::arena& arena, std::string_view filename);
~parser() = default;
/**
* @brief Move constructor.
*/
parser(parser&&) = default;
/**
* @brief Move constructor.
*/
parser& operator=(parser&&) = default;
parser(const parser&) = delete;
parser& operator=(const parser&) = delete;
public:
/**
* @brief Returns a parsed program.
*
* @return Handle to an AST node of the program.
*/
ast::program_node_r parse() &;
private:
ast::type_r parse_type();
ast::declaration_node_r parse_declaration();
ast::statement_node_r parse_statement();
ast::expression_node_r parse_expression(std::uint32_t precedence = 16);
ast::expression_node_r parse_expression_primary();
ast::expression_node_r parse_expression_unary(std::uint32_t precedence);
ast::expression_node_r parse_expression_rhs(ast::expression_node_p&& init, std::uint32_t precedence);
ast::body_r parse_body();
private:
token_r next_token();
const token_r& peek_token();
token_r eat_token(token_t type);
private:
std::string m_filename;
std::string m_content;
lexer m_lexer;
furlang::arena* m_arena;
std::vector<token_r> m_peekBuffer;
};
} // namespace front
} // namespace furc
#endif // FURC_FRONT_PARSER_HPP
-41
View File
@@ -1,41 +0,0 @@
#ifndef FURC_FRONT_POST_PROCESS_HPP
#define FURC_FRONT_POST_PROCESS_HPP
#include "furlang/ir/module.hpp"
#include <vector>
namespace furc {
namespace front {
/**
* @brief Post process pipeline.
*/
class post_process {
public:
enum stage { // NOLINT
Ssa,
Sccp,
Adce,
DeSsa,
};
public:
post_process() = default;
~post_process() = default;
post_process(post_process&&) noexcept = default;
post_process& operator=(post_process&&) noexcept = default;
post_process(const post_process&) = delete;
post_process& operator=(const post_process&) = delete;
public:
void push_stage(stage stage) { m_stages.push_back(stage); }
public:
void process(furlang::ir::mod& mod);
private:
std::vector<stage> m_stages;
};
} // namespace front
} // namespace furc
#endif // FURC_FRONT_POST_PROCESS_HPP
-412
View File
@@ -1,412 +0,0 @@
#ifndef FURC_FRONT_TOKEN_HPP
#define FURC_FRONT_TOKEN_HPP
#include "furc/diag.hpp"
#include "furlang/result.hpp"
#include <cassert>
#include <cstdint>
#include <ostream>
#include <string_view>
namespace furc {
namespace front {
/**
* @brief Token type.
*/
enum class token_t {
None, /**< None */
Identifier, /**< Identifier */
String, /**< String */
Keyword, /**< Keyword */
Integer, /**< Integer */
LParen, /**< `(` */
RParen, /**< `)` */
LBrace, /**< `{` */
RBrace, /**< `}` */
LBracket, /**< `[` */
RBracket, /**< `]` */
Semicolon, /**< `;` */
Colon, /**< `:` */
Comma, /**< `,` */
Dot, /**< `.` */
Plus, /**< `+` */
Minus, /**< `-` */
Star, /**< `*` */
Slash, /**< `/` */
Percent, /**< `%` */
DPlus, /**< `++` */
DMinus, /**< `--` */
Eq, /**< `=` */
PlusEq, /**< `+=` */
MinusEq, /**< `-=` */
StarEq, /**< `*=` */
SlashEq, /**< `/=` */
PercentEq, /**< `%=` */
DEq, /**< `==` */
NotEq, /**< `!=` */
LessThan, /**< `<` */
GreaterThan, /**< `>` */
LessEq, /**< `<=` */
GreaterEq, /**< `>=` */
SlimArrow, /**< `->` */
FatArrow, /**< `=>` */
};
static inline std::ostream& operator<<(std::ostream& os, token_t type) {
switch (type) {
case token_t::None: return os << "none";
case token_t::Identifier: return os << "identifier";
case token_t::String: return os << "string";
case token_t::Keyword: return os << "keyword";
case token_t::Integer: return os << "integer";
case token_t::LParen: return os << "'('";
case token_t::RParen: return os << "')'";
case token_t::LBrace: return os << "'{'";
case token_t::RBrace: return os << "'}'";
case token_t::LBracket: return os << "'['";
case token_t::RBracket: return os << "']'";
case token_t::Semicolon: return os << "';'";
case token_t::Colon: return os << "':'";
case token_t::Comma: return os << "','";
case token_t::Dot: return os << "'.'";
case token_t::Plus: return os << "'+'";
case token_t::Minus: return os << "'-'";
case token_t::Star: return os << "'*'";
case token_t::Slash: return os << "'/'";
case token_t::Percent: return os << "'%'";
case token_t::DPlus: return os << "++";
case token_t::DMinus: return os << "--";
case token_t::Eq: return os << "=";
case token_t::PlusEq: return os << "+=";
case token_t::MinusEq: return os << "-=";
case token_t::StarEq: return os << "*=";
case token_t::SlashEq: return os << "/=";
case token_t::PercentEq: return os << "%=";
case token_t::DEq: return os << "==";
case token_t::NotEq: return os << "!=";
case token_t::LessThan: return os << "<";
case token_t::GreaterThan: return os << ">";
case token_t::LessEq: return os << "<=";
case token_t::GreaterEq: return os << ">=";
case token_t::SlimArrow: return os << "->";
case token_t::FatArrow: return os << "=>";
}
return os;
}
static inline std::string operator+(const std::string& str, token_t type) {
switch (type) {
case token_t::None: return str + "none";
case token_t::Identifier: return str + "identifier";
case token_t::String: return str + "string";
case token_t::Keyword: return str + "keyword";
case token_t::Integer: return str + "integer";
case token_t::LParen: return str + "'('";
case token_t::RParen: return str + "')'";
case token_t::LBrace: return str + "'{'";
case token_t::RBrace: return str + "'}'";
case token_t::LBracket: return str + "'['";
case token_t::RBracket: return str + "']'";
case token_t::Semicolon: return str + "';'";
case token_t::Colon: return str + "':'";
case token_t::Comma: return str + "','";
case token_t::Dot: return str + "'.'";
case token_t::Plus: return str + "'+'";
case token_t::Minus: return str + "'-'";
case token_t::Star: return str + "'*'";
case token_t::Slash: return str + "'/'";
case token_t::Percent: return str + "'%'";
case token_t::DPlus: return str + "++";
case token_t::DMinus: return str + "--";
case token_t::Eq: return str + "=";
case token_t::PlusEq: return str + "+=";
case token_t::MinusEq: return str + "-=";
case token_t::StarEq: return str + "*=";
case token_t::SlashEq: return str + "/=";
case token_t::PercentEq: return str + "%=";
case token_t::DEq: return str + "==";
case token_t::NotEq: return str + "!=";
case token_t::LessThan: return str + "<";
case token_t::GreaterThan: return str + ">";
case token_t::LessEq: return str + "<=";
case token_t::GreaterEq: return str + ">=";
case token_t::SlimArrow: return str + "->";
case token_t::FatArrow: return str + "=>";
}
return str;
}
/**
* @brief Keyword token.
*/
enum class keyword_token {
None, /**< None */
Func, /**< `func` */
Return, /**< `return` */
If, /**< `if` */
Else, /**< `else` */
While, /**< `while` */
Import, /**< `import` */
Native, /**< `native` */
Public, /**< `public` */
Private, /**< `private` */
Pointerof, /**< `pointerof` */
Sizeof, /**< `sizeof` */
Int32, /**< `int32` */
};
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
switch (keyword) {
case keyword_token::None: return os << "none";
case keyword_token::Func: return os << "func";
case keyword_token::Return: return os << "return";
case keyword_token::If: return os << "if";
case keyword_token::Else: return os << "else";
case keyword_token::While: return os << "while";
case keyword_token::Import: return os << "import";
case keyword_token::Native: return os << "native";
case keyword_token::Public: return os << "public";
case keyword_token::Private: return os << "private";
case keyword_token::Pointerof: return os << "pointerof";
case keyword_token::Sizeof: return os << "sizeof";
case keyword_token::Int32: return os << "int32";
}
return os;
}
static inline std::string operator+(const std::string& str, keyword_token keyword) {
switch (keyword) {
case keyword_token::None: return str + "none";
case keyword_token::Func: return str + "func";
case keyword_token::Return: return str + "return";
case keyword_token::If: return str + "if";
case keyword_token::Else: return str + "else";
case keyword_token::While: return str + "while";
case keyword_token::Import: return str + "import";
case keyword_token::Native: return str + "native";
case keyword_token::Public: return str + "public";
case keyword_token::Private: return str + "private";
case keyword_token::Pointerof: return str + "pointerof";
case keyword_token::Sizeof: return str + "sizeof";
case keyword_token::Int32: return str + "int32";
}
return str;
}
using integer_token = std::uint64_t; /**< Integer token. */
/**
* @brief Token.
*/
struct token {
location location; /**< Token location. */
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; /**< Token value. */
token() = default;
/**
* @brief Construct a new null token.
*
* @param location Token's location.
* @param type Token's type.
*/
token(struct location location, token_t type)
: location(location), type(type) {}
/**
* @brief Construct a new string token.
*
* @param location Token's location.
* @param type Token's type.
* @param value String value.
*/
token(struct location location, token_t type, std::string_view value)
: location(location), type(type), value(value) {}
/**
* @brief Construct a new keyword token.
*
* @param location Token's location.
* @param keyword Keyword value.
*/
token(struct location location, keyword_token keyword)
: location(location), type(token_t::Keyword), value(keyword) {}
/**
* @brief Construct a new integer token.
*
* @param location Token's location.
* @param integer Integer value.
*/
token(struct location location, integer_token integer)
: location(location), 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) {
case token_t::Identifier:
case token_t::String: return value.string == rhs.value.string;
case token_t::Keyword: return value.keyword == rhs.value.keyword;
case token_t::Integer: return value.integer == rhs.value.integer;
default: return true;
}
}
};
static inline std::ostream& operator<<(std::ostream& os, const token& token) {
switch (token.type) {
case token_t::Identifier:
case token_t::String: return os << token.value.string;
case token_t::Keyword: return os << token.value.keyword;
case token_t::Integer: return os << token.value.integer;
default: return os << token.type;
}
}
/**
* @brief Token error type
*/
enum class token_error_t {
EndOfFile, /**< End of file */
UnexpectedEof, /**< Unexpected end of file */
UnexpectedCharacter, /**< Unexpected character */
UnexpectedToken, /**< Unexpected character */
IntegerOverflow, /**< Integer overflow */
};
/**
* @brief Token error
*
* For token_r alias.
*/
struct token_error {
location location; /**< Error location. */
token_error_t type; /**< Error type. */
std::string message; /**< Error message. */
/**
* @brief Compares two token errors for equality.
*
* @param rhs Error to compare against.
* @return true if the errors are equal.
*/
bool operator==(const token_error& rhs) const {
return location == rhs.location && type == rhs.type && message == rhs.message;
}
/**
* @brief Compares two token errors for inequality.
*
* @param rhs Error to compare against.
* @return true if the errors are not equal.
*/
bool operator!=(const token_error& rhs) const { return !this->operator==(rhs); }
/**
* @brief Prints a token error to an output stream.
*
* @param os Output stream.
* @param error Token error to print.
* @return The output stream.
*/
friend std::ostream& operator<<(std::ostream& os, const token_error& error) {
return os << error.location << ": error: unknown";
}
};
using token_r = furlang::result<token, token_error>; /**< Alias to a token result. */
} // namespace front
} // namespace furc
#endif // FURC_FRONT_TOKEN_HPP