-
Notifications
You must be signed in to change notification settings - Fork 1
/
cognate.js
972 lines (901 loc) · 30.2 KB
/
cognate.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
// Uncomment for tests and packaging
// import TreeSitter from 'web-tree-sitter';
import { ident2kind, Builtins, initIdent2kind, normalizeIdentifier, value2object, Docs as builtinsDocs, cognate2string, escape, stringEscapes } from './builtins.js';
import * as marked from 'marked';
const CALLSTACK_LIMIT = 3000;
const cacheRegex = Object.create(null);
export class PreludeError extends Error {
constructor(message, options) {
super(message, options);
}
}
// Acts as a signal for the Begin continuation. It should only be treated as an
// error if caught outside of Begin.
export class BeginSignal extends Error {
constructor() {
super("cannot exit 'Begin' from outside the 'Begin' block");
}
}
// Acts as a signal for the Stop function.
export class StopSignal extends Error {
constructor(message, options) {
super(message, options);
}
}
// Global state
const G = {
preludeEnv: {},
preludeReady: false,
ts: {
parser: null,
},
};
export async function initTS() {
await TreeSitter.init()
const parser = new TreeSitter();
const Cognate = await TreeSitter.Language.load("tree-sitter-cognate.wasm");
parser.setLanguage(Cognate);
G.ts.parser = parser;
}
export function initPrelude(preludeText) {
if (preludeText == undefined || preludeText == '' || !G.ts.parser) {
throw new PreludeError("prelude content and parser must be initialized first");
}
const runner = new Runner();
// Parse
const preludeTree = G.ts.parser.parse(preludeText);
let result = runner.parse(preludeTree);
if (result.bail || runner.hasErrors()) {
throw new PreludeError("failed to parse prelude");
}
// Exec
result.error = "";
try {
result = runner.process(result.rootBlock, [], []);
} catch (err) {
if (!(err instanceof StopSignal)) {
console.error(err);
result.error = result.error || err.message;
}
}
if (result.error != '') {
throw new PreludeError(`failed to execute prelude: ${result.error}`);
}
G.preludeEnv = result.env;
}
export async function initCognate() {
await G.ts.init();
}
export function setPreludeReady() {
G.preludeReady = true;
initIdent2kind(G.preludeEnv);
}
const node2object = {
number: (node, userCode) => ({
type: 'number',
value: Number.parseFloat(node.text),
node: node,
userCode: userCode,
}),
string: (node, s, userCode) => ({
type: 'string',
value: s.slice(1, s.length-1),
node: node,
userCode: userCode,
}),
boolean: (node, userCode) => ({
type: 'boolean',
value: (node.text.toLowerCase() == 'true'),
node: node,
userCode: userCode,
}),
identifier: (node, userCode) => ({
type: 'identifier',
value: normalizeIdentifier(node.text),
node: node,
userCode: userCode,
}),
symbol: (node, userCode) => ({
type: 'symbol',
value: node.text.slice(1).toLowerCase(),
node: node,
userCode: userCode,
}),
block: (body, userCode, parent) => ({
type: 'block',
body: body, // list of objects
// This env is a 'spec' of how the env should be initialized when
// the block is executed (again). **IT SHOULD NOT BE MUTATED DIRECTLY**.
//
// Parent of the outmost block of the user's code is the preludeEnv.
// The parent of that, in turn, in undefined.
env: { parent: parent ? parent.env : undefined },
userCode: userCode,
}),
};
export const mockFrontend = {
errors: {
add() {},
redraw() {},
clear() {},
hasAny() { return false; },
},
diagnostics: {
add() {},
clear() {},
},
style: {
marked(s) { return s; },
light(s) { return s; },
},
editor: {
addMark() {},
applyMarks() {},
},
store: {
saveInput() {},
},
// TODO: A more structured approach that's independent of the DOM entirely.
$stack: {},
output: {
add() {},
newline() {},
clear() {},
},
};
export class Runner {
constructor(f) {
// f for 'frontend'
this.callStackSize = 0;
this.tree = undefined;
// Either you give it all or none at all.
this.ui = f ? f : mockFrontend;
this.editor = (f && f.editor) ? f.editor : mockFrontend.editor;
this.store = (f && f.store) ? f.store : mockFrontend.store;
this.$stack = (f && f.$stack) ? f.$stack : {};
this.output = (f && f.output) ? f.output : mockFrontend.output;
}
appendError(err) { this.ui.errors.add(err); }
redrawErrors(heading) { this.ui.errors.redraw(heading); }
clearErrors() { this.ui.errors.clear(); }
hasErrors() { return this.ui.errors.hasAny(); }
clearOutput() { this.output.clear(); }
textMarked(s) { return this.ui.style.marked(s); }
textLight(s) { return this.ui.style.light(s); }
addDiagnostic(node, severity, message) { this.ui.diagnostics.add(node, severity, message); }
clearDiagnostics() { this.ui.diagnostics.clear(); }
// Object to string for displaying the stack & debugging
repr(item) {
switch (item.type) {
case 'identifier':
return escape(item.value);
case 'number':
return item.value;
case 'boolean':
return this.textMarked(item.value ? 'True' : 'False');
case 'string':
return `"${escape(item.value)}"`;
case 'symbol':
return `\\${escape(item.value)}`;
case 'list':
return "(" + [...item.list].reverse().map(this.repr, this).join(", ") + ")";
case 'box':
return "<" + this.repr(item.value[0]) + ">";
case 'list':
return "(" + [...item.list].reverse().map(this.repr, this).join(", ") + ")";
default:
return this.textLight(`(unknown item of type ${this.textMarked(escape(item.type))})`);
}
}
// Represent a stack in a single line as a string.
reprArr(arr) {
let output = "";
const iter = item => {
if (item.type == 'block') {
output += "[";
if (item.body.length > 0) {
item.body.forEach(iter);
output = output.slice(0, output.length-2);
}
output += "], ";
} else {
// output += `${this.repr(item)}, `;
let s = cognate2string(item);
output += `${s.style ? this.ui.style[s.style](s.value) : s.value}, `;
}
};
arr.forEach(iter);
return `[${output.slice(0, output.length-2)}]`;
}
// Parse a syntax tree into a nested block to be processed, flattening
// statement items in reverse order as per how Cognate operates the stack
// within statements.
//
// The entire program has a root "block" representing the outer scope.
parse(tree, rootEnv, userCode) {
const root = tree.rootNode;
let bail = false;
let doc = undefined;
let rootBlock = node2object.block([], userCode);
rootBlock.env.parent = rootEnv;
const inner = (node, currentBlock) => {
let inStmt = false;
let inBlock = false;
let name;
if (node.isMissing) {
name = `MISSING ${node.type}`;
this.appendError(`missing: ${this.textMarked(node.type)} ` + this.textLight(`(${node.startPosition.row}, ${node.startPosition.column})`));
bail = true;
return;
} else if (node.isNamed) {
name = node.type;
} else {
return;
}
if (name == "ERROR") {
if (node.text != '') {
this.appendError(
`unexpected token: '${this.textMarked(node.text)}' ` + this.textLight(`(${node.startPosition.row}, ${node.startPosition.column})`)
);
this.addDiagnostic(node, "error", "unexpected token");
} else {
this.appendError("syntax error " + this.textLight(`(${node.startPosition.row}, ${node.startPosition.column})`));
this.addDiagnostic(node, "error", "syntax error");
}
bail = true;
return;
}
switch (name) {
case "block":
inBlock = true;
currentBlock.body.push(node2object.block([], userCode, currentBlock));
break;
case "statement":
inStmt = true;
currentBlock.body.push(node2object.block([], userCode));
currentBlock.body[currentBlock.body.length-1].env = currentBlock.env;
break;
case "multiline_comment":
doc = node.text.substring(1, node.text.length-1).trim();
break;
case "inline_comment":
case "line_comment":
return;
case "identifier": {
let normalized = normalizeIdentifier(node.text);
if (userCode) {
// TODO: Prevent extra call in node2object
if (ident2kind[normalized]) {
this.editor.addMark(node, ident2kind[normalized]);
}
}
if (normalized == 'Def') {
let obj = node2object.identifier(node, userCode);
obj.doc = doc;
currentBlock.body.push(obj);
doc = undefined;
break;
}
// Fallthrough;
}
case "number":
case "boolean":
case "symbol":
currentBlock.body.push(node2object[name](node, userCode));
break;
case "string": {
let str = node.text;
let finalStr = '';
if (node.children.length != 0) {
let start = 0;
for (let child of node.children) {
// XXX: nodes of other types ignored?
if (child.type == 'escape_sequence') {
let esc = child.text.substr(1);
if (stringEscapes[esc] == undefined) {
// This implementation of cognate will not be able to support all
// escape sequences cognac does, such as the terminal bell.
continue;
}
let startIndex = child.startPosition.column - node.startPosition.column;
finalStr += str.substr(start, startIndex - start);
finalStr += stringEscapes[esc];
start = startIndex + 2;
}
}
finalStr += str.substr(start);
} else {
finalStr = str;
}
currentBlock.body.push(node2object.string(node, finalStr, userCode));
break;
}
case "source_file":
break;
default:
if (!name.startsWith("MISSING")) {
this.appendError(`INTERNAL ERROR: unknown token type ${this.textMarked(name)} from tree-sitter!`);
bail = true;
return;
}
}
if (inStmt) {
let pushto = currentBlock.body[currentBlock.body.length-1];
node.children.forEach(child => inner(child, pushto));
let stmt = currentBlock.body.pop();
for(let i = stmt.body.length-1; i>=0; i--) {
let item = stmt.body[i];
let previous = stmt.body[i+1];
if (item.type == 'identifier') {
if (['Def', 'Let'].includes(item.value)) {
if (!(previous && previous.type == 'identifier')) {
this.appendError(`syntax error: identifier expected after ${item.value}`);
this.addDiagnostic(item.node, "error", `syntax error: identifier expected after ${item.value}`);
bail = true;
return;
} else {
if (currentBlock.env[previous.value]) {
this.appendError(`${item.value} ${this.textMarked(previous.value)}: cannot shadow in the same block`);;
this.addDiagnostic(previous.node, "error", "cannot shadow in the same block");
bail = true;
} else {
currentBlock.env[previous.value] = { type: '_predeclared', kind: item.value };
}
if (!userCode && currentBlock.env.parent == undefined) {
builtinsDocs[previous.value] = marked.parse(item.doc);
}
}
}
} else if (item.type == 'block') {
item.env.parent = currentBlock.env;
}
currentBlock.body.push(item);
previous = item;
}
} else if (inBlock) {
let pushto = currentBlock.body[currentBlock.body.length-1];
node.children.forEach(child => inner(child, pushto));
} else if (node.type == 'source_file') {
node.children.forEach(child => inner(child, currentBlock));
} else {
// XXX: nodes of other types ignored?
}
}
inner(root, rootBlock);
return { rootBlock: rootBlock, bail: bail }
}
analyze(currentBlock) {
let bail = false;
for (let item of currentBlock.body) {
if (bail) {
return;
}
if (item.type == 'identifier' && ident2kind[item.value] == undefined) {
let foundDecl = false;
let e = currentBlock.env;
while (e) {
// TODO
// PERF: find ways to cache
if (e[item.value]) {
switch (e[item.value].kind) {
case 'Let':
foundDecl = true;
break;
case 'Def':
foundDecl = true;
this.editor.addMark(item.node, "function");
break;
}
break;
}
e = e.parent;
}
if (!foundDecl) {
this.appendError(`undefined symbol ${this.textMarked(escape(item.value))}`);
this.addDiagnostic(item.node, "error", "undefined symbol");
}
} else if (item.type == 'block') {
this.analyze(item);
}
}
}
run(code, edited) {
if (!G.ts.parser || !G.preludeReady) {
return;
}
this.clearErrors();
this.clearDiagnostics();
this.clearOutput();
this.$stack.innerHTML = "";
// Parse
this.tree = G.ts.parser.parse(code, this.tree);
let result = this.parse(this.tree, G.preludeEnv, true);
this.analyze(result.rootBlock);
this.editor.applyMarks(true);
if (result.bail || this.hasErrors()) {
this.redrawErrors("Error during parsing!");
} else {
this.redrawErrors();
// Exec
this.callStackSize = 0;
result.error = "";
try {
result = this.process(result.rootBlock, [], []);
} catch (err) {
if (!(err instanceof StopSignal)) {
if (!(err instanceof BeginSignal))
console.error(err); // Only *unexpected* errors should be printed to console
result.error = result.error || err.message;
}
}
if (result.stack !== undefined) this.$stack.innerHTML = this.reprArr(result.stack);
if (result.error != '') {
this.appendError(result.error);
this.redrawErrors("Runtime error!");
} else {
this.redrawErrors();
}
}
// Save input after process finishes to prevent inability to exit potential
// loop where program cannot terminate without editing source code.
if (edited)
this.store.saveInput(code);
}
// Execute a block within a possibly `scoped` environment, with an initial
// stack `op`.
process(/*readonly*/ currentBlock, op, beginSignals) {
let env = {...currentBlock.env, parent: currentBlock.env.parent};
let error = "";
this.callStackSize += 1;
if (this.callStackSize == CALLSTACK_LIMIT) {
error = "call stack overflowed!";
}
function getVar(item) {
if (item.type == 'identifier') {
let e = env;
while (e) {
let value = e[item.value];
if (value != undefined) {
if (value.type == '_predeclared') {
error = `${this.textMarked(item.value)} used before declaration`;
this.addDiagnostic(item.node, "error", "variable used before declaration");
return undefined;
}
return value;
}
e = e.parent;
}
// Should not happen, since these are already checked in analyze(), and
// we are confident that the prelude is *correct*.
return undefined;
} else {
return item;
}
}
function exists(item, kind) {
if (item == undefined) {
error = `expected ${kind}`;
return undefined;
}
return item;
}
function expect(item, type) {
if (item == undefined) {
return undefined;
}
if (type == 'any' || item.type == type) {
return item;
}
error = `expected ${type}, got ${item.type}`;
return undefined;
}
const handleBuiltin = (fnName) => {
const builtin = Builtins[fnName];
let useFn;
let args = [];
if (builtin.overloads != undefined) {
let draftError = `expected one of [${builtin.overloads.map((o) => o.params.map((p) => p.type)).join(', ')}]`
let arg = op.pop();
if (arg == undefined) {
error = draftError;
return;
}
args.push(arg);
useFn = builtin.overloads.find((test) => test.params[0].type == arg.type);
if (useFn == undefined) {
error = `${draftError}, got ${arg.type}`;
return;
}
} else {
useFn = builtin;
}
const {params, returns, fn} = useFn;
for(let i = args.length; i < params.length; i++) {
let param = params[i];
let arg = expect(exists(op.pop(), param.name), param.type);
if (arg == undefined) {
return undefined;
}
args.push(arg);
}
let ret = fn(...args);
// NOTE: fn must not return undefined.
// Either return
// - a JS object with error field, or
// - a value suitable for value2object[returns field]
if (ret.error != undefined) {
error = `in ${this.textMarked(fnName)}: ` + ret.error;
return undefined;
}
if (returns !== null) {
op.push(ret.type == returns ? ret : value2object[returns](ret));
}
}
// Execute the block
for (let s = 0; s < currentBlock.body.length; s++) {
let item = currentBlock.body[s];
let next = currentBlock.body[s+1];
if (error != "") {
break;
}
switch (item.type) {
case 'block': {
// XXX: Fix for:
// Def M (
// Def P;
// Let N;
// Print N;
// P;
// Do If == N 0
// then ()
// else (M (P) - 1 N);
// );
// M (Print "in P") 3;
op.push({
type: 'block',
body: item.body,
env: { ...item.env, parent: env },
});
break;
}
case 'identifier':
if (next != undefined && next.type == 'identifier') {
if (['Def', 'Let'].includes(next.value)) {
op.push(item);
continue;
}
}
{
let fn = getVar(item);
if (fn && fn.type == 'function') {
let result = { error: "" };
try {
result = this.process(fn.block, op, beginSignals);
} catch (err) {
if (beginSignals.includes(err) || err instanceof StopSignal)
throw err;
if (!(err instanceof BeginSignal))
console.error(err);
result.error = result.error || err.message;
}
if (result.error != "") {
error = `in ${this.textMarked(item.value)}: ${result.error}`;
break;
}
op = result.stack;
continue;
}
}
switch (item.value) {
// Binding
case 'Def': {
// This check is technically done already during parsing.
let a = expect(exists(op.pop(), 'identifier'), 'identifier');
if (a == undefined) {
error = `in ${this.textMarked('Def')}: ${error}`;
break;
}
let b = expect(exists(op.pop(), `function body`), 'block');
if (b == undefined) {
error = `in ${this.textMarked('Def')}: ${error}`;
break;
}
env[a.value] = { type: 'function', block: b, doc: item.doc };
break;
}
case 'Let': {
let a = expect(exists(op.pop(), 'identifier'), 'identifier');
if (a == undefined) {
error = `in ${this.textMarked('Let')}: ${error}`;
break;
}
let b = op.pop();
if (b == undefined) {
error = `in ${this.textMarked('Let')}: expected value to set`;
break;
}
env[a.value] = {...b};
break;
}
case 'Set': {
let a = expect(exists(op.pop(), 'box'), 'box');
if (a == undefined) {
error = `in ${this.textMarked('Set')}: ${error}`;
break;
}
let b = op.pop();
if (b == undefined) {
error = `in ${this.textMarked('Set')}: expected value to set`;
break;
}
a.value[0] = b;
break;
}
// Special types
case 'List': {
let block = expect(exists(op.pop(), 'block'), 'block');
if (block == undefined) {
error = `in ${this.textMarked('List')}: ${error}`;
break;
}
let list = [];
let result = { error: "" };
try {
result = this.process(block, list, beginSignals);
} catch (err) {
if (beginSignals.includes(err) || err instanceof StopSignal)
throw err;
if (!(err instanceof BeginSignal))
console.error(err);
result.error = result.error || err.message;
}
if (result.error != "") {
error = `in ${this.textMarked('List')}: ${result.error}`;
break;
}
op.push(value2object.list(list));
break;
}
case 'Table': {
let block = expect(exists(op.pop(), 'block'), 'block');
if (block == undefined) {
error = `in ${this.textMarked('Table')}: ${error}`;
break;
}
if (block.body.length % 2 != 0) {
error = `in ${this.textMarked('Table')}: Table initializer must be key-value pairs`;
break;
}
let pairs = [];
let result = { error: "" };
try {
result = this.process(block, pairs, beginSignals);
} catch (err) {
if (beginSignals.includes(err) || err instanceof StopSignal)
throw err;
if (!(err instanceof BeginSignal))
console.error(err);
result.error = result.error || err.message;
}
if (result.error != "") {
error = `in ${this.textMarked('List')}: ${result.error}`;
break;
}
let table;
for (let i = 0; i < pairs.length; i += 2) {
table = Builtins.Insert.fn(pairs[i+1], pairs[i], {table: table});
}
op.push(value2object.table(table));
break;
}
case 'Box': {
let value = exists(op.pop(), 'value');
if (value == undefined) {
error = `in ${this.textMarked('Box')}: ${error}`;
break;
}
op.push(value2object.box(value));
break;
}
case 'Unbox': {
let box = expect(exists(op.pop(), 'box'), 'box');
if (box == undefined) {
error = `in ${this.textMarked('Unbox')}: ${error}`;
break;
}
op.push(box.value[0]);
break;
}
case 'Regex': {
let s = expect(exists(op.pop(), 'regex string'), 'string');
if (s.value.length == 0) {
/// Not supported by CognaC
error = `in ${this.textMarked('Regex')}: empty regex is invalid`;
break;
}
let t = expect(exists(op.pop(), 'test string'), 'string');
if (t == undefined) {
error = `in ${this.textMarked('Regex')}: ${error}`;
break;
}
let regex;
try {
regex = cacheRegex[s.value] || new RegExp(s.value);
} catch (err) {
error = `in ${this.textMarked('Regex')}: regex compile error: ${err}`;
break;
}
cacheRegex[s.value] = regex;
op.push(value2object.boolean(regex.test(t.value)));
break;
}
case 'Regex-match': {
let s = expect(exists(op.pop(), 'regex string'), 'string');
if (s.value.length == 0) {
/// Not supported by CognaC
error = `in ${this.textMarked('Regex-match')}: empty regex is invalid`;
break;
}
let t = expect(exists(op.pop(), 'test string'), 'string');
if (t == undefined) {
/// Not supported by CognaC
error = `in ${this.textMarked('Regex-match')}: ${error}`;
break;
}
let regex;
try {
regex = cacheRegex[s.value] || new RegExp(s.value);
} catch (err) {
error = `in ${this.textMarked('Regex-match')}: regex compile error: ${err}`;
break;
}
cacheRegex[s.value] = regex;
let result = regex.exec(t.value);
if (result) {
for (let capture of [...result].splice(1).reverse()) {
op.push(value2object.string(capture));
}
}
op.push(value2object.boolean(result != null));
break;
}
// Special
case 'Stack': {
let list = [...op];
op.push(value2object.list(list));
break;
}
case 'Clear': {
while (op.length > 0) {
op.pop();
}
break;
}
case 'Error': {
let msg = expect(exists(op.pop(), 'error message'), 'string');
if (msg == undefined) {
// How ironic
error = `in ${this.textMarked('Error')}: ${error}`;
} else {
error = msg.value;
}
break;
}
case 'Stop': {
throw new StopSignal();
}
case 'Begin': {
let block = expect(exists(op.pop(), 'block'), 'block');
if (block == undefined) {
error = `in ${this.textMarked('Begin')}: ${error}`;
break;
}
let beginSignal = new BeginSignal();
op.push({
type: 'block',
body: [{type: 'identifier', value: '_exitBegin'}],
env: { beginSignal },
});
let result = { error: "" };
try {
result = this.process(block, op, [...beginSignals, beginSignal]);
} catch (err) {
if (beginSignals.includes(err) || err instanceof StopSignal)
throw err;
if (err !== beginSignal) {
if (!(err instanceof BeginSignal))
console.error(err);
result.error = result.error || err.message;
}
}
if (result.error != "") {
error = `in ${this.textMarked('Begin')}: ${result.error}`;
}
break;
}
case '_exitBegin': {
throw env.beginSignal;
}
// I/O
case 'Show': {
let item = exists(op.pop(), 'value');
let str = cognate2string(item, false);
if (str != undefined) {
op.push(str);
}
break;
}
case 'Print': {
let item = exists(op.pop(), 'value');
let str = cognate2string(item, false);
if (str != undefined) {
this.output.add(str);
this.output.newline();
} else {
error = `in ${this.textMarked('Print')}: ${error}`
}
break;
}
case 'Put': {
let item = exists(op.pop(), 'value');
let str = cognate2string(item, false);
if (str != undefined) {
this.output.add(str);
} else {
error = `in ${this.textMarked('Print')}: ${error}`
}
break;
}
// Types
case 'Number?':
case 'String?':
case 'Symbol?':
case 'Block?':
case 'List?':
case 'Boolean?': {
let a = exists(op.pop(), 'value');
let type = item.value.slice(0, item.value.length-1).toLowerCase();
if (a != undefined)
op.push(value2object.boolean(a.type == type));
else
error = `in ${this.textMarked(item.value)}: ${error}`;
break;
}
case 'Number!':
case 'Symbol!':
case 'String!':
case 'Block!':
case 'List!':
case 'Boolean!': {
let a = exists(op[op.length-1], 'value');
let type = item.value.slice(0, item.value.length-1).toLowerCase();
if (a != undefined && a.type != type)
error = `in ${this.textMarked(item.value)}: ${type} assertion failed`
break;
}
default: {
if (Builtins[item.value]) {
handleBuiltin(item.value);
break;
} else {
let a = getVar(item);
if (a != undefined) {
op.push(a);
} else {
// Should not happen, since undefined symbols are caught in `analyzeBlock`
}
}
break;
}
};
break;
default:
op.push(item);
break;
};
}
this.callStackSize -= 1;
return {stack: op, error: error, env: env};
}
};
export { initIdent2kind, ident2kind, escape };