cymoca
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
expression.h
Go to the documentation of this file.
1 #ifndef CYMOCA_AST_EXPRESSION_EXPRESSION_H_
2 #define CYMOCA_AST_EXPRESSION_EXPRESSION_H_
3 
4 #include <string>
5 #include "../node.h"
6 
7 namespace cymoca::ast::expression {
8 
12 class Reference : public Base {
13  protected:
14  std::string m_name;
15 
16  public:
18  virtual ~Reference() = default;
19  explicit Reference(std::string name) : m_name(std::move(name)) {}
20  const std::string &getName() const { return m_name; }
21  void setName(const std::string &name) { m_name = name; }
22  std::vector<INode *> getChildren() override { return {}; }
23  std::unique_ptr<INode> clone() const override {
24  return std::make_unique<Reference>(getName());
25  }
26 };
27 
28 #define BINARY_EXPR_MACRO(NAME) \
29  class NAME : public TBinary<Base, Base, Base> { \
30  public: \
31  NODE_MACRO \
32  using TBinary::TBinary; \
33  std::unique_ptr<INode> clone() const override { \
34  return cloneBinary<NAME>(); \
35  } \
36  };
37 
42 
43 
47 
52 
57 
61 class Number : public Base {
62  protected:
63  double _value;
64 
65  public:
67  explicit Number(double value) : _value(value) {}
68  double getValue() const { return _value; }
69  void setValue(double value) { _value = value; }
70  std::vector<INode *> getChildren() override { return {}; }
71  std::unique_ptr<INode> clone() const override {
72  return std::make_unique<Number>(getValue());
73  }
74 };
75 
79 class List : public TList<Base, INode> {
80  public:
82  using TList::TList;
83  std::unique_ptr<INode> clone() const override { return cloneList<List>(); }
84 };
85 
89 class Function : public Base {
90  protected:
91  std::unique_ptr<Reference> m_ref;
92  std::unique_ptr<List> m_args;
93 
94  public:
96  explicit Function(std::unique_ptr<Reference> ref, std::unique_ptr<List> args)
97  : m_ref(std::move(ref)), m_args(std::move(args)) {}
98  const Reference &getReference() const { return *m_ref; }
99  std::vector<INode *> getChildren() override {
100  return {m_ref.get(), m_args.get()};
101  }
102  std::unique_ptr<INode> clone() const override {
103  return std::make_unique<Function>(m_ref->cloneAs<Reference>(),
104  m_args->cloneAs<List>());
105  }
106 };
107 
108 #define UNARY_EXPR_MACRO(NAME) \
109  class NAME : public TUnary<Base, Base> { \
110  public: \
111  NODE_MACRO \
112  using TUnary::TUnary; \
113  std::unique_ptr<INode> clone() const override { \
114  return cloneUnary<NAME>(); \
115  } \
116  };
117 
122 
123 } // namespace cymoca::ast::expression
124 #endif
std::vector< INode * > getChildren() override
Definition: expression.h:22
std::unique_ptr< INode > clone() const override
Definition: expression.h:83
std::vector< INode * > getChildren() override
Definition: expression.h:99
std::unique_ptr< INode > clone() const override
Definition: expression.h:102
std::vector< INode * > getChildren() override
Definition: expression.h:70
const Reference & getReference() const
Definition: expression.h:98
std::unique_ptr< Reference > m_ref
Definition: expression.h:91
Function(std::unique_ptr< Reference > ref, std::unique_ptr< List > args)
Definition: expression.h:96
const std::string & getName() const
Definition: expression.h:20
void setValue(double value)
Definition: expression.h:69
#define NODE_MACRO
Definition: node.h:15
#define UNARY_EXPR_MACRO(NAME)
Definition: expression.h:108
void setName(const std::string &name)
Definition: expression.h:21
std::unique_ptr< INode > clone() const override
Definition: expression.h:71
std::unique_ptr< INode > clone() const override
Definition: expression.h:23
#define BINARY_EXPR_MACRO(NAME)
Definition: expression.h:28
std::unique_ptr< List > m_args
Definition: expression.h:92