IR generation #3
Symlink
+1
@@ -0,0 +1 @@
|
||||
build/compile_commands.json
|
||||
@@ -5,15 +5,12 @@
|
||||
#include "furc/ast/statement.hpp"
|
||||
#include "furc/front/token.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace furc {
|
||||
namespace ast {
|
||||
|
||||
enum class declaration_node_t {
|
||||
FunctionDeclaration,
|
||||
FunctionDefinition,
|
||||
Variable,
|
||||
};
|
||||
|
||||
class declaration_node : public statement_node {
|
||||
@@ -45,27 +42,14 @@ protected:
|
||||
front::token m_name;
|
||||
};
|
||||
|
||||
struct function_body {
|
||||
location begin, end;
|
||||
std::vector<node_handle<statement_node>> statements;
|
||||
|
||||
bool operator==(const function_body& rhs) const {
|
||||
return begin == rhs.begin && end == rhs.end && statements == rhs.statements;
|
||||
}
|
||||
|
||||
bool operator!=(const function_body& rhs) const { return !this->operator==(rhs); }
|
||||
};
|
||||
|
||||
using function_body_h = handle<ast::function_body>;
|
||||
|
||||
class function_definition_node : public function_declaration_node {
|
||||
class function_definition_node final : public function_declaration_node {
|
||||
public:
|
||||
function_definition_node(front::token name, function_body_h&& body)
|
||||
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; }
|
||||
|
||||
const function_body_h& body() const { return m_body; }
|
||||
const body_h& body() const { return m_body; }
|
||||
public:
|
||||
void accept(visitor& visitor) const override;
|
||||
|
||||
@@ -73,7 +57,7 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
function_body_h m_body;
|
||||
body_h m_body;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -27,7 +27,7 @@ protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
class var_read_expression_node : public expression_node {
|
||||
class var_read_expression_node final : public expression_node {
|
||||
public:
|
||||
var_read_expression_node(handle<std::string_view>&& name)
|
||||
: m_name(std::move(name)) {}
|
||||
@@ -55,7 +55,7 @@ enum class unaryop_expression_node_t {
|
||||
PostfixDecrement,
|
||||
};
|
||||
|
||||
class unaryop_expression_node : public expression_node {
|
||||
class unaryop_expression_node final : public expression_node {
|
||||
public:
|
||||
unaryop_expression_node(unaryop_expression_node_t type, expression_node_h&& node)
|
||||
: m_type(type), m_node(std::move(node)) {}
|
||||
@@ -95,7 +95,7 @@ enum class binop_expression_node_t {
|
||||
GreaterEqual,
|
||||
};
|
||||
|
||||
class binop_expression_node : public expression_node {
|
||||
class binop_expression_node final : public expression_node {
|
||||
public:
|
||||
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)) {}
|
||||
@@ -121,7 +121,7 @@ private:
|
||||
expression_node_h m_rhs;
|
||||
};
|
||||
|
||||
class var_assign_expression_node : public expression_node {
|
||||
class var_assign_expression_node final : public expression_node {
|
||||
public:
|
||||
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)) {}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "furc/handle.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace furc {
|
||||
namespace ast {
|
||||
@@ -38,6 +39,21 @@ using binop_expression_node_h = node_handle<binop_expression_node>;
|
||||
class var_assign_expression_node;
|
||||
using var_assign_expression_node_h = node_handle<var_assign_expression_node>;
|
||||
|
||||
struct body {
|
||||
location begin, end;
|
||||
std::vector<statement_node_h> statements;
|
||||
|
||||
bool operator==(const body& rhs) const {
|
||||
return begin == rhs.begin && end == rhs.end && statements == rhs.statements;
|
||||
}
|
||||
|
||||
bool operator!=(const body& rhs) const { return !this->operator==(rhs); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream&, const body&);
|
||||
};
|
||||
|
||||
using body_h = handle<body>;
|
||||
|
||||
class function_declaration_node;
|
||||
using function_declaration_node_h = node_handle<function_declaration_node>;
|
||||
class function_definition_node;
|
||||
@@ -45,6 +61,10 @@ using function_definition_node_h = node_handle<function_definition_node>;
|
||||
|
||||
class return_statement_node;
|
||||
using return_statement_node_h = node_handle<return_statement_node>;
|
||||
class if_statement_node;
|
||||
using if_statement_node_h = node_handle<if_statement_node>;
|
||||
class compound_statement_node;
|
||||
using compound_statement_node_h = node_handle<compound_statement_node>;
|
||||
|
||||
} // namespace ast
|
||||
} // namespace furc
|
||||
|
||||
@@ -24,7 +24,7 @@ protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
class string_literal_node : public literal_node {
|
||||
class string_literal_node final : public literal_node {
|
||||
public:
|
||||
string_literal_node(handle<std::string_view>&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
@@ -42,7 +42,7 @@ private:
|
||||
handle<std::string_view> m_value;
|
||||
};
|
||||
|
||||
class integer_literal_node : public literal_node {
|
||||
class integer_literal_node final : public literal_node {
|
||||
public:
|
||||
integer_literal_node(handle<front::integer_token>&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace furc {
|
||||
namespace ast {
|
||||
|
||||
class program_node : public node {
|
||||
class program_node final : public node {
|
||||
public:
|
||||
program_node() = default;
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ enum class statement_node_t {
|
||||
Expression,
|
||||
Declaration,
|
||||
Return,
|
||||
If,
|
||||
Compound,
|
||||
};
|
||||
|
||||
class statement_node : public node {
|
||||
@@ -21,14 +23,14 @@ protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
class return_statement_node : public statement_node {
|
||||
class return_statement_node final : public statement_node {
|
||||
public:
|
||||
return_statement_node() = default;
|
||||
|
||||
return_statement_node(node_handle<expression_node>&& value)
|
||||
return_statement_node(expression_node_h&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
public:
|
||||
node_handle<expression_node> value() const { return m_value; }
|
||||
expression_node_h value() const { return m_value; }
|
||||
public:
|
||||
statement_node_t statement_type() const override { return statement_node_t::Return; }
|
||||
public:
|
||||
@@ -38,7 +40,50 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
node_handle<expression_node> m_value;
|
||||
expression_node_h m_value;
|
||||
};
|
||||
|
||||
class if_statement_node final : public statement_node {
|
||||
public:
|
||||
if_statement_node(expression_node_h&& cond, statement_node_h&& then)
|
||||
: m_cond(std::move(cond)), m_then(std::move(then)) {}
|
||||
|
||||
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:
|
||||
expression_node_h cond() const { return m_cond; }
|
||||
const statement_node_h& then() const { return m_then; }
|
||||
const statement_node_h& elze() const { return m_else; }
|
||||
public:
|
||||
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_h m_cond;
|
||||
statement_node_h m_then;
|
||||
statement_node_h m_else;
|
||||
};
|
||||
|
||||
class compound_statement_node final : public statement_node {
|
||||
public:
|
||||
compound_statement_node(body_h&& body)
|
||||
: m_body(std::move(body)) {}
|
||||
public:
|
||||
const body_h& body() const { return m_body; }
|
||||
public:
|
||||
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:
|
||||
body_h m_body;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -16,15 +16,17 @@ public:
|
||||
visitor(const visitor&) = default;
|
||||
visitor& operator=(const visitor&) = default;
|
||||
public:
|
||||
virtual void visit_string_literal_node(const string_literal_node&) {}
|
||||
virtual void visit_integer_literal_node(const integer_literal_node&) {}
|
||||
virtual void visit_var_read_expression_node(const var_read_expression_node&) {}
|
||||
virtual void visit_unaryop_expression_node(const unaryop_expression_node&) {}
|
||||
virtual void visit_binop_expression_node(const binop_expression_node&) {}
|
||||
virtual void visit_var_assign_expression_node(const var_assign_expression_node&) {}
|
||||
virtual void visit_function_declaration_node(const function_declaration_node&) {}
|
||||
virtual void visit_function_definition_node(const function_definition_node&) {}
|
||||
virtual void visit_return_statement_node(const return_statement_node&) {}
|
||||
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&) {}
|
||||
|
||||
virtual void visit_error(const node_handle<node>& handle) {}
|
||||
};
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
namespace furc {
|
||||
namespace front {
|
||||
|
||||
using ir_register = std::uint32_t;
|
||||
|
||||
class ir_generator final : public ast::visitor {
|
||||
public:
|
||||
ir_generator() = default;
|
||||
@@ -17,25 +19,37 @@ public:
|
||||
|
||||
ir_generator(ir_generator&&) = default;
|
||||
ir_generator& operator=(ir_generator&&) = default;
|
||||
ir_generator(const ir_generator&) = default;
|
||||
ir_generator& operator=(const ir_generator&) = default;
|
||||
ir_generator(const ir_generator&) = delete;
|
||||
ir_generator& operator=(const ir_generator&) = delete;
|
||||
public:
|
||||
furlang::ir::module&& move_module() { return std::move(m_module); }
|
||||
public:
|
||||
void visit_function_definition_node(const ast::function_definition_node& funcDef) override;
|
||||
void visit_return_statement_node(const ast::return_statement_node& returnStmt) override;
|
||||
void visit_string_literal_node(const ast::string_literal_node& node) override;
|
||||
void visit_integer_literal_node(const ast::integer_literal_node& node) override;
|
||||
void visit_var_read_expression_node(const ast::var_read_expression_node& node) override;
|
||||
void visit_unaryop_expression_node(const ast::unaryop_expression_node& node) override;
|
||||
void visit_binop_expression_node(const ast::binop_expression_node& node) override;
|
||||
void visit_var_assign_expression_node(const ast::var_assign_expression_node& node) override;
|
||||
void visit(const ast::function_definition_node& funcDef) override;
|
||||
void visit(const ast::return_statement_node& returnStmt) override;
|
||||
void visit(const ast::if_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::unaryop_expression_node& node) override;
|
||||
void visit(const ast::binop_expression_node& node) override;
|
||||
void visit(const ast::var_assign_expression_node& node) override;
|
||||
private:
|
||||
furlang::ir::module m_module;
|
||||
std::shared_ptr<furlang::ir::block> m_currentBlock;
|
||||
std::uint32_t m_registerCounter = 0;
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<std::string_view, std::uint32_t> m_variableMap;
|
||||
furlang::ir::block_index push_block();
|
||||
private:
|
||||
furlang::ir::module 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
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
#ifndef FURC_FRONT_PARSER_HPP
|
||||
#define FURC_FRONT_PARSER_HPP
|
||||
|
||||
#include "furc/ast/declaration.hpp"
|
||||
#include "furc/ast/expression.hpp"
|
||||
#include "furc/ast/literal.hpp"
|
||||
#include "furc/ast/program.hpp"
|
||||
#include "furc/ast/fwd.hpp"
|
||||
#include "furc/front/lexer.hpp"
|
||||
#include "furlang/arena.hpp"
|
||||
|
||||
@@ -36,7 +33,7 @@ private:
|
||||
ast::expression_node_h parse_expression_unary(std::uint32_t precedence);
|
||||
ast::expression_node_h parse_expression_rhs(ast::expression_node_h&& init, std::uint32_t precedence);
|
||||
|
||||
ast::function_body_h parse_body();
|
||||
ast::body_h parse_body();
|
||||
private:
|
||||
token_handle<> next_token();
|
||||
const token_handle<>& peek_token();
|
||||
|
||||
@@ -16,12 +16,12 @@ enum class token_t {
|
||||
String,
|
||||
Keyword,
|
||||
Integer,
|
||||
Lparen,
|
||||
Rparen,
|
||||
Lbrace,
|
||||
Rbrace,
|
||||
Lbracket,
|
||||
Rbracket,
|
||||
LParen,
|
||||
RParen,
|
||||
LBrace,
|
||||
RBrace,
|
||||
LBracket,
|
||||
RBracket,
|
||||
Semicolon,
|
||||
Colon,
|
||||
Comma,
|
||||
@@ -57,12 +57,12 @@ static inline std::ostream& operator<<(std::ostream& os, token_t type) {
|
||||
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::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 << "','";
|
||||
@@ -97,12 +97,12 @@ static inline std::string operator+(const std::string& str, token_t type) {
|
||||
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::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 + "','";
|
||||
@@ -134,6 +134,8 @@ enum class keyword_token {
|
||||
None,
|
||||
Func,
|
||||
Return,
|
||||
If,
|
||||
Else,
|
||||
};
|
||||
|
||||
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
|
||||
@@ -141,7 +143,10 @@ static inline std::ostream& operator<<(std::ostream& os, keyword_token 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";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
static inline std::string operator+(const std::string& str, keyword_token keyword) {
|
||||
@@ -149,7 +154,10 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
|
||||
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";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
using integer_token = std::uint64_t;
|
||||
|
||||
+45
-9
@@ -13,7 +13,7 @@ bool literal_node::equal(const node& rhs) const {
|
||||
}
|
||||
|
||||
void string_literal_node::accept(visitor& visitor) const {
|
||||
visitor.visit_string_literal_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& string_literal_node::print(std::ostream& os) const {
|
||||
@@ -26,7 +26,7 @@ bool string_literal_node::equal(const node& rhs) const {
|
||||
}
|
||||
|
||||
void integer_literal_node::accept(visitor& visitor) const {
|
||||
visitor.visit_integer_literal_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& integer_literal_node::print(std::ostream& os) const {
|
||||
@@ -43,7 +43,7 @@ bool expression_node::equal(const node& rhs) const {
|
||||
}
|
||||
|
||||
void var_read_expression_node::accept(visitor& visitor) const {
|
||||
visitor.visit_var_read_expression_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& var_read_expression_node::print(std::ostream& os) const {
|
||||
@@ -69,7 +69,7 @@ std::ostream& operator<<(std::ostream& os, unaryop_expression_node_t type) {
|
||||
}
|
||||
|
||||
void unaryop_expression_node::accept(visitor& visitor) const {
|
||||
visitor.visit_unaryop_expression_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& unaryop_expression_node::print(std::ostream& os) const {
|
||||
@@ -108,7 +108,7 @@ std::ostream& operator<<(std::ostream& os, binop_expression_node_t type) {
|
||||
}
|
||||
|
||||
void binop_expression_node::accept(visitor& visitor) const {
|
||||
visitor.visit_binop_expression_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& binop_expression_node::print(std::ostream& os) const {
|
||||
@@ -122,7 +122,7 @@ bool binop_expression_node::equal(const node& rhsNode) const {
|
||||
}
|
||||
|
||||
void var_assign_expression_node::accept(visitor& visitor) const {
|
||||
visitor.visit_var_assign_expression_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& var_assign_expression_node::print(std::ostream& os) const {
|
||||
@@ -139,7 +139,7 @@ bool declaration_node::equal(const node& rhs) const {
|
||||
}
|
||||
|
||||
void function_declaration_node::accept(visitor& visitor) const {
|
||||
visitor.visit_function_declaration_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& function_declaration_node::print(std::ostream& os) const {
|
||||
@@ -151,7 +151,7 @@ bool function_declaration_node::equal(const node& rhs) const {
|
||||
}
|
||||
|
||||
void function_definition_node::accept(visitor& visitor) const {
|
||||
visitor.visit_function_definition_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& function_definition_node::print(std::ostream& os) const {
|
||||
@@ -175,7 +175,7 @@ bool statement_node::equal(const node& rhs) const {
|
||||
}
|
||||
|
||||
void return_statement_node::accept(visitor& visitor) const {
|
||||
visitor.visit_return_statement_node(*this);
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& return_statement_node::print(std::ostream& os) const {
|
||||
@@ -188,6 +188,34 @@ bool return_statement_node::equal(const node& rhs) const {
|
||||
return statement_node::equal(rhs) && m_value == reinterpret_cast<const return_statement_node&>(rhs).m_value;
|
||||
}
|
||||
|
||||
void if_statement_node::accept(visitor& visitor) const {
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& if_statement_node::print(std::ostream& os) const {
|
||||
os << "if " << *m_cond << ", then:\n";
|
||||
os << m_then;
|
||||
if (m_else.present()) os << m_else;
|
||||
return os;
|
||||
}
|
||||
|
||||
bool if_statement_node::equal(const node& rhsNode) const {
|
||||
const auto& rhs = reinterpret_cast<const if_statement_node&>(rhsNode);
|
||||
return statement_node::equal(rhs) && m_cond == rhs.m_cond && m_then == rhs.m_then && m_else == rhs.m_else;
|
||||
}
|
||||
|
||||
void compound_statement_node::accept(visitor& visitor) const {
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& compound_statement_node::print(std::ostream& os) const {
|
||||
return os << m_body;
|
||||
}
|
||||
|
||||
bool compound_statement_node::equal(const node& rhs) const {
|
||||
return statement_node::equal(rhs) && m_body == reinterpret_cast<const compound_statement_node&>(rhs).m_body;
|
||||
}
|
||||
|
||||
void program_node::accept(visitor& visitor) const {
|
||||
for (const auto& decl : m_declarations) {
|
||||
if (decl.has_error()) {
|
||||
@@ -210,4 +238,12 @@ bool program_node::equal(const node& rhs) const {
|
||||
return m_declarations == reinterpret_cast<const program_node&>(rhs).m_declarations;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const body& body) {
|
||||
os << "body:";
|
||||
for (const auto& stmt : body.statements) {
|
||||
os << '\n' << stmt;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace furc::ast
|
||||
@@ -13,10 +13,10 @@ namespace {
|
||||
namespace ir = furlang::ir;
|
||||
}
|
||||
|
||||
void ir_generator::visit_function_definition_node(const ast::function_definition_node& funcDef) {
|
||||
auto func = std::make_unique<furlang::ir::function>(std::string(funcDef.name()->string));
|
||||
void ir_generator::visit(const ast::function_definition_node& funcDef) {
|
||||
m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()->string));
|
||||
|
||||
m_currentBlock = func->push();
|
||||
push_block();
|
||||
if (funcDef.body().has_error()) {
|
||||
std::cerr << funcDef.body().error() << '\n';
|
||||
return;
|
||||
@@ -24,38 +24,70 @@ void ir_generator::visit_function_definition_node(const ast::function_definition
|
||||
for (const auto& stmt : funcDef.body()->statements) {
|
||||
stmt->accept(*this);
|
||||
}
|
||||
m_currentBlock->emplace<ir::return_instruction>();
|
||||
|
||||
m_module.push(std::move(func));
|
||||
m_module.push(std::move(m_currentFunction));
|
||||
}
|
||||
|
||||
void ir_generator::visit_return_statement_node(const ast::return_statement_node& returnStmt) {
|
||||
void ir_generator::visit(const ast::return_statement_node& returnStmt) {
|
||||
if (returnStmt.value().has_error()) {
|
||||
std::cerr << returnStmt.value() << '\n';
|
||||
}
|
||||
|
||||
if (returnStmt.value().present()) {
|
||||
returnStmt.value()->accept(*this);
|
||||
m_currentBlock->emplace<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
|
||||
push<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
|
||||
} else {
|
||||
m_currentBlock->emplace<ir::return_instruction>();
|
||||
push<ir::return_instruction>();
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_string_literal_node(const ast::string_literal_node& node) {
|
||||
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
|
||||
void ir_generator::visit(const ast::if_statement_node& node) {
|
||||
node.cond()->accept(*this);
|
||||
ir_register cond = m_registerCounter - 1;
|
||||
push<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
|
||||
m_currentFunction->blocks().size(),
|
||||
m_currentFunction->blocks().size() + 1);
|
||||
|
||||
push_block(); // then block
|
||||
node.then()->accept(*this);
|
||||
if (node.elze().present()) {
|
||||
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
|
||||
|
||||
push_block(); // else block
|
||||
node.elze()->accept(*this);
|
||||
}
|
||||
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size());
|
||||
|
||||
push_block(); // merge block
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::compound_statement_node& node) {
|
||||
for (const auto& stmt : node.body()->statements) {
|
||||
stmt->accept(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::string_literal_node& node) {
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
}
|
||||
|
||||
void ir_generator::visit_integer_literal_node(const ast::integer_literal_node& node) {
|
||||
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_integer(*node.value()),
|
||||
void ir_generator::visit(const ast::integer_literal_node& node) {
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_integer(*node.value()),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
}
|
||||
|
||||
void ir_generator::visit_var_read_expression_node(const ast::var_read_expression_node& node) {
|
||||
throw std::runtime_error("unimplemented");
|
||||
void ir_generator::visit(const ast::var_read_expression_node& node) {
|
||||
if (auto it = m_variables.find(*node.get_name()); it != m_variables.end()) {
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_reg(it->second),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
} else {
|
||||
throw std::runtime_error("unknown variable");
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_unaryop_expression_node(const ast::unaryop_expression_node& node) {
|
||||
void ir_generator::visit(const ast::unaryop_expression_node& node) {
|
||||
throw std::runtime_error("unimplemented");
|
||||
}
|
||||
|
||||
@@ -77,20 +109,50 @@ static inline furlang::ir::binary_op_instruction_t binary_op_instruction_t(ast::
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_binop_expression_node(const ast::binop_expression_node& node) {
|
||||
void ir_generator::visit(const ast::binop_expression_node& node) {
|
||||
node.lhs()->accept(*this);
|
||||
std::uint32_t lhs = m_registerCounter - 1;
|
||||
ir_register lhs = m_registerCounter - 1;
|
||||
node.rhs()->accept(*this);
|
||||
std::uint32_t rhs = m_registerCounter - 1;
|
||||
std::uint32_t dst = m_registerCounter++;
|
||||
m_currentBlock->emplace<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
|
||||
ir_register rhs = m_registerCounter - 1;
|
||||
ir_register dst = m_registerCounter++;
|
||||
push<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
|
||||
ir::operand::new_reg(lhs),
|
||||
ir::operand::new_reg(rhs),
|
||||
ir::operand::new_reg(dst));
|
||||
}
|
||||
|
||||
void ir_generator::visit_var_assign_expression_node(const ast::var_assign_expression_node& node) {
|
||||
throw std::runtime_error("unimplemented");
|
||||
void ir_generator::visit(const ast::var_assign_expression_node& node) {
|
||||
node.rhs()->accept(*this);
|
||||
ir_register rhs = m_registerCounter - 1;
|
||||
assert(node.lhs()->expression_type() == ast::expression_node_t::VarRead);
|
||||
ast::var_read_expression_node_h lhs = node.lhs();
|
||||
|
||||
ir_register reg = 0;
|
||||
if (auto it = m_variables.find(*lhs->get_name()); it != m_variables.end()) {
|
||||
reg = it->second;
|
||||
} else {
|
||||
m_variables[*lhs->get_name()] = reg = m_registerCounter++;
|
||||
}
|
||||
|
||||
auto compound = node.compound();
|
||||
if (compound != ast::binop_expression_node_t::None) {
|
||||
push<ir::binary_op_instruction>(binary_op_instruction_t(compound),
|
||||
ir::operand::new_reg(reg),
|
||||
ir::operand::new_reg(rhs),
|
||||
ir::operand::new_reg(reg));
|
||||
} else {
|
||||
push<ir::assign_instruction>(ir::operand::new_reg(rhs), ir::operand::new_reg(reg));
|
||||
}
|
||||
}
|
||||
|
||||
furlang::ir::block_index ir_generator::push_block() {
|
||||
if (!m_currentFunction->blocks().empty() && !m_currentFunction->blocks().back()->has_exit()) {
|
||||
throw std::runtime_error(
|
||||
"block " + std::to_string(m_currentFunction->blocks().size() - 1) + " is lacking an exit");
|
||||
}
|
||||
ir::block_index index = m_currentFunction->blocks().size();
|
||||
m_currentBlock = m_currentFunction->push();
|
||||
return index;
|
||||
}
|
||||
|
||||
} // namespace furc::front
|
||||
@@ -90,6 +90,8 @@ token_handle<> lexer::next_token() {
|
||||
static std::unordered_map<std::string_view, keyword_token> s_keywords = {
|
||||
{ "func", keyword_token::Func },
|
||||
{ "return", keyword_token::Return },
|
||||
{ "if", keyword_token::If },
|
||||
{ "else", keyword_token::Else },
|
||||
};
|
||||
|
||||
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second, value };
|
||||
@@ -104,12 +106,12 @@ token_handle<> lexer::next_token() {
|
||||
};
|
||||
|
||||
static std::map<std::string_view, token_t, compare> s_tokens = {
|
||||
{ "(", token_t::Lparen },
|
||||
{ ")", token_t::Rparen },
|
||||
{ "{", token_t::Lbrace },
|
||||
{ "}", token_t::Rbrace },
|
||||
{ "[", token_t::Lbracket },
|
||||
{ "]", token_t::Rbracket },
|
||||
{ "(", token_t::LParen },
|
||||
{ ")", token_t::RParen },
|
||||
{ "{", token_t::LBrace },
|
||||
{ "}", token_t::RBrace },
|
||||
{ "[", token_t::LBracket },
|
||||
{ "]", token_t::RBracket },
|
||||
{ ";", token_t::Semicolon },
|
||||
{ ":", token_t::Colon },
|
||||
{ ",", token_t::Comma },
|
||||
|
||||
+66
-27
@@ -1,6 +1,10 @@
|
||||
#include "furc/front/parser.hpp"
|
||||
|
||||
#include "furc/ast/declaration.hpp"
|
||||
#include "furc/ast/declaration.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/expression.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/program.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -46,16 +50,16 @@ ast::declaration_node_h parser::parse_declaration() {
|
||||
token_handle<> name = eat_token(token_t::Identifier);
|
||||
if (name.has_error()) return { name.location(), name.error() };
|
||||
|
||||
auto tok = eat_token(token_t::Lparen);
|
||||
auto tok = eat_token(token_t::LParen);
|
||||
if (tok.has_error()) return tok;
|
||||
tok = eat_token(token_t::Rparen);
|
||||
tok = eat_token(token_t::RParen);
|
||||
if (tok.has_error()) return tok;
|
||||
|
||||
const auto& peek = peek_token();
|
||||
if (peek.has_error()) return peek;
|
||||
switch (peek->type) {
|
||||
case token_t::Lbrace: {
|
||||
ast::function_body_h body = parse_body();
|
||||
case token_t::LBrace: {
|
||||
ast::body_h body = parse_body();
|
||||
if (body.has_error()) return body;
|
||||
return ast::function_definition_node_h{ first.location(), m_arena, *name, std::move(body) };
|
||||
}
|
||||
@@ -72,12 +76,12 @@ ast::declaration_node_h parser::parse_declaration() {
|
||||
case token_t::None:
|
||||
case token_t::Identifier:
|
||||
case token_t::Integer:
|
||||
case token_t::Lparen:
|
||||
case token_t::Rparen:
|
||||
case token_t::Lbrace:
|
||||
case token_t::Rbrace:
|
||||
case token_t::Lbracket:
|
||||
case token_t::Rbracket:
|
||||
case token_t::LParen:
|
||||
case token_t::RParen:
|
||||
case token_t::LBrace:
|
||||
case token_t::RBrace:
|
||||
case token_t::LBracket:
|
||||
case token_t::RBracket:
|
||||
case token_t::Semicolon:
|
||||
case token_t::Colon:
|
||||
default: {
|
||||
@@ -89,19 +93,54 @@ ast::declaration_node_h parser::parse_declaration() {
|
||||
ast::statement_node_h parser::parse_statement() {
|
||||
const auto& tok = peek_token();
|
||||
if (tok.has_error()) return tok;
|
||||
auto location = tok.location();
|
||||
switch (tok->type) {
|
||||
case token_t::Keyword: {
|
||||
auto tok = next_token();
|
||||
if (peek_token()->type == token_t::Semicolon) {
|
||||
next_token();
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena };
|
||||
}
|
||||
switch (tok->value.keyword) {
|
||||
case keyword_token::Return: {
|
||||
auto tok = next_token();
|
||||
if (peek_token()->type == token_t::Semicolon) {
|
||||
next_token();
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena };
|
||||
}
|
||||
|
||||
auto value = parse_expression();
|
||||
auto err = eat_token(token_t::Semicolon);
|
||||
if (err.has_error()) return err;
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena, std::move(value) };
|
||||
auto value = parse_expression();
|
||||
auto err = eat_token(token_t::Semicolon);
|
||||
if (err.has_error()) return err;
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena, std::move(value) };
|
||||
}
|
||||
case keyword_token::If: {
|
||||
auto tok = next_token();
|
||||
auto err = eat_token(token_t::LParen);
|
||||
if (err.has_error()) return err;
|
||||
|
||||
auto cond = parse_expression();
|
||||
|
||||
err = eat_token(token_t::RParen);
|
||||
if (err.has_error()) return err;
|
||||
|
||||
auto then = parse_statement();
|
||||
if (then.has_error()) return then;
|
||||
|
||||
if (peek_token().present() && peek_token()->type == token_t::Keyword &&
|
||||
peek_token()->value.keyword == keyword_token::Else) {
|
||||
next_token();
|
||||
|
||||
return ast::if_statement_node_h{ location,
|
||||
m_arena,
|
||||
std::move(cond),
|
||||
std::move(then),
|
||||
std::move(parse_statement()) };
|
||||
}
|
||||
|
||||
return ast::if_statement_node_h{ location, m_arena, std::move(cond), std::move(then) };
|
||||
}
|
||||
case keyword_token::None:
|
||||
case keyword_token::Func:
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
case token_t::LBrace: return ast::compound_statement_node_h{ location, m_arena, parse_body() };
|
||||
default: break;
|
||||
}
|
||||
|
||||
@@ -152,10 +191,10 @@ ast::expression_node_h parser::parse_expression_primary() {
|
||||
m_arena,
|
||||
handle<std::string_view>{ tok.location(), (*tok)->string } };
|
||||
}
|
||||
case token_t::Lparen: {
|
||||
case token_t::LParen: {
|
||||
auto tok = next_token();
|
||||
auto node = parse_expression();
|
||||
auto err = eat_token(token_t::Rparen);
|
||||
auto err = eat_token(token_t::RParen);
|
||||
if (err.has_error()) return err;
|
||||
return node;
|
||||
}
|
||||
@@ -328,18 +367,18 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
|
||||
return lhs;
|
||||
}
|
||||
|
||||
ast::function_body_h parser::parse_body() {
|
||||
ast::function_body body;
|
||||
ast::body_h parser::parse_body() {
|
||||
ast::body body;
|
||||
|
||||
token_handle<> begin = eat_token(token_t::Lbrace);
|
||||
token_handle<> begin = eat_token(token_t::LBrace);
|
||||
if (begin.has_error()) return begin;
|
||||
body.begin = begin.location();
|
||||
|
||||
while (!peek_token().has_error() && peek_token()->type != token_t::None && peek_token()->type != token_t::Rbrace) {
|
||||
while (!peek_token().has_error() && peek_token()->type != token_t::None && peek_token()->type != token_t::RBrace) {
|
||||
body.statements.push_back(parse_statement());
|
||||
}
|
||||
|
||||
token_handle<> end = eat_token(token_t::Rbrace);
|
||||
token_handle<> end = eat_token(token_t::RBrace);
|
||||
if (end.has_error()) return end;
|
||||
body.end = end.location();
|
||||
|
||||
|
||||
+19
-3
@@ -1,12 +1,27 @@
|
||||
// #ifndef LIBFURC
|
||||
#ifndef LIBFURC
|
||||
|
||||
#include "furc/ast/program.hpp"
|
||||
#include "furc/front/ir_generator.hpp"
|
||||
#include "furc/front/parser.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(void) {
|
||||
furc::front::parser parser("<TEMP>", "func main() {\n return 7 + 6 * 10;\n}");
|
||||
std::string programStr = R"(
|
||||
func main() {
|
||||
x = 5;
|
||||
x -= 3;
|
||||
if (x < 3) {
|
||||
y = x * 2;
|
||||
w = y;
|
||||
} else {
|
||||
y = x - 3;
|
||||
}
|
||||
w = x - y;
|
||||
z = x + y;
|
||||
}
|
||||
)";
|
||||
furc::front::parser parser("<TEMP>", programStr);
|
||||
furc::front::ir_generator generator;
|
||||
|
||||
auto program = parser.parse();
|
||||
@@ -25,10 +40,11 @@ int main(void) {
|
||||
for (const auto& instruction : block->instructions()) {
|
||||
std::cout << " " << *instruction << '\n';
|
||||
}
|
||||
std::cout << " " << *block->exit() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// #endif // LIBFURC
|
||||
#endif // LIBFURC
|
||||
|
||||
+61
-40
@@ -1,55 +1,76 @@
|
||||
// NOLINTBEGIN(readability-identifier-naming)
|
||||
|
||||
#include "furc/front/lexer.hpp"
|
||||
|
||||
#include "furlang/result.hpp"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace furc::front;
|
||||
using namespace std::string_view_literals;
|
||||
using namespace std::string_literals;
|
||||
|
||||
#define EXPECT_TOKEN(lexer, t) EXPECT_EQ((lexer).next_token(), (t));
|
||||
#define EXPECT_EOF(lexer) EXPECT_EQ((lexer).next_token(), (token{}));
|
||||
#define EXPECT_ERROR(lexer, err) \
|
||||
do { \
|
||||
auto handle = (lexer).next_token(); \
|
||||
EXPECT_TRUE(handle.has_error()); \
|
||||
EXPECT_EQ(handle.error(), (err)); \
|
||||
} while (0)
|
||||
using token_r = furlang::result<token, std::string>;
|
||||
using lexer_case = std::pair<std::string, std::vector<token_r>>;
|
||||
|
||||
TEST(Lexer, Tokens) {
|
||||
lexer lexer("<TEMP>", "()\n\t\t{\n}[\"shto-to\"]; :,.main return func");
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Lparen }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Rparen }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Lbrace }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Rbrace }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Lbracket }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::String, "shto-to"sv }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Rbracket }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Semicolon }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Colon }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Comma }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Dot }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Identifier, "main"sv }));
|
||||
EXPECT_TOKEN(lexer, (token{ keyword_token::Return }));
|
||||
EXPECT_TOKEN(lexer, (token{ keyword_token::Func }));
|
||||
EXPECT_EOF(lexer);
|
||||
class LexerTestingFixture : public testing::TestWithParam<lexer_case> {};
|
||||
|
||||
TEST_P(LexerTestingFixture, LexerTest) {
|
||||
auto [code, expected] = GetParam();
|
||||
|
||||
lexer lexer("<TEMP>", code);
|
||||
auto it = expected.begin();
|
||||
while (it != expected.end()) {
|
||||
const auto& expected = *it++;
|
||||
|
||||
auto token = std::move(lexer.next_token());
|
||||
if (expected.has_error()) {
|
||||
EXPECT_TRUE(token.has_error());
|
||||
EXPECT_EQ(token.error(), expected.error());
|
||||
} else {
|
||||
EXPECT_EQ(token, expected.value());
|
||||
}
|
||||
}
|
||||
// EOF
|
||||
EXPECT_EQ(lexer.next_token(), token{});
|
||||
}
|
||||
|
||||
TEST(Lexer, Comments) {
|
||||
lexer lexer("<TEMP>", "(/** skibidi **/func{//)\n}");
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Lparen, "("sv })); // left out the string-view deliberately
|
||||
EXPECT_TOKEN(lexer, (token{ keyword_token::Func }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Lbrace }));
|
||||
EXPECT_TOKEN(lexer, (token{ token_t::Rbrace }));
|
||||
EXPECT_EOF(lexer);
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(EmptyTests,
|
||||
LexerTestingFixture,
|
||||
testing::Values(lexer_case("", {}), lexer_case(" ", {}), lexer_case("\t", {}), lexer_case("\n", {})));
|
||||
|
||||
TEST(Lexer, Integers) {
|
||||
lexer lexer("<TEMP>", "67 18446744073709551615 18446744073709551616");
|
||||
EXPECT_TOKEN(lexer, (token{ 67ULL }));
|
||||
EXPECT_TOKEN(lexer, (token{ 18446744073709551615ULL }));
|
||||
EXPECT_ERROR(lexer, "integer 18446744073709551616 is too big");
|
||||
EXPECT_EOF(lexer);
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(Comments,
|
||||
LexerTestingFixture,
|
||||
testing::Values(lexer_case("(/** skibidi **/func{//)\n}",
|
||||
{ { token_t::LParen }, { keyword_token::Func }, { token_t::LBrace }, { token_t::RBrace } })));
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(Integers,
|
||||
LexerTestingFixture,
|
||||
testing::Values(lexer_case("67 6\n7", { { 67 }, { 6 }, { 7 } }),
|
||||
lexer_case("18446744073709551615 18446744073709551616",
|
||||
{ { 18446744073709551615ULL }, token_r(std::string("integer 18446744073709551616 is too big")) })));
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(Tokens,
|
||||
LexerTestingFixture,
|
||||
testing::Values(lexer_case("()\n\t\t{\n}[\"shto-to\"]; :,.main return func",
|
||||
{ { token_t::LParen },
|
||||
{ token_t::RParen },
|
||||
{ token_t::LBrace },
|
||||
{ token_t::RBrace },
|
||||
{ token_t::LBracket },
|
||||
{ token_t::String, "shto-to"sv },
|
||||
{ token_t::RBracket },
|
||||
{ token_t::Semicolon },
|
||||
{ token_t::Colon },
|
||||
{ token_t::Comma },
|
||||
{ token_t::Dot },
|
||||
{ token_t::Identifier, "main"sv },
|
||||
{ keyword_token::Return },
|
||||
{ keyword_token::Func } })));
|
||||
|
||||
} // namespace
|
||||
|
||||
// NOLINTEND(readability-identifier-naming)
|
||||
@@ -1,5 +1,11 @@
|
||||
#include "furc/front/parser.hpp"
|
||||
|
||||
#include "furc/ast/declaration.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/expression.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/program.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -18,14 +18,26 @@ public:
|
||||
block() = default;
|
||||
public:
|
||||
template <typename T, typename... Args, typename = std::enable_if_t<std::is_base_of_v<instruction, T>>>
|
||||
void emplace(Args&&... args) {
|
||||
m_instructions.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
|
||||
bool emplace(Args&&... args) {
|
||||
if (has_exit()) return false;
|
||||
auto instr = std::make_unique<T>(std::forward<Args>(args)...);
|
||||
if (is_exit_instruction(instr->type())) {
|
||||
m_exit = std::move(instr);
|
||||
} else {
|
||||
m_instructions.emplace_back(std::move(instr));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<value_type>& instructions() { return m_instructions; }
|
||||
const std::vector<value_type>& instructions() const { return m_instructions; }
|
||||
|
||||
bool has_exit() const { return m_exit != nullptr; }
|
||||
value_type& exit() { return m_exit; }
|
||||
const value_type& exit() const { return m_exit; }
|
||||
private:
|
||||
std::vector<value_type> m_instructions;
|
||||
value_type m_exit;
|
||||
};
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -20,6 +20,15 @@ enum class instruction_t {
|
||||
Return,
|
||||
};
|
||||
|
||||
static inline bool is_exit_instruction(instruction_t type) {
|
||||
switch (type) {
|
||||
case instruction_t::Branch:
|
||||
case instruction_t::BranchCond:
|
||||
case instruction_t::Return: return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
class instruction {
|
||||
public:
|
||||
instruction() = default;
|
||||
|
||||
@@ -11,11 +11,13 @@ namespace ir {
|
||||
enum class operand_t {
|
||||
None,
|
||||
Register,
|
||||
Variable,
|
||||
Integer,
|
||||
String,
|
||||
};
|
||||
|
||||
using register_operand = std::uint32_t;
|
||||
using variable_operand = std::string;
|
||||
using integer_operand = std::uint64_t;
|
||||
using string_operand = std::string;
|
||||
|
||||
@@ -34,11 +36,14 @@ public:
|
||||
case operand_t::Register: {
|
||||
m_value.reg = other.m_value.reg;
|
||||
} break;
|
||||
case operand_t::Variable: {
|
||||
new (&m_value.variable) variable_operand(std::move(other.m_value.variable));
|
||||
} break;
|
||||
case operand_t::Integer: {
|
||||
m_value.integer = other.m_value.integer;
|
||||
} break;
|
||||
case operand_t::String: {
|
||||
new (&m_value.string) std::string(std::move(other.m_value.string));
|
||||
new (&m_value.string) string_operand(std::move(other.m_value.string));
|
||||
} break;
|
||||
}
|
||||
other.m_value.destroy(other.m_type);
|
||||
@@ -52,11 +57,14 @@ public:
|
||||
case operand_t::Register: {
|
||||
m_value.reg = other.m_value.reg;
|
||||
} break;
|
||||
case operand_t::Variable: {
|
||||
new (&m_value.variable) variable_operand(std::move(other.m_value.variable));
|
||||
} break;
|
||||
case operand_t::Integer: {
|
||||
m_value.integer = other.m_value.integer;
|
||||
} break;
|
||||
case operand_t::String: {
|
||||
new (&m_value.string) std::string(std::move(other.m_value.string));
|
||||
new (&m_value.string) string_operand(std::move(other.m_value.string));
|
||||
} break;
|
||||
}
|
||||
other.m_value.destroy(other.m_type);
|
||||
@@ -68,29 +76,31 @@ public:
|
||||
public:
|
||||
static operand new_reg(register_operand value) {
|
||||
operand operand;
|
||||
operand.m_type = operand_t::Register;
|
||||
new (&operand.m_value) union value(value);
|
||||
operand.m_type = operand_t::Register;
|
||||
operand.m_value.reg = value;
|
||||
return operand;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static operand new_variable(T&& value) {
|
||||
operand operand;
|
||||
operand.m_type = operand_t::Variable;
|
||||
new (&operand.m_value.variable) variable_operand(std::forward<T>(value));
|
||||
return operand;
|
||||
}
|
||||
|
||||
static operand new_integer(integer_operand value) {
|
||||
operand operand;
|
||||
operand.m_type = operand_t::Integer;
|
||||
new (&operand.m_value) union value(value);
|
||||
operand.m_type = operand_t::Integer;
|
||||
operand.m_value.integer = value;
|
||||
return operand;
|
||||
}
|
||||
|
||||
static operand new_string(const string_operand& value) {
|
||||
template <typename T>
|
||||
static operand new_string(T&& value) {
|
||||
operand operand;
|
||||
operand.m_type = operand_t::String;
|
||||
new (&operand.m_value) union value(value);
|
||||
return operand;
|
||||
}
|
||||
|
||||
static operand new_string(string_operand&& value) {
|
||||
operand operand;
|
||||
operand.m_type = operand_t::String;
|
||||
new (&operand.m_value) union value(std::move(value));
|
||||
new (&operand.m_value.string) string_operand(std::forward<T>(value));
|
||||
return operand;
|
||||
}
|
||||
public:
|
||||
@@ -103,6 +113,7 @@ public:
|
||||
switch (operand.m_type) {
|
||||
case operand_t::None: return os << "none";
|
||||
case operand_t::Register: return os << '%' << operand.m_value.reg;
|
||||
case operand_t::Variable: return os << operand.m_value.variable;
|
||||
case operand_t::Integer: return os << operand.m_value.integer;
|
||||
case operand_t::String: return os << '"' << operand.m_value.string << '"';
|
||||
}
|
||||
@@ -115,6 +126,7 @@ private:
|
||||
union value {
|
||||
std::nullptr_t null = nullptr;
|
||||
register_operand reg;
|
||||
variable_operand variable;
|
||||
integer_operand integer;
|
||||
string_operand string;
|
||||
|
||||
@@ -131,18 +143,6 @@ private:
|
||||
value() = default;
|
||||
~value() {}
|
||||
|
||||
value(register_operand reg)
|
||||
: reg(reg) {}
|
||||
|
||||
value(integer_operand integer)
|
||||
: integer(integer) {}
|
||||
|
||||
value(const std::string& string)
|
||||
: string(string) {}
|
||||
|
||||
value(std::string&& string)
|
||||
: string(std::move(string)) {}
|
||||
|
||||
value(value&&) noexcept = delete;
|
||||
value& operator=(value&&) noexcept = delete;
|
||||
value(const value&) = delete;
|
||||
|
||||
@@ -42,12 +42,12 @@ public:
|
||||
|
||||
explicit result(const error_type& error)
|
||||
: m_error(true) {
|
||||
m_value.error = error;
|
||||
new (&m_value.error) error_type(error);
|
||||
}
|
||||
|
||||
explicit result(error_type&& error)
|
||||
: m_error(true) {
|
||||
m_value.error = std::move(error);
|
||||
new (&m_value.error) error_type(std::move(error));
|
||||
}
|
||||
|
||||
~result() {
|
||||
@@ -58,17 +58,43 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
result(result&& other) noexcept {}
|
||||
result(result&& other) noexcept
|
||||
: m_error(other.m_error) {
|
||||
if (m_error) {
|
||||
new (&m_value.error) error_type(std::move(other.m_value.error));
|
||||
} else {
|
||||
new (&m_value.result) value_type(std::move(other.m_value.result));
|
||||
}
|
||||
}
|
||||
|
||||
result& operator=(result&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
m_error = other.m_error;
|
||||
if (m_error) {
|
||||
new (&m_value.error) error_type(std::move(other.m_value.error));
|
||||
} else {
|
||||
new (&m_value.result) value_type(std::move(other.m_value.result));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
result(const result& other) {}
|
||||
result(const result& other)
|
||||
: m_error(other.m_error) {
|
||||
if (m_error) {
|
||||
new (&m_value.error) error_type(other.m_value.error);
|
||||
} else {
|
||||
new (&m_value.result) value_type(other.m_value.result);
|
||||
}
|
||||
}
|
||||
|
||||
result& operator=(const result& other) {
|
||||
if (this == &other) return *this;
|
||||
m_error = other.m_error;
|
||||
if (m_error) {
|
||||
new (&m_value.error) error_type(other.m_value.error);
|
||||
} else {
|
||||
new (&m_value.result) value_type(other.m_value.result);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
@@ -134,6 +160,14 @@ private:
|
||||
union value {
|
||||
value_type result;
|
||||
error_type error;
|
||||
|
||||
value() {}
|
||||
~value() {}
|
||||
|
||||
value(value&&) noexcept {}
|
||||
value& operator=(value&&) noexcept {}
|
||||
value(const value&) {}
|
||||
value& operator=(const value&) {}
|
||||
} m_value;
|
||||
bool m_error = false;
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ arena::~arena() {
|
||||
region* next = nullptr;
|
||||
for (region* region = m_head; region != nullptr; region = next) {
|
||||
next = region->next;
|
||||
delete region;
|
||||
std::free(region); // NOLINT
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user