forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContinuation.h
272 lines (242 loc) · 8.07 KB
/
Continuation.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2020 Telegram Systems LLP
*/
#pragma once
#include "common/refcnt.hpp"
#include "common/refint.h"
#include "vm/stack.hpp"
namespace fift {
using td::Ref;
struct IntCtx;
/*
*
* FIFT CONTINUATIONS
*
*/
class FiftCont : public td::CntObject {
public:
FiftCont() = default;
virtual ~FiftCont() override = default;
virtual Ref<FiftCont> run_tail(IntCtx& ctx) const = 0;
virtual Ref<FiftCont> run_modify(IntCtx& ctx) {
return run_tail(ctx);
}
virtual Ref<FiftCont> handle_tail(IntCtx& ctx) const {
return {};
}
virtual Ref<FiftCont> handle_modify(IntCtx& ctx) {
return handle_tail(ctx);
}
virtual Ref<FiftCont> up() const {
return {};
}
virtual bool is_list() const {
return false;
}
virtual long long list_size() const {
return -1;
}
virtual const Ref<FiftCont>* get_list() const {
return nullptr;
}
virtual bool is_literal() const {
return false;
}
virtual int literal_count() const {
return -1;
}
virtual std::vector<vm::StackEntry> get_literals() const {
return {};
}
std::string get_dict_name(const IntCtx& ctx) const;
bool print_dict_name(std::ostream& os, const IntCtx& ctx) const;
bool print_dummy_name(std::ostream& os, const IntCtx& ctx) const;
virtual bool print_name(std::ostream& os, const IntCtx& ctx) const;
virtual bool dump(std::ostream& os, const IntCtx& ctx) const;
Ref<FiftCont> self() const {
return Ref<FiftCont>{this};
}
};
class QuitCont : public FiftCont {
int exit_code;
public:
QuitCont(int _exit_code = -1) : exit_code(_exit_code) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
};
class SeqCont : public FiftCont {
Ref<FiftCont> first, second;
public:
SeqCont(Ref<FiftCont> _first, Ref<FiftCont> _second) : first(std::move(_first)), second(std::move(_second)) {
}
static Ref<FiftCont> seq(Ref<FiftCont> _first, Ref<FiftCont> _second) {
return _second.is_null() ? std::move(_first) : td::make_ref<SeqCont>(std::move(_first), std::move(_second));
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
Ref<FiftCont> up() const override {
return second;
}
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
bool dump(std::ostream& os, const IntCtx& ctx) const override;
};
class TimesCont : public FiftCont {
Ref<FiftCont> body, after;
int count;
public:
TimesCont(Ref<FiftCont> _body, Ref<FiftCont> _after, int _count)
: body(std::move(_body)), after(std::move(_after)), count(_count) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
Ref<FiftCont> up() const override {
return after;
}
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
bool dump(std::ostream& os, const IntCtx& ctx) const override;
};
class UntilCont : public FiftCont {
Ref<FiftCont> body, after;
public:
UntilCont(Ref<FiftCont> _body, Ref<FiftCont> _after) : body(std::move(_body)), after(std::move(_after)) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
bool dump(std::ostream& os, const IntCtx& ctx) const override;
};
class WhileCont : public FiftCont {
Ref<FiftCont> cond, body, after;
bool stage;
public:
WhileCont(Ref<FiftCont> _cond, Ref<FiftCont> _body, Ref<FiftCont> _after, bool _stage = false)
: cond(std::move(_cond)), body(std::move(_body)), after(std::move(_after)), stage(_stage) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
Ref<FiftCont> up() const override {
return after;
}
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
bool dump(std::ostream& os, const IntCtx& ctx) const override;
};
class LoopCont : public FiftCont {
Ref<FiftCont> func, after;
int state;
public:
LoopCont(Ref<FiftCont> _func, Ref<FiftCont> _after, int _state = 0)
: func(std::move(_func)), after(std::move(_after)), state(_state) {
}
LoopCont(const LoopCont&) = default;
virtual bool init(IntCtx& ctx) {
return true;
}
virtual bool pre_exec(IntCtx& ctx) {
return true;
}
virtual bool post_exec(IntCtx& ctx) {
return true;
}
virtual bool finalize(IntCtx& ctx) {
return true;
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
Ref<FiftCont> up() const override {
return after;
}
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
};
class GenericLitCont : public FiftCont {
public:
bool is_literal() const override {
return true;
}
std::vector<vm::StackEntry> get_literals() const override = 0;
bool print_name(std::ostream& os, const IntCtx& ctx) const override;
};
class SmallIntLitCont : public GenericLitCont {
long long value_;
public:
SmallIntLitCont(long long value) : value_(value) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
std::vector<vm::StackEntry> get_literals() const override;
static Ref<FiftCont> literal(long long int_value) {
return td::make_ref<SmallIntLitCont>(int_value);
}
};
class IntLitCont : public GenericLitCont {
td::RefInt256 value_;
public:
IntLitCont(td::RefInt256 value) : value_(std::move(value)) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
std::vector<vm::StackEntry> get_literals() const override {
return {vm::StackEntry(value_)};
}
static Ref<FiftCont> literal(td::RefInt256 int_value);
static Ref<FiftCont> literal(long long int_value) {
return SmallIntLitCont::literal(int_value);
}
};
class LitCont : public GenericLitCont {
vm::StackEntry value_;
public:
LitCont(const vm::StackEntry& value) : value_(value) {
}
LitCont(vm::StackEntry&& value) : value_(std::move(value)) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
std::vector<vm::StackEntry> get_literals() const override {
return {value_};
}
static Ref<FiftCont> literal(vm::StackEntry value);
static Ref<FiftCont> literal(td::RefInt256 int_value) {
return IntLitCont::literal(std::move(int_value));
}
static Ref<FiftCont> literal(long long int_value) {
return SmallIntLitCont::literal(int_value);
}
};
class MultiLitCont : public GenericLitCont {
std::vector<vm::StackEntry> values_;
public:
MultiLitCont(const std::vector<vm::StackEntry>& values) : values_(values) {
}
MultiLitCont(std::vector<vm::StackEntry>&& values) : values_(std::move(values)) {
}
MultiLitCont(std::initializer_list<vm::StackEntry> value_list) : values_(value_list) {
}
Ref<FiftCont> run_tail(IntCtx& ctx) const override;
Ref<FiftCont> run_modify(IntCtx& ctx) override;
std::vector<vm::StackEntry> get_literals() const override {
return values_;
}
MultiLitCont& push_back(vm::StackEntry new_literal);
vm::StackEntry at(int idx) const;
};
class InterpretCont : public FiftCont {
public:
InterpretCont() = default;
Ref<FiftCont> run_tail(IntCtx&) const override; // text interpreter, defined in words.cpp
bool print_name(std::ostream& os, const IntCtx& ctx) const override {
os << "<text interpreter continuation>";
return true;
}
};
} // namespace fift