-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathengine.js
651 lines (585 loc) · 19.2 KB
/
engine.js
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
'use strict';
var evaluate = require('./evaluate');
var describe = require('./describe');
function weigh(scope, randomer, expressions, weights) {
var weight = 0;
for (var i = 0; i < expressions.length; i++) {
weights[i] = evaluate(scope, randomer, expressions[i]);
weight += weights[i];
}
return weight;
}
function pick(weights, weight, randomer) {
var offset = Math.floor(randomer.random() * weight);
var passed = 0;
for (var i = 0; i < weights.length; i++) {
passed += weights[i];
if (offset < passed) {
return i;
}
}
return null;
}
function pop(array, index) {
array[index] = array[array.length - 1];
array.length--;
}
module.exports = class Engine {
debug = typeof process === 'object' && process.env.DEBUG_ENGINE
constructor(args) {
this.story = args.story;
this.labels = Object.keys(this.story);
this.handler = args.handler;
this.options = [];
this.keywords = {};
this.noOption = null;
this.global = new Global(this.handler);
this.top = this.global;
this.start = args.start || 'start';
this.label = this.start;
this.instruction = {type: 'goto', next: this.start};
this.render = args.render;
this.dialog = args.dialog;
this.dialog.engine = this;
this.randomer = args.randomer || Math;
this.waypoint = this.capture();
Object.seal(this);
}
reset() {
this.labels = Object.keys(this.story);
this.options = [];
this.keywords = {};
this.noOption = null;
this.global = new Global(this.handler);
this.top = this.global;
this.label = this.start;
this.instruction = {type: 'goto', next: this.start};
this.waypoint = this.capture();
this.resume();
}
continue() {
var _continue;
do {
if (this.debug) {
console.log(this.label + ' ' + this.instruction.type + ' ' + describe(this.instruction));
}
if (this.instruction == null) {
// TODO user error for non-console interaction.
console.log('The label ' + JSON.stringify(this.label) + ' does not exist in this story');
this.end();
return;
}
if (!this['$' + this.instruction.type]) {
console.error('Unexpected instruction type: ' + this.instruction.type, this.instruction);
this.resume();
}
_continue = this['$' + this.instruction.type](this.instruction);
} while (_continue);
}
goto(label) {
while (this.top != null && (label == 'ESC' || label === 'RET')) {
if (this.debug) {
console.log(label.toLowerCase());
}
if (this.top.stopOption) {
this.render.stopOption();
}
if (label === 'ESC') {
label = this.top.branch;
} else {
label = this.top.next;
}
this.top = this.top.parent;
}
if (label === 'RET') {
return this.end();
}
var next = this.story[label];
if (!next) {
console.error('Story missing label', label);
return this.resume();
}
if (!next) {
console.error('Story missing instruction for label: ' + label);
return this.resume();
}
if (this.handler && this.handler.goto) {
this.handler.goto(label, next);
}
this.label = label;
this.instruction = next;
return true;
}
gothrough(sequence, next) {
var prev = this.label;
for (var i = sequence.length - 1; i >= 0; i--) {
if (next !== 'RET') {
this.top = new Frame(this.top, [], next, 'RET', prev);
}
prev = next;
next = sequence[i];
}
return this.goto(next);
}
end() {
if (this.handler && this.handler.end) {
this.handler.end(this);
}
this.display();
this.dialog.close();
return false;
}
ask() {
if (this.options.length) {
this.display();
if (this.handler && this.handler.ask) {
this.handler.ask(this);
}
this.dialog.ask();
} else if (this.noOption != null) {
var closure = this.noOption;
var option = this.story[closure.label];
this.top = closure.scope;
var answer = option.answer;
this.flush();
this.gothrough(answer, 'RET');
this.continue();
} else {
return this.goto('RET');
}
}
read() {
this.display();
if (this.handler && this.handler.ask) {
this.handler.ask(this);
}
this.dialog.ask(this.instruction.cue);
}
answer(text) {
if (this.handler && this.handler.answer) {
this.handler.answer(text, this);
}
this.render.flush();
if (this.instruction.type === 'read') {
this.top.set(this.instruction.variable, text);
this.render.clear();
if (this.goto(this.instruction.next)) {
this.continue();
}
return;
}
var choice = text - 1;
if (choice >= 0 && choice < this.options.length) {
return this.choice(this.options[choice]);
} else if (this.keywords[text]) {
return this.choice(this.keywords[text]);
} else {
this.render.pardon();
this.ask();
}
}
choice(closure) {
var option = this.story[closure.label];
if (this.handler && this.handler.choice) {
this.handler.choice(option, this);
}
this.render.clear();
this.waypoint = this.capture(closure);
if (this.handler && this.handler.waypoint) {
this.handler.waypoint(this.waypoint, this);
}
// Resume in the option's closure scope.
this.top = closure.scope;
// There is no known case where gothrough would immediately exit for
// lack of further instructions, so
if (this.gothrough(option.answer, 'RET')) {
this.flush();
this.continue();
}
}
display() {
this.render.display();
}
flush() {
this.options.length = 0;
this.noOption = null;
this.keywords = {};
}
write(text) {
this.render.write(this.instruction.lift, text, this.instruction.drop);
return this.goto(this.instruction.next);
}
capture(closure) {
var label, top;
if (closure != null) {
label = closure.label;
top = closure.scope;
} else {
label = this.label;
top = this.top;
}
var stack = [];
for (; top != this.global; top = top.parent) {
stack.push(top.capture(this));
}
return [
this.indexOfLabel(label),
stack,
this.global.capture(),
[
this.randomer._state0U | 0,
this.randomer._state0L | 0,
this.randomer._state1U | 0,
this.randomer._state1L | 0
],
];
}
resume(snapshot) {
this.render.clear();
this.flush();
this.label = this.start;
this.instruction = this.story[this.label];
this.global = new Global(this.handler);
this.top = this.global;
if (snapshot == null) {
if (this.handler && this.handler.waypoint) {
this.handler.waypoint(null, this);
}
this.continue();
return;
}
// Destructure snapshot
var label = this.labelOfIndex(snapshot[0]);
var stack = snapshot[1];
var global = snapshot[2];
var random = snapshot[3];
// Restore globals
var keys = global[0];
var values = global[1];
for (var i = 0; i < keys.length; i++) {
this.global.set(keys[i], values[i]);
}
// Restore stack
var engine = this;
this.top = stack.reduceRight(function (parent, snapshot) {
return Frame.restore(engine, snapshot, parent);
}, this.global);
// Restore prng
this.randomer._state0U = random[0];
this.randomer._state0L = random[1];
this.randomer._state1U = random[2];
this.randomer._state1L = random[3];
var instruction = this.story[label];
if (instruction.type === 'opt') {
if (this.gothrough(instruction.answer, 'RET')) {
this.flush();
this.continue();
}
} else {
this.label = label;
this.flush();
this.continue();
}
}
log() {
this.top.log();
console.log('');
}
labelOfIndex(index) {
if (index == -2) {
return 'RET';
} else if (index === -3) {
return 'ESC';
}
return this.labels[index];
}
indexOfLabel(label) {
if (label === 'RET') {
return -2;
} else if (label === 'ESC') {
return -3;
}
return this.labels.indexOf(label);
}
// Here begin the instructions
$text() {
return this.write(this.instruction.text);
}
$echo() {
return this.write('' + evaluate(this.top, this.randomer, this.instruction.expression));
}
$br() {
this.render.break();
return this.goto(this.instruction.next);
}
$par() {
this.render.paragraph();
return this.goto(this.instruction.next);
}
$rule() {
// TODO
this.render.paragraph();
return this.goto(this.instruction.next);
}
$goto() {
return this.goto(this.instruction.next);
}
$call() {
var label = this.instruction.label;
var def = this.story[label];
if (!def) {
console.error('no such procedure ' + label, this.instruction);
return this.resume();
}
if (def.type !== 'def') {
console.error('Can\'t call non-procedure ' + label, this.instruction);
return this.resume();
}
if (def.locals.length !== this.instruction.args.length) {
console.error('Argument length mismatch for ' + label, this.instruction);
return this.resume();
}
// TODO replace this.global with closure scope if scoped procedures become
// viable. This will require that the engine create references to closures
// when entering a new scope (calling a procedure), in addition to
// capturing locals. As such the parser will need to retain a reference to
// the enclosing procedure and note all of the child procedures as they are
// encountered.
this.top = new Frame(this.top, def.locals, this.instruction.next, this.instruction.branch, this.label);
for (var i = 0; i < this.instruction.args.length; i++) {
var arg = this.instruction.args[i];
var value = evaluate(this.top.parent, this.randomer, arg);
this.top.set(def.locals[i], value);
}
return this.goto(label);
}
$def() {
// Procedure argument instructions exist as targets for labels as well as
// for reference to locals in calls.
return this.goto(this.instruction.next);
}
$opt() {
var closure = new Closure(this.top, this.label);
for (var i = 0; i < this.instruction.keywords.length; i++) {
var keyword = this.instruction.keywords[i];
// The first option to introduce a keyword wins, not the last.
if (!this.keywords[keyword]) {
this.keywords[keyword] = closure;
}
}
if (this.instruction.question.length > 0) {
this.options.push(closure);
this.render.startOption();
this.top = new Frame(this.top, [], this.instruction.next, 'RET', this.label, true);
return this.gothrough(this.instruction.question, 'RET');
} else if (this.noOption == null) {
this.noOption = closure;
}
return this.goto(this.instruction.next);
}
$move() {
var value = evaluate(this.top, this.randomer, this.instruction.source);
var name = evaluate.nominate(this.top, this.randomer, this.instruction.target);
if (this.debug) {
console.log(this.top.at() + '/' + this.label + ' ' + name + ' = ' + value);
}
this.top.set(name, value);
return this.goto(this.instruction.next);
}
$jump() {
var j = this.instruction;
if (evaluate(this.top, this.randomer, j.condition)) {
return this.goto(this.instruction.branch);
} else {
return this.goto(this.instruction.next);
}
}
$switch() {
var branches = this.instruction.branches.slice();
var weightExpressions = this.instruction.weights.slice();
var samples = 1;
var nexts = [];
if (this.instruction.mode === 'pick') {
samples = evaluate(this.top, this.randomer, this.instruction.expression);
}
for (var i = 0; i < samples; i++) {
var value;
var weights = [];
var weight = weigh(this.top, this.randomer, weightExpressions, weights);
if (this.instruction.mode === 'rand' || this.instruction.mode === 'pick') {
if (weights.length === weight) {
value = Math.floor(this.randomer.random() * branches.length);
} else {
value = pick(weights, weight, this.randomer);
if (value == null) {
break;
}
}
} else {
value = evaluate(this.top, this.randomer, this.instruction.expression);
if (this.instruction.variable != null) {
this.top.set(this.instruction.variable, value + this.instruction.value);
}
}
if (this.instruction.mode === 'loop') {
// actual modulo, wraps negatives
value = ((value % branches.length) + branches.length) % branches.length;
} else if (this.instruction.mode === 'hash') {
value = evaluate.hash(value) % branches.length;
}
value = Math.min(value, branches.length - 1);
value = Math.max(value, 0);
var next = branches[value];
pop(branches, value);
pop(weightExpressions, value);
nexts.push(next);
}
if (this.debug) {
console.log(this.top.at() + '/' + this.label + ' ' + value + ' -> ' + next);
}
return this.gothrough(nexts, this.instruction.next);
}
$cue() {
if (this.handler != null && this.handler.cue != null) {
return this.handler.cue(this.instruction.cue, this.instruction.next, this);
} else {
return this.goto(this.instruction.next);
}
}
$ask() {
this.ask();
return false;
}
$read() {
this.read();
return false;
}
}
class Global {
constructor(handler) {
this.scope = Object.create(null);
this.handler = handler;
this.next = 'RET';
this.branch = 'RET';
Object.seal(this);
}
get(name) {
if (this.handler && this.handler.has && this.handler.has(name)) {
return this.handler.get(name);
} else {
return this.scope[name] || 0;
}
}
set(name, value) {
if (this.handler && this.handler.has && this.handler.has(name)) {
this.handler.set(name, value);
} else {
this.scope[name] = value;
}
if (this.handler && this.handler.changed) {
this.handler.changed(name, value);
}
}
log() {
var names = Object.keys(this.scope);
names.sort();
for (var i = 0; i < names.length; i++) {
var name = names[i];
var value = this.scope[name];
console.log(name + ' = ' + value);
}
console.log('');
}
at() {
return '';
}
capture() {
var names = Object.keys(this.scope);
var values = [];
for (var i = 0; i < names.length; i++) {
values[i] = this.scope[names[i]] || 0;
}
return [names, values];
}
}
class Frame {
static restore(engine, snapshot, parent) {
var label = engine.labelOfIndex(snapshot[0]);
var next = engine.labelOfIndex(snapshot[1]);
var branch = engine.labelOfIndex(snapshot[2]);
var values = snapshot[3];
var stopOption = Boolean(snapshot[4]);
var frame = new Frame(parent, [], next, branch, label, stopOption);
// Technically, not all frames correspond to subroutine calls, but all
// frames that remain when the engine pauses ought to be.
// The exceptions would be interstitial frames generated by gothrough,
// but all of these are exhausted before the engine stops to ask a prompt.
var call = engine.story[label];
var def = engine.story[call.label];
frame.locals = def.locals;
for (var i = 0; i < values.length; i++) {
var name = def.locals[i];
frame.scope[name] = values[i];
}
return frame;
}
constructor(parent, locals, next, branch, label, stopOption) {
this.locals = locals;
this.scope = Object.create(null);
for (var i = 0; i < locals.length; i++) {
this.scope[locals[i]] = 0;
}
this.parent = parent;
this.next = next;
this.branch = branch;
this.label = label;
this.stopOption = stopOption || false;
Object.seal(this);
}
get(name) {
if (this.locals.indexOf(name) >= 0) {
return this.scope[name];
}
return this.parent.get(name);
}
set(name, value) {
if (this.locals.indexOf(name) >= 0) {
this.scope[name] = value;
return;
}
this.parent.set(name, value);
}
log() {
this.parent.log();
console.log('--- ' + this.label + ' -> ' + this.next);
for (var i = 0; i < this.locals.length; i++) {
var name = this.locals[i];
var value = this.scope[name];
console.log(name + ' = ' + value);
}
}
at() {
return this.parent.at() + '/' + this.label;
}
capture(engine) {
var values = [];
for (var i = 0; i < this.locals.length; i++) {
var local = this.locals[i];
values.push(this.scope[local] || 0);
}
return [
engine.indexOfLabel(this.label),
engine.indexOfLabel(this.next),
engine.indexOfLabel(this.branch),
values,
+this.stopOption,
];
}
}
class Closure {
constructor(scope, label) {
this.scope = scope;
this.label = label;
Object.seal(this);
}
}