-
Notifications
You must be signed in to change notification settings - Fork 3
/
manool.cc
448 lines (428 loc) · 27.4 KB
/
manool.cc
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// manool.cc -- MANOOL plug-in API
/* Copyright (C) 2018, 2019, 2020 Alexey Protasov (AKA Alex or rusini)
This file is part of MANOOL.
MANOOL is free software: you can redistribute it and/or modify it under the terms of the version 3 of the GNU General Public License
as published by the Free Software Foundation (and only version 3).
MANOOL 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 MANOOL. If not, see <https://www.gnu.org/licenses/>. */
# include "config.tcc"
# include "base.tcc"
# include <cstdio> // stderr, fprintf, fflush
# include <deque>
namespace MNL_AUX_UUID { using namespace aux;
namespace aux {
using std::_Exit; // <cstdlib>
using std::fprintf; using std::fflush; // <cstdio>
using std::deque;
}
// Compile-time accounting
MNL_IF_WITH_MT(thread_local) decltype(tmp_cnt) pub::tmp_cnt; // count for current frame layout
MNL_IF_WITH_MT(thread_local) decltype(tmp_ids) pub::tmp_ids; // all temporaries
// Run-time accounting
MNL_IF_WITH_MT(thread_local) decltype(tmp_stk) pub::tmp_stk; // stack
MNL_IF_WITH_MT(thread_local) decltype(tmp_frm) pub::tmp_frm; // frame pointer
namespace aux {
code optimize(expr_lit<>);
code optimize(expr_apply0<>), optimize(expr_apply1<>), optimize(expr_apply2<>), optimize(expr_apply3<>), optimize(expr_apply4<>);
}
code pub::make_lit(const val &value) { return optimize(expr_lit<>{value}); }
code aux::compile_apply(code &&target, const form &form, const loc &_loc) {
if (form.size() - 1 > val::max_argc) MNL_ERR(MNL_SYM("LimitExceeded"));
opt1: // Application without input/output arguments
for (auto &&el: form + 1)
if (!el.is_list() || el.empty() || el[0] != MNL_SYM("?")); else goto opt2;
{ vector<code> args; args.reserve(form.size() - 1);
for (auto &&el: form + 1) args.push_back(compile_rval(el, _loc));
struct expr_apply5 { MNL_LVALUE(target.is_lvalue())
code target, a0, a1, a2, a3, a4; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv[]{a0.execute(), a1.execute(), a2.execute(), a3.execute(), a4.execute()}, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv); } catch (...) { trace_execute(_loc); }
}
MNL_INLINE void exec_in(val &&value) const {
target.exec_in([&]()->val{
val argv[]{move(value), a0.execute(), a1.execute(), a2.execute(), a3.execute(), a4.execute(), target.exec_out()};
argv->swap(argv[std::extent<decltype(argv)>::value - 1]);
try { return MNL_SYM("Repl")(std::extent<decltype(argv)>::value, argv); } catch (...) { trace_exec_in(_loc); }
}());
}
MNL_INLINE val exec_out() const {
val argv_out[7];
target.exec_in([&]()->val{
val argv[]{{}, a0.execute(), a1.execute(), a2.execute(), a3.execute(), a4.execute(), target.exec_out()};
argv->swap(argv[std::extent<decltype(argv)>::value - 1]);
try { return MNL_SYM("Repl")(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_exec_out(_loc); }
}());
return move(argv_out[std::extent<decltype(argv_out)>::value - 1]);
}
};
struct expr_apply6 { MNL_LVALUE(target.is_lvalue())
code target, a0, a1, a2, a3, a4, a5; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv[]{a0.execute(), a1.execute(), a2.execute(), a3.execute(), a4.execute(), a5.execute()}, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv); } catch (...) { trace_execute(_loc); }
}
MNL_INLINE void exec_in(val &&value) const {
target.exec_in([&]()->val{
val argv[]{move(value), a0.execute(), a1.execute(), a2.execute(), a3.execute(), a4.execute(), a5.execute(), target.exec_out()};
argv->swap(argv[std::extent<decltype(argv)>::value - 1]);
try { return MNL_SYM("Repl")(std::extent<decltype(argv)>::value, argv); } catch (...) { trace_exec_in(_loc); }
}());
}
MNL_INLINE val exec_out() const {
val argv_out[8];
target.exec_in([&]()->val{
val argv[]{{}, a0.execute(), a1.execute(), a2.execute(), a3.execute(), a4.execute(), a5.execute(), target.exec_out()};
argv->swap(argv[std::extent<decltype(argv)>::value - 1]);
try { return MNL_SYM("Repl")(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_exec_out(_loc); }
}());
return move(argv_out[std::extent<decltype(argv_out)>::value - 1]);
}
};
switch (args.size()) {
case 0: return optimize(expr_apply0<>{move(target), _loc});
case 1: return optimize(expr_apply1<>{move(target), move(args[0]), _loc});
case 2: return optimize(expr_apply2<>{move(target), move(args[0]), move(args[1]), _loc});
case 3: return optimize(expr_apply3<>{move(target), move(args[0]), move(args[1]), move(args[2]), _loc});
case 4: return optimize(expr_apply4<>{move(target), move(args[0]), move(args[1]), move(args[2]), move(args[3]), _loc});
case 5: return expr_apply5{move(target), move(args[0]), move(args[1]), move(args[2]), move(args[3]), move(args[4]), _loc};
case 6: return expr_apply6{move(target), move(args[0]), move(args[1]), move(args[2]), move(args[3]), move(args[4]), move(args[5]), _loc};
}
struct expr_apply { MNL_LVALUE(target.is_lvalue())
code target; vector<code> args; loc _loc; // implementation-defined destruction order for "args"
public:
MNL_INLINE val execute(bool) const {
int argc = args.size(); auto args = this->args.data();
val argv[argc];
for (int sn = 0; sn < argc; ++sn) args[sn].execute().swap(argv[sn]); val target = this->target.execute();
try { return move(target)(argc, argv); } catch (...) { trace_execute(_loc); }
}
MNL_INLINE void exec_in(val &&value) const {
int argc = args.size(); auto args = this->args.data();
target.exec_in([&]()->val{
val argv[argc + 2];
for (int sn = 0; sn < argc; ++sn) args[sn].execute().swap(argv[sn + 1]); target.exec_out().swap(argv[0]); value.swap(argv[argc + 1]);
try { return MNL_SYM("Repl")(argc, argv); } catch (...) { trace_exec_in(_loc); }
}());
}
MNL_INLINE val exec_out() const {
int argc = args.size(); auto args = this->args.data();
val argv_out[argc + 2];
target.exec_in([&]()->val{
val argv[argc + 2];
for (int sn = 0; sn < argc; ++sn) args[sn].execute().swap(argv[sn + 1]); target.exec_out().swap(argv[0]);
try { return MNL_SYM("Repl")(argc, argv, argv_out); } catch (...) { trace_exec_out(_loc); }
}());
return move(argv_out[argc + 1]);
}
};
return expr_apply{move(target), move(args), _loc};
}
opt2: // Application with at least one input/output argument
for (auto &&el: form + 1)
if (!el.is_list() || el.empty() || el[0] != MNL_SYM("?") || el.size() == 2); else goto opt3;
{ vector<pair<code, bool>> args; args.reserve(form.size() - 1);
for (auto &&el: form + 1) args.push_back(el.is_list() && el.size() == 2 && el[0] == MNL_SYM("?") ?
make_pair(compile_lval(el[1], _loc), true) : make_pair(compile_rval(el, _loc), false));
switch (args.size()) {
{ struct expr { MNL_RVALUE() // 1 argument
code target, args[1]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public: // "const" might help Alias Analysis and it's definitely harmless here (but in fact, compilers seem to ignore it)
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 1: return expr{move(target),
move(args[0].first),
move(args[0].second), _loc};
}
{ struct expr { MNL_RVALUE() // 2 arguments
code target, args[2]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 2: return expr{move(target),
move(args[0].first), move(args[1].first),
move(args[0].second), move(args[1].second), _loc};
}
{ struct expr { MNL_RVALUE() // 3 arguments
code target, args[3]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute(),
MNL_UNLIKELY(mods[2]) ? args[2].exec_out() : args[2].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[2])) args[2].exec_in(move(argv_out[2]));
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 3: return expr{move(target),
move(args[0].first), move(args[1].first), move(args[2].first),
move(args[0].second), move(args[1].second), move(args[2].second), _loc};
}
{ struct expr { MNL_RVALUE() // 4 arguments
code target, args[4]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute(),
MNL_UNLIKELY(mods[2]) ? args[2].exec_out() : args[2].execute(),
MNL_UNLIKELY(mods[3]) ? args[3].exec_out() : args[3].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[3])) args[3].exec_in(move(argv_out[3]));
if (MNL_UNLIKELY(mods[2])) args[2].exec_in(move(argv_out[2]));
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 4: return expr{move(target),
move(args[0].first), move(args[1].first), move(args[2].first), move(args[3].first),
move(args[0].second), move(args[1].second), move(args[2].second), move(args[3].second), _loc};
}
{ struct expr { MNL_RVALUE() // 5 arguments
code target, args[5]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute(),
MNL_UNLIKELY(mods[2]) ? args[2].exec_out() : args[2].execute(),
MNL_UNLIKELY(mods[3]) ? args[3].exec_out() : args[3].execute(),
MNL_UNLIKELY(mods[4]) ? args[4].exec_out() : args[4].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[4])) args[4].exec_in(move(argv_out[4]));
if (MNL_UNLIKELY(mods[3])) args[3].exec_in(move(argv_out[3]));
if (MNL_UNLIKELY(mods[2])) args[2].exec_in(move(argv_out[2]));
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 5: return expr{move(target),
move(args[0].first), move(args[1].first), move(args[2].first), move(args[3].first), move(args[4].first),
move(args[0].second), move(args[1].second), move(args[2].second), move(args[3].second), move(args[4].second), _loc};
}
{ struct expr { MNL_RVALUE() // 6 arguments
code target, args[6]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute(),
MNL_UNLIKELY(mods[2]) ? args[2].exec_out() : args[2].execute(),
MNL_UNLIKELY(mods[3]) ? args[3].exec_out() : args[3].execute(),
MNL_UNLIKELY(mods[4]) ? args[4].exec_out() : args[4].execute(),
MNL_UNLIKELY(mods[5]) ? args[5].exec_out() : args[5].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[5])) args[5].exec_in(move(argv_out[5]));
if (MNL_UNLIKELY(mods[4])) args[4].exec_in(move(argv_out[4]));
if (MNL_UNLIKELY(mods[3])) args[3].exec_in(move(argv_out[3]));
if (MNL_UNLIKELY(mods[2])) args[2].exec_in(move(argv_out[2]));
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 6: return expr{move(target),
move(args[0].first), move(args[1].first), move(args[2].first),
move(args[3].first), move(args[4].first), move(args[5].first),
move(args[0].second), move(args[1].second), move(args[2].second),
move(args[3].second), move(args[4].second), move(args[5].second), _loc};
}
{ struct expr { MNL_RVALUE() // 7 arguments
code target, args[7]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute(),
MNL_UNLIKELY(mods[2]) ? args[2].exec_out() : args[2].execute(),
MNL_UNLIKELY(mods[3]) ? args[3].exec_out() : args[3].execute(),
MNL_UNLIKELY(mods[4]) ? args[4].exec_out() : args[4].execute(),
MNL_UNLIKELY(mods[5]) ? args[5].exec_out() : args[5].execute(),
MNL_UNLIKELY(mods[6]) ? args[6].exec_out() : args[6].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[6])) args[6].exec_in(move(argv_out[6]));
if (MNL_UNLIKELY(mods[5])) args[5].exec_in(move(argv_out[5]));
if (MNL_UNLIKELY(mods[4])) args[4].exec_in(move(argv_out[4]));
if (MNL_UNLIKELY(mods[3])) args[3].exec_in(move(argv_out[3]));
if (MNL_UNLIKELY(mods[2])) args[2].exec_in(move(argv_out[2]));
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 7: return expr{move(target),
move(args[0].first), move(args[1].first), move(args[2].first),
move(args[3].first), move(args[4].first), move(args[5].first), move(args[6].first),
move(args[0].second), move(args[1].second), move(args[2].second),
move(args[3].second), move(args[4].second), move(args[5].second), move(args[6].second), _loc};
}
{ struct expr { MNL_RVALUE() // 8 arguments
code target, args[8]; const bool mods[std::extent<decltype(args)>::value]; loc _loc;
public:
MNL_INLINE val execute(bool) const {
val argv_out[std::extent<decltype(args)>::value], res = [&]()->val{
val argv[]{
MNL_UNLIKELY(mods[0]) ? args[0].exec_out() : args[0].execute(),
MNL_UNLIKELY(mods[1]) ? args[1].exec_out() : args[1].execute(),
MNL_UNLIKELY(mods[2]) ? args[2].exec_out() : args[2].execute(),
MNL_UNLIKELY(mods[3]) ? args[3].exec_out() : args[3].execute(),
MNL_UNLIKELY(mods[4]) ? args[4].exec_out() : args[4].execute(),
MNL_UNLIKELY(mods[5]) ? args[5].exec_out() : args[5].execute(),
MNL_UNLIKELY(mods[6]) ? args[6].exec_out() : args[6].execute(),
MNL_UNLIKELY(mods[7]) ? args[7].exec_out() : args[7].execute() }, target = this->target.execute();
try { return move(target)(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
if (MNL_UNLIKELY(mods[7])) args[7].exec_in(move(argv_out[7]));
if (MNL_UNLIKELY(mods[6])) args[6].exec_in(move(argv_out[6]));
if (MNL_UNLIKELY(mods[5])) args[5].exec_in(move(argv_out[5]));
if (MNL_UNLIKELY(mods[4])) args[4].exec_in(move(argv_out[4]));
if (MNL_UNLIKELY(mods[3])) args[3].exec_in(move(argv_out[3]));
if (MNL_UNLIKELY(mods[2])) args[2].exec_in(move(argv_out[2]));
if (MNL_UNLIKELY(mods[1])) args[1].exec_in(move(argv_out[1]));
if (MNL_UNLIKELY(mods[0])) args[0].exec_in(move(argv_out[0]));
return res;
}
};
case 8: return expr{move(target),
move(args[0].first), move(args[1].first), move(args[2].first), move(args[3].first),
move(args[4].first), move(args[5].first), move(args[6].first), move(args[7].first),
move(args[0].second), move(args[1].second), move(args[2].second), move(args[3].second),
move(args[4].second), move(args[5].second), move(args[6].second), move(args[7].second), _loc};
}
}
{ struct expr { MNL_RVALUE() // more than 8 arguments
code target; vector<pair<code, bool>> args; loc _loc; // implementation-defined destruction order for "args"
public:
MNL_INLINE val execute(bool) const {
int argc = args.size(); auto args = this->args.data();
val argv_out[argc]; val res = [&]()->val{
val argv[argc];
for (int sn = 0; sn < argc; ++sn) (MNL_UNLIKELY(args[sn].second) ? args[sn].first.exec_out() : args[sn].first.execute()).swap(argv[sn]);
val target = this->target.execute();
try { return move(target)(argc, argv, argv_out); } catch (...) { trace_execute(_loc); }
}();
for (int sn = argc; sn--;) if (MNL_UNLIKELY(args[sn].second)) args[sn].first.exec_in(move(argv_out[sn]));
return res;
}
};
return expr{move(target), move(args), _loc};
}
}
opt3:
err_compile("invalid form", _loc);
}
code pub::compile_rval(const form &form, const loc &_loc) {
auto res = compile(form, _loc);
if (!res.is_rvalue()) err_compile("not an R-value expression (nested in this context)", _loc);
return res;
}
code pub::compile_lval(const form &form, const loc &_loc) {
auto res = compile(form, _loc);
if (!res.is_lvalue()) err_compile("not an L-value expression (nested in this context)", _loc);
return res;
}
sym pub::eval_sym(const form &form, const loc &_loc) {
auto res = compile_rval(form, _loc).execute();
if (!test<sym>(res)) err_compile("type mismatch", _loc);
return cast<const sym &>(res);
}
code pub::compile_rval(form::vci_range range, const loc &_loc) {
struct expr_seq { MNL_LVALUE(second.is_lvalue())
code first, second;
MNL_INLINE val execute(bool fast_sig) const { if (MNL_UNLIKELY(first.execute(fast_sig), sig_state.first)) return {}; return second.execute(fast_sig); }
MNL_INLINE void exec_in(val &&value) const { first.execute(); second.exec_in(move(value)); }
MNL_INLINE val exec_out() const { first.execute(); return second.exec_out(); }
};
auto res = compile_rval(*range.begin(), _loc);
for (auto &&el: form::vci_range{range.begin() + 1, range.end()}) res = expr_seq{move(res), compile_rval(el, _loc)};
return res;
}
int val::default_order(const val &rhs) const noexcept {
auto mask1 = MNL_AUX_RAND(unsigned);
auto mask2 = MNL_AUX_RAND(size_t);
bool mask3 = MNL_AUX_RAND(unsigned) & 1;
return
MNL_LIKELY(rep.tag() != 0x7FF8u) || MNL_LIKELY(rhs.rep.tag() != 0x7FF8u) ?
(((~rep.tag() & 0x7FF0u ? 0 : rep.tag()) ^ mask1) < ((~rhs.rep.tag() & 0x7FF0u ? 0 : rhs.rep.tag()) ^ mask1)) -
(((~rhs.rep.tag() & 0x7FF0u ? 0 : rhs.rep.tag()) ^ mask1) < ((~rep.tag() & 0x7FF0u ? 0 : rep.tag()) ^ mask1)) :
MNL_LIKELY(!test<object>()) || MNL_LIKELY(!rhs.test<object>()) ?
(typeid(*static_cast<root *>(rep.dat<void *>())).hash_code() ^ mask2) < (typeid(*static_cast<root *>(rhs.rep.dat<void *>())).hash_code() ^ mask2) ? -1 :
(typeid(*static_cast<root *>(rhs.rep.dat<void *>())).hash_code() ^ mask2) < (typeid(*static_cast<root *>(rep.dat<void *>())).hash_code() ^ mask2) ? +1 :
typeid(*static_cast<root *>(rep.dat<void *>())).before(typeid(*static_cast<root *>(rhs.rep.dat<void *>()))) ? mask3 ? -1 : +1 :
typeid(*static_cast<root *>(rhs.rep.dat<void *>())).before(typeid(*static_cast<root *>(rep.dat<void *>()))) ? mask3 ? +1 : -1 : 0 :
// else
order(cast<const object &>().descr, rhs.cast<const object &>().descr);
}
code expr_export::compile(code &&, const pub::form &form, const loc &_loc) const {
if (form.size() < 3 || form[1] != MNL_SYM("in")) err_compile("invalid form", _loc);
if (form.size() == 3 && test<sym>(form[2])) for (auto &&el: bind) if (MNL_UNLIKELY(el.first == cast<const sym &>(form[2]))) return el.second; // shortcut
deque<code> overriden_ents;
for (auto &&el: bind) overriden_ents.push_back(symtab[el.first]), symtab.update(el);
auto body = form.size() == 3 ? pub::compile(form[2], _loc) : compile_rval(form + 2, _loc);
for (auto &&el: bind) symtab.update(el.first, move(overriden_ents.front())), overriden_ents.pop_front();
return body;
}
template class code::box<expr_export>;
void aux::panic(const decltype(sig_state) &sig) {
if (sig.first == MNL_SYM("CompileError")) { // should be uninterned?
sig_trace.clear();
if (!test<string>(sig.second)) panic({MNL_SYM("TypeMismatch"), {}});
fprintf(stderr, "%s\n", cast<const string &>(sig.second).c_str());
} else {
fprintf(stderr, "Uncaught signal %s\n", (const char *)sig.first);
if (sig_trace.empty())
fprintf(stderr, "*** invocation backtrace empty ***\n");
else {
fprintf(stderr, "====== invocation backtrace ======\n");
int sn = 0;
for (auto &&el: sig_trace) {
if (sn == sig_trace.capacity() - 1) break;
fprintf(stderr, "%02d at (%s) %d:%d-%d:%d %s\n", sn++,
el.first.origin->c_str(), el.first._start.first, el.first._start.second, el.first._final.first, el.first._final.second - 1, el.second);
}
if (sn != sig_trace.capacity() - 1)
fprintf(stderr, "======== end of backtrace ========\n");
else
fprintf(stderr, "==== more invocations omitted ====\n");
}
}
fflush(stderr), _Exit(EXIT_FAILURE);
}
} // namespace MNL_AUX_UUID