commit c2bf5312aab77745da9b24a4939bdd0ce5640766
parent ab68dc5dca69cdb3bf52369039df0645ad02d29c
Author: Mohammad-Reza Nabipoor <m.nabipoor@yahoo.com>
Date: Fri, 28 Aug 2020 17:18:31 +0430
kaleidoscope_ast.hpp: Add node_type func to ASTNode
Return the type of the value inside the polymorphic type `ASTNode`.
Diffstat:
| M | kaleidoscope_ast.hpp | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 79 insertions(+), 0 deletions(-)
diff --git a/kaleidoscope_ast.hpp b/kaleidoscope_ast.hpp
@@ -32,6 +32,70 @@ to_string(const Prototype&);
std::string
to_string(const Function&);
+//---
+
+enum class NodeType
+{
+ None = -1,
+ Number,
+ Variable,
+ BinaryOp,
+ Call,
+ Prototype,
+ Function,
+};
+
+template<typename T>
+inline NodeType
+node_type(const T&)
+{
+ return NodeType::None;
+}
+
+template<>
+inline NodeType
+node_type(const Number&)
+{
+ return NodeType::Number;
+}
+
+template<>
+inline NodeType
+node_type(const Variable&)
+{
+ return NodeType::Variable;
+}
+
+template<>
+inline NodeType
+node_type(const BinaryOp&)
+{
+ return NodeType::BinaryOp;
+}
+
+template<>
+inline NodeType
+node_type(const Call&)
+{
+ return NodeType::Call;
+}
+
+template<>
+inline NodeType
+node_type(const Prototype&)
+{
+ return NodeType::Prototype;
+}
+
+template<>
+inline NodeType
+node_type(const Function&)
+{
+ return NodeType::Function;
+}
+
+//---
+
// https://youtu.be/QGcVXgEVMJg?t=49m13s
class ASTNode
{
@@ -58,6 +122,13 @@ public:
return n.self_->codegen();
}
+ friend NodeType node_type(const ASTNode& n)
+ {
+ assert(n.self_ != nullptr);
+
+ return n.self_->node_type();
+ }
+
friend bool operator==(const ASTNode& x, const ASTNode& y)
{
assert(x.self_ != nullptr && y.self_ != nullptr);
@@ -76,6 +147,7 @@ private:
virtual ~Concept() = default;
virtual std::string to_string(void) const = 0;
virtual llvm::Value* codegen(void) const = 0;
+ virtual NodeType node_type(void) const = 0;
};
template<typename T>
@@ -99,6 +171,13 @@ private:
return codegen(value_);
}
+ NodeType node_type(void) const override
+ {
+ using kal::node_type;
+
+ return node_type(value_);
+ }
+
T value_;
};