-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.hh
More file actions
398 lines (347 loc) · 11.9 KB
/
statement.hh
File metadata and controls
398 lines (347 loc) · 11.9 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Copyright © 2016 Flamewing <flamewing.sonic@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "expression.hh"
#include "polymorphic_value.hh"
#include "util.hh"
#include <algorithm>
#include <experimental/iterator>
#include <map>
#include <ostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
class driver;
// Generic statement
class Statement {
public:
Statement() noexcept = default;
virtual ~Statement() noexcept = default;
Statement(Statement const&) = default;
Statement(Statement&&) noexcept = default;
auto operator=(Statement const&) -> Statement& = default;
auto operator=(Statement&&) noexcept -> Statement& = default;
auto write(std::ostream& out, size_t indent) const noexcept
-> std::ostream& {
return write_impl(out, indent);
}
[[nodiscard]] static auto getIndent(size_t indent) {
return std::string(indent, ' ');
}
private:
[[nodiscard]] virtual auto is_simple() const noexcept -> bool {
return true;
}
protected:
virtual auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& {
ignore_unused_variable_warning(indent);
return out;
}
};
// A generic statement containing content
class ContentStatement : public Statement {
public:
ContentStatement() = default;
explicit ContentStatement(std::string text) : content(std::move(text)) {}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
return out << getIndent(indent) << content << '\n';
}
private:
std::string content;
};
// A generic statement containing a choice
class ChoiceStatement : public Statement {
public:
ChoiceStatement() = default;
explicit ChoiceStatement(
std::string text, nonstd::polymorphic_value<Expression> cond = {})
: content(std::move(text)), condition(std::move(cond)) {}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
out << getIndent(indent) << "* ";
if (condition) {
out << "{ ";
condition->write(out, false) << " } ";
}
return out << content << '\n';
}
private:
std::string content;
nonstd::polymorphic_value<Expression> condition;
};
// A variable assignment statement
class AssignmentStatement : public Statement {
public:
explicit AssignmentStatement(
std::string name, nonstd::polymorphic_value<Expression> expr,
bool decl)
: varName(std::move(name)), expression(std::move(expr)),
declare(decl) {}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
out << getIndent(indent) << "~ ";
if (declare) {
out << "temp ";
}
out << varName;
return expression->write(out, false) << '\n';
}
private:
std::string varName;
nonstd::polymorphic_value<Expression> expression;
bool declare;
};
// A generic statement containing an expression
class ExpressionStatement : public Statement {
public:
ExpressionStatement() = default;
explicit ExpressionStatement(nonstd::polymorphic_value<Expression> expr)
: expression(std::move(expr)) {}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
out << getIndent(indent) << "~ ";
return expression->write(out, false) << '\n';
}
private:
nonstd::polymorphic_value<Expression> expression;
};
// A generic statement containing an expression
class ReturnStatement : public Statement {
public:
ReturnStatement() = default;
explicit ReturnStatement(nonstd::polymorphic_value<Expression> expr)
: expression(std::move(expr)) {}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
out << getIndent(indent) << "~ return ";
return expression->write(out, false) << '\n';
}
private:
nonstd::polymorphic_value<Expression> expression;
};
// A generic statement block statement
class BlockStatement : public Statement {
public:
using StatementList = std::vector<nonstd::polymorphic_value<Statement>>;
BlockStatement() = default;
void add_statement(nonstd::polymorphic_value<Statement> stmt) {
statements.emplace_back(std::move(stmt));
}
void steal_statements(StatementList& other) noexcept {
statements.swap(other);
}
auto get_statements() noexcept -> StatementList& {
return statements;
}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
for (auto const& elem : statements) {
elem->write(out, indent);
}
return out;
}
private:
StatementList statements;
};
// An if statement
class ElseStatement : public BlockStatement {
public:
ElseStatement() = default;
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
out << getIndent(indent) << "- else:\n";
return BlockStatement::write_impl(out, indent + 4);
}
};
// An if statement
class IfStatement : public Statement {
public:
IfStatement() = default;
IfStatement(
nonstd::polymorphic_value<Expression> cond,
nonstd::polymorphic_value<Statement> then)
: condExpr(std::move(cond)), thenStmt(std::move(then)) {}
IfStatement(
nonstd::polymorphic_value<Expression> cond,
nonstd::polymorphic_value<Statement> then,
nonstd::polymorphic_value<Statement> else_)
: condExpr(std::move(cond)), thenStmt(std::move(then)),
elseStmt(std::move(else_)) {}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
out << getIndent(indent) << "- ";
condExpr->write(out, false) << "\n";
thenStmt->write(out, indent + 4);
if (elseStmt) {
elseStmt->write(out, indent) << "\n";
}
return out;
}
private:
nonstd::polymorphic_value<Expression> condExpr;
nonstd::polymorphic_value<Statement> thenStmt;
nonstd::polymorphic_value<Statement> elseStmt;
};
// A global variable definition
class GlobalVariableStatement final : public Statement {
public:
GlobalVariableStatement() noexcept = default;
explicit GlobalVariableStatement(std::string name, std::string value)
: varName(std::move(name)), varValue(std::move(value)) {
add_global(varName);
}
static auto add_global(std::string var) -> bool {
return variables.insert(std::move(var)).second;
}
static auto is_global(const std::string& var) -> bool {
return variables.find(var) != variables.cend();
}
private:
// Global variables
static inline std::set<std::string> variables;
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& final {
ignore_unused_variable_warning(indent);
return out << "VAR " << varName << " = " << varValue << '\n';
}
private:
std::string varName;
std::string varValue;
};
// Base for knots, stitches, and functions
class TopLevelStatement : public BlockStatement {
public:
TopLevelStatement() noexcept = default;
TopLevelStatement(std::string name_, driver& drv_)
: drv(&drv_), name(std::move(name_)) {}
[[nodiscard]] auto getName() const noexcept -> std::string const& {
return name;
}
[[nodiscard]] auto has_variable(std::string const& var) const -> bool {
return headerVariables.find(var) != headerVariables.cend();
}
void add_variable(std::string const& var, bool ref) {
headerVariables.emplace(var, ref);
}
protected:
void init(std::string name_, driver& drv_) {
drv = &drv_;
name = std::move(name_);
}
auto write_header_base(std::ostream& out) const noexcept -> std::ostream& {
out << name;
if (!headerVariables.empty()) {
out << '(';
std::transform(
headerVariables.cbegin(), headerVariables.cend(),
std::experimental::make_ostream_joiner(out, ", "),
[](auto const& kvPair) {
auto const& [varName, isRef] = kvPair;
if (isRef) {
using namespace std::literals::string_literals;
return "ref "s + varName;
}
return varName;
});
out << ')';
}
return out;
}
virtual auto write_header(std::ostream& out) const noexcept
-> std::ostream& {
return out;
}
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& override {
write_header(out);
return BlockStatement::write_impl(out, indent) << '\n';
}
private:
driver* drv = nullptr;
std::string name;
std::map<std::string, bool> headerVariables;
};
// Class representing a stitch and all its statements
class StitchStatement final : public TopLevelStatement {
public:
StitchStatement() noexcept = default;
StitchStatement(std::string const& name_, driver& drv_) {
init(name_, drv_);
}
private:
auto write_header(std::ostream& out) const noexcept -> std::ostream& final {
out << "= ";
return write_header_base(out) << '\n';
}
};
// Class representing a knot and all its stitches and statements
class KnotStatement final : public TopLevelStatement {
public:
KnotStatement() noexcept = default;
KnotStatement(std::string const& name_, driver& drv_) {
init(name_, drv_);
}
void add_stitch(nonstd::polymorphic_value<StitchStatement> stitch) {
if (stitch->getName() == getName()) {
steal_statements(stitch->get_statements());
} else {
stitches.emplace_back(std::move(stitch));
}
}
private:
auto write_header(std::ostream& out) const noexcept -> std::ostream& final {
out << "=== ";
return write_header_base(out) << " ===\n";
}
protected:
auto write_impl(std::ostream& out, size_t indent) const noexcept
-> std::ostream& final {
write_header(out);
TopLevelStatement::write_impl(out, indent) << '\n';
for (auto const& elem : stitches) {
elem->write(out, indent);
}
return out << '\n';
}
private:
std::vector<nonstd::polymorphic_value<StitchStatement>> stitches;
};
// Class representing a function and all its statements
class FunctionStatement final : public TopLevelStatement {
public:
FunctionStatement() noexcept = default;
FunctionStatement(std::string const& name_, driver& drv_) {
init(name_, drv_);
}
private:
auto write_header(std::ostream& out) const noexcept -> std::ostream& final {
out << "=== function ";
return write_header_base(out) << " ===\n";
}
};