-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutor.cpp
506 lines (452 loc) · 22.3 KB
/
Executor.cpp
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
///
/// Langulus::Flow
/// Copyright (c) 2017 Dimo Markov <[email protected]>
/// Part of the Langulus framework, see https://langulus.com
///
/// SPDX-License-Identifier: GPL-3.0-or-later
///
#include "Executor.hpp"
#include "verbs/Do.inl"
#include "verbs/Interpret.inl"
#include "verbs/Create.inl"
#include "inner/Missing.hpp"
#if 0
#define VERBOSE(...) Logger::Verbose(__VA_ARGS__)
#define VERBOSE_TAB(...) const auto tab = Logger::VerboseTab(__VA_ARGS__)
#else
#define VERBOSE(...) LANGULUS(NOOP)
#define VERBOSE_TAB(...) LANGULUS(NOOP)
#endif
#define FLOW_ERRORS(...) Logger::Error(__VA_ARGS__)
namespace Langulus::Flow
{
/// Nested AND/OR scope execution with output
/// @param flow - the flow to execute
/// @param context - the environment in which scope will be executed
/// @param output - [out] verb result will be pushed here
/// @param integrate - execution happens in two styles:
/// 1. integration - everything not executed will still be pushed to
/// output, preserving the hierarchy. useful when integrating verbs
/// 2. not integration - only unexecuted verbs will push to output,
/// useful for collecting side-effects when updating
/// @param silent - whether or not to silence logging, in case we're
/// executing at compile-time, for example
/// @return true of no errors occured
bool Execute(
const Many& flow, Many& context, Many& output,
const bool integrate, const bool silent
) {
bool skipVerbs = false;
return Execute(flow, context, output, integrate, skipVerbs, silent);
}
/// Nested AND/OR scope execution with output
/// @param flow - the flow to execute
/// @param context - the environment in which scope will be executed
/// @param output - [out] verb result will be pushed here
/// @param integrate - execution happens in two styles:
/// 1. integration - everything not executed will still be pushed to
/// output, preserving the hierarchy. useful when integrating verbs
/// 2. not integration - only unexecuted verbs will push to output,
/// useful for collecting side-effects when updating
/// @param skipVerbs - [in/out] whether to skip verbs after OR success
/// @param silent - whether or not to silence logging, in case we're
/// executing at compile-time, for example
/// @return true of no errors occured
bool Execute(
const Many& flow, Many& context, Many& output,
const bool integrate, bool& skipVerbs, const bool silent
) {
auto results = Many::FromState(flow);
if (flow) {
if (integrate)
VERBOSE_TAB("Executing scope (integrating): [", flow, ']');
else
VERBOSE_TAB("Executing scope: [", flow, ']');
try {
if (flow.IsOr())
ExecuteOR(flow, context, results, integrate, skipVerbs, silent);
else
ExecuteAND(flow, context, results, integrate, skipVerbs, silent);
}
catch (const Except::Flow&) {
// Execution failed
return false;
}
}
output.SmartPush(IndexBack, Abandon(results));
return true;
}
/// Nested AND scope execution
/// @param flow - the flow to execute
/// @param context - the environment in which scope will be executed
/// @param output - [out] verb result will be pushed here
/// @param integrate - execution happens in two styles:
/// 1. integration - everything not executed will still be pushed to
/// output, preserving the hierarchy. useful when integrating verbs
/// 2. not integration - only unexecuted verbs will push to output,
/// useful for collecting side-effects when updating
/// @param skipVerbs - [in/out] whether to skip verbs after OR success
/// @param silent - whether or not to silence logging, in case we're
/// executing at compile-time, for example
/// @return true of no errors occured
bool ExecuteAND(
const Many& flow, Many& context, Many& output,
const bool integrate, bool& skipVerbs, const bool silent
) {
Count executed = 0;
if (flow.IsDeep() and flow.IsDense()) {
executed = flow.ForEach([&](const Many& block) {
// Nest if deep
Many local;
if (not Execute(block, context, local, integrate, skipVerbs, silent)) {
if (silent)
LANGULUS_THROW(Flow, "Deep AND failure");
else
LANGULUS_OOPS(Flow, "Deep AND failure: ", flow);
}
output.SmartPush(IndexBack, Abandon(local));
});
}
else if (flow.IsDense()) {
executed = flow.ForEach(
[&](const Inner::Missing& missing) {
// Nest if missing points
Many local;
if (not Execute(missing.mContent, context, local, integrate, skipVerbs, silent)) {
if (silent)
LANGULUS_THROW(Flow, "Missing point failure");
else
LANGULUS_OOPS(Flow, "Missing point failure: ", flow);
}
output.SmartPush(IndexBack, Abandon(local));
},
[&](const Trait& trait) {
// Nest if traits, but retain each trait
if (trait.IsMissing()) {
// Never touch missing stuff, only propagate it
output.SmartPush(IndexBack, trait);
return;
}
Many local;
if (not Execute(trait, context, local, integrate, skipVerbs, silent)) {
if (silent)
LANGULUS_THROW(Flow, "Trait AND failure");
else
LANGULUS_OOPS(Flow, "Trait AND failure: ", flow);
}
output.SmartPush(IndexBack, Trait::From(trait.GetTrait(), Abandon(local)));
},
[&](const Construct& construct) {
// Nest if constructs, but retain each construct
VERBOSE("Executing construct: ", construct);
Many local;
if (not Execute(construct.GetDescriptor(), context, local, integrate, skipVerbs, silent)) {
if (silent)
LANGULUS_THROW(Flow, "Construct AND failure");
else
LANGULUS_OOPS(Flow, "Construct AND failure: ", flow);
}
Construct solved {
construct.GetType(), Abandon(local), construct.GetCharge()
};
// We can attempt an implicit Verbs::Create to make
// the data at compile-time. Allowed only if no producer
// was specified.
if (not construct.GetType()->mProducerRetriever) {
Verbs::Create creator {&solved};
if (Verb::GenericExecuteStateless(creator)) {
output.SmartPush(IndexBack, Abandon(creator.GetOutput()));
return;
}
}
// Otherwise just propagate
output.SmartPush(IndexBack, Abandon(solved));
},
[&](const Neat& neat) {
// And order-independent container
// Make a shallow copy of the Neat, and strip all
// verbs from it. Some of them might get reinserted, if
// missing, but generally they will be substituted with
// the corresponding results
VERBOSE("Executing neat: ", neat);
Neat local = neat;
local.template RemoveData<A::Verb>();
VERBOSE("Executing neat (verbs stripped): ", local);
local.ForEach(
[&](const A::Verb& constVerb) {
if (constVerb.IsMissing()) {
// Never touch missing stuff, only propagate it
local << constVerb;
return;
}
// Execute all verbs, push their outputs to the
// local shallow-copied construct
auto verb = Verb::FromMeta(
constVerb.GetVerb(),
constVerb.GetArgument(),
constVerb,
constVerb.GetVerbState()
);
verb.SetSource(constVerb.GetSource());
if (not ExecuteVerb(context, verb, silent)) {
if (silent)
LANGULUS_THROW(Flow, "Construct AND failure");
else
LANGULUS_OOPS(Flow, "Construct AND failure: ", flow);
}
else if (verb.GetOutput())
local << Abandon(verb.GetOutput());
}
);
VERBOSE("Executing neat (verbs executed): ", local);
output.SmartPush(IndexBack, Abandon(local));
},
[&](const A::Verb& constVerb) {
// Execute verbs
if (skipVerbs)
return Loop::Break;
if (constVerb.IsDone()) {
// Verb has already been executed
// Don't do anything
return Loop::Continue;
}
// Shallow-copy the verb to make it mutable
// Also resets its output
auto verb = Verb::FromMeta(
constVerb.GetVerb(),
constVerb.GetArgument(),
constVerb,
constVerb.GetVerbState()
);
verb.SetSource(constVerb.GetSource());
// Execute the verb
if (not ExecuteVerb(context, verb, silent)) {
if (silent)
LANGULUS_THROW(Flow, "Verb AND failure");
else
LANGULUS_OOPS(Flow, "Verb AND failure: ", verb);
}
// Make sure the original verb has been marked done, so
// that it isn't executed every time.
const_cast<A::Verb&>(constVerb).Done();
output.SmartPush(IndexBack, Abandon(verb.GetOutput()));
return Loop::Continue;
}
);
}
if (not executed and integrate) {
// If this is reached, then we had non-verb content
// Just propagate its contents
output.SmartPush(IndexBack, flow);
}
VERBOSE(Logger::Green, "AND scope done: ", flow);
return true;
}
/// Nested OR execution
/// @param flow - the flow to execute
/// @param context - the context in which scope will be executed
/// @param output - [out] verb result will be pushed here
/// @param integrate - execution happens in two styles:
/// 1. integration - everything not executed will still be pushed to
/// output, preserving the hierarchy. useful when integrating verbs
/// 2. not integration - only unexecuted verbs will push to output,
/// useful for collecting side-effects when updating
/// @param skipVerbs - [out] whether to skip verbs after OR success
/// @param silent - whether or not to silence logging, in case we're
/// executing at compile-time, for example
/// @return true of no errors occured
bool ExecuteOR(
const Many& flow, Many& context, Many& output,
const bool integrate, bool& skipVerbs, const bool silent
) {
Count executed = 0;
bool localSkipVerbs = false;
if (flow.IsDeep() and flow.IsDense()) {
executed = flow.ForEach([&](const Many& block) {
// Nest if deep
Many local;
if (Execute(block, context, local, integrate, localSkipVerbs, silent)) {
executed = true;
output.SmartPush(IndexBack, Abandon(local));
}
});
}
else if (flow.IsDense()) {
executed = flow.ForEach(
[&](const Trait& trait) {
// Nest if traits, but retain each trait
if (trait.IsMissing()) {
// Never touch missing stuff, only propagate it
output.SmartPush(IndexBack, trait);
return;
}
Many local;
if (Execute(trait, context, local, integrate, silent)) {
executed = true;
output.SmartPush(IndexBack, Trait::From(trait.GetTrait(), Abandon(local)));
}
},
[&](const Construct& construct) {
// Nest if constructs, but retain each construct
Many local;
if (Execute(construct.GetDescriptor(), context, local, integrate, skipVerbs, silent)) {
executed = true;
Construct solved {
construct.GetType(), Abandon(local), construct.GetCharge()
};
// We can attempt an implicit Verbs::Create to make
// the data at compile-time. Allowed only if no
// producer was specified.
if (not construct.GetType()->mProducerRetriever) {
Verbs::Create creator {&solved};
if (Verb::GenericExecuteStateless(creator)) {
output.SmartPush(IndexBack, Abandon(creator.GetOutput()));
return;
}
}
// Otherwise just propagate
output.SmartPush(IndexBack, Abandon(solved));
}
},
[&](const Neat& neat) {
// Make a shallow copy of the neat, and strip all
// verbs from it. Some of them might get reinserted, if
// missing, but generally they will be substituted with
// the corresponding results
Neat local = neat;
local.template RemoveData<A::Verb>();
local.ForEach(
[&](const A::Verb& constVerb) noexcept {
if (constVerb.IsMissing()) {
// Never touch missing stuff, only propagate it
local << constVerb;
return;
}
// Execute all verbs, push their outputs to the
// local shallow-copied construct
auto verb = Verb::FromMeta(
constVerb.GetVerb(),
constVerb.GetArgument(),
constVerb,
constVerb.GetVerbState()
);
verb.SetSource(constVerb.GetSource());
if (ExecuteVerb(context, verb, silent))
executed = true;
}
);
output.SmartPush(IndexBack, Abandon(local));
},
[&](const Verb& constVerb) {
// Execute verbs
if (localSkipVerbs)
return Loop::Break;
// Shallow-copy the verb to make it mutable
// Also resets its output
auto verb = Verb::FromMeta(
constVerb.GetVerb(),
constVerb.GetArgument(),
constVerb,
constVerb.GetVerbState()
);
if (not ExecuteVerb(context, verb, silent))
return Loop::Continue;
executed = true;
output.SmartPush(IndexBack, Abandon(verb.GetOutput()));
return Loop::Continue;
}
);
}
skipVerbs |= localSkipVerbs;
if (not executed and integrate) {
// If this is reached, then we have non-verb flat content
// Just propagate it
output.SmartPush(IndexBack, flow);
++executed;
}
if (executed) VERBOSE(Logger::Green, "OR scope done: ", flow);
else VERBOSE(Logger::Red, "OR scope failed: ", flow);
return executed;
}
/// Integrate all parts of a verb inside this environment
/// @param context - [in/out] the context for integration
/// @param verb - [in/out] verb to integrate
/// @param silent - whether or not to silence logging, in case we're
/// executing at compile-time, for example
/// @return true of no errors occured
bool IntegrateVerb(Many& context, Verb& verb, const bool silent) {
if (verb.IsMonocast()) {
// We're executing on whole argument/source, so be lazy
if (verb.GetSource().IsInvalid())
verb.SetSource(context);
return true;
}
// Integrate the verb source to environment
Many localSource;
if (not Execute(verb.GetSource(), context, localSource, true, silent)) {
// It's considered error only if verb is not monocast
if (not silent)
FLOW_ERRORS("Error at source of: ", verb);
return false;
}
if (localSource.IsInvalid())
localSource = context;
// Integrate the verb argument to the source
Many localArgument;
if (not Execute(verb.GetArgument(), localSource, localArgument, true, silent)) {
// It's considered error only if verb is not monocast
if (not silent)
FLOW_ERRORS("Error at argument of: ", verb);
return false;
}
verb.SetSource(Abandon(localSource));
verb.SetArgument(Abandon(localArgument));
return true;
}
/// Execute a single verb, and all subverbs in it, if any
/// @param context - [in/out] the context in which verb will be executed
/// @param verb - [in/out] verb to execute
/// @param silent - whether or not to silence logging, in case we're
/// executing at compile-time, for example
/// @return true of no errors occured
bool ExecuteVerb(Many& context, Verb& verb, const bool silent) {
// Integration (and execution of subverbs if any)
// Source and argument will be executed locally if scripts, and
// substituted with their results in the verb
if (not IntegrateVerb(context, verb, silent)) {
if (not silent) {
FLOW_ERRORS("Error integrating verb: ",
verb, " (", verb.GetVerb(), ')');
}
return false;
}
if (verb.IsVerb<Verbs::Do>()) {
// A Do verb is done at this point, because the subverbs
// inside (if any) should be done in the integration phase
// Just making sure that the integrated argument & source are
// propagated to the verb's output
if (not verb.GetOutput()) {
if (verb)
verb << Move(verb.GetArgument());
else
verb << Move(verb.GetSource());
}
return true;
}
VERBOSE_TAB("Executing verb: ",
Logger::Cyan, verb, " (", verb.GetVerb(), ')');
// Dispatch the verb to the context, executing it
// Any results should be inside verb.mOutput afterwards
Many contextCopy = verb.GetSource();
if (not DispatchDeep(contextCopy, verb)) {
if (not silent) {
FLOW_ERRORS("Error executing verb: ",
verb, " (", verb.GetVerb(), ')');
}
return false;
}
VERBOSE("Executed: ",
Logger::Green, verb, " (", verb.GetVerb(), ')');
return true;
}
} // namespace Langulus::Flow