-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.cpp
More file actions
186 lines (163 loc) · 4.35 KB
/
expr.cpp
File metadata and controls
186 lines (163 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef EXPR_CPP
#define EXPR_CPP
#include "token.cpp"
#include <any>
#include <memory>
#include <vector>
template <typename T>
class visitor;
template <typename T>
struct Expr {
Expr() {};
virtual T accept(visitor<T> *visitor)
{
return visitor->visit(this);
}
virtual ~Expr() {};
};
template <typename T>
class Binary : public Expr<T>{
public:
std::unique_ptr<Expr<T>>left;
token Operator;
std::unique_ptr<Expr<T>>right;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Binary(std::unique_ptr<Expr<T>> &left, token Operator, std::unique_ptr<Expr<T>> &right)
: left(std::move(left)), Operator(Operator), right(std::move(right)) {}
virtual ~Binary() = default;
};
template <typename T>
class Grouping: public Expr<T>{
public:
std::unique_ptr<Expr<T>>expression;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Grouping(std::unique_ptr<Expr<T>>expression) : expression(std::move(expression)) {}
~Grouping() override = default;
};
template <typename T>
class Literal : public Expr<T>{
public:
std::any value;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Literal(std::any value) : value(value) {}
Literal() {}
virtual ~Literal() override = default;
};
template <typename T>
class Unary : public Expr<T> {
public:
token Operator;
std::unique_ptr<Expr<T>>right;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Unary(token &Operator, std::unique_ptr<Expr<T>> &right) :
Operator(Operator), right(std::move(right)) {}
~Unary() override = default;
};
template <typename T>
class Variable : public Expr<T> {
public:
token name;
Variable(token name) : name(name) {};
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
~Variable() override = default;
};
template <typename T>
class Assign : public Expr<T> {
public:
token name;
std::unique_ptr<Expr<T>> value;
Assign(token name, std::unique_ptr<Expr<T>> &value) :
name(name), value(std::move(value)) {}
T accept(visitor <T> *visitor) override
{
return visitor->visit(this);
}
~Assign() override = default;
};
template <typename T>
class Logical : public Expr<T> {
public:
std::unique_ptr<Expr<T>>left;
token Operator;
std::unique_ptr<Expr<T>> right ;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Logical(std::unique_ptr<Expr<T>> &left, token Operator, std::unique_ptr<Expr<T>> &right) :
left(std::move(left)), Operator(Operator), right(std::move(right)) {}
virtual ~Logical() override = default;
};
template <typename T>
struct Ternary : public Expr<T> {
std::unique_ptr<Expr<T>>condition ;
std::unique_ptr<Expr<T>>then ;
std::unique_ptr<Expr<T>>_else ;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Ternary(std::unique_ptr<Expr<T>> &condition, std::unique_ptr<Expr<T>> &then, std::unique_ptr<Expr<T>> &_else) : condition(std::move(condition)), then(std::move(then)), _else(std::move(_else)) {}
virtual ~Ternary() override = default;
};
template <typename T>
struct Increment : public Expr<T> {
token target_name;
token Operator;
bool postfix;
T accept(visitor<T> *visitor) override {return visitor->visit(this); }
std::unique_ptr<Expr<T>>clone()
{
return new Increment<T>(*this);
}
Increment(const token &target_name, const token &Operator, const bool &postfix) : target_name(target_name), Operator(Operator), postfix(postfix) {}
~Increment() override = default;
};
template <typename T>
struct Call : public Expr<T> {
std::unique_ptr<Expr<T>>callee ;
token paren;
std::vector<std::unique_ptr<Expr<T>>> arguments;
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Call(std::unique_ptr<Expr<T>> &callee, token paren, std::vector<std::unique_ptr<Expr<T>>> &arguments) : callee(std::move(callee)), paren(paren), arguments(std::move(arguments)) {}
virtual ~Call() override = default;
};
template<typename T>
struct Function;
template <typename T>
struct Lambda : public Expr<T> {
std::unique_ptr<Function<T>> declaration;
Lambda(std::unique_ptr<Function<T>> &declaration) : declaration(std::move(declaration)) {}
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
};
template <typename T>
struct This_expr : public Expr<T> {
token Token;
This_expr(token Token) : Token(Token) {}
T accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
};
#endif