Add if statement
Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -5,8 +5,6 @@
|
||||
#include "furc/ast/statement.hpp"
|
||||
#include "furc/front/token.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace furc {
|
||||
namespace ast {
|
||||
|
||||
@@ -45,27 +43,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 {
|
||||
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 +58,7 @@ public:
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
function_body_h m_body;
|
||||
body_h m_body;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -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,8 @@ 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>;
|
||||
|
||||
} // namespace ast
|
||||
} // namespace furc
|
||||
|
||||
@@ -10,6 +10,7 @@ enum class statement_node_t {
|
||||
Expression,
|
||||
Declaration,
|
||||
Return,
|
||||
If,
|
||||
};
|
||||
|
||||
class statement_node : public node {
|
||||
@@ -25,10 +26,10 @@ class return_statement_node : 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 +39,32 @@ 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 : 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;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -25,6 +25,7 @@ public:
|
||||
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_if_statement_node(const if_statement_node&) {}
|
||||
|
||||
virtual void visit_error(const node_handle<node>& handle) {}
|
||||
};
|
||||
|
||||
@@ -17,13 +17,14 @@ 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_if_statement_node(const ast::if_statement_node& node) 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;
|
||||
@@ -31,9 +32,12 @@ public:
|
||||
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;
|
||||
private:
|
||||
furlang::ir::module m_module;
|
||||
std::shared_ptr<furlang::ir::block> m_currentBlock;
|
||||
std::uint32_t m_registerCounter = 0;
|
||||
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;
|
||||
std::uint32_t m_registerCounter = 0;
|
||||
|
||||
std::unordered_map<std::string_view, std::uint32_t> m_variableMap;
|
||||
};
|
||||
|
||||
@@ -36,7 +36,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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user