-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.hpp
140 lines (127 loc) · 3.87 KB
/
stmt.hpp
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
#ifndef LOX_STMT
#define LOX_STMT
#include "expr.cpp"
#include <stdexcept>
#include <vector>
#include <list>
#include <memory>
template <typename T>
class visitor;
template <typename T>
class Stmt {
public:
virtual ~Stmt() = default;
virtual void accept(visitor<T> *visitor) {return visitor->visit(this);}
std::unique_ptr<Expr<T>> expression;
};
template <typename T>
class Expression : public Stmt<T> {
public:
void accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
Expression(std::unique_ptr<Expr<T>> &expression) : expression(std::move(expression)) {}
std::unique_ptr<Expr<T>>expression;
~Expression() override = default;
};
template <typename T>
class Print : public Stmt<T> {
public:
Print(std::unique_ptr<Expr<T>> &expression) : expression(std::move(expression)) {}
void accept(visitor<T> *visitor) override
{
return visitor->visit(this);
}
std::unique_ptr<Expr<T>>expression;
~Print() override = default;
};
template <typename T>
class Var : public Stmt<T> {
public:
Var(token name, std::unique_ptr<Expr<T>> &initializer) : name(name), initializer(std::move(initializer)) {}
void accept(visitor<T> *visitor) {return visitor->visit(this);}
token name;
std::unique_ptr<Expr<T>>initializer;
~Var() override = default;
};
template <typename T>
class Block : public Stmt<T> {
public:
Block(std::vector<std::shared_ptr<Stmt<T>>> &statements) : statements(std::move(statements)) {}
Block() {}
~Block() override = default;
void accept(visitor<T> *visitor) {return visitor->visit(this);}
std::vector<std::shared_ptr<Stmt<T>>> statements;
};
template <typename T>
class If : public Stmt<T> {
public:
~If() override = default;
If(std::unique_ptr<Expr<T>> &condition, std::unique_ptr<Stmt<T>>&Then, std::unique_ptr<Stmt<T>>&Else) :
condition(std::move(condition)), Then(std::move(Then)), Else(std::move(Else)) {}
void accept(visitor<T> *visitor) {return visitor->visit(this);}
std::unique_ptr<Expr<T>>condition;
std::unique_ptr<Stmt<T>>Then;
std::unique_ptr<Stmt<T>>Else;
};
template <typename T>
class While : public Stmt<T> {
public:
While(std::unique_ptr<Expr<T>> &condition, std::unique_ptr<Stmt<T>> &Then) : condition(std::move(condition)), Then(std::move(Then)) {}
~While() override = default;
void accept(visitor<T> *visitor) {return visitor->visit(this);}
std::unique_ptr<Expr<T>>condition;
std::unique_ptr<Stmt<T>>Then;
};
template <typename T>
class Break : public Stmt<T>, std::exception {
public:
token keyword;
Break(token &keyword) : keyword(keyword) {}
Break() {}
void accept(visitor<T> *visitor) {return visitor->visit(this);}
};
template <typename T>
class Function : public Stmt<T> {
public:
void accept(visitor<T> *visitor) {return visitor->visit(this);}
token name;
std::vector<token> params;
std::vector<std::shared_ptr<Stmt<T>>> body;
Function(token name, std::vector<token> ¶ms, std::vector<std::shared_ptr<Stmt<T>>>&body) : name(name), params(std::move(params)), body(std::move(body)) {}
Function(std::vector<token> ¶ms, std::vector<std::shared_ptr<Stmt<T>>>&body) : params(std::move(params)), body(std::move(body)) {}
Function(Function<T> &&other)
{
this->name = other.name;
this->params = other.params;
this->body = std::move(other.body);
}
Function(Function<T> &other)
{
this->name = other.name;
this->params = other.params;
this->body = other.body;
}
const Function &operator=(const Function &other)
{
if (this == &other)
return *this;
this->name = other.name;
this->params = other.params;
this->body = other.body;
return *this;
}
~Function() override = default;
Function() {}
};
template <typename T>
class Return : public Stmt<T> {
public:
void accept(visitor<T> *visitor) {return visitor->visit(this);}
explicit Return(token &keyword, std::unique_ptr<Expr<T>> &value) : keyword(keyword), value(std::move(value)) {}
token keyword;
std::unique_ptr<Expr<T>>value ;
~Return() override = default;
};
#endif