mevento 1.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.
@@ -0,0 +1,1619 @@
1
+ import { __awaiter } from 'tslib';
2
+
3
+ function error(token) {
4
+ const text = `Invalid token ${token.type}[${token.value}] at ${token.line}, ${token.col}\n`;
5
+ throw (text);
6
+ }
7
+ function isNum(value) {
8
+ return typeof value === 'number';
9
+ }
10
+ function isBoolean(value) {
11
+ return typeof value === 'boolean';
12
+ }
13
+ function isString(value) {
14
+ return typeof value === 'string';
15
+ }
16
+ function _boolValue(test) {
17
+ return test !== null &&
18
+ ((isNum(test) && test !== 0) ||
19
+ (isString(test) && test.length > 0) ||
20
+ (isBoolean(test) && test));
21
+ }
22
+ ;
23
+ function hashCode(str) {
24
+ let hash = 0;
25
+ let i = 0;
26
+ let chr;
27
+ if (str.length === 0)
28
+ return hash;
29
+ for (i = 0; i < str.length; i++) {
30
+ chr = str.charCodeAt(i);
31
+ // tslint:disable-next-line:no-bitwise
32
+ hash = ((hash << 5) - hash) + chr;
33
+ // tslint:disable-next-line:no-bitwise
34
+ hash |= 0; // Convert to 32bit integer
35
+ }
36
+ return hash;
37
+ }
38
+ ;
39
+ class TokenType {
40
+ }
41
+ TokenType.id = 0;
42
+ TokenType.comma = 1;
43
+ TokenType.semi = 2;
44
+ TokenType.numberConst = 3;
45
+ TokenType.stringConst = 4;
46
+ TokenType.equal = 5;
47
+ TokenType.lparen = 6;
48
+ TokenType.rparen = 7;
49
+ TokenType.eol = 8;
50
+ TokenType.eof = 9;
51
+ TokenType.lbrace = 10;
52
+ TokenType.rbrace = 11;
53
+ TokenType.lbracket = 12;
54
+ TokenType.rbracket = 13;
55
+ TokenType.great = 14;
56
+ TokenType.greatEq = 15;
57
+ TokenType.less = 16;
58
+ TokenType.lessEq = 17;
59
+ TokenType.eqeq = 18;
60
+ TokenType.IF = 19;
61
+ TokenType.ELSE = 20;
62
+ TokenType.TRUE = 21;
63
+ TokenType.FALSE = 22;
64
+ TokenType.NULL = 23;
65
+ TokenType.not = 24;
66
+ TokenType.notEq = 25;
67
+ TokenType.and = 26;
68
+ TokenType.or = 27;
69
+ TokenType.plus = 28;
70
+ TokenType.minus = 29;
71
+ TokenType.div = 30;
72
+ TokenType.mult = 31;
73
+ TokenType.mod = 32;
74
+ TokenType.invalid = 33;
75
+ TokenType.colon = 34;
76
+ class Token {
77
+ constructor(type, value, line = 1, col = 1) {
78
+ this.type = type;
79
+ this.value = value;
80
+ this.line = line;
81
+ this.col = col;
82
+ }
83
+ static from(type, value) {
84
+ return new Token(type, value);
85
+ }
86
+ toString() {
87
+ return `[${this.type.toString()}, ${this.value}]`;
88
+ }
89
+ }
90
+ class Chars {
91
+ }
92
+ Chars.equal = '='.charCodeAt(0);
93
+ Chars.comma = ','.charCodeAt(0);
94
+ Chars.semiColon = ';'.charCodeAt(0);
95
+ Chars.lparen = '('.charCodeAt(0);
96
+ Chars.rparen = ')'.charCodeAt(0);
97
+ Chars.backslash = '\\'.charCodeAt(0);
98
+ Chars.quote = '"'.charCodeAt(0);
99
+ Chars.squote = "'".charCodeAt(0);
100
+ Chars.plus = "+".charCodeAt(0);
101
+ Chars.minus = "-".charCodeAt(0);
102
+ Chars.star = "*".charCodeAt(0);
103
+ Chars.slash = "/".charCodeAt(0);
104
+ Chars.percent = "%".charCodeAt(0);
105
+ Chars.lbrace = "{".charCodeAt(0);
106
+ Chars.rbrace = "}".charCodeAt(0);
107
+ Chars.lbracket = "[".charCodeAt(0);
108
+ Chars.rbracket = "]".charCodeAt(0);
109
+ Chars.not = "!".charCodeAt(0);
110
+ Chars.great = ">".charCodeAt(0);
111
+ Chars.less = "<".charCodeAt(0);
112
+ Chars.and = "&".charCodeAt(0);
113
+ Chars.pipe = "|".charCodeAt(0);
114
+ Chars.colon = ":".charCodeAt(0);
115
+ class LexerDictionary {
116
+ constructor(lang, keywords) {
117
+ this.keywords = {};
118
+ this.keywords = Object.assign({}, keywords);
119
+ this.lang = lang;
120
+ }
121
+ }
122
+ /**
123
+ * Lexing => Parsing ===> AST ===> Walking
124
+ */
125
+ /**
126
+ * Lexer
127
+ */
128
+ class Lexer {
129
+ constructor(source) {
130
+ this._position = 0;
131
+ this._line = 1;
132
+ this._col = 1;
133
+ this._currentChar = -1;
134
+ this._source = source;
135
+ this._currentChar = this._source[this._position].charCodeAt(0);
136
+ this._resolveLanguage();
137
+ }
138
+ get source() {
139
+ return this._source;
140
+ }
141
+ _resolveLanguage() {
142
+ let token = this.nextToken();
143
+ if (token.type === TokenType.less) {
144
+ // language setting
145
+ const idToken = this.nextToken();
146
+ if (idToken.type !== TokenType.id) {
147
+ error(token);
148
+ }
149
+ const lang = idToken.value.toString();
150
+ this._language = Lexer.languages.find((element) => element.lang === lang) || Lexer._defaultLanguage;
151
+ token = this.nextToken();
152
+ if (token.type !== TokenType.great) {
153
+ error(token);
154
+ }
155
+ }
156
+ else {
157
+ this._language = Lexer._defaultLanguage;
158
+ // reset reading
159
+ this._position = 0;
160
+ this._currentChar = this._source[this._position].charCodeAt(0);
161
+ }
162
+ }
163
+ _advance() {
164
+ this._position++;
165
+ if (this._position >= this._source.length) {
166
+ this._currentChar = -1;
167
+ return;
168
+ }
169
+ this._currentChar = this._source[this._position].charCodeAt(0);
170
+ this._col++;
171
+ }
172
+ _pick() {
173
+ if (this._position >= this._source.length) {
174
+ return -1;
175
+ }
176
+ return this._source[this._position].charCodeAt(0);
177
+ }
178
+ _isId(code) {
179
+ if (code < 48) {
180
+ return code === 36;
181
+ }
182
+ if (code < 58) {
183
+ return true;
184
+ }
185
+ if (code < 65) {
186
+ return false;
187
+ }
188
+ if (code < 91) {
189
+ return true;
190
+ }
191
+ if (code < 97) {
192
+ return code === 95;
193
+ }
194
+ if (code < 123) {
195
+ return true;
196
+ }
197
+ return false;
198
+ }
199
+ _isIdStart(code) {
200
+ if (code < 65) {
201
+ return code === 36;
202
+ }
203
+ if (code < 91) {
204
+ return true;
205
+ }
206
+ if (code < 97) {
207
+ return code === 95;
208
+ }
209
+ if (code < 123) {
210
+ return true;
211
+ }
212
+ return false;
213
+ }
214
+ _id() {
215
+ let ret = "";
216
+ const lastC = this._col;
217
+ const lastP = this._position;
218
+ const line = this._line;
219
+ while (this._isId(this._currentChar)) {
220
+ ret += String.fromCharCode(this._currentChar);
221
+ this._advance();
222
+ }
223
+ const translatedId = this._language
224
+ ? (this._language.keywords[ret]) || ret
225
+ : ret;
226
+ return Lexer.RESERVED[translatedId] || new Token(TokenType.id, ret, line, lastC);
227
+ }
228
+ _isLineEnd(c) {
229
+ return c === 10 ||
230
+ c === 13 ||
231
+ ['\n', '\r', '\u2028', '\u2029'].includes(String.fromCharCode(c));
232
+ }
233
+ _isWhiteSpace(c) {
234
+ return String.fromCharCode(c).trim() === "" &&
235
+ String.fromCharCode(c).length > 0;
236
+ }
237
+ _isDigit(c) {
238
+ // tslint:disable-next-line:no-bitwise
239
+ return c > 0 && (c ^ 0x30) <= 9;
240
+ }
241
+ _skipWhiteSpace() {
242
+ while (this._isWhiteSpace(this._currentChar) === true) {
243
+ this._advance();
244
+ }
245
+ }
246
+ _number() {
247
+ let ret = "";
248
+ const lastC = this._col;
249
+ const lastP = this._position;
250
+ const line = this._line;
251
+ const first = String.fromCharCode(this._currentChar);
252
+ this._advance();
253
+ const bc = String.fromCharCode(this._currentChar);
254
+ let base = 10;
255
+ if (first === '0' && ['b', 'B', 'x', 'X', 'o', 'O'].includes(bc)) {
256
+ // look ahead to see that the number is tagged
257
+ this._advance();
258
+ switch (bc.toLowerCase()) {
259
+ case 'b':
260
+ base = 2;
261
+ break;
262
+ case "o":
263
+ base = 8;
264
+ break;
265
+ case "x":
266
+ base = 16;
267
+ break;
268
+ default:
269
+ base = 10;
270
+ }
271
+ }
272
+ else {
273
+ ret += first;
274
+ base = 10;
275
+ }
276
+ while (this._isDigit(this._currentChar) ||
277
+ (base === 16 &&
278
+ ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f']
279
+ .includes(String.fromCharCode(this._currentChar)))) {
280
+ ret += String.fromCharCode(this._currentChar);
281
+ this._advance();
282
+ }
283
+ if (String.fromCharCode(this._currentChar) === '.' && this._isDigit(this._pick()) === true) {
284
+ if (base !== 10) {
285
+ error(new Token(TokenType.id, bc, line, lastP));
286
+ }
287
+ // floating number
288
+ ret += String.fromCharCode(this._currentChar);
289
+ this._advance();
290
+ while (this._isDigit(this._currentChar)) {
291
+ ret += String.fromCharCode(this._currentChar);
292
+ this._advance();
293
+ }
294
+ return new Token(TokenType.numberConst, parseFloat(ret), line, lastC);
295
+ }
296
+ return new Token(TokenType.numberConst, parseInt(ret, base), line, lastC);
297
+ }
298
+ _literalString(startChar) {
299
+ let ret = "";
300
+ let lastChar = -1;
301
+ const lastP = this._position;
302
+ const lastC = this._col;
303
+ const lastL = this._line;
304
+ while (this._currentChar !== -1) {
305
+ const next = String.fromCharCode(this._pick());
306
+ if (this._currentChar === Chars.backslash) {
307
+ // scape char
308
+ if ([
309
+ startChar,
310
+ '\\',
311
+ '0',
312
+ 'a',
313
+ 'b',
314
+ 'e',
315
+ 'f',
316
+ 'n',
317
+ 'r',
318
+ 't',
319
+ 'u',
320
+ 'U',
321
+ 'v',
322
+ 'x'
323
+ ].includes(next)) {
324
+ lastChar = this._currentChar;
325
+ this._advance();
326
+ continue;
327
+ }
328
+ }
329
+ if (this._currentChar === startChar && lastChar !== Chars.backslash) {
330
+ // end
331
+ break;
332
+ }
333
+ ret += String.fromCharCode(this._currentChar);
334
+ lastChar = this._currentChar;
335
+ this._advance();
336
+ }
337
+ return new Token(TokenType.stringConst, ret, lastL, lastC);
338
+ }
339
+ // f=SUBMIT_FORM(xxx);
340
+ // r = CALL_API(API_ID, f);
341
+ // DISPLAY("MSG #r")
342
+ nextToken() {
343
+ const lastL = this._line;
344
+ const lastC = this._col;
345
+ const lastP = this._position;
346
+ while (this._currentChar !== -1) {
347
+ if (this._isLineEnd(this._currentChar)) {
348
+ this._line++;
349
+ this._col = 1;
350
+ this._advance();
351
+ return new Token(TokenType.eol, "\n", lastL, lastC);
352
+ }
353
+ if (this._isWhiteSpace(this._currentChar)) {
354
+ this._skipWhiteSpace();
355
+ continue;
356
+ }
357
+ if (this._isDigit(this._currentChar)) {
358
+ return this._number();
359
+ }
360
+ if (this._isIdStart(this._currentChar)) {
361
+ return this._id();
362
+ }
363
+ if (this._currentChar === Chars.equal) {
364
+ this._advance();
365
+ if (this._currentChar === Chars.equal) {
366
+ this._advance();
367
+ return new Token(TokenType.eqeq, "==", lastL, lastC);
368
+ }
369
+ return new Token(TokenType.equal, "=", lastL, lastC);
370
+ }
371
+ if (this._currentChar === Chars.great) {
372
+ this._advance();
373
+ if (this._currentChar === Chars.equal) {
374
+ this._advance();
375
+ return new Token(TokenType.greatEq, ">=", lastL, lastC);
376
+ }
377
+ return new Token(TokenType.great, ">", lastL, lastC);
378
+ }
379
+ if (this._currentChar === Chars.less) {
380
+ this._advance();
381
+ if (this._currentChar === Chars.equal) {
382
+ this._advance();
383
+ return new Token(TokenType.lessEq, "<=", lastL, lastC);
384
+ }
385
+ return new Token(TokenType.less, "<", lastL, lastC);
386
+ }
387
+ if (this._currentChar === Chars.semiColon) {
388
+ this._advance();
389
+ return new Token(TokenType.semi, ";", lastL, lastC);
390
+ }
391
+ if (this._currentChar === Chars.lparen) {
392
+ this._advance();
393
+ return new Token(TokenType.lparen, "(", lastL, lastC);
394
+ }
395
+ if (this._currentChar === Chars.rparen) {
396
+ this._advance();
397
+ return new Token(TokenType.rparen, ")", lastL, lastC);
398
+ }
399
+ if (this._currentChar === Chars.comma) {
400
+ this._advance();
401
+ return new Token(TokenType.comma, ",", lastL, lastC);
402
+ }
403
+ if (this._currentChar === Chars.lbrace) {
404
+ this._advance();
405
+ return new Token(TokenType.lbrace, "{", lastL, lastC);
406
+ }
407
+ if (this._currentChar === Chars.rbrace) {
408
+ this._advance();
409
+ return new Token(TokenType.rbrace, "}", lastL, lastC);
410
+ }
411
+ if (this._currentChar === Chars.lbracket) {
412
+ this._advance();
413
+ return new Token(TokenType.lbracket, "[", lastL, lastC);
414
+ }
415
+ if (this._currentChar === Chars.rbracket) {
416
+ this._advance();
417
+ return new Token(TokenType.rbracket, "]", lastL, lastC);
418
+ }
419
+ if (this._currentChar === Chars.plus) {
420
+ this._advance();
421
+ return new Token(TokenType.plus, "+", lastL, lastC);
422
+ }
423
+ if (this._currentChar === Chars.minus) {
424
+ this._advance();
425
+ return new Token(TokenType.minus, "-", lastL, lastC);
426
+ }
427
+ if (this._currentChar === Chars.slash) {
428
+ this._advance();
429
+ return new Token(TokenType.div, "/", lastL, lastC);
430
+ }
431
+ if (this._currentChar === Chars.star) {
432
+ this._advance();
433
+ return new Token(TokenType.mult, "*", lastL, lastC);
434
+ }
435
+ if (this._currentChar === Chars.percent) {
436
+ this._advance();
437
+ return new Token(TokenType.mod, "%", lastL, lastC);
438
+ }
439
+ if (this._currentChar === Chars.colon) {
440
+ this._advance();
441
+ return new Token(TokenType.colon, ":", lastL, lastC);
442
+ }
443
+ if (this._currentChar === Chars.not) {
444
+ this._advance();
445
+ if (this._currentChar === Chars.equal) {
446
+ this._advance();
447
+ return new Token(TokenType.notEq, "!=", lastL, lastC);
448
+ }
449
+ return new Token(TokenType.not, "!", lastL, lastC);
450
+ }
451
+ if (this._currentChar === Chars.and && this._pick() === Chars.and) {
452
+ this._advance();
453
+ this._advance();
454
+ return new Token(TokenType.and, "&&", lastL, lastC);
455
+ }
456
+ if (this._currentChar === Chars.pipe && this._pick() === Chars.pipe) {
457
+ this._advance();
458
+ this._advance();
459
+ return new Token(TokenType.and, '||', lastL, lastC);
460
+ }
461
+ if (this._currentChar === Chars.quote || this._currentChar === Chars.squote) {
462
+ const startChar = this._currentChar;
463
+ this._advance();
464
+ const t = this._literalString(startChar);
465
+ this._advance();
466
+ return t;
467
+ }
468
+ return new Token(TokenType.invalid, String.fromCharCode(this._currentChar), lastL, lastC);
469
+ }
470
+ return new Token(TokenType.eof, "", lastL, lastC);
471
+ }
472
+ }
473
+ Lexer._defaultLanguage = new LexerDictionary("en", {
474
+ "if": "if",
475
+ "else": "else",
476
+ "true": "true",
477
+ "false": "false",
478
+ "null": "null",
479
+ });
480
+ Lexer.languages = [
481
+ Lexer._defaultLanguage,
482
+ new LexerDictionary("fr", {
483
+ "si": "if",
484
+ "sinon": "else",
485
+ "vrai": "else",
486
+ "faux": "false",
487
+ "nul": "null",
488
+ }),
489
+ new LexerDictionary("bm", {
490
+ "nii": "if",
491
+ "note": "else",
492
+ "tien": "true",
493
+ "galon": "false",
494
+ "gansan": "null",
495
+ }),
496
+ ];
497
+ Lexer.RESERVED = {
498
+ "if": Token.from(TokenType.IF, "if"),
499
+ "else": Token.from(TokenType.ELSE, "else"),
500
+ "true": Token.from(TokenType.TRUE, true),
501
+ "false": Token.from(TokenType.FALSE, false),
502
+ "null": Token.from(TokenType.NULL, null),
503
+ };
504
+ /**
505
+ * Parser
506
+ */
507
+ class AST {
508
+ constructor(line, col) {
509
+ this.line = line;
510
+ this.col = col;
511
+ }
512
+ dump() {
513
+ return this.toString();
514
+ }
515
+ }
516
+ class RootAST extends AST {
517
+ constructor(body, name, source) {
518
+ super(1, 1);
519
+ this.body = body;
520
+ this.name = name;
521
+ this.source = source;
522
+ }
523
+ dump() {
524
+ let builder = `Module ${this.name} Start {`;
525
+ for (const it of this.body) {
526
+ builder += `${it.dump()}\n`;
527
+ }
528
+ builder += "}";
529
+ return builder;
530
+ }
531
+ }
532
+ class BlockStatementAST extends AST {
533
+ constructor(body, token) {
534
+ super(token.line, token.col);
535
+ this.body = body;
536
+ }
537
+ toString() {
538
+ return `{\n${this.body.map((e) => e.toString()).join("\n")}}`;
539
+ }
540
+ }
541
+ class IdentifierAST extends AST {
542
+ constructor(token) {
543
+ super(token.line, token.col);
544
+ this.value = token.value.toString();
545
+ }
546
+ toString() {
547
+ return this.value;
548
+ }
549
+ }
550
+ class LiteralAST extends AST {
551
+ constructor(token, raw) {
552
+ super(token.line, token.col);
553
+ this.value = token.value;
554
+ this.raw = raw;
555
+ }
556
+ toString() {
557
+ return this.value.toString();
558
+ }
559
+ }
560
+ class AssignmentExpressionAST extends AST {
561
+ constructor(identifier, init) {
562
+ super(identifier.line, identifier.col);
563
+ this.identifier = identifier;
564
+ this.init = init;
565
+ }
566
+ toString() {
567
+ return `${this.identifier} = ${this.init}`;
568
+ }
569
+ }
570
+ class ExpressionStatementAST extends AST {
571
+ constructor(expression) {
572
+ super(expression.line, expression.col);
573
+ this.expression = expression;
574
+ }
575
+ toString() {
576
+ return this.expression.toString();
577
+ }
578
+ }
579
+ class CallExpressionAST extends AST {
580
+ constructor(callee, ags) {
581
+ super(callee.line, callee.col);
582
+ this.callee = callee;
583
+ this.arguments = ags;
584
+ }
585
+ toString() {
586
+ return `${this.callee.toString()}(...${this.arguments.length})`;
587
+ }
588
+ }
589
+ class BinaryExpressionAST extends AST {
590
+ constructor(left, operation, right) {
591
+ super(left.line, left.col);
592
+ this.left = left;
593
+ this.operation = operation;
594
+ this.right = right;
595
+ }
596
+ toString() {
597
+ return `${this.left} ${this.operation} ${this.right}`;
598
+ }
599
+ }
600
+ class UnaryExpressionAST extends AST {
601
+ constructor(operation, argument) {
602
+ super(operation.line, operation.col);
603
+ this.operation = operation, this.argument = argument;
604
+ }
605
+ toString() {
606
+ return `${this.operation} ${this.argument}`;
607
+ }
608
+ }
609
+ class IfStatementAST extends AST {
610
+ constructor(test, consequent, alternate) {
611
+ super(test.line, test.col);
612
+ this.test = test;
613
+ this.consequent = consequent;
614
+ this.alternate = alternate;
615
+ }
616
+ toString() {
617
+ return `if ${this.test} ${this.consequent} ${this.alternate !== null ? `else ${this.alternate} ` : ''}`;
618
+ }
619
+ }
620
+ class LogicalExpressionAST extends AST {
621
+ constructor(left, operator, right) {
622
+ super(left.line, left.col);
623
+ this.left = left;
624
+ this.operator = operator;
625
+ this.right = right;
626
+ }
627
+ toString() {
628
+ return `${this.left} ${this.operator.value} ${this.right}`;
629
+ }
630
+ }
631
+ class IndexAccessorAST extends AST {
632
+ constructor(owner, key, computed = false) {
633
+ super(owner.line, owner.col);
634
+ this.computed = false;
635
+ this.owner = owner;
636
+ this.key = key;
637
+ this.computed = computed;
638
+ }
639
+ toString() {
640
+ return `${this.owner}[${this.key}]`;
641
+ }
642
+ }
643
+ class ObjectExpression extends AST {
644
+ constructor(props, startToken, endToken) {
645
+ super(startToken === null || startToken === void 0 ? void 0 : startToken.line, startToken === null || startToken === void 0 ? void 0 : startToken.col);
646
+ this.properties = props;
647
+ }
648
+ toString() {
649
+ return "{...}";
650
+ }
651
+ }
652
+ class ArrayExpression extends AST {
653
+ constructor(props, start, end) {
654
+ super(start === null || start === void 0 ? void 0 : start.line, start === null || start === void 0 ? void 0 : start.col);
655
+ this.elements = props;
656
+ }
657
+ toString() {
658
+ return "[...]";
659
+ }
660
+ }
661
+ class ObjectProperty extends AST {
662
+ constructor(key, value) {
663
+ super(key.line, key.col);
664
+ this.value = value;
665
+ this.key = key;
666
+ }
667
+ }
668
+ class Parser {
669
+ constructor(lexer) {
670
+ this.currentToken = lexer.nextToken();
671
+ this.lexer = lexer;
672
+ }
673
+ _eat(type) {
674
+ var _a;
675
+ if (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === type) {
676
+ this.currentToken = this.lexer.nextToken();
677
+ }
678
+ else {
679
+ error(this.currentToken);
680
+ }
681
+ }
682
+ _eatEOL() {
683
+ var _a;
684
+ while (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === TokenType.eol) {
685
+ this._eat(TokenType.eol);
686
+ }
687
+ }
688
+ _eatSemiOrEOL() {
689
+ var _a, _b;
690
+ while (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === TokenType.eol ||
691
+ ((_b = this.currentToken) === null || _b === void 0 ? void 0 : _b.type) === TokenType.semi) {
692
+ this._eat(this.currentToken.type);
693
+ }
694
+ }
695
+ _eatSemi() {
696
+ var _a;
697
+ while (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === TokenType.semi) {
698
+ this._eat(TokenType.semi);
699
+ }
700
+ }
701
+ _variable() {
702
+ const node = new IdentifierAST(this.currentToken);
703
+ this._eat(TokenType.id);
704
+ return node;
705
+ }
706
+ _factor() {
707
+ const token = this.currentToken;
708
+ switch (token.type) {
709
+ case TokenType.plus:
710
+ case TokenType.minus:
711
+ case TokenType.not:
712
+ this._eat(this.currentToken.type);
713
+ return new UnaryExpressionAST(token, this._factor());
714
+ case TokenType.numberConst:
715
+ this._eat(TokenType.numberConst);
716
+ return new LiteralAST(token, token.value.toString());
717
+ case TokenType.stringConst:
718
+ this._eat(TokenType.stringConst);
719
+ return new LiteralAST(token, token.value.toString());
720
+ case TokenType.lparen:
721
+ this._eat(TokenType.lparen);
722
+ const node = this._expression();
723
+ this._eat(TokenType.rparen);
724
+ return node;
725
+ case TokenType.TRUE:
726
+ case TokenType.FALSE:
727
+ this._eat(this.currentToken.type);
728
+ return new LiteralAST(token, token.value.toString());
729
+ case TokenType.NULL:
730
+ this._eat(TokenType.NULL);
731
+ return new LiteralAST(token, "null");
732
+ case TokenType.lbracket:
733
+ // literal array expression
734
+ return this._arrayExpression();
735
+ case TokenType.lbrace:
736
+ return this._objectExpression();
737
+ default:
738
+ return this._variable();
739
+ }
740
+ }
741
+ _term() {
742
+ let node = this._factor();
743
+ node = this._tryParsingMemberExpression(node);
744
+ node = this._tryParsingFunctionCall(node);
745
+ return node;
746
+ }
747
+ _expression() {
748
+ let node = this._term();
749
+ node = this._tryBinaryExpression(0, node);
750
+ while ([TokenType.and, TokenType.or].includes(this.currentToken.type)) {
751
+ const token = this.currentToken;
752
+ this._eat(token.type);
753
+ node = new LogicalExpressionAST(node, token, this._expression());
754
+ }
755
+ if (this._expect(TokenType.equal)) {
756
+ if (node instanceof IdentifierAST || node instanceof IndexAccessorAST) {
757
+ const token = this.currentToken;
758
+ this._eat(TokenType.equal);
759
+ node = new AssignmentExpressionAST(node, this._expression());
760
+ }
761
+ else {
762
+ throw new Error("Unexpected token");
763
+ }
764
+ }
765
+ return node;
766
+ }
767
+ _objectProperty() {
768
+ var _a;
769
+ let key;
770
+ switch ((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) {
771
+ case TokenType.stringConst:
772
+ {
773
+ key = new LiteralAST(this.currentToken, this.currentToken.value);
774
+ this._eat(TokenType.stringConst);
775
+ break;
776
+ }
777
+ case TokenType.lbracket:
778
+ {
779
+ this._eat(TokenType.lbracket);
780
+ var expr = this._expression();
781
+ this._eat(TokenType.rbracket);
782
+ key = expr;
783
+ break;
784
+ }
785
+ case TokenType.id:
786
+ {
787
+ const id = this._variable();
788
+ key = new LiteralAST(new Token(TokenType.id, id.value, id.line, id.col), id.value);
789
+ break;
790
+ }
791
+ default:
792
+ throw `Unexpected token ${this.currentToken}`;
793
+ }
794
+ this._eat(TokenType.colon);
795
+ var value = this._expression();
796
+ return new ObjectProperty(key, value);
797
+ }
798
+ _property() {
799
+ return this._objectProperty();
800
+ }
801
+ _objectProperties() {
802
+ var _a, _b;
803
+ const properties = [];
804
+ if (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) != TokenType.rbrace) {
805
+ properties.push(this._property());
806
+ }
807
+ while (((_b = this.currentToken) === null || _b === void 0 ? void 0 : _b.type) == TokenType.comma) {
808
+ this._eat(TokenType.comma);
809
+ properties.push(this._property());
810
+ }
811
+ return properties;
812
+ }
813
+ _objectExpression(braceToken) {
814
+ var token = braceToken !== null && braceToken !== void 0 ? braceToken : this.currentToken;
815
+ if (!braceToken)
816
+ this._eat(TokenType.lbrace);
817
+ var properties = this._objectProperties();
818
+ this._eat(TokenType.rbrace);
819
+ return new ObjectExpression(properties, token, this.currentToken);
820
+ }
821
+ _arrayExpression() {
822
+ this._eat(TokenType.lbracket);
823
+ const elements = this._expect(TokenType.rbracket) ? [] : this._expressionsList();
824
+ this._eat(TokenType.rbracket);
825
+ var startNode = elements.length !== 0 ? elements[0] : undefined;
826
+ var lastNode = elements.length !== 0 ? elements[elements.length - 1] : undefined;
827
+ return new ArrayExpression(elements, startNode, lastNode);
828
+ }
829
+ _tryParsingMemberExpression(n) {
830
+ let node = n;
831
+ while (this.currentToken.type === TokenType.lbracket) {
832
+ this._eat(TokenType.lbracket);
833
+ const key = this._expression();
834
+ node = new IndexAccessorAST(node, key);
835
+ this._eat(TokenType.rbracket);
836
+ }
837
+ return node;
838
+ }
839
+ _tryBinaryExpression(prec, left) {
840
+ let node = left;
841
+ while (true) {
842
+ const currentPrec = Parser._binopPrecdences[this.currentToken.type] || -1;
843
+ if (currentPrec < prec)
844
+ return node;
845
+ const operator = this.currentToken;
846
+ this._eat(operator.type);
847
+ let right = this._term();
848
+ const nextPrec = Parser._binopPrecdences[this.currentToken.type] || -1;
849
+ if (currentPrec < nextPrec) {
850
+ const tmp = this._tryBinaryExpression(currentPrec + 1, right);
851
+ if (tmp === node)
852
+ return tmp;
853
+ right = tmp;
854
+ }
855
+ node = new BinaryExpressionAST(node, operator, right);
856
+ }
857
+ }
858
+ _expressionsList() {
859
+ var _a;
860
+ this._eatEOL();
861
+ // if (expect(Lexer.TokenType.RPAREN)) return emptyList()
862
+ let expr = this._expression();
863
+ const result = [expr];
864
+ while (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === TokenType.comma) {
865
+ this._eat(TokenType.comma);
866
+ expr = this._expression();
867
+ result.push(expr);
868
+ }
869
+ return result;
870
+ }
871
+ _callExpression(node) {
872
+ this._eat(TokenType.lparen);
873
+ let args = [];
874
+ if (!this._expect(TokenType.rparen)) {
875
+ args = this._expressionsList();
876
+ }
877
+ this._eat(TokenType.rparen);
878
+ if (!(node instanceof IdentifierAST)) {
879
+ error(this.currentToken);
880
+ }
881
+ return new CallExpressionAST(node, args);
882
+ }
883
+ _tryParsingFunctionCall(n) {
884
+ let node = n;
885
+ while (this.currentToken.type === TokenType.lparen) {
886
+ // probable function call
887
+ node = this._callExpression(node);
888
+ }
889
+ return node;
890
+ }
891
+ _statementExpression() {
892
+ const node = this._expression();
893
+ if (![TokenType.semi, TokenType.eol, TokenType.eof]
894
+ .includes(this.currentToken.type)) {
895
+ error(this.currentToken);
896
+ }
897
+ return node;
898
+ }
899
+ _blockStatement(ignoreFirstBrace = false) {
900
+ var _a;
901
+ if (!ignoreFirstBrace)
902
+ this._eat(TokenType.lbrace);
903
+ this._eatEOL();
904
+ if (this._expect(TokenType.rbrace)) {
905
+ this._eat(TokenType.rbrace);
906
+ return new BlockStatementAST([], this.currentToken);
907
+ }
908
+ // const body = _statements();
909
+ const body = [this._statement()];
910
+ while (this.currentToken.type === TokenType.eol ||
911
+ this.currentToken.type === TokenType.semi) {
912
+ this._eatSemiOrEOL();
913
+ if (this.currentToken.type === TokenType.rbrace || this.currentToken.type === TokenType.eof) {
914
+ // _eat(this.currentToken!.type);
915
+ break;
916
+ }
917
+ body.push(this._statement());
918
+ if (this.currentToken.type !== TokenType.eol &&
919
+ this.currentToken.type !== TokenType.semi &&
920
+ this.currentToken.type !== TokenType.eof) {
921
+ error(this.currentToken);
922
+ }
923
+ }
924
+ this._eat(TokenType.rbrace);
925
+ if (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === TokenType.rbrace) {
926
+ // endBlock
927
+ this._eat(TokenType.rbrace);
928
+ }
929
+ return new BlockStatementAST(body, this.currentToken);
930
+ }
931
+ _ifStatement() {
932
+ this._eat(TokenType.IF);
933
+ const withPar = this.currentToken.type === TokenType.lparen;
934
+ if (withPar) {
935
+ this._eat(TokenType.lparen);
936
+ }
937
+ const test = this._expression();
938
+ if (withPar) {
939
+ this._eat(TokenType.rparen);
940
+ }
941
+ let consequent;
942
+ if (this.currentToken.type === TokenType.lbrace) {
943
+ consequent = this._blockStatement();
944
+ }
945
+ else {
946
+ consequent = this._expression();
947
+ }
948
+ this._eatSemiOrEOL();
949
+ let alternate;
950
+ if (this.currentToken.type === TokenType.ELSE) {
951
+ this._eat(TokenType.ELSE);
952
+ switch (this.currentToken.type) {
953
+ case TokenType.IF:
954
+ alternate = this._ifStatement();
955
+ break;
956
+ case TokenType.lbrace:
957
+ alternate = this._blockStatement();
958
+ break;
959
+ default:
960
+ alternate = this._expression();
961
+ }
962
+ }
963
+ return new IfStatementAST(test, consequent, alternate);
964
+ }
965
+ _statement() {
966
+ switch (this.currentToken.type) {
967
+ case TokenType.semi:
968
+ this._eatSemi();
969
+ return this._statement();
970
+ case TokenType.eol:
971
+ this._eatEOL();
972
+ return this._statement();
973
+ case TokenType.IF:
974
+ return this._ifStatement();
975
+ default:
976
+ return this._statementExpression();
977
+ }
978
+ }
979
+ _expect(type) {
980
+ var _a;
981
+ return ((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === type;
982
+ }
983
+ _statements() {
984
+ this._eatEOL();
985
+ if (this._expect(TokenType.eof)) {
986
+ return [];
987
+ }
988
+ const results = [this._statement()];
989
+ while ((this.currentToken.type === TokenType.eol ||
990
+ this.currentToken.type === TokenType.semi)) {
991
+ this._eatSemiOrEOL();
992
+ if (this.currentToken.type === TokenType.eof) {
993
+ this._eat(TokenType.eof);
994
+ break;
995
+ }
996
+ results.push(this._statement());
997
+ if (this.currentToken.type !== TokenType.eol &&
998
+ this.currentToken.type !== TokenType.semi &&
999
+ this.currentToken.type !== TokenType.eof) {
1000
+ error(this.currentToken);
1001
+ }
1002
+ }
1003
+ return results;
1004
+ }
1005
+ _root() {
1006
+ const source = this.lexer.source;
1007
+ const name = "<module>";
1008
+ const list = this._statements();
1009
+ return new RootAST(list, name, source);
1010
+ }
1011
+ parse() {
1012
+ return this._root();
1013
+ }
1014
+ }
1015
+ Parser._binopPrecdences = {
1016
+ [TokenType.eqeq]: 10,
1017
+ [TokenType.notEq]: 10,
1018
+ [TokenType.great]: 10,
1019
+ [TokenType.greatEq]: 10,
1020
+ [TokenType.less]: 10,
1021
+ [TokenType.lessEq]: 10,
1022
+ [TokenType.plus]: 20,
1023
+ [TokenType.minus]: 20,
1024
+ [TokenType.mult]: 40,
1025
+ [TokenType.div]: 40,
1026
+ [TokenType.mod]: 40,
1027
+ };
1028
+ // AST Wallker
1029
+ class ANodeVisitor {
1030
+ constructor() {
1031
+ this._nodesVisitors = {};
1032
+ }
1033
+ registerVisitor(type, visitor) {
1034
+ const methodName = `visit${type.name}`;
1035
+ // console.log('Reg::::::', methodName, type);
1036
+ this._nodesVisitors[methodName] = visitor;
1037
+ }
1038
+ }
1039
+ class AsyncNodeVisitor extends ANodeVisitor {
1040
+ visit(node) {
1041
+ return __awaiter(this, void 0, void 0, function* () {
1042
+ const methodName = `visit${node.constructor.name}`;
1043
+ // const fn = Object.getPrototypeOf(this)[methodName];
1044
+ const fn = this._nodesVisitors[methodName];
1045
+ return yield (fn === null || fn === void 0 ? void 0 : fn.call(this, node));
1046
+ });
1047
+ }
1048
+ }
1049
+ class NodeVisitor extends ANodeVisitor {
1050
+ visit(node) {
1051
+ const methodName = `visit${node.constructor.name}`;
1052
+ // const fn = Object.getPrototypeOf(this)[methodName];
1053
+ const fn = this._nodesVisitors[methodName];
1054
+ return fn === null || fn === void 0 ? void 0 : fn.call(this, node);
1055
+ }
1056
+ }
1057
+ class MEvento extends NodeVisitor {
1058
+ constructor() {
1059
+ super();
1060
+ this._memory = {};
1061
+ this._functionsRegistry = {};
1062
+ this.registerVisitor(RootAST, this.visitRootAST);
1063
+ this.registerVisitor(BlockStatementAST, this.visitBlockStatementAST);
1064
+ this.registerVisitor(IdentifierAST, this.visitIdentifierAST);
1065
+ this.registerVisitor(LiteralAST, this.visitLiteralAST);
1066
+ this.registerVisitor(AssignmentExpressionAST, this.visitAssignmentExpressionAST);
1067
+ this.registerVisitor(ExpressionStatementAST, this.visitExpressionStatementAST);
1068
+ this.registerVisitor(CallExpressionAST, this.visitCallExpressionAST);
1069
+ this.registerVisitor(BinaryExpressionAST, this.visitBinaryExpressionAST);
1070
+ this.registerVisitor(UnaryExpressionAST, this.visitUnaryExpressionAST);
1071
+ this.registerVisitor(IfStatementAST, this.visitIfStatementAST);
1072
+ this.registerVisitor(LogicalExpressionAST, this.visitLogicalExpressionAST);
1073
+ this.registerVisitor(IndexAccessorAST, this.visitIndexAccessorAST);
1074
+ this.registerVisitor(ObjectProperty, this.visitObjectProperty);
1075
+ this.registerVisitor(ObjectExpression, this.visitObjectExpression);
1076
+ this.registerVisitor(ArrayExpression, this.visitArrayExpression);
1077
+ this._functionsRegistry = Object.assign({}, MEvento._globalFunctionsRegistry);
1078
+ }
1079
+ visitRootAST(node) {
1080
+ const list = node.body;
1081
+ let last;
1082
+ for (const n of list) {
1083
+ last = this.visit(n);
1084
+ }
1085
+ return last;
1086
+ }
1087
+ visitBlockStatementAST(node) {
1088
+ const list = node.body;
1089
+ let last;
1090
+ for (const n of list) {
1091
+ last = this.visit(n);
1092
+ }
1093
+ return last;
1094
+ }
1095
+ visitIdentifierAST(node) {
1096
+ const id = node.value;
1097
+ return this._memory[id];
1098
+ }
1099
+ visitLiteralAST(node) {
1100
+ const value = node.value;
1101
+ return value;
1102
+ }
1103
+ visitAssignmentExpressionAST(node) {
1104
+ const id = node.identifier;
1105
+ const init = node.init;
1106
+ let value = null;
1107
+ if (id instanceof IndexAccessorAST) {
1108
+ var target = this.visit(id.owner);
1109
+ value = this.visit(node.init);
1110
+ var property = this.visit(id.key);
1111
+ this.assignProperty(target, property, value);
1112
+ }
1113
+ else if (id instanceof IdentifierAST) {
1114
+ value = this.visit(init);
1115
+ this._memory[id.value] = value;
1116
+ }
1117
+ return value;
1118
+ }
1119
+ assignProperty(owner, property, value) {
1120
+ if (Array.isArray(owner)) {
1121
+ owner[property] = value;
1122
+ }
1123
+ else if (typeof owner === 'object') {
1124
+ owner[property] = value;
1125
+ }
1126
+ else { }
1127
+ }
1128
+ visitExpressionStatementAST(node) {
1129
+ const expr = node.expression;
1130
+ return this.visit(expr);
1131
+ }
1132
+ visitCallExpressionAST(node) {
1133
+ const callee = node.callee;
1134
+ const args = node.arguments;
1135
+ const calleeName = callee.value;
1136
+ const fn = this._functionsRegistry[calleeName];
1137
+ const argValues = args.map((e) => this.visit(e));
1138
+ // print("argValues::", argValues)
1139
+ return fn === null || fn === void 0 ? void 0 : fn(argValues, this);
1140
+ }
1141
+ visitBinaryExpressionAST(node) {
1142
+ const left = node.left;
1143
+ const right = node.right;
1144
+ const op = node.operation;
1145
+ const lValue = this.visit(left);
1146
+ const rValue = this.visit(right);
1147
+ switch (op.type) {
1148
+ case TokenType.plus:
1149
+ if (isNum(lValue) && isNum(rValue)) {
1150
+ return lValue + rValue;
1151
+ }
1152
+ return `${lValue}${rValue}`;
1153
+ case TokenType.minus:
1154
+ if (isNum(lValue) && isNum(rValue)) {
1155
+ return lValue - rValue;
1156
+ }
1157
+ if (isString(lValue) && isNum(rValue)) {
1158
+ return lValue * rValue;
1159
+ }
1160
+ if (isNum(lValue) && isString(rValue)) {
1161
+ return rValue * lValue;
1162
+ }
1163
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1164
+ case TokenType.mult:
1165
+ if (isNum(lValue) && isNum(rValue)) {
1166
+ return lValue * rValue;
1167
+ }
1168
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1169
+ case TokenType.div:
1170
+ if (isNum(lValue) && isNum(rValue)) {
1171
+ if (rValue === 0) {
1172
+ throw new Error("Invalid division by 0");
1173
+ }
1174
+ return lValue / rValue;
1175
+ }
1176
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1177
+ case TokenType.mod:
1178
+ if (isNum(lValue) && isNum(rValue)) {
1179
+ return lValue % rValue;
1180
+ }
1181
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1182
+ case TokenType.great:
1183
+ if (isNum(lValue) && isNum(rValue)) {
1184
+ return lValue > rValue;
1185
+ }
1186
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1187
+ case TokenType.greatEq:
1188
+ if (isNum(lValue) && isNum(rValue)) {
1189
+ return lValue >= rValue;
1190
+ }
1191
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1192
+ case TokenType.less:
1193
+ if (isNum(lValue) && isNum(rValue)) {
1194
+ return lValue < rValue;
1195
+ }
1196
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1197
+ case TokenType.lessEq:
1198
+ if (isNum(lValue) && isNum(rValue)) {
1199
+ return lValue <= rValue;
1200
+ }
1201
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1202
+ case TokenType.eqeq:
1203
+ return lValue === rValue;
1204
+ case TokenType.notEq:
1205
+ return lValue !== rValue;
1206
+ default:
1207
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1208
+ }
1209
+ }
1210
+ visitUnaryExpressionAST(node) {
1211
+ const arg = node.argument;
1212
+ const op = node.operation;
1213
+ const argValue = this.visit(arg);
1214
+ if (op.type === TokenType.not) {
1215
+ return _boolValue(argValue) === false;
1216
+ }
1217
+ if (isNum(argValue)) {
1218
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1219
+ }
1220
+ if (op.type === TokenType.plus) {
1221
+ return argValue;
1222
+ }
1223
+ else if (op.type === TokenType.minus) {
1224
+ return -argValue;
1225
+ }
1226
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1227
+ }
1228
+ visitIfStatementAST(node) {
1229
+ const testNode = node.test;
1230
+ const test = this.visit(testNode);
1231
+ const testBoolValue = _boolValue(test);
1232
+ if (testBoolValue) {
1233
+ return this.visit(node.consequent);
1234
+ }
1235
+ if (node.alternate !== null) {
1236
+ return this.visit(node.alternate);
1237
+ }
1238
+ return null;
1239
+ }
1240
+ visitLogicalExpressionAST(node) {
1241
+ const leftNode = node.left;
1242
+ const test = this.visit(leftNode);
1243
+ const testBoolValue = _boolValue(test);
1244
+ if (node.operator.type === TokenType.and) {
1245
+ return testBoolValue && _boolValue(this.visit(node.right));
1246
+ }
1247
+ if (node.operator.type === TokenType.or) {
1248
+ return testBoolValue || _boolValue(this.visit(node.right));
1249
+ }
1250
+ return false;
1251
+ }
1252
+ visitIndexAccessorAST(node) {
1253
+ const ownerNode = node.owner;
1254
+ const owner = this.visit(ownerNode);
1255
+ if (typeof owner === 'object') {
1256
+ const key = this.visit(node.key);
1257
+ return owner[key];
1258
+ }
1259
+ else if (Array.isArray(owner)) {
1260
+ const key = this.visit(node.key);
1261
+ return isNum(key) && owner.length < key && key >= 0 ? owner[key] : null;
1262
+ }
1263
+ return null;
1264
+ }
1265
+ visitObjectProperty(node) { }
1266
+ visitObjectExpression(ast) {
1267
+ const instance = {};
1268
+ const node = ast;
1269
+ for (const it of node.properties) {
1270
+ if (it instanceof ObjectProperty) {
1271
+ let key = this.visit(it.key);
1272
+ key = isString(key) ? key : key.toString();
1273
+ instance[key] = this.visit(it.value);
1274
+ }
1275
+ }
1276
+ return instance;
1277
+ }
1278
+ visitArrayExpression(ast) {
1279
+ const node = ast;
1280
+ const args = this.resolveArguments(node.elements);
1281
+ return args;
1282
+ }
1283
+ resolveArguments(args) {
1284
+ return args.map((it) => {
1285
+ return this.visit(it);
1286
+ });
1287
+ }
1288
+ registerFunction(id, fn) {
1289
+ this._functionsRegistry[id] = fn;
1290
+ }
1291
+ unregisterFunction(id) {
1292
+ delete this._functionsRegistry[id];
1293
+ }
1294
+ execute(source, cache = true) {
1295
+ const module = MEvento.compile(source, cache);
1296
+ return this.visit(module);
1297
+ }
1298
+ static compile(source, cache = false) {
1299
+ const hash = hashCode(source);
1300
+ if (cache && this._cache.has(hash)) {
1301
+ return this._cache.get(hash);
1302
+ }
1303
+ const lexer = new Lexer(source);
1304
+ const parser = new Parser(lexer);
1305
+ const module = parser.parse();
1306
+ if (cache)
1307
+ this._cache.set(hash, module);
1308
+ return module;
1309
+ }
1310
+ static register(id, fn) {
1311
+ MEvento._globalFunctionsRegistry[id] = fn;
1312
+ }
1313
+ static unregister(id) {
1314
+ delete MEvento._globalFunctionsRegistry[id];
1315
+ }
1316
+ static run(source, cache = false) {
1317
+ const mevento = new MEvento();
1318
+ const module = MEvento.compile(source, cache);
1319
+ return mevento.visit(module);
1320
+ }
1321
+ static newInstance() {
1322
+ // const lexer = Lexer(source);
1323
+ // const parser = Parser(lexer);
1324
+ // const module = parser.parse();
1325
+ return new MEvento();
1326
+ }
1327
+ clone() {
1328
+ var ret = new MEvento();
1329
+ ret._functionsRegistry = Object.assign({}, this._functionsRegistry);
1330
+ ret._memory = Object.assign({}, this._memory);
1331
+ return ret;
1332
+ }
1333
+ newAsyncInstance() {
1334
+ const asyncIns = MEventoAsync.newInstance();
1335
+ asyncIns._memory = this._memory;
1336
+ /// ????
1337
+ asyncIns._functionsRegistry = this._functionsRegistry;
1338
+ return asyncIns;
1339
+ }
1340
+ ;
1341
+ }
1342
+ MEvento._globalFunctionsRegistry = {};
1343
+ MEvento._cache = new Map();
1344
+ class MEventoAsync extends MEvento {
1345
+ constructor() {
1346
+ super();
1347
+ }
1348
+ visitRootAST(node) {
1349
+ return __awaiter(this, void 0, void 0, function* () {
1350
+ const list = node.body;
1351
+ let last;
1352
+ for (const n of list) {
1353
+ last = yield this.visit(n);
1354
+ }
1355
+ return last;
1356
+ });
1357
+ }
1358
+ visitBlockStatementAST(node) {
1359
+ return __awaiter(this, void 0, void 0, function* () {
1360
+ const list = node.body;
1361
+ let last;
1362
+ for (const n of list) {
1363
+ last = yield this.visit(n);
1364
+ }
1365
+ return last;
1366
+ });
1367
+ }
1368
+ visitIdentifierAST(node) {
1369
+ return __awaiter(this, void 0, void 0, function* () {
1370
+ const id = node.value;
1371
+ return this._memory[id];
1372
+ });
1373
+ }
1374
+ visitLiteralAST(node) {
1375
+ return __awaiter(this, void 0, void 0, function* () {
1376
+ const value = node.value;
1377
+ return value;
1378
+ });
1379
+ }
1380
+ visitAssignmentExpressionAST(node) {
1381
+ return __awaiter(this, void 0, void 0, function* () {
1382
+ const id = node.identifier;
1383
+ const init = node.init;
1384
+ let value = null;
1385
+ if (id instanceof IndexAccessorAST) {
1386
+ var target = yield this.visit(id.owner);
1387
+ value = yield this.visit(node.init);
1388
+ var property = yield this.visit(id.key);
1389
+ this.assignProperty(target, property, value);
1390
+ }
1391
+ else if (id instanceof IdentifierAST) {
1392
+ value = yield this.visit(init);
1393
+ this._memory[id.value] = value;
1394
+ }
1395
+ return value;
1396
+ });
1397
+ }
1398
+ visitExpressionStatementAST(node) {
1399
+ return __awaiter(this, void 0, void 0, function* () {
1400
+ const expr = node.expression;
1401
+ return yield this.visit(expr);
1402
+ });
1403
+ }
1404
+ visitCallExpressionAST(node) {
1405
+ return __awaiter(this, void 0, void 0, function* () {
1406
+ const callee = node.callee;
1407
+ const args = node.arguments;
1408
+ const calleeName = callee.value;
1409
+ const fn = this._functionsRegistry[calleeName];
1410
+ const argValues = yield Promise.all(args.map((e) => this.visit(e)));
1411
+ // print("argValues::", argValues)
1412
+ return yield (fn === null || fn === void 0 ? void 0 : fn(argValues, this));
1413
+ });
1414
+ }
1415
+ visitBinaryExpressionAST(node) {
1416
+ return __awaiter(this, void 0, void 0, function* () {
1417
+ const left = node.left;
1418
+ const right = node.right;
1419
+ const op = node.operation;
1420
+ const lValue = yield this.visit(left);
1421
+ const rValue = yield this.visit(right);
1422
+ switch (op.type) {
1423
+ case TokenType.plus:
1424
+ if (isNum(lValue) && isNum(rValue)) {
1425
+ return lValue + rValue;
1426
+ }
1427
+ return `${lValue}${rValue}`;
1428
+ case TokenType.minus:
1429
+ if (isNum(lValue) && isNum(rValue)) {
1430
+ return lValue - rValue;
1431
+ }
1432
+ if (isString(lValue) && isNum(rValue)) {
1433
+ return lValue * rValue;
1434
+ }
1435
+ if (isNum(lValue) && isString(rValue)) {
1436
+ return rValue * lValue;
1437
+ }
1438
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1439
+ case TokenType.mult:
1440
+ if (isNum(lValue) && isNum(rValue)) {
1441
+ return lValue * rValue;
1442
+ }
1443
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1444
+ case TokenType.div:
1445
+ if (isNum(lValue) && isNum(rValue)) {
1446
+ if (rValue === 0) {
1447
+ throw new Error("Invalid division by 0");
1448
+ }
1449
+ return lValue / rValue;
1450
+ }
1451
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1452
+ case TokenType.mod:
1453
+ if (isNum(lValue) && isNum(rValue)) {
1454
+ return lValue % rValue;
1455
+ }
1456
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1457
+ case TokenType.great:
1458
+ if (isNum(lValue) && isNum(rValue)) {
1459
+ return lValue > rValue;
1460
+ }
1461
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1462
+ case TokenType.greatEq:
1463
+ if (isNum(lValue) && isNum(rValue)) {
1464
+ return lValue >= rValue;
1465
+ }
1466
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1467
+ case TokenType.less:
1468
+ if (isNum(lValue) && isNum(rValue)) {
1469
+ return lValue < rValue;
1470
+ }
1471
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1472
+ case TokenType.lessEq:
1473
+ if (isNum(lValue) && isNum(rValue)) {
1474
+ return lValue <= rValue;
1475
+ }
1476
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1477
+ case TokenType.eqeq:
1478
+ return lValue === rValue;
1479
+ case TokenType.notEq:
1480
+ return lValue !== rValue;
1481
+ default:
1482
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1483
+ }
1484
+ });
1485
+ }
1486
+ visitUnaryExpressionAST(node) {
1487
+ return __awaiter(this, void 0, void 0, function* () {
1488
+ const arg = node.argument;
1489
+ const op = node.operation;
1490
+ const argValue = yield this.visit(arg);
1491
+ if (isNum(argValue)) {
1492
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1493
+ }
1494
+ if (op.type === TokenType.plus) {
1495
+ return argValue;
1496
+ }
1497
+ else if (op.type === TokenType.minus) {
1498
+ return -argValue;
1499
+ }
1500
+ throw new Error(`Operation ${op.value} not allowed no num value`);
1501
+ });
1502
+ }
1503
+ visitIfStatementAST(node) {
1504
+ return __awaiter(this, void 0, void 0, function* () {
1505
+ const testNode = node.test;
1506
+ const test = yield this.visit(testNode);
1507
+ const testBoolValue = _boolValue(test);
1508
+ if (testBoolValue) {
1509
+ return yield this.visit(node.consequent);
1510
+ }
1511
+ if (node.alternate !== null) {
1512
+ return yield this.visit(node.alternate);
1513
+ }
1514
+ return null;
1515
+ });
1516
+ }
1517
+ visitLogicalExpressionAST(node) {
1518
+ return __awaiter(this, void 0, void 0, function* () {
1519
+ const leftNode = node.left;
1520
+ const test = yield this.visit(leftNode);
1521
+ const testBoolValue = _boolValue(test);
1522
+ if (node.operator.type === TokenType.and) {
1523
+ return testBoolValue && _boolValue(yield this.visit(node.right));
1524
+ }
1525
+ if (node.operator.type === TokenType.or) {
1526
+ return testBoolValue || _boolValue(yield this.visit(node.right));
1527
+ }
1528
+ return false;
1529
+ });
1530
+ }
1531
+ visitIndexAccessorAST(node) {
1532
+ return __awaiter(this, void 0, void 0, function* () {
1533
+ const ownerNode = node.owner;
1534
+ const owner = yield this.visit(ownerNode);
1535
+ if (typeof owner === 'object') {
1536
+ const key = yield this.visit(node.key);
1537
+ return owner[key];
1538
+ }
1539
+ else if (Array.isArray(owner)) {
1540
+ const key = yield this.visit(node.key);
1541
+ return isNum(key) && owner.length < key && key >= 0 ? owner[key] : null;
1542
+ }
1543
+ return null;
1544
+ });
1545
+ }
1546
+ visitObjectProperty(node) {
1547
+ return __awaiter(this, void 0, void 0, function* () { });
1548
+ }
1549
+ visitObjectExpression(ast) {
1550
+ return __awaiter(this, void 0, void 0, function* () {
1551
+ const instance = {};
1552
+ const node = ast;
1553
+ for (const it of node.properties) {
1554
+ if (it instanceof ObjectProperty) {
1555
+ let key = yield this.visit(it.key);
1556
+ key = isString(key) ? key : key.toString();
1557
+ instance[key] = yield this.visit(it.value);
1558
+ }
1559
+ }
1560
+ return instance;
1561
+ });
1562
+ }
1563
+ visitArrayExpression(ast) {
1564
+ return __awaiter(this, void 0, void 0, function* () {
1565
+ const node = ast;
1566
+ const args = yield this.resolveArgumentsAsync(node.elements);
1567
+ return args;
1568
+ });
1569
+ }
1570
+ resolveArgumentsAsync(args) {
1571
+ return __awaiter(this, void 0, void 0, function* () {
1572
+ return yield Promise.all(args.map((it) => __awaiter(this, void 0, void 0, function* () {
1573
+ return yield this.visit(it);
1574
+ })));
1575
+ });
1576
+ }
1577
+ registerFunction(id, fn) {
1578
+ this._functionsRegistry[id] = fn;
1579
+ }
1580
+ unregisterFunction(id) {
1581
+ delete this._functionsRegistry[id];
1582
+ }
1583
+ execute(source, cache = true) {
1584
+ return __awaiter(this, void 0, void 0, function* () {
1585
+ const module = MEvento.compile(source, cache);
1586
+ return yield this.visit(module);
1587
+ });
1588
+ }
1589
+ static run(source, cache = false) {
1590
+ return __awaiter(this, void 0, void 0, function* () {
1591
+ const mevento = new MEventoAsync();
1592
+ const module = MEvento.compile(source, cache);
1593
+ return yield mevento.visit(module);
1594
+ });
1595
+ }
1596
+ static newInstance() {
1597
+ // const lexer = Lexer(source);
1598
+ // const parser = Parser(lexer);
1599
+ // const module = parser.parse();
1600
+ return new MEventoAsync();
1601
+ }
1602
+ clone() {
1603
+ var ret = new MEventoAsync();
1604
+ ret._functionsRegistry = Object.assign({}, this._functionsRegistry);
1605
+ ret._memory = Object.assign({}, this._memory);
1606
+ return ret;
1607
+ }
1608
+ }
1609
+
1610
+ /*
1611
+ * Public API Surface of mevento
1612
+ */
1613
+
1614
+ /**
1615
+ * Generated bundle index. Do not edit.
1616
+ */
1617
+
1618
+ export { MEvento, MEventoAsync };
1619
+ //# sourceMappingURL=mevento.mjs.map