-
Notifications
You must be signed in to change notification settings - Fork 10
/
grammar.js
1607 lines (1437 loc) · 50.5 KB
/
grammar.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const Scope = require('./scope');
exports.start = start;
function start(story, path, base) {
const scope = new Scope(story, path, base);
const stop = new Stop(scope);
const start = scope.create('goto', 'RET', '1:1');
return new Thread(scope.zerothChild(), stop, [start], []);
}
class Stop {
constructor(scope) {
this.scope = scope;
Object.freeze(this);
}
next(type, _space, text, scanner) {
// The only way to reach this method is for there to be a bug in the
// outline lexer, or a bug in the grammar.
if (type !== 'stop') {
this.scope.error(scanner.position() + ': Expected end of file, got ' + tokenName(type, text) + '.');
}
return new End();
}
return(_scope, rets, escs, _scanner) {
Scope.tie(rets, 'RET');
Scope.tie(escs, 'ESC');
return this;
}
}
class End {
constructor() {
Object.freeze(this);
}
next(_type, _space, _text, _scanner) {
return this;
}
}
// rets are tied to the next instruction
// escs are tied off after the next encountered prompt
class Thread {
constructor(scope, parent, rets, escs) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.escs = escs;
Object.freeze(this);
}
next(type, space, text, scanner) {
if (type === 'symbol'|| type === 'alphanum' || type === 'number' || type === 'literal' || text === '--' || text === '---') {
return new Text(this.scope, space, text, this, this.rets);
} else if (type === 'token') {
if (text === '{') {
return new Block(this.scope, new ThenExpect('token', '}', this), this.rets);
} else if (text === '@') {
return label(this.scope, new Label(this, this.rets));
} else if (text === '->') {
return label(this.scope, new Goto(this, this.rets));
} else if (text === '<-') {
// Explicitly tie rets to null by dropping them.
Scope.tie(this.rets, 'RET');
// Continue carrying escs to the next encountered prompt.
// Advance the path so that option thread don't appear empty.
return new Thread(this.scope.next(), this.parent, [], this.escs);
} else if (text === '/') {
const node = this.scope.create('break', null, scanner.position());
this.scope.tie(this.rets);
return new Thread(this.scope.next(), this.parent, [node], this.escs);
} else if (text === '//') {
const node = this.scope.create('paragraph', null, scanner.position());
this.scope.tie(this.rets);
return new Thread(this.scope.next(), this.parent, [node], this.escs);
} else if (text === '{"' || text === '{\'' || text === '"}' || text === '\'}') {
return new Text(this.scope, space, '', this, this.rets)
.next(type, '', text, scanner);
} else if (text === '<') {
return label(this.scope, new ThenExpect('token', '>', new Cue(this, this.rets, this.escs)));
}
} else if (type === 'start') {
if (text === '+' || text === '*') {
return new MaybeOption(this.scope, new ThenExpect('stop', '', this), this.rets, [], text);
} else if (text === '-') {
return new MaybeThread(this.scope, new ThenExpect('stop', '', this), this.rets, [], [], ' ');
} else if (text === '>') {
// tie off rets to the prompt.
this.scope.tie(this.rets);
// promote escs to rets, tying them off after the prompt.
const rets = this.escs.slice();
this.escs.length = 0;
return new Ask(this.scope, new ThenExpect('stop', '', this), rets, []);
} else { // if text === '!') {
return new Program(this.scope, new ThenExpect('stop', '', this), this.rets, []);
}
} else if (type === 'dash') {
const node = this.scope.create('rule', null, scanner.position());
this.scope.tie(this.rets);
return new Thread(this.scope.next(), this.parent, [node], this.escs);
} else if (type === 'break') {
return this;
}
if (type === 'stop' || text === '|' || text === ']' || text === '[' || text === '}') {
return this.parent.return(this.scope, this.rets, this.escs, scanner)
.next(type, space, text, scanner);
}
return new Text(this.scope, space, text, this, this.rets);
}
return(scope, rets, escs, _scanner) {
// All rules above (in next) guarantee that this.rets has been passed to
// any rule that might use them. If the rule fails to use them, they must
// return them. However, escs are never passed to any rule that returns.
return new Thread(scope, this.parent, rets, this.escs.concat(escs));
}
}
class Text {
constructor(scope, lift, text, parent, rets) {
this.scope = scope;
this.lift = lift;
this.text = text;
this.parent = parent;
this.rets = rets;
Object.seal(this);
}
next(type, space, text, scanner) {
if (type === 'alphanum' || type === 'number' || type === 'symbol' || type === 'literal') {
this.text += space + text;
return this;
} else if (type === 'token') {
if (text === '{"') {
this.text += space + '“';
return this;
} else if (text === '"}') {
this.text += space + '”';
return this;
} else if (text === '{\'') {
this.text += space + '‘';
return this;
} else if (text === '\'}') {
this.text += space + '’';
return this;
} else if ((text === '/' || text === '//') && space === '') {
// This is an exception to accommodate hyperlinks.
// Paragraphs and line breaks must be expressed by the / and //
// tokens and must be preceded by space.
this.text += text;
return this;
}
} else if (text === '--') {
this.text += space + '–'; // en-dash
return this;
} else if (text === '---') {
this.text += space + '—'; // em-dash
return this;
}
this.scope.tie(this.rets);
const node = this.scope.create('text', this.text, scanner.position());
node.lift = this.lift;
node.drop = space;
return this.parent.return(this.scope.next(), [node], [], scanner)
.next(type, space, text, scanner);
}
}
class MaybeThread {
constructor(scope, parent, rets, escs, skips, space) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.escs = escs;
this.skips = skips;
this.space = space || '';
Object.freeze(this);
}
next(type, space, text, scanner) {
if (type === 'token') {
if (text === '{') {
return expression(this.scope,
new ThenExpect('token', '}',
new ThreadCondition(this.parent, this.rets, this.escs, this.skips)));
}
}
return new Thread(this.scope, this, this.rets, this.escs)
.next(type, this.space || space, text, scanner);
}
return(scope, rets, escs, scanner) {
return this.parent.return(scope, rets.concat(this.skips), escs, scanner);
}
}
class ThreadCondition {
constructor(parent, rets, escs, skips) {
this.parent = parent;
this.rets = rets;
this.escs = escs;
this.skips = skips;
Object.freeze(this);
}
return(scope, args, scanner) {
const node = scope.create('jump', invertExpression(args), scanner.position());
const branch = new Branch(node);
scope.tie(this.rets);
return new MaybeThread(scope.next(), this.parent, [node], this.escs, this.skips.concat([branch]));
}
}
class MaybeOption {
constructor(scope, parent, rets, escs, leader) {
this.scope = scope;
this.at = scope;
this.parent = parent;
this.rets = rets;
this.escs = escs;
this.leader = leader;
this.conditions = [];
this.consequences = [];
this.keywords = {};
this.descended = false;
Object.seal(this);
}
next(type, space, text, scanner) {
if (type === 'token') {
if (text === '{') {
return new OptionOperator(this.scope,
new ThenExpect('token', '}', this));
}
// Recognize the inequality token as individual keyword tokens with an
// empty string amid them in this context.
if (text === '<>') {
return this.return(this.scope, 'keyword', '');
}
if (text === '<') {
return new Keyword(this.scope, this);
}
}
return this.option(scanner).next(type, space, text, scanner);
}
return(_scope, operator, expression, modifier, _scanner) {
if (operator === '+' || operator === '-' || operator === '!') {
modifier = modifier || ['val', 1];
}
if (operator === '?') {
modifier = modifier || ['val', 0];
}
if (operator === '+' || operator === '-') {
this.consequences.push([expression, [operator, expression, modifier]]);
}
if (operator === '-') {
this.conditions.push(['>=', expression, modifier]);
}
if (operator === '') {
this.conditions.push(expression);
}
if (operator === '=' || operator === '!' || operator === '?') {
this.conditions.push(['<>', expression, modifier]);
this.consequences.push([expression, modifier]);
}
if (operator === 'keyword') {
this.keywords[expression] = true;
}
return this;
}
advance() {
if (this.descended) {
this.at = this.at.next();
} else {
this.at = this.at.firstChild();
this.descended = true;
}
}
option(scanner) {
const variable = this.scope.name();
const rets = [];
this.at.tie(this.rets);
if (this.leader === '*') {
this.consequences.push([['get', variable], ['+', ['get', variable], ['val', 1]]]);
const jump = this.at.create('jump', ['<>', ['get', variable], ['val', 0]], scanner.position());
const jumpBranch = new Branch(jump);
rets.push(jumpBranch);
this.advance();
this.at.tie([jump]);
}
for (const condition of this.conditions) {
const jump = this.at.create('jump', ['==', condition, ['val', 0]], scanner.position());
const jumpBranch = new Branch(jump);
rets.push(jumpBranch);
this.advance();
this.at.tie([jump]);
}
const option = new Option(this.scope, this.parent, rets, this.escs, this.leader, this.consequences);
option.node = this.at.create('option', null, scanner.position());
option.node.keywords = Object.keys(this.keywords).sort();
this.advance();
option.next = this.at;
return option.thread(scanner,
new OptionThread(this.at, option, [], option, 'qa', AfterInitialQA));
}
}
// Captures <keyword> annotations on options.
class Keyword {
constructor(scope, parent) {
this.scope = scope;
this.parent = parent;
this.keyword = '';
this.space = '';
Object.seal(this);
}
next(_type, space, text, _scanner) {
if (text === '>') {
return this.parent.return(this.scope, 'keyword', this.keyword);
}
this.keyword += (this.space && space) + text;
this.space = ' ';
return this;
}
}
// {+x}, {-x}, {!x}, {+n x}, {-n x}, {=n x} or simply {x}
class OptionOperator {
constructor(scope, parent) {
this.scope = scope;
this.parent = parent;
Object.freeze(this);
}
next(type, space, text, scanner) {
if (text === '+' || text === '-' || text === '=' || text === '!' || text === '?') {
return expression(this.scope,
new OptionArgument(this.parent, text));
} else {
return expression(this.scope,
new OptionArgument2(this.parent, ''))
.next(type, space, text, scanner);
}
}
}
class OptionArgument {
constructor(parent, operator) {
this.parent = parent;
this.operator = operator;
Object.freeze(this);
}
return(scope, args, scanner) {
if (args[0] === 'get' || args[0] === 'var') {
return this.parent.return(scope, this.operator, args, this.args, scanner);
} else {
return expression(scope,
new OptionArgument2(this.parent, this.operator, args));
}
}
}
class OptionArgument2 {
constructor(parent, operator, args) {
this.parent = parent;
this.operator = operator;
this.args = args;
Object.freeze(this);
}
return(scope, args, scanner) {
return this.parent.return(scope, this.operator, args, this.args, scanner);
}
}
class Option {
constructor(scope, parent, rets, escs, leader, consequences) {
this.scope = scope;
this.parent = parent;
this.rets = rets; // to tie off to the next option
this.escs = escs; // to tie off to the next node after the next prompt
this.node = null;
this.leader = leader;
this.consequences = consequences;
this.next = scope.next();
this.mode = '';
this.branch = null;
Object.seal(this);
}
return(scope, rets, escs, scanner) {
// Create a jump from the end of the answer.
if (this.mode !== 'a') {
// If the answer is reused in the question, create a dedicated jump and
// add it to the end of the answer.
const jump = scope.create('goto', 'RET', scanner.position());
this.node.answer.push(scope.name());
rets.push(jump);
}
return this.parent.return(
this.scope.next(),
this.rets.concat([this.node]),
this.escs.concat(rets, escs),
scanner
);
}
thread(scanner, parent) {
// Creat a dummy node, to replace if necessary, for arcs that begin with a
// goto/divert arrow that otherwise would have loose rets to forward.
const placeholder = this.next.create('goto', 'RET', scanner.position());
return new Thread(this.next, parent, [placeholder], []);
}
push(scope, mode) {
const next = this.next.name();
const end = scope.name();
if (next !== end) {
if (mode === 'q' || mode === 'qa') {
this.node.question.push(next);
}
if (mode === 'a' || mode === 'qa') {
this.node.answer.push(next);
}
this.next = scope;
this.mode = mode;
}
}
}
// An option thread captures the end of an arc, and if the path has advanced,
// adds that arc to the option's questions and/or answer depending on the
// "mode" ("q", "a", or "qa") and proceeds to the following state.
class OptionThread {
constructor(scope, parent, rets, option, mode, Next) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.option = option;
this.mode = mode;
this.Next = Next;
Object.freeze(this);
}
return(scope, rets, escs, _scanner) {
this.option.push(scope, this.mode);
// TODO investigate whether we can consistently tie off received rets
// instead of passing them forward to OptionThread, which consistently
// just terminates them on their behalf.
Scope.tie(this.rets, 'RET');
// TODO no test exercises this kind of jump.
Scope.tie(escs, 'ESC');
return new this.Next(scope, this.parent, rets, this.option);
}
}
// Every option begins with a (potentially empty) thread before the first open
// backet that will contribute both to the question and the answer.
class AfterInitialQA {
constructor(scope, parent, rets, option) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.option = option;
Object.freeze(this);
}
next(type, _space, text, scanner) {
if (type === 'token' && text === '[') {
return this.option.thread(scanner, new AfterQorA(this.scope, this, this.rets, this.option));
} else {
this.scope.error(scanner.position() + ': Expected "[]" brackets in option but got ' + tokenName(type, text) + '.');
return this.return(this.scope, this.rets, [], scanner);
}
}
// The thread returns to this level after capturing the bracketed terms,
// after which anything and everything to the end of the block contributes
// to the answer.
return(scope, rets, escs, scanner) {
Scope.tie(rets, 'RET');
// TODO no test exercises these escs.
Scope.tie(escs, 'ESC');
rets = [];
// Thread consequences, including incrementing the option variable name
const consequences = this.option.consequences;
if (consequences.length) {
this.option.node.answer.push(scope.name());
}
for (const consequence of consequences) {
const node = scope.create('move', null, scanner.position());
node.source = consequence[1];
node.target = consequence[0];
scope.tie(rets);
scope = scope.next();
rets = [node];
}
this.option.next = scope;
return this.option.thread(scanner,
new OptionThread(scope, this.parent, rets, this.option, 'a', AfterFinalA));
}
}
// After capturing the first arc within brackets, which may either contribute
// to the question or the answer, we decide which based on whether there is a
// following bracket.
class DecideQorA {
constructor(scope, parent, rets, option) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.option = option;
Object.freeze(this);
}
next(type, _space, text, scanner) {
if (type === 'token' && text === '[') { // A
this.option.push(this.scope, 'a');
return this.option.thread(scanner,
new OptionThread(this.scope, this, this.rets, this.option, 'q', ExpectFinalBracket));
} else if (type === 'token' && text === ']') { // Q
this.option.push(this.scope, 'q');
return this.parent.return(this.scope, this.rets, [], scanner);
} else {
this.scope.error(scanner.position() + ': Expected "]" to end option but got ' + tokenName(type, text) + '.');
return this.parent.return(this.scope, this.rets, [], scanner);
}
}
// If the brackets contain a sequence of question thread like [A [Q] QA [Q]
// QA...], then after each [question], we return here for continuing QA arcs.
return(scope, rets, escs, scanner) {
// TODO no test exercises these escs.
Scope.tie(escs, 'ESC');
return this.option.thread(scanner,
new OptionThread(scope, this.parent, rets, this.option, 'qa', AfterQA));
}
}
// After a Question/Answer thread, there can always be another [Q] thread
// ad nauseam. Here we check whether this is the end of the bracketed
// expression or continue after a [Question].
class AfterQA {
constructor(scope, parent, rets, option) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.option = option;
Object.freeze(this);
}
next(type, _space, text, scanner) {
if (type === 'token' && text === '[') {
return this.option.thread(scanner,
new OptionThread(this.scope, this, this.rets, this.option, 'q', ExpectFinalBracket));
} else if (type === 'token' && text === ']') {
return this.parent.return(this.scope, this.rets, [], scanner);
} else {
this.scope.error(scanner.position() + ': Expected "]" to end option but got ' + tokenName(type, text) + '.');
return this.parent.return(this.scope, this.rets, [], scanner);
}
}
return(_scope, rets, escs, scanner) {
// TODO terminate returned scope
// TODO no test exercises these escapes.
Scope.tie(escs, 'ESC');
return this.option.thread(scanner,
new OptionThread(this.scope, this.parent, rets, this.option, 'qa', ExpectFinalBracket));
}
}
// The bracketed terms may either take the form [Q] or [A, ([Q] QA)*].
// This captures the first arc without committing to either Q or A until we
// know whether it is followed by a bracketed term.
class AfterQorA {
constructor(scope, parent, rets, option) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.option = option;
Object.freeze(this);
}
// Just capture the path and proceed.
return(scope, rets, escs, _scanner) {
// TODO consider whether this could have been done earlier.
Scope.tie(this.rets, 'RET');
// TODO no test exercises these escapes.
Scope.tie(escs, 'ESC');
return new DecideQorA(scope, this.parent, rets, this.option);
}
}
// After a [Q] or [A [Q] QA...] block, there must be a closing bracket and we
// return to the parent arc of the option.
class ExpectFinalBracket {
constructor(scope, parent, rets, option) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.option = option;
Object.freeze(this);
}
next(type, space, text, scanner) {
if (type !== 'token' || text !== ']') {
this.scope.error(scanner.position() + ': Expected "]" to end option.');
return this.parent.return(this.scope, this.rets, [], scanner)
.next('token', space, ']', scanner);
}
return this.parent.return(this.scope, this.rets, [], scanner);
}
}
// After the closing bracket in an option], everything that remains is the last
// node of the answer. After that thread has been submitted, we expect the
// block to end.
class AfterFinalA {
constructor(scope, parent, rets, _option) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
Object.freeze(this);
}
next(type, space, text, scanner) {
return this.parent.return(this.scope, this.rets, [], scanner)
.next(type, space, text, scanner);
}
}
// This concludes the portion dedicated to parsing options
// Branch is a fake story node. It serves to mark that the wrapped node's
// "branch" label should be tied instead of its "next" label.
class Branch {
constructor(node) {
this.type = 'branch';
this.node = node;
Object.freeze(this);
}
}
class Label {
constructor(parent, rets) {
this.parent = parent;
this.rets = rets;
Object.freeze(this);
}
return(scope, expression, scanner) {
const [head, ...tail] = expression;
if (head === 'get') {
const [label] = tail;
if (label === '...') {
const node = scope.create('goto', 'RET', scanner.position());
scope.tie(this.rets);
return new Thread(scope, new Loop(scope, this.parent), [node], []);
} else {
const labelScope = scope.label(label);
// place-holder goto thunk
const node = labelScope.create('goto', 'RET', scanner.position());
scope.tie(this.rets);
// rets also forwarded so they can be tied off if the goto is replaced.
return this.parent.return(labelScope, this.rets.concat([node]), [], scanner);
}
} else if (head === 'call') {
const [label, ...args] = tail;
const labelScope = scope.label(label[1]);
const node = labelScope.create('def', null, scanner.position());
const params = [];
for (const arg of args) {
if (arg[0] === 'get') {
params.push(arg[1]);
} else {
scope.error(scanner.position() + ': Expected parameter name but got expression.');
}
}
node.locals = params;
return new Thread(labelScope.next(),
new ConcludeProcedure(scope, this.parent, this.rets),
[node], []);
} else {
scope.error(scanner.position() + ': Expected label after "@".');
return new Thread(scope, this.parent, this.rets, []);
}
}
}
class Loop {
constructor(scope, parent) {
this.scope = scope;
this.parent = parent;
this.label = scope.name();
Object.freeze(this);
}
return(scope, rets, _escs, scanner) {
// tie back rets
this.scope.tie(rets);
// TODO tie back escs
return this.parent.return(scope, [], [], scanner);
}
}
class ConcludeProcedure {
constructor(scope, parent, rets) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
Object.freeze(this);
}
return(_scope, rets, escs, scanner) {
// After a procedure, connect prior rets.
Scope.tie(rets, 'RET');
// Dangling escs go to an escape instruction, to follow the jump path in
// the parent scope, determined at run time.
Scope.tie(escs, 'ESC');
return this.parent.return(this.scope, this.rets, [], scanner);
}
}
class Goto {
constructor(parent, rets) {
this.parent = parent;
this.rets = rets;
}
return(scope, args, scanner) {
if (args[0] === 'get') {
Scope.tie(this.rets, args[1]);
return this.parent.return(scope.next(), [], [], scanner);
} else if (args[0] === 'call') {
const label = args[1][1];
const node = scope.create('call', label, scanner.position());
node.args = args.slice(2);
scope.tie(this.rets);
return this.parent.return(scope.next(), [node], [new Branch(node)], scanner);
} else {
scope.error(scanner.position() + ': Expected label after goto arrow but got expression.');
return new Thread(scope, this.parent, this.rets, []);
}
}
}
class Cue {
constructor(parent, rets, escs) {
this.parent = parent;
this.rets = rets;
this.escs = escs;
Object.freeze(this);
}
return(scope, expression, scanner) {
if (expression.length === 0 || expression[0] !== 'get') {
scope.error(scanner.position() + ': Expected cue.');
return this.parent.return(scope, this.rets, this.escs, scanner);
} else {
const cue = expression[1];
const node = scope.create('cue', cue, scanner.position());
scope.tie(this.rets);
return this.parent.return(scope.next(), [node], this.escs, scanner);
}
}
}
const mutators = {
'=': true,
'+': true,
'-': true,
'*': true,
'/': true,
};
const toggles = {
'!': ['val', 1],
'?': ['val', 0],
};
const variables = {
'@': 'loop',
'#': 'hash',
'^': 'pick'
};
const switches = {
'&': 'loop',
'~': 'rand'
};
class Block {
constructor(scope, parent, rets) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
Object.freeze(this);
}
next(type, space, text, scanner) {
if (type !== 'start') {
if (text === '(') {
return expression(this.scope, new ExpressionBlock(this.parent, this.rets, 'walk'))
.next(type, space, text, scanner);
} else if (mutators[text]) {
return expression(this.scope, new SetBlock(this.parent, this.rets, text));
} else if (toggles[text]) {
return expression(this.scope, new ToggleBlock(this.parent, this.rets, toggles[text]));
} else if (variables[text]) {
return expression(this.scope, new ExpressionBlock(this.parent, this.rets, variables[text]));
} else if (switches[text]) {
return new SwitchBlock(this.scope, this.parent, this.rets)
.start(scanner, null, this.scope.name(), null, switches[text]);
}
}
return new SwitchBlock(this.scope, this.parent, this.rets)
.start(scanner, null, this.scope.name(), 1, 'walk') // with variable and value, waiting for case to start
.next(type, space, text, scanner);
}
}
class SetBlock {
constructor(parent, rets, op) {
this.op = op;
this.parent = parent;
this.rets = rets;
Object.freeze(this);
}
return(scope, expression, _scanner) {
return new MaybeSetVariable(scope, this.parent, this.rets, this.op, expression);
}
}
class MaybeSetVariable {
constructor(scope, parent, rets, op, expression) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.op = op;
this.expression = expression;
Object.freeze(this);
}
next(type, space, text, scanner) {
if (type === 'token' && text === '}') {
return this.set(['val', 1], this.expression, scanner)
.next(type, space, text, scanner);
}
return expression(this.scope, this)
.next(type, space, text, scanner);
}
set(source, target, scanner) {
const node = this.scope.create('move', null, scanner.position());
if (this.op === '=') {
node.source = source;
} else {
node.source = [this.op, target, source];
}
node.target = target;
this.scope.tie(this.rets);
return this.parent.return(this.scope.next(), [node], [], scanner);
}
return(_scope, target, scanner) {
return this.set(this.expression, target, scanner);
}
}
class ToggleBlock {
constructor(parent, rets, source) {
this.parent = parent;
this.rets = rets;
this.source = source;
Object.freeze(this);
}
return(scope, expression, scanner) {
const node = scope.create('move', null, scanner.position());
node.source = this.source;
node.target = expression;
scope.tie(this.rets);
return this.parent.return(scope.next(), [node], [], scanner);
}
}
class ExpressionBlock {
constructor(parent, rets, mode) {
this.parent = parent;
this.rets = rets;
this.mode = mode;
Object.freeze(this);
}
return(scope, expression, _scanner) {
return new AfterExpressionBlock(scope, this.parent, this.rets, this.mode, expression);
}
}
class AfterExpressionBlock {
constructor(scope, parent, rets, mode, expression) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.mode = mode;
this.expression = expression;
Object.freeze(this);
}
next(type, space, text, scanner) {
if (text === '|') {
return new SwitchBlock(this.scope, this.parent, this.rets)
.start(scanner, this.expression, null, 0, this.mode);
} else if (text === '?') {
return new SwitchBlock(this.scope, this.parent, this.rets)
.start(scanner, invertExpression(this.expression), null, 0, this.mode, 2);
} else if (text === '}') {
const node = this.scope.create('echo', this.expression, scanner.position());
this.scope.tie(this.rets);
return this.parent.return(this.scope.next(), [node], [], scanner)
.next(type, space, text, scanner);
} else {
this.scope.error(scanner.position() + ': Expected "|", "?", or "}" after expression but got ' + tokenName(type, text) + '.');
return this.parent.return(this.scope, [], [], scanner);
}
}
}
class SwitchBlock {
constructor(scope, parent, rets) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.node = null;
this.branches = [];
this.weights = [];
Object.seal(this);
}
start(scanner, expression, variable, value, mode, min) {
value = value || 0;
if (mode === 'loop' && !expression) {
value = 1;
}
expression = expression || ['get', this.scope.name()];
const node = this.scope.create('switch', expression, scanner.position());
this.node = node;
node.variable = variable;
node.value = value;
node.mode = mode;
this.scope.tie(this.rets);
node.branches = this.branches;
node.weights = this.weights;
return new MaybeWeightedCase(this.scope, new Case(this.scope.firstChild(), this, [], this.branches, min || 0));
}
return(_scope, rets, escs, scanner) {
if (this.node.mode === 'pick') {
Scope.tie(rets, 'RET');
rets = [this.node];
// TODO think about what to do with escs.
} else {
this.node.next = 'RET';
}
return this.parent.return(this.scope.next(), rets, escs, scanner);
}
}
class Case {
constructor(scope, parent, rets, branches, min) {
this.scope = scope;
this.parent = parent;
this.rets = rets;
this.branches = branches;
this.min = min;
Object.freeze(this);
}