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) {}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user