mevento 2.0.6 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2175 +0,0 @@
1
- function error(token, expected) {
2
- const text = `Invalid token ${token.type}[${token.value}] at ${token.line}, ${token.col} ${expected ? `: expecting ${expected} token` : ''}\n`;
3
- throw Error(text);
4
- }
5
- function isNum(value) {
6
- return typeof value === 'number';
7
- }
8
- function isBoolean(value) {
9
- return typeof value === 'boolean';
10
- }
11
- function isString(value) {
12
- return typeof value === 'string';
13
- }
14
- function _boolValue(test) {
15
- return (test === null ||
16
- (isNum(test) && test === 0) ||
17
- (isString(test) && test.length === 0) ||
18
- (isBoolean(test) && !test)) ? false : true;
19
- }
20
- ;
21
- function hashCode(str) {
22
- let hash = 0;
23
- let i = 0;
24
- let chr;
25
- if (str.length === 0)
26
- return hash;
27
- for (i = 0; i < str.length; i++) {
28
- chr = str.charCodeAt(i);
29
- hash = ((hash << 5) - hash) + chr;
30
- hash |= 0; // Convert to 32bit integer
31
- }
32
- return hash;
33
- }
34
- ;
35
- class TokenType {
36
- }
37
- TokenType.id = 0;
38
- TokenType.comma = 1;
39
- TokenType.semi = 2;
40
- TokenType.numberConst = 3;
41
- TokenType.stringConst = 4;
42
- TokenType.equal = 5;
43
- TokenType.lparen = 6;
44
- TokenType.rparen = 7;
45
- TokenType.eol = 8;
46
- TokenType.eof = 9;
47
- TokenType.lbrace = 10;
48
- TokenType.rbrace = 11;
49
- TokenType.lbracket = 12;
50
- TokenType.rbracket = 13;
51
- TokenType.great = 14;
52
- TokenType.greatEq = 15;
53
- TokenType.less = 16;
54
- TokenType.lessEq = 17;
55
- TokenType.eqeq = 18;
56
- TokenType.IF = 19;
57
- TokenType.ELSE = 20;
58
- TokenType.TRUE = 21;
59
- TokenType.FALSE = 22;
60
- TokenType.NULL = 23;
61
- TokenType.not = 24;
62
- TokenType.notEq = 25;
63
- TokenType.and = 26;
64
- TokenType.or = 27;
65
- TokenType.plus = 28;
66
- TokenType.minus = 29;
67
- TokenType.div = 30;
68
- TokenType.mult = 31;
69
- TokenType.mod = 32;
70
- TokenType.invalid = 33;
71
- TokenType.colon = 34;
72
- TokenType.WHILE_TILL = 35;
73
- TokenType.FOR_LOOP = 36;
74
- TokenType.up = 37;
75
- TokenType.down = 38;
76
- TokenType.with = 39;
77
- TokenType.in = 40;
78
- TokenType.TILL = 41;
79
- TokenType.BREAK = 42;
80
- TokenType.CONTINUE = 43;
81
- TokenType.nullity = 44;
82
- TokenType.RETURN = 45;
83
- class Token {
84
- constructor(type, value, line = 1, col = 1) {
85
- this.type = type;
86
- this.value = value;
87
- this.line = line;
88
- this.col = col;
89
- }
90
- static from(type, value) {
91
- return new Token(type, value);
92
- }
93
- toString() {
94
- return `[${this.type.toString()}, ${this.value}]`;
95
- }
96
- }
97
- class Chars {
98
- }
99
- Chars.equal = '='.charCodeAt(0);
100
- Chars.comma = ','.charCodeAt(0);
101
- Chars.semiColon = ';'.charCodeAt(0);
102
- Chars.lparen = '('.charCodeAt(0);
103
- Chars.rparen = ')'.charCodeAt(0);
104
- Chars.backslash = '\\'.charCodeAt(0);
105
- Chars.quote = '"'.charCodeAt(0);
106
- Chars.squote = "'".charCodeAt(0);
107
- Chars.plus = "+".charCodeAt(0);
108
- Chars.minus = "-".charCodeAt(0);
109
- Chars.star = "*".charCodeAt(0);
110
- Chars.slash = "/".charCodeAt(0);
111
- Chars.percent = "%".charCodeAt(0);
112
- Chars.lbrace = "{".charCodeAt(0);
113
- Chars.rbrace = "}".charCodeAt(0);
114
- Chars.lbracket = "[".charCodeAt(0);
115
- Chars.rbracket = "]".charCodeAt(0);
116
- Chars.not = "!".charCodeAt(0);
117
- Chars.great = ">".charCodeAt(0);
118
- Chars.less = "<".charCodeAt(0);
119
- Chars.and = "&".charCodeAt(0);
120
- Chars.pipe = "|".charCodeAt(0);
121
- Chars.colon = ":".charCodeAt(0);
122
- Chars.questionMark = "?".charCodeAt(0);
123
- Chars.shebang = "#".charCodeAt(0);
124
- class LexerDictionary {
125
- constructor(lang, keywords) {
126
- this.keywords = {};
127
- this.keywords = { ...keywords };
128
- this.lang = lang;
129
- }
130
- }
131
- /**
132
- * Lexing => Parsing ===> AST ===> Walking
133
- */
134
- /**
135
- * Lexer
136
- */
137
- class Lexer {
138
- constructor(source) {
139
- this._position = 0;
140
- this._line = 1;
141
- this._col = 1;
142
- this._currentChar = -1;
143
- this._source = source;
144
- this._currentChar = this._source[this._position].charCodeAt(0);
145
- this._resolveLanguage();
146
- }
147
- get source() {
148
- return this._source;
149
- }
150
- _resolveLanguage() {
151
- let token = this.nextToken();
152
- if (token.type === TokenType.less) {
153
- // language setting
154
- const idToken = this.nextToken();
155
- if (idToken.type !== TokenType.id) {
156
- error(token);
157
- }
158
- const lang = idToken.value.toString();
159
- this._language = Lexer.languages.find((element) => element.lang === lang) ?? Lexer._defaultLanguage;
160
- token = this.nextToken();
161
- if (token.type !== TokenType.great) {
162
- error(token);
163
- }
164
- }
165
- else {
166
- this._language = Lexer._defaultLanguage;
167
- // reset reading
168
- this._position = 0;
169
- this._currentChar = this._source[this._position].charCodeAt(0);
170
- }
171
- }
172
- _advance() {
173
- this._position++;
174
- if (this._position >= this._source.length) {
175
- this._currentChar = -1;
176
- return;
177
- }
178
- this._currentChar = this._source[this._position].charCodeAt(0);
179
- this._col++;
180
- }
181
- _pick() {
182
- if (this._position + 1 >= this._source.length) {
183
- return -1;
184
- }
185
- return this._source[this._position + 1].charCodeAt(0);
186
- }
187
- _isId(code) {
188
- if (code < 48) {
189
- return code === 36;
190
- }
191
- if (code < 58) {
192
- return true;
193
- }
194
- if (code < 65) {
195
- return false;
196
- }
197
- if (code < 91) {
198
- return true;
199
- }
200
- if (code < 97) {
201
- return code === 95;
202
- }
203
- if (code < 123) {
204
- return true;
205
- }
206
- return false;
207
- }
208
- _isIdStart(code) {
209
- if (code < 65) {
210
- return code === 36;
211
- }
212
- if (code < 91) {
213
- return true;
214
- }
215
- if (code < 97) {
216
- return code === 95;
217
- }
218
- if (code < 123) {
219
- return true;
220
- }
221
- return false;
222
- }
223
- _id() {
224
- let ret = "";
225
- const lastC = this._col;
226
- const lastP = this._position;
227
- const line = this._line;
228
- while (this._isId(this._currentChar)) {
229
- ret += String.fromCharCode(this._currentChar);
230
- this._advance();
231
- }
232
- const translatedId = this._language
233
- ? (this._language.keywords[ret]) ?? ret
234
- : ret;
235
- return Lexer.RESERVED[translatedId] ?? new Token(TokenType.id, ret, line, lastC);
236
- }
237
- _isLineEnd(c) {
238
- return c === 10 ||
239
- c === 13 ||
240
- ['\n', '\r', '\u2028', '\u2029'].includes(String.fromCharCode(c));
241
- }
242
- _isWhiteSpace(c) {
243
- return [' ', '\t'].includes(String.fromCharCode(c));
244
- }
245
- _isDigit(c) {
246
- // tslint:disable-next-line:no-bitwise
247
- return c > 0 && (c ^ 0x30) <= 9;
248
- }
249
- _skipWhiteSpace() {
250
- while (this._isWhiteSpace(this._currentChar) === true) {
251
- this._advance();
252
- }
253
- }
254
- _number() {
255
- let ret = "";
256
- const lastC = this._col;
257
- const lastP = this._position;
258
- const line = this._line;
259
- const first = String.fromCharCode(this._currentChar);
260
- this._advance();
261
- const bc = String.fromCharCode(this._currentChar);
262
- let base = 10;
263
- if (first === '0' && ['b', 'B', 'x', 'X', 'o', 'O'].includes(bc)) {
264
- // look ahead to see that the number is tagged
265
- this._advance();
266
- switch (bc.toLowerCase()) {
267
- case 'b':
268
- base = 2;
269
- break;
270
- case "o":
271
- base = 8;
272
- break;
273
- case "x":
274
- base = 16;
275
- break;
276
- default:
277
- base = 10;
278
- }
279
- }
280
- else {
281
- ret += first;
282
- base = 10;
283
- }
284
- while (this._isDigit(this._currentChar) ||
285
- (base === 16 &&
286
- ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f']
287
- .includes(String.fromCharCode(this._currentChar)))) {
288
- ret += String.fromCharCode(this._currentChar);
289
- this._advance();
290
- }
291
- if (String.fromCharCode(this._currentChar) === '.' && this._isDigit(this._pick()) === true) {
292
- if (base !== 10) {
293
- error(new Token(TokenType.id, bc, line, lastP));
294
- }
295
- // floating number
296
- ret += String.fromCharCode(this._currentChar);
297
- this._advance();
298
- while (this._isDigit(this._currentChar)) {
299
- ret += String.fromCharCode(this._currentChar);
300
- this._advance();
301
- }
302
- return new Token(TokenType.numberConst, parseFloat(ret), line, lastC);
303
- }
304
- return new Token(TokenType.numberConst, parseInt(ret, base), line, lastC);
305
- }
306
- _literalString(startChar) {
307
- let ret = "";
308
- let lastChar = -1;
309
- const lastP = this._position;
310
- const lastC = this._col;
311
- const lastL = this._line;
312
- while (this._currentChar !== -1) {
313
- const next = String.fromCharCode(this._pick());
314
- if (this._currentChar === Chars.backslash) {
315
- // scape char
316
- if ([
317
- startChar,
318
- '\\',
319
- '0',
320
- 'a',
321
- 'b',
322
- 'e',
323
- 'f',
324
- 'n',
325
- 'r',
326
- 't',
327
- 'u',
328
- 'U',
329
- 'v',
330
- 'x'
331
- ].includes(next)) {
332
- lastChar = this._currentChar;
333
- this._advance();
334
- continue;
335
- }
336
- }
337
- if (this._currentChar === startChar && lastChar !== Chars.backslash) {
338
- // end
339
- break;
340
- }
341
- ret += String.fromCharCode(this._currentChar);
342
- lastChar = this._currentChar;
343
- this._advance();
344
- }
345
- return new Token(TokenType.stringConst, ret, lastL, lastC);
346
- }
347
- _skipLineComment() {
348
- while (!this._isLineEnd(this._currentChar) === true && this._currentChar != -1) {
349
- this._advance();
350
- }
351
- }
352
- _skipComment() {
353
- while (this._currentChar !== -1) {
354
- if (this._currentChar === Chars.star && this._pick() === Chars.shebang) {
355
- // end of block comment
356
- this._advance();
357
- this._advance();
358
- break;
359
- }
360
- this._advance();
361
- }
362
- }
363
- // f=SUBMIT_FORM(xxx);
364
- // r = CALL_API(API_ID, f);
365
- // DISPLAY("MSG #r")
366
- nextToken() {
367
- const lastL = this._line;
368
- const lastC = this._col;
369
- const lastP = this._position;
370
- while (this._currentChar !== -1) {
371
- if (this._isLineEnd(this._currentChar)) {
372
- this._line++;
373
- this._col = 1;
374
- this._advance();
375
- return new Token(TokenType.eol, "\n", lastL, lastC);
376
- }
377
- if (this._isWhiteSpace(this._currentChar)) {
378
- this._skipWhiteSpace();
379
- continue;
380
- }
381
- if (this._currentChar == Chars.shebang) {
382
- this._advance();
383
- if (this._currentChar === Chars.star) {
384
- // mutli block comment
385
- this._advance();
386
- this._skipComment();
387
- }
388
- else {
389
- this._skipLineComment();
390
- }
391
- continue;
392
- }
393
- if (this._isDigit(this._currentChar)) {
394
- return this._number();
395
- }
396
- if (this._isIdStart(this._currentChar)) {
397
- return this._id();
398
- }
399
- if (this._currentChar === Chars.equal) {
400
- this._advance();
401
- if (this._currentChar === Chars.equal) {
402
- this._advance();
403
- return new Token(TokenType.eqeq, "==", lastL, lastC);
404
- }
405
- return new Token(TokenType.equal, "=", lastL, lastC);
406
- }
407
- if (this._currentChar === Chars.great) {
408
- this._advance();
409
- if (this._currentChar === Chars.equal) {
410
- this._advance();
411
- return new Token(TokenType.greatEq, ">=", lastL, lastC);
412
- }
413
- return new Token(TokenType.great, ">", lastL, lastC);
414
- }
415
- if (this._currentChar === Chars.less) {
416
- this._advance();
417
- if (this._currentChar === Chars.equal) {
418
- this._advance();
419
- return new Token(TokenType.lessEq, "<=", lastL, lastC);
420
- }
421
- return new Token(TokenType.less, "<", lastL, lastC);
422
- }
423
- if (this._currentChar === Chars.semiColon) {
424
- this._advance();
425
- return new Token(TokenType.semi, ";", lastL, lastC);
426
- }
427
- if (this._currentChar === Chars.lparen) {
428
- this._advance();
429
- return new Token(TokenType.lparen, "(", lastL, lastC);
430
- }
431
- if (this._currentChar === Chars.rparen) {
432
- this._advance();
433
- return new Token(TokenType.rparen, ")", lastL, lastC);
434
- }
435
- if (this._currentChar === Chars.comma) {
436
- this._advance();
437
- return new Token(TokenType.comma, ",", lastL, lastC);
438
- }
439
- if (this._currentChar === Chars.lbrace) {
440
- this._advance();
441
- return new Token(TokenType.lbrace, "{", lastL, lastC);
442
- }
443
- if (this._currentChar === Chars.rbrace) {
444
- this._advance();
445
- return new Token(TokenType.rbrace, "}", lastL, lastC);
446
- }
447
- if (this._currentChar === Chars.lbracket) {
448
- this._advance();
449
- return new Token(TokenType.lbracket, "[", lastL, lastC);
450
- }
451
- if (this._currentChar === Chars.rbracket) {
452
- this._advance();
453
- return new Token(TokenType.rbracket, "]", lastL, lastC);
454
- }
455
- if (this._currentChar === Chars.plus) {
456
- this._advance();
457
- return new Token(TokenType.plus, "+", lastL, lastC);
458
- }
459
- if (this._currentChar === Chars.minus) {
460
- this._advance();
461
- return new Token(TokenType.minus, "-", lastL, lastC);
462
- }
463
- if (this._currentChar === Chars.slash) {
464
- this._advance();
465
- return new Token(TokenType.div, "/", lastL, lastC);
466
- }
467
- if (this._currentChar === Chars.star) {
468
- this._advance();
469
- return new Token(TokenType.mult, "*", lastL, lastC);
470
- }
471
- if (this._currentChar === Chars.percent) {
472
- this._advance();
473
- return new Token(TokenType.mod, "%", lastL, lastC);
474
- }
475
- if (this._currentChar === Chars.colon) {
476
- this._advance();
477
- return new Token(TokenType.colon, ":", lastL, lastC);
478
- }
479
- if (this._currentChar === Chars.not) {
480
- this._advance();
481
- if (this._currentChar === Chars.equal) {
482
- this._advance();
483
- return new Token(TokenType.notEq, "!=", lastL, lastC);
484
- }
485
- return new Token(TokenType.not, "!", lastL, lastC);
486
- }
487
- if (this._currentChar === Chars.and && this._pick() === Chars.and) {
488
- this._advance();
489
- this._advance();
490
- return new Token(TokenType.and, "&&", lastL, lastC);
491
- }
492
- if (this._currentChar === Chars.pipe && this._pick() === Chars.pipe) {
493
- this._advance();
494
- this._advance();
495
- return new Token(TokenType.or, '||', lastL, lastC);
496
- }
497
- if (this._currentChar === Chars.questionMark && this._pick() === Chars.questionMark) {
498
- this._advance();
499
- this._advance();
500
- return new Token(TokenType.nullity, '??', lastL, lastC);
501
- }
502
- if (this._currentChar === Chars.quote || this._currentChar === Chars.squote) {
503
- const startChar = this._currentChar;
504
- this._advance();
505
- const t = this._literalString(startChar);
506
- this._advance();
507
- return t;
508
- }
509
- return new Token(TokenType.invalid, String.fromCharCode(this._currentChar), lastL, lastC);
510
- }
511
- return new Token(TokenType.eof, "", lastL, lastC);
512
- }
513
- }
514
- Lexer._defaultLanguage = new LexerDictionary("en", {
515
- "if": "if",
516
- "else": "else",
517
- "true": "true",
518
- "false": "false",
519
- "null": "null",
520
- "while": "while",
521
- "for": "for",
522
- "with": "with",
523
- "up": "up",
524
- "down": "down",
525
- "till": "till",
526
- "in": "in",
527
- "break": "break",
528
- "continue": "continue",
529
- "return": "return",
530
- });
531
- Lexer.languages = [
532
- Lexer._defaultLanguage,
533
- new LexerDictionary("fr", {
534
- "si": "if",
535
- "sinon": "else",
536
- "vrai": "true",
537
- "faux": "false",
538
- "nul": "null",
539
- "tanque": "while",
540
- "pour": "for",
541
- "avec": "with",
542
- "mont": "up",
543
- "desc": "down",
544
- "jusqua": "till",
545
- "dans": "in",
546
- "couper": "break",
547
- "continuer": "continue",
548
- "returner": "return",
549
- }),
550
- new LexerDictionary("bm", {
551
- "nii": "if",
552
- "note": "else",
553
- "tien": "true",
554
- "galon": "false",
555
- "gansan": "null",
556
- "foo": "while",
557
- "seginka": "for",
558
- "niin": "with",
559
- "kay": "up",
560
- "kaj": "down",
561
- "kata": "till",
562
- "kono": "in",
563
- "tike": "break",
564
- "ipan": "continue",
565
- "segin": "return",
566
- }),
567
- ];
568
- Lexer.RESERVED = {
569
- "if": Token.from(TokenType.IF, "if"),
570
- "else": Token.from(TokenType.ELSE, "else"),
571
- "true": Token.from(TokenType.TRUE, true),
572
- "false": Token.from(TokenType.FALSE, false),
573
- "null": Token.from(TokenType.NULL, null),
574
- "for": Token.from(TokenType.FOR_LOOP, "for"),
575
- "while": Token.from(TokenType.WHILE_TILL, "while"),
576
- "with": Token.from(TokenType.with, "with"),
577
- "up": Token.from(TokenType.up, "up"),
578
- "down": Token.from(TokenType.down, "down"),
579
- "till": Token.from(TokenType.TILL, "till"),
580
- "in": Token.from(TokenType.in, "in"),
581
- "break": Token.from(TokenType.BREAK, "break"),
582
- "continue": Token.from(TokenType.CONTINUE, "continue"),
583
- "return": Token.from(TokenType.RETURN, "return"),
584
- };
585
- /**
586
- * Parser
587
- */
588
- class AST {
589
- constructor(line, col) {
590
- this.line = line;
591
- this.col = col;
592
- }
593
- dump() {
594
- return this.toString();
595
- }
596
- }
597
- class RootAST extends AST {
598
- constructor(body, name, source) {
599
- super(1, 1);
600
- this.body = body;
601
- this.name = name;
602
- this.source = source;
603
- }
604
- dump() {
605
- let builder = `Module ${this.name} Start {`;
606
- for (const it of this.body) {
607
- builder += `${it.dump()}\n`;
608
- }
609
- builder += "}";
610
- return builder;
611
- }
612
- }
613
- class BlockStatementAST extends AST {
614
- constructor(body, token) {
615
- super(token.line, token.col);
616
- this.body = body;
617
- }
618
- toString() {
619
- return `{\n${this.body.map((e) => e.toString()).join("\n")}}`;
620
- }
621
- }
622
- class IdentifierAST extends AST {
623
- constructor(token) {
624
- super(token.line, token.col);
625
- this.value = token.value.toString();
626
- }
627
- toString() {
628
- return this.value;
629
- }
630
- }
631
- class LiteralAST extends AST {
632
- constructor(token, raw) {
633
- super(token.line, token.col);
634
- this.value = token.value;
635
- this.raw = raw;
636
- }
637
- toString() {
638
- return this.value.toString();
639
- }
640
- }
641
- class AssignmentExpressionAST extends AST {
642
- constructor(identifier, init) {
643
- super(identifier.line, identifier.col);
644
- this.identifier = identifier;
645
- this.init = init;
646
- }
647
- toString() {
648
- return `${this.identifier} = ${this.init}`;
649
- }
650
- }
651
- class ExpressionStatementAST extends AST {
652
- constructor(expression) {
653
- super(expression.line, expression.col);
654
- this.expression = expression;
655
- }
656
- toString() {
657
- return this.expression.toString();
658
- }
659
- }
660
- class CallExpressionAST extends AST {
661
- constructor(callee, ags) {
662
- super(callee.line, callee.col);
663
- this.callee = callee;
664
- this.arguments = ags;
665
- }
666
- toString() {
667
- return `${this.callee.toString()}(...${this.arguments.length})`;
668
- }
669
- }
670
- class BinaryExpressionAST extends AST {
671
- constructor(left, operation, right) {
672
- super(left.line, left.col);
673
- this.left = left;
674
- this.operation = operation;
675
- this.right = right;
676
- }
677
- toString() {
678
- return `${this.left} ${this.operation} ${this.right}`;
679
- }
680
- }
681
- class UnaryExpressionAST extends AST {
682
- constructor(operation, argument) {
683
- super(operation.line, operation.col);
684
- this.operation = operation, this.argument = argument;
685
- }
686
- toString() {
687
- return `${this.operation} ${this.argument}`;
688
- }
689
- }
690
- class IfStatementAST extends AST {
691
- constructor(test, consequent, alternate) {
692
- super(test.line, test.col);
693
- this.test = test;
694
- this.consequent = consequent;
695
- this.alternate = alternate;
696
- }
697
- toString() {
698
- return `if ${this.test} ${this.consequent} ${this.alternate ? `else ${this.alternate} ` : ''}`;
699
- }
700
- }
701
- class LogicalExpressionAST extends AST {
702
- constructor(left, operator, right) {
703
- super(left.line, left.col);
704
- this.left = left;
705
- this.operator = operator;
706
- this.right = right;
707
- }
708
- toString() {
709
- return `${this.left} ${this.operator.value} ${this.right}`;
710
- }
711
- }
712
- class IndexAccessorAST extends AST {
713
- constructor(owner, key, computed = false) {
714
- super(owner.line, owner.col);
715
- this.computed = false;
716
- this.owner = owner;
717
- this.key = key;
718
- this.computed = computed;
719
- }
720
- toString() {
721
- return `${this.owner}[${this.key}]`;
722
- }
723
- }
724
- class ObjectExpression extends AST {
725
- constructor(props, startToken, endToken) {
726
- super(startToken?.line, startToken?.col);
727
- this.properties = props;
728
- }
729
- toString() {
730
- return "{...}";
731
- }
732
- }
733
- class ArrayExpression extends AST {
734
- constructor(props, start, end) {
735
- super(start?.line, start?.col);
736
- this.elements = props;
737
- }
738
- toString() {
739
- return "[...]";
740
- }
741
- }
742
- class ObjectProperty extends AST {
743
- constructor(key, value) {
744
- super(key.line, key.col);
745
- this.value = value;
746
- this.key = key;
747
- }
748
- }
749
- class WhileLoopStatement extends AST {
750
- constructor(test, body, startToken, endToken, retain = false) {
751
- super(startToken?.line, startToken?.col);
752
- this.retain = false;
753
- this.test = test;
754
- this.body = body;
755
- this.retain = retain;
756
- }
757
- }
758
- class ForLoopStatement extends AST {
759
- constructor(init, test, update, direction, body, startToken, endToken, retain = false) {
760
- super(startToken?.line, startToken?.col);
761
- this.init = init;
762
- this.test = test;
763
- this.update = update;
764
- this.direction = direction;
765
- this.body = body;
766
- this.retain = retain;
767
- }
768
- }
769
- class ForOfStatement extends AST {
770
- constructor(identifier, collection, body, startToken, endToken, retain = false) {
771
- super(startToken?.line, startToken?.col);
772
- this.identifier = identifier;
773
- this.collection = collection;
774
- this.body = body;
775
- this.retain = retain;
776
- }
777
- }
778
- class TupleExpression extends AST {
779
- constructor(first, second) {
780
- super(first.line, first.col);
781
- this.first = first;
782
- this.second = second;
783
- }
784
- }
785
- class BreakAST extends AST {
786
- constructor(line, col) {
787
- super(line, col);
788
- }
789
- }
790
- class ReturnAST extends AST {
791
- constructor(value, line, col) {
792
- super(line, col);
793
- this.value = value;
794
- }
795
- }
796
- class ContinueAST extends AST {
797
- constructor(line, col) {
798
- super(line, col);
799
- }
800
- }
801
- class Parser {
802
- constructor(lexer) {
803
- this.currentToken = lexer.nextToken();
804
- this.lexer = lexer;
805
- }
806
- _eat(type) {
807
- if (this.currentToken?.type === type) {
808
- this.currentToken = this.lexer.nextToken();
809
- }
810
- else {
811
- error(this.currentToken, type);
812
- }
813
- }
814
- _eatEOL() {
815
- while (this.currentToken?.type === TokenType.eol) {
816
- this._eat(TokenType.eol);
817
- }
818
- }
819
- _eatSemiOrEOL() {
820
- while (this.currentToken?.type === TokenType.eol ||
821
- this.currentToken?.type === TokenType.semi) {
822
- this._eat(this.currentToken.type);
823
- }
824
- }
825
- _eatSemi() {
826
- while (this.currentToken?.type === TokenType.semi) {
827
- this._eat(TokenType.semi);
828
- }
829
- }
830
- _variable() {
831
- const node = new IdentifierAST(this.currentToken);
832
- this._eat(TokenType.id);
833
- return node;
834
- }
835
- _return() {
836
- const token = this.currentToken;
837
- let value = undefined;
838
- if (!this._expect(TokenType.eol) && !this._expect(TokenType.semi)) {
839
- value = this._expression();
840
- }
841
- return new ReturnAST(value, token?.line, token?.col);
842
- }
843
- _factor() {
844
- const token = this.currentToken;
845
- switch (token.type) {
846
- case TokenType.plus:
847
- case TokenType.minus:
848
- case TokenType.not:
849
- this._eat(this.currentToken.type);
850
- return new UnaryExpressionAST(token, this._term());
851
- case TokenType.numberConst:
852
- this._eat(TokenType.numberConst);
853
- return new LiteralAST(token, token.value.toString());
854
- case TokenType.stringConst:
855
- this._eat(TokenType.stringConst);
856
- return new LiteralAST(token, token.value.toString());
857
- case TokenType.lparen:
858
- this._eat(TokenType.lparen);
859
- const node = this._expression();
860
- this._eat(TokenType.rparen);
861
- return node;
862
- case TokenType.TRUE:
863
- case TokenType.FALSE:
864
- this._eat(this.currentToken.type);
865
- return new LiteralAST(token, token.value.toString());
866
- case TokenType.NULL:
867
- this._eat(TokenType.NULL);
868
- return new LiteralAST(token, "null");
869
- case TokenType.lbracket:
870
- // literal array expression
871
- return this._arrayExpression();
872
- case TokenType.lbrace:
873
- return this._objectExpression();
874
- case TokenType.IF:
875
- return this._ifStatement();
876
- default:
877
- return this._variable();
878
- }
879
- }
880
- _term() {
881
- let node = this._factor();
882
- node = this._tryParsingFunctionCall(node);
883
- node = this._tryParsingMemberExpression(node);
884
- return node;
885
- }
886
- _expression() {
887
- let node = this._term();
888
- node = this._tryBinaryExpression(0, node);
889
- while ([TokenType.and, TokenType.or, TokenType.nullity].includes(this.currentToken.type)) {
890
- const token = this.currentToken;
891
- this._eat(token.type);
892
- node = new LogicalExpressionAST(node, token, this._expression());
893
- }
894
- if (this._expect(TokenType.equal)) {
895
- if (node instanceof IdentifierAST || node instanceof IndexAccessorAST) {
896
- const token = this.currentToken;
897
- this._eat(TokenType.equal);
898
- node = new AssignmentExpressionAST(node, this._expression());
899
- }
900
- else {
901
- throw new Error("Unexpected token");
902
- }
903
- }
904
- return node;
905
- }
906
- _objectProperty() {
907
- let key;
908
- switch (this.currentToken?.type) {
909
- case TokenType.stringConst:
910
- {
911
- key = new LiteralAST(this.currentToken, this.currentToken.value);
912
- this._eat(TokenType.stringConst);
913
- break;
914
- }
915
- case TokenType.lbracket:
916
- {
917
- this._eat(TokenType.lbracket);
918
- var expr = this._expression();
919
- this._eat(TokenType.rbracket);
920
- key = expr;
921
- break;
922
- }
923
- case TokenType.id:
924
- {
925
- const id = this._variable();
926
- key = new LiteralAST(new Token(TokenType.id, id.value, id.line, id.col), id.value);
927
- break;
928
- }
929
- default:
930
- throw `Unexpected token ${this.currentToken}`;
931
- }
932
- this._eat(TokenType.colon);
933
- var value = this._expression();
934
- return new ObjectProperty(key, value);
935
- }
936
- _property() {
937
- return this._objectProperty();
938
- }
939
- _objectProperties() {
940
- const properties = [];
941
- if (this.currentToken?.type != TokenType.rbrace) {
942
- this._eatEOL();
943
- properties.push(this._property());
944
- this._eatEOL();
945
- }
946
- while (this.currentToken?.type === TokenType.comma) {
947
- this._eat(TokenType.comma);
948
- this._eatEOL();
949
- if (this._expect(TokenType.rbrace)) {
950
- break;
951
- }
952
- properties.push(this._property());
953
- this._eatEOL();
954
- }
955
- return properties;
956
- }
957
- _objectExpression(braceToken) {
958
- var token = braceToken ?? this.currentToken;
959
- if (!braceToken)
960
- this._eat(TokenType.lbrace);
961
- var properties = this._objectProperties();
962
- this._eat(TokenType.rbrace);
963
- return new ObjectExpression(properties, token, this.currentToken);
964
- }
965
- _arrayExpression() {
966
- this._eat(TokenType.lbracket);
967
- const elements = this._expect(TokenType.rbracket) ? [] : this._expressionsList();
968
- this._eat(TokenType.rbracket);
969
- var startNode = elements.length !== 0 ? elements[0] : undefined;
970
- var lastNode = elements.length !== 0 ? elements[elements.length - 1] : undefined;
971
- return new ArrayExpression(elements, startNode, lastNode);
972
- }
973
- _tryParsingMemberExpression(n) {
974
- let node = n;
975
- while (this.currentToken.type === TokenType.lbracket) {
976
- this._eat(TokenType.lbracket);
977
- const key = this._expression();
978
- node = new IndexAccessorAST(node, key);
979
- this._eat(TokenType.rbracket);
980
- }
981
- return node;
982
- }
983
- _tryBinaryExpression(prec, left) {
984
- let node = left;
985
- while (true) {
986
- const currentPrec = Parser._binopPrecdences[this.currentToken.type] || -1;
987
- if (currentPrec < prec)
988
- return node;
989
- const operator = this.currentToken;
990
- this._eat(operator.type);
991
- let right = this._term();
992
- const nextPrec = Parser._binopPrecdences[this.currentToken.type] || -1;
993
- if (currentPrec < nextPrec) {
994
- const tmp = this._tryBinaryExpression(currentPrec + 1, right);
995
- if (tmp === node)
996
- return tmp;
997
- right = tmp;
998
- }
999
- node = new BinaryExpressionAST(node, operator, right);
1000
- }
1001
- }
1002
- _expressionsList() {
1003
- this._eatEOL();
1004
- // if (expect(Lexer.TokenType.RPAREN)) return emptyList()
1005
- let expr = this._expression();
1006
- this._eatEOL();
1007
- const result = [expr];
1008
- while (this.currentToken?.type === TokenType.comma) {
1009
- this._eat(TokenType.comma);
1010
- this._eatEOL();
1011
- if (this._expect(TokenType.rbracket)) {
1012
- break;
1013
- }
1014
- expr = this._expression();
1015
- result.push(expr);
1016
- this._eatEOL();
1017
- }
1018
- return result;
1019
- }
1020
- _callExpression(node) {
1021
- this._eat(TokenType.lparen);
1022
- let args = [];
1023
- if (!this._expect(TokenType.rparen)) {
1024
- args = this._expressionsList();
1025
- }
1026
- this._eat(TokenType.rparen);
1027
- if (!(node instanceof IdentifierAST)) {
1028
- error(this.currentToken);
1029
- }
1030
- return new CallExpressionAST(node, args);
1031
- }
1032
- _tryParsingFunctionCall(n) {
1033
- let node = n;
1034
- while (this.currentToken.type === TokenType.lparen) {
1035
- // probable function call
1036
- node = this._callExpression(node);
1037
- }
1038
- return node;
1039
- }
1040
- _statementExpression() {
1041
- const node = this._expression();
1042
- if (![TokenType.semi, TokenType.eol, TokenType.eof]
1043
- .includes(this.currentToken.type)) {
1044
- error(this.currentToken);
1045
- }
1046
- return node;
1047
- }
1048
- _blockStatement(ignoreFirstBrace = false) {
1049
- if (!ignoreFirstBrace)
1050
- this._eat(TokenType.lbrace);
1051
- this._eatEOL();
1052
- if (this._expect(TokenType.rbrace)) {
1053
- this._eat(TokenType.rbrace);
1054
- return new BlockStatementAST([], this.currentToken);
1055
- }
1056
- // const body = _statements();
1057
- const body = [this._statement()];
1058
- while (true) {
1059
- this._eatSemiOrEOL();
1060
- if (this.currentToken.type === TokenType.rbrace || this.currentToken.type === TokenType.eof) {
1061
- // _eat(this.currentToken!.type);
1062
- break;
1063
- }
1064
- body.push(this._statement());
1065
- if (this._expect(TokenType.rbrace))
1066
- break;
1067
- if (this.currentToken.type !== TokenType.eol &&
1068
- this.currentToken.type !== TokenType.semi &&
1069
- this.currentToken.type !== TokenType.eof) {
1070
- error(this.currentToken);
1071
- }
1072
- }
1073
- this._eat(TokenType.rbrace);
1074
- if (this.currentToken?.type === TokenType.rbrace) {
1075
- // endBlock
1076
- this._eat(TokenType.rbrace);
1077
- }
1078
- return new BlockStatementAST(body, this.currentToken);
1079
- }
1080
- _ifStatement() {
1081
- this._eat(TokenType.IF);
1082
- const withPar = this.currentToken.type === TokenType.lparen;
1083
- if (withPar) {
1084
- this._eat(TokenType.lparen);
1085
- }
1086
- const test = this._expression();
1087
- if (withPar) {
1088
- this._eat(TokenType.rparen);
1089
- }
1090
- let consequent;
1091
- if (this.currentToken.type === TokenType.lbrace) {
1092
- consequent = this._blockStatement();
1093
- }
1094
- else {
1095
- consequent = this._expression();
1096
- }
1097
- // this._eatSemiOrEOL();
1098
- let alternate;
1099
- if (this.currentToken.type === TokenType.ELSE) {
1100
- this._eat(TokenType.ELSE);
1101
- switch (this.currentToken.type) {
1102
- case TokenType.IF:
1103
- alternate = this._ifStatement();
1104
- break;
1105
- case TokenType.lbrace:
1106
- alternate = this._blockStatement();
1107
- break;
1108
- default:
1109
- alternate = this._expression();
1110
- }
1111
- }
1112
- return new IfStatementAST(test, consequent, alternate);
1113
- }
1114
- _whileLoop() {
1115
- const token = this.currentToken;
1116
- this._eat(TokenType.WHILE_TILL);
1117
- const test = this._expression();
1118
- const body = this.currentToken.type === TokenType.lbrace ? this._blockStatement() : this._expression();
1119
- return new WhileLoopStatement(test, body, token, this.currentToken);
1120
- }
1121
- _forOfIdentifier() {
1122
- switch (this.currentToken.type) {
1123
- case TokenType.lparen: {
1124
- // probable tuple
1125
- this._eat(TokenType.lparen);
1126
- const first = this._variable();
1127
- this._eat(TokenType.comma);
1128
- const second = this._variable();
1129
- this._eat(TokenType.rparen);
1130
- return new TupleExpression(first, second);
1131
- }
1132
- // TokenType.LBRACE -> objectPattern()
1133
- // TokenType.LBRACKET -> arrayPattern()
1134
- default:
1135
- return this._variable();
1136
- }
1137
- }
1138
- /**
1139
- * # ForLoopStatement with step
1140
- * (seginka|for) a=0 (foo|to) 10 (kay|kaj) niin 2 { # for a = 0; a<10; a+=2
1141
- *
1142
- * }
1143
- * (seginka|for) mali kono a
1144
- */
1145
- _forLoop() {
1146
- const startToken = this.currentToken;
1147
- this._eat(TokenType.FOR_LOOP);
1148
- let forOf = [TokenType.lparen].includes(this.currentToken.type);
1149
- let node;
1150
- if (forOf) {
1151
- node = this._forOfIdentifier();
1152
- }
1153
- else {
1154
- const expr = this._expression();
1155
- if (!(expr instanceof AssignmentExpressionAST)) {
1156
- forOf = true;
1157
- }
1158
- node = expr;
1159
- }
1160
- if (!forOf && node instanceof AssignmentExpressionAST) {
1161
- // normal for
1162
- this._eat(TokenType.TILL);
1163
- const test = this._expression();
1164
- let direction;
1165
- if (this.currentToken.type === TokenType.up || this.currentToken.type === TokenType.down) {
1166
- const t = this.currentToken;
1167
- this._eat(t.type);
1168
- direction = t;
1169
- }
1170
- else {
1171
- direction = new Token(TokenType.up, "up");
1172
- }
1173
- let update;
1174
- if (this._expect(TokenType.with)) {
1175
- this._eat(TokenType.with);
1176
- const expr = this._expression();
1177
- update = expr;
1178
- }
1179
- else {
1180
- update = new LiteralAST(new Token(TokenType.numberConst, 1, this.currentToken.line, this.currentToken.col), "1");
1181
- }
1182
- const body = (this.currentToken.type === TokenType.lbrace) ? this._blockStatement() : this._expression();
1183
- node = new ForLoopStatement(node, test, update, direction, body, startToken, this.currentToken);
1184
- }
1185
- else if (forOf) {
1186
- this._eat(TokenType.in);
1187
- const collection = this._expression();
1188
- const body = (this.currentToken.type === TokenType.lbrace) ? this._blockStatement() : this._expression();
1189
- node = new ForOfStatement(node, collection, body, startToken, this.currentToken);
1190
- }
1191
- else {
1192
- error(this.currentToken);
1193
- }
1194
- return node;
1195
- }
1196
- _statement() {
1197
- switch (this.currentToken.type) {
1198
- case TokenType.BREAK:
1199
- this._eat(TokenType.BREAK);
1200
- return new BreakAST(this.currentToken?.line, this.currentToken?.col);
1201
- case TokenType.CONTINUE:
1202
- this._eat(TokenType.CONTINUE);
1203
- return new ContinueAST(this.currentToken?.line, this.currentToken?.col);
1204
- case TokenType.RETURN:
1205
- this._eat(TokenType.RETURN);
1206
- return this._return();
1207
- case TokenType.semi:
1208
- this._eatSemi();
1209
- return this._statement();
1210
- case TokenType.eol:
1211
- this._eatEOL();
1212
- return this._statement();
1213
- // case TokenType.IF:
1214
- // return this._ifStatement();
1215
- case TokenType.WHILE_TILL:
1216
- return this._whileLoop();
1217
- case TokenType.FOR_LOOP:
1218
- return this._forLoop();
1219
- default:
1220
- return this._statementExpression();
1221
- }
1222
- }
1223
- _expect(type) {
1224
- return this.currentToken?.type === type;
1225
- }
1226
- _definition() {
1227
- this._eatSemiOrEOL();
1228
- if (this._expect(TokenType.eof)) {
1229
- return [];
1230
- }
1231
- const results = [this._statement()];
1232
- while (true) {
1233
- this._eatSemiOrEOL();
1234
- if (this.currentToken.type === TokenType.eof) {
1235
- this._eat(TokenType.eof);
1236
- break;
1237
- }
1238
- if (this.currentToken.type === TokenType.lbrace) {
1239
- results.push(this._blockStatement());
1240
- }
1241
- else {
1242
- results.push(this._statement());
1243
- }
1244
- // if (this.currentToken!.type !== TokenType.eol &&
1245
- // this.currentToken!.type !== TokenType.semi &&
1246
- // this.currentToken!.type !== TokenType.eof) {
1247
- // error(this.currentToken!);
1248
- // }
1249
- }
1250
- return results;
1251
- }
1252
- _root() {
1253
- const source = this.lexer.source;
1254
- const name = "<module>";
1255
- const list = this._definition();
1256
- return new RootAST(list, name, source);
1257
- }
1258
- parse() {
1259
- return this._root();
1260
- }
1261
- }
1262
- Parser._binopPrecdences = {
1263
- [TokenType.eqeq]: 10,
1264
- [TokenType.notEq]: 10,
1265
- [TokenType.great]: 10,
1266
- [TokenType.greatEq]: 10,
1267
- [TokenType.less]: 10,
1268
- [TokenType.lessEq]: 10,
1269
- [TokenType.plus]: 20,
1270
- [TokenType.minus]: 20,
1271
- [TokenType.mult]: 40,
1272
- [TokenType.div]: 40,
1273
- [TokenType.mod]: 40,
1274
- };
1275
- class LoopControl {
1276
- }
1277
- class BreakBranch extends LoopControl {
1278
- }
1279
- class ContinueBranch extends LoopControl {
1280
- }
1281
- class ReturnBranch {
1282
- constructor(value) {
1283
- this.value = value;
1284
- }
1285
- }
1286
- ;
1287
- // AST Wallker
1288
- class ANodeVisitor {
1289
- constructor() {
1290
- this._nodesVisitors = {};
1291
- }
1292
- registerVisitor(type, visitor) {
1293
- const methodName = `visit${type.name}`;
1294
- this._nodesVisitors[methodName] = visitor;
1295
- }
1296
- }
1297
- class NodeVisitor extends ANodeVisitor {
1298
- constructor() {
1299
- super();
1300
- this.registerVisitor(RootAST, this.visitRootAST);
1301
- this.registerVisitor(BlockStatementAST, this.visitBlockStatementAST);
1302
- this.registerVisitor(IdentifierAST, this.visitIdentifierAST);
1303
- this.registerVisitor(LiteralAST, this.visitLiteralAST);
1304
- this.registerVisitor(AssignmentExpressionAST, this.visitAssignmentExpressionAST);
1305
- this.registerVisitor(ExpressionStatementAST, this.visitExpressionStatementAST);
1306
- this.registerVisitor(CallExpressionAST, this.visitCallExpressionAST);
1307
- this.registerVisitor(BinaryExpressionAST, this.visitBinaryExpressionAST);
1308
- this.registerVisitor(UnaryExpressionAST, this.visitUnaryExpressionAST);
1309
- this.registerVisitor(IfStatementAST, this.visitIfStatementAST);
1310
- this.registerVisitor(LogicalExpressionAST, this.visitLogicalExpressionAST);
1311
- this.registerVisitor(IndexAccessorAST, this.visitIndexAccessorAST);
1312
- this.registerVisitor(ObjectProperty, this.visitObjectProperty);
1313
- this.registerVisitor(ObjectExpression, this.visitObjectExpression);
1314
- this.registerVisitor(ArrayExpression, this.visitArrayExpression);
1315
- this.registerVisitor(WhileLoopStatement, this.visitWhileLoopStatement);
1316
- this.registerVisitor(ForLoopStatement, this.visitForLoopStatement);
1317
- this.registerVisitor(ForOfStatement, this.visitForOfStatement);
1318
- this.registerVisitor(BreakAST, this.visitBreakAST);
1319
- this.registerVisitor(ContinueAST, this.visitContinueAST);
1320
- this.registerVisitor(ReturnAST, this.visitReturnAST);
1321
- }
1322
- visit(node) {
1323
- try {
1324
- const methodName = `visit${node.constructor.name}`;
1325
- // const fn = Object.getPrototypeOf(this)[methodName];
1326
- const fn = this._nodesVisitors[methodName];
1327
- return fn?.call(this, node) ?? null;
1328
- }
1329
- catch (error) {
1330
- return null;
1331
- }
1332
- }
1333
- assignProperty(owner, property, value) {
1334
- if (Array.isArray(owner)) {
1335
- owner[property] = value;
1336
- }
1337
- else if (typeof owner === 'object') {
1338
- owner[property] = value;
1339
- }
1340
- else { }
1341
- }
1342
- }
1343
- class MEventScope {
1344
- constructor(name, memory, parent) {
1345
- this.memory = {};
1346
- this.name = name;
1347
- this.memory = memory;
1348
- this.parent = parent;
1349
- }
1350
- resolve(key) {
1351
- return Object.keys(this.memory).includes(key) ? this.memory[key] : (this.parent?.resolve(key) ?? null);
1352
- }
1353
- change(key, value, declare = true) {
1354
- if (Object.keys(this.memory).includes(key)) {
1355
- // change here
1356
- this.memory[key] = value;
1357
- return true;
1358
- }
1359
- if (this.parent && this.parent.change(key, value, false)) {
1360
- return true;
1361
- }
1362
- if (!declare) {
1363
- return false;
1364
- }
1365
- this.memory[key] = value;
1366
- return true;
1367
- }
1368
- }
1369
- class MEvento extends NodeVisitor {
1370
- constructor() {
1371
- super();
1372
- this.rootScope = new MEventScope("Program", {});
1373
- this.currentScope = this.rootScope;
1374
- this.debug = false;
1375
- this._functionsRegistry = {};
1376
- this._functionsRegistry = { ...MEvento._globalFunctionsRegistry };
1377
- }
1378
- resolve(name) {
1379
- return this.currentScope?.resolve(name) ?? null;
1380
- }
1381
- changeVariable(name, value) {
1382
- if (this.currentScope?.change(name, value)) {
1383
- return value;
1384
- }
1385
- else
1386
- return null;
1387
- }
1388
- pushScope(name) {
1389
- const scope = new MEventScope(name, {}, this.currentScope);
1390
- this.currentScope = scope;
1391
- }
1392
- popScope() {
1393
- this.currentScope = this.currentScope?.parent;
1394
- }
1395
- log(message) {
1396
- if (this.debug) {
1397
- console.log(message);
1398
- }
1399
- }
1400
- visitRootAST(node) {
1401
- const list = node.body;
1402
- let last;
1403
- for (const n of list) {
1404
- last = this.visit(n);
1405
- if (last instanceof ReturnBranch) {
1406
- return last.value ?? null;
1407
- }
1408
- }
1409
- return last ?? null;
1410
- }
1411
- visitBlockStatementAST(node) {
1412
- const list = node.body;
1413
- let last;
1414
- this.pushScope("Block");
1415
- for (const n of list) {
1416
- last = this.visit(n);
1417
- if (last instanceof LoopControl) {
1418
- break;
1419
- }
1420
- if (last instanceof ReturnBranch) {
1421
- break;
1422
- }
1423
- }
1424
- this.popScope();
1425
- return last ?? null;
1426
- }
1427
- visitIdentifierAST(node) {
1428
- const id = node.value;
1429
- return this?.resolve(id) ?? null;
1430
- }
1431
- visitLiteralAST(node) {
1432
- const value = node.value;
1433
- return value;
1434
- }
1435
- visitAssignmentExpressionAST(node) {
1436
- const id = node.identifier;
1437
- const init = node.init;
1438
- let value = null;
1439
- if (id instanceof IndexAccessorAST) {
1440
- var target = this.visit(id.owner);
1441
- value = this.visit(node.init);
1442
- var property = this.visit(id.key);
1443
- this.assignProperty(target, property, value);
1444
- }
1445
- else if (id instanceof IdentifierAST) {
1446
- value = this.visit(init);
1447
- this.changeVariable(id.value, value);
1448
- }
1449
- return value;
1450
- }
1451
- visitExpressionStatementAST(node) {
1452
- const expr = node.expression;
1453
- return this.visit(expr);
1454
- }
1455
- visitCallExpressionAST(node) {
1456
- const callee = node.callee;
1457
- const args = node.arguments;
1458
- const calleeName = callee.value;
1459
- const fn = this.resolveFunction(calleeName);
1460
- const argValues = args.map((e) => this.visit(e));
1461
- // print("argValues::", argValues)
1462
- return fn?.(argValues, this);
1463
- }
1464
- visitBinaryExpressionAST(node) {
1465
- const left = node.left;
1466
- const right = node.right;
1467
- const op = node.operation;
1468
- const lValue = this.visit(left);
1469
- const rValue = this.visit(right);
1470
- switch (op.type) {
1471
- case TokenType.plus:
1472
- if (isNum(lValue) && isNum(rValue)) {
1473
- return lValue + rValue;
1474
- }
1475
- return `${lValue}${rValue}`;
1476
- case TokenType.minus:
1477
- if (isNum(lValue) && isNum(rValue)) {
1478
- return lValue - rValue;
1479
- }
1480
- throw new Error(`Operation ${op.value} not allowed no num value`);
1481
- case TokenType.mult:
1482
- if (isNum(lValue) && isNum(rValue)) {
1483
- return lValue * rValue;
1484
- }
1485
- if (isString(lValue) && isNum(rValue)) {
1486
- return lValue.repeat(rValue);
1487
- }
1488
- if (isNum(lValue) && isString(rValue)) {
1489
- return rValue.repeat(lValue);
1490
- }
1491
- throw new Error(`Operation ${op.value} not allowed no num value`);
1492
- case TokenType.div:
1493
- if (isNum(lValue) && isNum(rValue)) {
1494
- if (rValue === 0) {
1495
- throw new Error("Invalid division by 0");
1496
- }
1497
- return lValue / rValue;
1498
- }
1499
- throw new Error(`Operation ${op.value} not allowed no num value`);
1500
- case TokenType.mod:
1501
- if (isNum(lValue) && isNum(rValue)) {
1502
- return lValue % rValue;
1503
- }
1504
- throw new Error(`Operation ${op.value} not allowed no num value`);
1505
- case TokenType.great:
1506
- if (isNum(lValue) && isNum(rValue)) {
1507
- return lValue > rValue;
1508
- }
1509
- throw new Error(`Operation ${op.value} not allowed no num value`);
1510
- case TokenType.greatEq:
1511
- if (isNum(lValue) && isNum(rValue)) {
1512
- return lValue >= rValue;
1513
- }
1514
- throw new Error(`Operation ${op.value} not allowed no num value`);
1515
- case TokenType.less:
1516
- if (isNum(lValue) && isNum(rValue)) {
1517
- return lValue < rValue;
1518
- }
1519
- throw new Error(`Operation ${op.value} not allowed no num value`);
1520
- case TokenType.lessEq:
1521
- if (isNum(lValue) && isNum(rValue)) {
1522
- return lValue <= rValue;
1523
- }
1524
- throw new Error(`Operation ${op.value} not allowed no num value`);
1525
- case TokenType.eqeq:
1526
- return lValue === rValue;
1527
- case TokenType.notEq:
1528
- return lValue !== rValue;
1529
- default:
1530
- throw new Error(`Operation ${op.value} not allowed no num value`);
1531
- }
1532
- }
1533
- visitUnaryExpressionAST(node) {
1534
- const arg = node.argument;
1535
- const op = node.operation;
1536
- const argValue = this.visit(arg);
1537
- if (op.type === TokenType.not) {
1538
- return _boolValue(argValue) === false;
1539
- }
1540
- if (!isNum(argValue)) {
1541
- throw new Error(`Operation ${op.value} not allowed no num value`);
1542
- }
1543
- if (op.type === TokenType.plus) {
1544
- return argValue;
1545
- }
1546
- else if (op.type === TokenType.minus) {
1547
- return -argValue;
1548
- }
1549
- throw new Error(`Operation ${op.value} not allowed no num value`);
1550
- }
1551
- visitIfStatementAST(node) {
1552
- const testNode = node.test;
1553
- const test = this.visit(testNode);
1554
- const testBoolValue = _boolValue(test);
1555
- if (testBoolValue) {
1556
- return this.visit(node.consequent);
1557
- }
1558
- if (node.alternate) {
1559
- return this.visit(node.alternate);
1560
- }
1561
- return null;
1562
- }
1563
- visitLogicalExpressionAST(node) {
1564
- const leftNode = node.left;
1565
- let test = this.visit(leftNode);
1566
- if (node.operator.type === TokenType.nullity) {
1567
- if (test != null) {
1568
- return test;
1569
- }
1570
- return this.visit(node.right);
1571
- }
1572
- const testBoolValue = _boolValue(test);
1573
- if (node.operator.type === TokenType.and) {
1574
- if (!testBoolValue) {
1575
- return false;
1576
- }
1577
- return _boolValue(this.visit(node.right));
1578
- }
1579
- if (node.operator.type === TokenType.or) {
1580
- if (testBoolValue) {
1581
- return true;
1582
- }
1583
- return _boolValue(this.visit(node.right));
1584
- }
1585
- return null;
1586
- }
1587
- visitIndexAccessorAST(node) {
1588
- const ownerNode = node.owner;
1589
- const owner = this.visit(ownerNode);
1590
- if (owner === null || owner === undefined) {
1591
- return null;
1592
- }
1593
- if (typeof owner === 'object') {
1594
- const key = this.visit(node.key);
1595
- return owner[key] ?? null;
1596
- }
1597
- else if (Array.isArray(owner)) {
1598
- const key = this.visit(node.key);
1599
- return isNum(key) && owner.length < key && key >= 0 ? owner[key] ?? null : null;
1600
- }
1601
- return null;
1602
- }
1603
- visitObjectProperty(node) { }
1604
- visitObjectExpression(ast) {
1605
- const instance = {};
1606
- const node = ast;
1607
- for (const it of node.properties) {
1608
- if (it instanceof ObjectProperty) {
1609
- let key = this.visit(it.key);
1610
- key = isString(key) ? key : key.toString();
1611
- instance[key] = this.visit(it.value);
1612
- }
1613
- }
1614
- return instance;
1615
- }
1616
- visitArrayExpression(ast) {
1617
- const node = ast;
1618
- const args = this.resolveArguments(node.elements);
1619
- return args;
1620
- }
1621
- visitBreakAST(node) {
1622
- return new BreakBranch();
1623
- }
1624
- visitWhileLoopStatement(node) {
1625
- this.log(`WhileLoopStatement ${node.test} ${node.body}`);
1626
- const retainer = node.retain ? [] : undefined;
1627
- while (_boolValue(this.visit(node.test))) {
1628
- const ret = this.visit(node.body);
1629
- if (ret instanceof BreakBranch) {
1630
- break;
1631
- }
1632
- if (ret instanceof ContinueBranch) {
1633
- continue;
1634
- }
1635
- if (ret instanceof ReturnBranch) {
1636
- this.popScope();
1637
- return ret;
1638
- }
1639
- retainer?.push(ret);
1640
- }
1641
- return retainer ?? null;
1642
- }
1643
- visitForLoopStatement(node) {
1644
- const retainer = node.retain ? [] : undefined;
1645
- const loopIdentifier = node.init.identifier;
1646
- if (!(loopIdentifier instanceof IdentifierAST))
1647
- throw new Error("Unexpected identifer found");
1648
- this.pushScope("ForLoopStatement");
1649
- const initialValue = this.visit(node.init.init);
1650
- this.changeVariable(loopIdentifier.value, initialValue);
1651
- retainer?.push(initialValue);
1652
- const test = () => {
1653
- const ret = this.visit(node.test);
1654
- if (isNum(ret)) {
1655
- const tmp = this.resolve(loopIdentifier.value);
1656
- return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1657
- }
1658
- return _boolValue(ret);
1659
- };
1660
- const update = () => {
1661
- const updateValue = this.visit(node.update);
1662
- if (isNum(updateValue)) {
1663
- // auto handle
1664
- const tmp = this.resolve(loopIdentifier.value);
1665
- if (!isNum(tmp))
1666
- throw Error("Cant update value");
1667
- this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1668
- }
1669
- else {
1670
- throw Error("Update value cant be non number");
1671
- }
1672
- };
1673
- while (test()) {
1674
- const ret = this.visit(node.body);
1675
- if (ret instanceof BreakBranch) {
1676
- break;
1677
- }
1678
- if (ret instanceof ContinueBranch) {
1679
- update();
1680
- continue;
1681
- }
1682
- if (ret instanceof ReturnBranch) {
1683
- this.popScope();
1684
- return ret;
1685
- }
1686
- retainer?.push(ret);
1687
- // update
1688
- update();
1689
- }
1690
- this.popScope();
1691
- return retainer ?? null;
1692
- }
1693
- visitForOfStatement(node) {
1694
- const collection = this.visit(node.collection);
1695
- if (!Array.isArray(collection)) {
1696
- throw Error("Can iterate non array object");
1697
- }
1698
- const retainer = node.retain ? [] : undefined;
1699
- this.pushScope("ForOfStatement");
1700
- for (const it of collection) {
1701
- this._declareForIdentifier(node.identifier, it);
1702
- const ret = this.visit(node.body);
1703
- if (ret instanceof BreakBranch) {
1704
- break;
1705
- }
1706
- if (ret instanceof ContinueBranch) {
1707
- continue;
1708
- }
1709
- if (ret instanceof ReturnBranch) {
1710
- this.popScope();
1711
- return ret;
1712
- }
1713
- retainer?.push(ret);
1714
- }
1715
- this.popScope();
1716
- return retainer ?? null;
1717
- }
1718
- visitContinueAST(node) {
1719
- return new ContinueBranch();
1720
- }
1721
- visitReturnAST(node) {
1722
- return new ReturnBranch(node.value != null ? this.visit(node.value) : null);
1723
- }
1724
- _declareForIdentifier(node, value) {
1725
- if (node instanceof TupleExpression) {
1726
- if (!Array.isArray(value)) {
1727
- throw Error("Unable to make a tuple from non Array element");
1728
- }
1729
- this.changeVariable(node.first.value, value[0]);
1730
- this.changeVariable(node.second.value, value[1]);
1731
- }
1732
- this.changeVariable(node.value, value);
1733
- }
1734
- resolveArguments(args) {
1735
- return args.map((it) => {
1736
- return this.visit(it);
1737
- });
1738
- }
1739
- setFunctionResolver(resolver) {
1740
- this._functionResolver = resolver;
1741
- }
1742
- resolveFunction(name) {
1743
- return this._functionsRegistry[name] ?? this._functionResolver?.(name);
1744
- }
1745
- registerFunction(id, fn) {
1746
- this._functionsRegistry[id] = fn;
1747
- }
1748
- unregisterFunction(id) {
1749
- delete this._functionsRegistry[id];
1750
- }
1751
- execute(source, cache = true, input) {
1752
- const module = MEvento.compile(source, cache);
1753
- if (input) {
1754
- Object.keys(input).forEach(k => this.changeVariable(k, input[k]));
1755
- }
1756
- return this.visit(module);
1757
- }
1758
- static compile(source, cache = false) {
1759
- const hash = hashCode(source);
1760
- if (cache && this._cache.has(hash)) {
1761
- return this._cache.get(hash);
1762
- }
1763
- const lexer = new Lexer(source);
1764
- const parser = new Parser(lexer);
1765
- const module = parser.parse();
1766
- if (cache)
1767
- this._cache.set(hash, module);
1768
- return module;
1769
- }
1770
- static register(id, fn) {
1771
- MEvento._globalFunctionsRegistry[id] = fn;
1772
- }
1773
- static unregister(id) {
1774
- delete MEvento._globalFunctionsRegistry[id];
1775
- }
1776
- static run(source, cache = false, input) {
1777
- const mevento = new MEvento();
1778
- const module = MEvento.compile(source, cache);
1779
- if (input) {
1780
- Object.keys(input).forEach(k => mevento.changeVariable(k, input[k]));
1781
- }
1782
- return mevento.visit(module);
1783
- }
1784
- static newInstance() {
1785
- // const lexer = Lexer(source);
1786
- // const parser = Parser(lexer);
1787
- // const module = parser.parse();
1788
- return new MEvento();
1789
- }
1790
- clone() {
1791
- var ret = new MEvento();
1792
- ret._functionsRegistry = { ...this._functionsRegistry };
1793
- ret.rootScope.memory = { ...this.rootScope.memory };
1794
- return ret;
1795
- }
1796
- newAsyncInstance() {
1797
- const asyncIns = MEventoAsync.newInstance();
1798
- asyncIns.rootScope.memory = this.rootScope.memory;
1799
- /// ????
1800
- asyncIns._functionsRegistry = this._functionsRegistry;
1801
- return asyncIns;
1802
- }
1803
- ;
1804
- }
1805
- MEvento._globalFunctionsRegistry = {};
1806
- MEvento._cache = new Map();
1807
- class MEventoAsync extends MEvento {
1808
- constructor() {
1809
- super();
1810
- }
1811
- async visitRootAST(node) {
1812
- const list = node.body;
1813
- let last;
1814
- for (const n of list) {
1815
- last = await this.visit(n);
1816
- if (last instanceof ReturnBranch) {
1817
- return last.value ?? null;
1818
- }
1819
- }
1820
- return last ?? null;
1821
- }
1822
- async visitBlockStatementAST(node) {
1823
- const list = node.body;
1824
- let last;
1825
- this.pushScope("Block");
1826
- for (const n of list) {
1827
- last = await this.visit(n);
1828
- if (last instanceof LoopControl) {
1829
- break;
1830
- }
1831
- if (last instanceof ReturnBranch) {
1832
- break;
1833
- }
1834
- }
1835
- this.popScope();
1836
- return last;
1837
- }
1838
- async visitIdentifierAST(node) {
1839
- const id = node.value;
1840
- return this.resolve(id);
1841
- }
1842
- async visitLiteralAST(node) {
1843
- const value = node.value;
1844
- return value;
1845
- }
1846
- async visitAssignmentExpressionAST(node) {
1847
- const id = node.identifier;
1848
- const init = node.init;
1849
- let value = null;
1850
- if (id instanceof IndexAccessorAST) {
1851
- var target = await this.visit(id.owner);
1852
- value = await this.visit(node.init);
1853
- var property = await this.visit(id.key);
1854
- this.assignProperty(target, property, value);
1855
- }
1856
- else if (id instanceof IdentifierAST) {
1857
- value = await this.visit(init);
1858
- this.changeVariable(id.value, value);
1859
- }
1860
- return value;
1861
- }
1862
- async visitExpressionStatementAST(node) {
1863
- const expr = node.expression;
1864
- return await this.visit(expr);
1865
- }
1866
- async visitCallExpressionAST(node) {
1867
- const callee = node.callee;
1868
- const args = node.arguments;
1869
- const calleeName = callee.value;
1870
- const fn = this._functionsRegistry[calleeName];
1871
- const argValues = await Promise.all(args.map((e) => this.visit(e)));
1872
- // print("argValues::", argValues)
1873
- return await fn?.(argValues, this);
1874
- }
1875
- async visitBinaryExpressionAST(node) {
1876
- const left = node.left;
1877
- const right = node.right;
1878
- const op = node.operation;
1879
- const lValue = await this.visit(left);
1880
- const rValue = await this.visit(right);
1881
- switch (op.type) {
1882
- case TokenType.plus:
1883
- if (isNum(lValue) && isNum(rValue)) {
1884
- return lValue + rValue;
1885
- }
1886
- return `${lValue}${rValue}`;
1887
- case TokenType.minus:
1888
- if (isNum(lValue) && isNum(rValue)) {
1889
- return lValue - rValue;
1890
- }
1891
- if (isString(lValue) && isNum(rValue)) {
1892
- return lValue.repeat(rValue);
1893
- }
1894
- if (isNum(lValue) && isString(rValue)) {
1895
- return rValue.repeat(lValue);
1896
- }
1897
- throw new Error(`Operation ${op.value} not allowed no num value`);
1898
- case TokenType.mult:
1899
- if (isNum(lValue) && isNum(rValue)) {
1900
- return lValue * rValue;
1901
- }
1902
- throw new Error(`Operation ${op.value} not allowed no num value`);
1903
- case TokenType.div:
1904
- if (isNum(lValue) && isNum(rValue)) {
1905
- if (rValue === 0) {
1906
- throw new Error("Invalid division by 0");
1907
- }
1908
- return lValue / rValue;
1909
- }
1910
- throw new Error(`Operation ${op.value} not allowed no num value`);
1911
- case TokenType.mod:
1912
- if (isNum(lValue) && isNum(rValue)) {
1913
- return lValue % rValue;
1914
- }
1915
- throw new Error(`Operation ${op.value} not allowed no num value`);
1916
- case TokenType.great:
1917
- if (isNum(lValue) && isNum(rValue)) {
1918
- return lValue > rValue;
1919
- }
1920
- throw new Error(`Operation ${op.value} not allowed no num value`);
1921
- case TokenType.greatEq:
1922
- if (isNum(lValue) && isNum(rValue)) {
1923
- return lValue >= rValue;
1924
- }
1925
- throw new Error(`Operation ${op.value} not allowed no num value`);
1926
- case TokenType.less:
1927
- if (isNum(lValue) && isNum(rValue)) {
1928
- return lValue < rValue;
1929
- }
1930
- throw new Error(`Operation ${op.value} not allowed no num value`);
1931
- case TokenType.lessEq:
1932
- if (isNum(lValue) && isNum(rValue)) {
1933
- return lValue <= rValue;
1934
- }
1935
- throw new Error(`Operation ${op.value} not allowed no num value`);
1936
- case TokenType.eqeq:
1937
- return lValue === rValue;
1938
- case TokenType.notEq:
1939
- return lValue !== rValue;
1940
- default:
1941
- throw new Error(`Operation ${op.value} not allowed no num value`);
1942
- }
1943
- }
1944
- async visitUnaryExpressionAST(node) {
1945
- const arg = node.argument;
1946
- const op = node.operation;
1947
- const argValue = await this.visit(arg);
1948
- if (op.type === TokenType.not) {
1949
- return _boolValue(argValue) === false;
1950
- }
1951
- if (!isNum(argValue)) {
1952
- throw new Error(`Operation ${op.value} not allowed no num value`);
1953
- }
1954
- if (op.type === TokenType.plus) {
1955
- return argValue;
1956
- }
1957
- else if (op.type === TokenType.minus) {
1958
- return -argValue;
1959
- }
1960
- throw new Error(`Operation ${op.value} not allowed no num value`);
1961
- }
1962
- async visitIfStatementAST(node) {
1963
- const testNode = node.test;
1964
- const test = await this.visit(testNode);
1965
- const testBoolValue = _boolValue(test);
1966
- if (testBoolValue) {
1967
- return await this.visit(node.consequent);
1968
- }
1969
- if (node.alternate) {
1970
- return await this.visit(node.alternate);
1971
- }
1972
- return null;
1973
- }
1974
- async visitLogicalExpressionAST(node) {
1975
- const leftNode = node.left;
1976
- let test = await this.visit(leftNode);
1977
- if (node.operator.type === TokenType.nullity) {
1978
- if (test != null) {
1979
- return test;
1980
- }
1981
- return await this.visit(node.right);
1982
- }
1983
- const testBoolValue = _boolValue(test);
1984
- if (node.operator.type === TokenType.and) {
1985
- if (!testBoolValue) {
1986
- return false;
1987
- }
1988
- return _boolValue(await this.visit(node.right));
1989
- }
1990
- if (node.operator.type === TokenType.or) {
1991
- if (testBoolValue) {
1992
- return true;
1993
- }
1994
- return _boolValue(await this.visit(node.right));
1995
- }
1996
- return false;
1997
- }
1998
- async visitIndexAccessorAST(node) {
1999
- const ownerNode = node.owner;
2000
- const owner = await this.visit(ownerNode);
2001
- if (owner === null || owner === undefined) {
2002
- return null;
2003
- }
2004
- if (typeof owner === 'object') {
2005
- const key = await this.visit(node.key);
2006
- return owner[key] ?? null;
2007
- }
2008
- else if (Array.isArray(owner)) {
2009
- const key = await this.visit(node.key);
2010
- return isNum(key) && owner.length < key && key >= 0 ? owner[key] ?? null : null;
2011
- }
2012
- return null;
2013
- }
2014
- async visitObjectProperty(node) { }
2015
- async visitObjectExpression(ast) {
2016
- const instance = {};
2017
- const node = ast;
2018
- for (const it of node.properties) {
2019
- if (it instanceof ObjectProperty) {
2020
- let key = await this.visit(it.key);
2021
- key = isString(key) ? key : key.toString();
2022
- instance[key] = await this.visit(it.value);
2023
- }
2024
- }
2025
- return instance;
2026
- }
2027
- async visitArrayExpression(ast) {
2028
- const node = ast;
2029
- const args = await this.resolveArgumentsAsync(node.elements);
2030
- return args;
2031
- }
2032
- async visitWhileLoopStatement(node) {
2033
- this.log(`WhileLoopStatement ${node.test} ${node.body}`);
2034
- const retainer = node.retain ? [] : undefined;
2035
- while (_boolValue(await this.visit(node.test))) {
2036
- const ret = await this.visit(node.body);
2037
- if (ret instanceof BreakBranch) {
2038
- break;
2039
- }
2040
- if (ret instanceof ContinueBranch) {
2041
- continue;
2042
- }
2043
- if (ret instanceof ReturnBranch) {
2044
- this.popScope();
2045
- return ret;
2046
- }
2047
- retainer?.push(ret);
2048
- }
2049
- return retainer;
2050
- }
2051
- async visitForLoopStatement(node) {
2052
- const retainer = node.retain ? [] : undefined;
2053
- const loopIdentifier = node.init.identifier;
2054
- if (!(loopIdentifier instanceof IdentifierAST))
2055
- throw new Error("Unexpected identifer found");
2056
- this.pushScope("ForLoopStatement");
2057
- const initialValue = await this.visit(node.init.init);
2058
- this.changeVariable(loopIdentifier.value, initialValue);
2059
- retainer?.push(initialValue);
2060
- const test = async () => {
2061
- const ret = await this.visit(node.test);
2062
- if (isNum(ret)) {
2063
- const tmp = this.resolve(loopIdentifier.value);
2064
- return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
2065
- }
2066
- return _boolValue(ret);
2067
- };
2068
- const update = async () => {
2069
- const updateValue = await this.visit(node.update);
2070
- if (isNum(updateValue)) {
2071
- // auto handle
2072
- const tmp = this.resolve(loopIdentifier.value);
2073
- if (!isNum(tmp))
2074
- throw Error("Cant update value");
2075
- this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
2076
- }
2077
- else {
2078
- throw Error("Update value cant be non number");
2079
- }
2080
- };
2081
- while (await test()) {
2082
- const ret = await this.visit(node.body);
2083
- if (ret instanceof BreakBranch) {
2084
- break;
2085
- }
2086
- if (ret instanceof ContinueBranch) {
2087
- await update();
2088
- continue;
2089
- }
2090
- if (ret instanceof ReturnBranch) {
2091
- this.popScope();
2092
- return ret;
2093
- }
2094
- retainer?.push(ret);
2095
- // update
2096
- await update();
2097
- }
2098
- this.popScope();
2099
- return retainer ?? null;
2100
- }
2101
- async visitForOfStatement(node) {
2102
- const collection = await this.visit(node.collection);
2103
- if (!Array.isArray(collection)) {
2104
- throw Error("Can iterate non array object");
2105
- }
2106
- const retainer = node.retain ? [] : undefined;
2107
- this.pushScope("ForOfStatement");
2108
- for (const it of collection) {
2109
- this._declareForIdentifier(node.identifier, it);
2110
- const ret = await this.visit(node.body);
2111
- if (ret instanceof BreakBranch) {
2112
- break;
2113
- }
2114
- if (ret instanceof ContinueBranch) {
2115
- continue;
2116
- }
2117
- if (ret instanceof ReturnBranch) {
2118
- this.popScope();
2119
- return ret;
2120
- }
2121
- retainer?.push(ret);
2122
- }
2123
- this.popScope();
2124
- return retainer;
2125
- }
2126
- async visitReturnAST(node) {
2127
- return new ReturnBranch(node.value != null ? await this.visit(node.value) : null);
2128
- }
2129
- async resolveArgumentsAsync(args) {
2130
- return await Promise.all(args.map(async (it) => {
2131
- return await this.visit(it);
2132
- }));
2133
- }
2134
- registerFunction(id, fn) {
2135
- this._functionsRegistry[id] = fn;
2136
- }
2137
- unregisterFunction(id) {
2138
- delete this._functionsRegistry[id];
2139
- }
2140
- async execute(source, cache = true, input) {
2141
- const module = MEvento.compile(source, cache);
2142
- if (input) {
2143
- Object.keys(input).forEach(k => this.changeVariable(k, input[k]));
2144
- }
2145
- return await this.visit(module);
2146
- }
2147
- static async run(source, cache = false, input) {
2148
- const mevento = new MEventoAsync();
2149
- const module = MEvento.compile(source, cache);
2150
- if (input) {
2151
- Object.keys(input).forEach(k => mevento.changeVariable(k, input[k]));
2152
- }
2153
- return await mevento.visit(module);
2154
- }
2155
- static newInstance() {
2156
- return new MEventoAsync();
2157
- }
2158
- clone() {
2159
- var ret = new MEventoAsync();
2160
- ret._functionsRegistry = { ...this._functionsRegistry };
2161
- ret.rootScope.memory = { ...this.rootScope.memory };
2162
- return ret;
2163
- }
2164
- }
2165
-
2166
- /*
2167
- * Public API Surface of mevento
2168
- */
2169
-
2170
- /**
2171
- * Generated bundle index. Do not edit.
2172
- */
2173
-
2174
- export { AST, ArrayExpression, AssignmentExpressionAST, BinaryExpressionAST, BlockStatementAST, BreakAST, BreakBranch, CallExpressionAST, ContinueAST, ContinueBranch, ExpressionStatementAST, ForLoopStatement, ForOfStatement, IdentifierAST, IfStatementAST, IndexAccessorAST, LexerDictionary, LiteralAST, LogicalExpressionAST, LoopControl, MEventScope, MEvento, MEventoAsync, NodeVisitor, ObjectExpression, ObjectProperty, ReturnAST, ReturnBranch, RootAST, Token, TokenType, TupleExpression, UnaryExpressionAST, WhileLoopStatement };
2175
- //# sourceMappingURL=mevento.mjs.map