Introduce binop_expression_node
Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -8,7 +8,8 @@ namespace furc {
|
|||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
enum class expression_node_t {
|
enum class expression_node_t {
|
||||||
Literal
|
Literal,
|
||||||
|
Binop
|
||||||
};
|
};
|
||||||
|
|
||||||
class expression_node : public statement_node {
|
class expression_node : public statement_node {
|
||||||
@@ -22,6 +23,38 @@ protected:
|
|||||||
bool equal(const node& rhs) const override;
|
bool equal(const node& rhs) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using expression_node_h = node_handle<expression_node>;
|
||||||
|
|
||||||
|
enum class binop_expression_node_t {
|
||||||
|
Add,
|
||||||
|
Sub,
|
||||||
|
Mul,
|
||||||
|
Div,
|
||||||
|
Mod,
|
||||||
|
};
|
||||||
|
|
||||||
|
class binop_expression_node : 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)) {}
|
||||||
|
|
||||||
|
binop_expression_node_t type() const { return m_type; };
|
||||||
|
const expression_node_h& lhs() const { return m_lhs; };
|
||||||
|
const expression_node_h& rhs() const { return m_rhs; };
|
||||||
|
public:
|
||||||
|
expression_node_t expression_type() const override { return expression_node_t::Binop; }
|
||||||
|
|
||||||
|
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_h m_lhs;
|
||||||
|
expression_node_h m_rhs;
|
||||||
|
};
|
||||||
|
|
||||||
|
using binop_expression_node_h = node_handle<binop_expression_node>;
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,25 @@ bool expression_node::equal(const node& rhs) const {
|
|||||||
return expression_type() == reinterpret_cast<const expression_node&>(rhs).expression_type();
|
return expression_type() == reinterpret_cast<const expression_node&>(rhs).expression_type();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, binop_expression_node_t type) {
|
||||||
|
switch (type) {
|
||||||
|
case binop_expression_node_t::Add: return os << '+';
|
||||||
|
case binop_expression_node_t::Sub: return os << '-';
|
||||||
|
case binop_expression_node_t::Mul: return os << '*';
|
||||||
|
case binop_expression_node_t::Div: return os << '/';
|
||||||
|
case binop_expression_node_t::Mod: return os << '%';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& binop_expression_node::print(std::ostream& os) const {
|
||||||
|
return os << *m_lhs << ' ' << m_type << ' ' << *m_rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool binop_expression_node::equal(const node& rhsNode) const {
|
||||||
|
const auto& rhs = reinterpret_cast<const binop_expression_node&>(rhsNode);
|
||||||
|
return expression_node::equal(rhsNode) && m_type == rhs.m_type && m_lhs == rhs.m_lhs && m_rhs == rhs.m_rhs;
|
||||||
|
}
|
||||||
|
|
||||||
bool declaration_node::equal(const node& rhs) const {
|
bool declaration_node::equal(const node& rhs) const {
|
||||||
return declaration_type() == reinterpret_cast<const declaration_node&>(rhs).declaration_type();
|
return declaration_type() == reinterpret_cast<const declaration_node&>(rhs).declaration_type();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user