Alexandria  2.22.0
Please provide a description of the project.
Node.h
Go to the documentation of this file.
1 
19 #ifndef PYSTON_NODE_H
20 #define PYSTON_NODE_H
21 
22 #include <boost/any.hpp>
23 #include <boost/variant.hpp>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <typeindex>
28 #include <vector>
29 
30 namespace Pyston {
31 
32 // Forward declaration
33 class Visitor;
34 
39 class NodeBase {
40 public:
46  explicit NodeBase(const std::type_index& type_index) : m_type_index{type_index} {}
47 
51  virtual ~NodeBase() = default;
52 
60  virtual std::string repr() const = 0;
61 
65  virtual void visit(Visitor&) const = 0;
66 
71  const std::type_index& type() const {
72  return m_type_index;
73  }
74 
75 private:
77 };
78 
82 using Attribute = boost::variant<bool, int64_t, double>;
84 
88 using Value = boost::variant<bool, int64_t, double, AttributeSet>;
89 
95 
100 
104 template <typename T>
105 class Node : public NodeBase {
106 public:
110  Node() : NodeBase(typeid(T)) {}
111 
115  virtual ~Node() = default;
116 
129  virtual T eval(const Context&, const Arguments&) const = 0;
130 
131  template <typename... Args>
132  T eval(const Context& context, Args... args) const {
133  Arguments arguments;
134  return eval_helper(context, arguments, args...);
135  }
136 
137  template <typename... Args>
138  T eval(Args... args) const {
139  return eval(Context{}, args...);
140  }
141 
142 protected:
143  T eval_helper(const Context& context, Arguments& arguments) const {
144  return eval(context, arguments);
145  }
146 
147  template <typename A0, typename... AN>
148  T eval_helper(const Context& context, Arguments& arguments, A0 v0, AN... an) const {
149  arguments.push_back(v0);
150  return eval_helper(context, arguments, an...);
151  }
152 };
153 
160 class Visitor {
161 public:
165  virtual void enter(const NodeBase*) = 0;
166 
170  virtual void exit(const NodeBase*) = 0;
171 };
172 
173 } // end of namespace Pyston
174 
175 #endif // PYSTON_NODE_H
const std::type_index m_type_index
Definition: Node.h:76
virtual void visit(Visitor &) const =0
virtual std::string repr() const =0
NodeBase(const std::type_index &type_index)
Definition: Node.h:46
virtual ~NodeBase()=default
const std::type_index & type() const
Definition: Node.h:71
virtual ~Node()=default
virtual T eval(const Context &, const Arguments &) const =0
T eval_helper(const Context &context, Arguments &arguments, A0 v0, AN... an) const
Definition: Node.h:148
T eval_helper(const Context &context, Arguments &arguments) const
Definition: Node.h:143
T eval(Args... args) const
Definition: Node.h:138
T eval(const Context &context, Args... args) const
Definition: Node.h:132
virtual void enter(const NodeBase *)=0
virtual void exit(const NodeBase *)=0
boost::variant< bool, int64_t, double, AttributeSet > Value
Definition: Node.h:88
boost::variant< bool, int64_t, double > Attribute
Definition: Node.h:82
T push_back(T... args)