mevento 2.1.0 → 3.0.1
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.
- package/{lib/mevento.d.ts → dist/cjs/index.d.ts} +43 -42
- package/dist/cjs/index.js +8 -0
- package/dist/esm/index.d.mts +349 -0
- package/dist/esm/index.mjs +8 -0
- package/package.json +23 -29
- package/README.md +0 -59
- package/esm2020/lib/mevento.mjs +0 -2264
- package/esm2020/mevento.mjs +0 -5
- package/esm2020/public-api.mjs +0 -5
- package/fesm2015/mevento.mjs +0 -2344
- package/fesm2015/mevento.mjs.map +0 -1
- package/fesm2020/mevento.mjs +0 -2274
- package/fesm2020/mevento.mjs.map +0 -1
- package/index.d.ts +0 -5
- package/public-api.d.ts +0 -1
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
declare class TokenType {
|
|
2
|
+
static id: number;
|
|
3
|
+
static comma: number;
|
|
4
|
+
static semi: number;
|
|
5
|
+
static numberConst: number;
|
|
6
|
+
static stringConst: number;
|
|
7
|
+
static equal: number;
|
|
8
|
+
static lparen: number;
|
|
9
|
+
static rparen: number;
|
|
10
|
+
static eol: number;
|
|
11
|
+
static eof: number;
|
|
12
|
+
static lbrace: number;
|
|
13
|
+
static rbrace: number;
|
|
14
|
+
static lbracket: number;
|
|
15
|
+
static rbracket: number;
|
|
16
|
+
static great: number;
|
|
17
|
+
static greatEq: number;
|
|
18
|
+
static less: number;
|
|
19
|
+
static lessEq: number;
|
|
20
|
+
static eqeq: number;
|
|
21
|
+
static IF: number;
|
|
22
|
+
static ELSE: number;
|
|
23
|
+
static TRUE: number;
|
|
24
|
+
static FALSE: number;
|
|
25
|
+
static NULL: number;
|
|
26
|
+
static not: number;
|
|
27
|
+
static notEq: number;
|
|
28
|
+
static and: number;
|
|
29
|
+
static or: number;
|
|
30
|
+
static plus: number;
|
|
31
|
+
static minus: number;
|
|
32
|
+
static div: number;
|
|
33
|
+
static mult: number;
|
|
34
|
+
static mod: number;
|
|
35
|
+
static invalid: number;
|
|
36
|
+
static colon: number;
|
|
37
|
+
static WHILE_TILL: number;
|
|
38
|
+
static FOR_LOOP: number;
|
|
39
|
+
static up: number;
|
|
40
|
+
static down: number;
|
|
41
|
+
static with: number;
|
|
42
|
+
static in: number;
|
|
43
|
+
static TILL: number;
|
|
44
|
+
static BREAK: number;
|
|
45
|
+
static CONTINUE: number;
|
|
46
|
+
static nullity: number;
|
|
47
|
+
static RETURN: number;
|
|
48
|
+
}
|
|
49
|
+
declare class Token {
|
|
50
|
+
type: number;
|
|
51
|
+
value: any;
|
|
52
|
+
line?: number;
|
|
53
|
+
col?: number;
|
|
54
|
+
constructor(type: number, value: any, line?: number, col?: number);
|
|
55
|
+
static from(type: number, value: any): Token;
|
|
56
|
+
toString(): string;
|
|
57
|
+
}
|
|
58
|
+
declare class LexerDictionary {
|
|
59
|
+
lang: string;
|
|
60
|
+
keywords: {
|
|
61
|
+
[key: string]: string;
|
|
62
|
+
};
|
|
63
|
+
constructor(lang: string, keywords: {
|
|
64
|
+
[key: string]: string;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parser
|
|
69
|
+
*/
|
|
70
|
+
declare abstract class AST {
|
|
71
|
+
line?: number;
|
|
72
|
+
col?: number;
|
|
73
|
+
constructor(line?: number, col?: number);
|
|
74
|
+
dump(): string;
|
|
75
|
+
}
|
|
76
|
+
declare class RootAST extends AST {
|
|
77
|
+
body: AST[];
|
|
78
|
+
source: string;
|
|
79
|
+
name: string;
|
|
80
|
+
constructor(body: AST[], name: string, source: string);
|
|
81
|
+
dump(): string;
|
|
82
|
+
}
|
|
83
|
+
declare class BlockStatementAST extends AST {
|
|
84
|
+
body: AST[];
|
|
85
|
+
constructor(body: AST[], token: Token);
|
|
86
|
+
toString(): string;
|
|
87
|
+
}
|
|
88
|
+
declare class IdentifierAST extends AST {
|
|
89
|
+
value: string;
|
|
90
|
+
constructor(token: Token);
|
|
91
|
+
toString(): string;
|
|
92
|
+
}
|
|
93
|
+
declare class LiteralAST extends AST {
|
|
94
|
+
value: any;
|
|
95
|
+
raw: string;
|
|
96
|
+
constructor(token: Token, raw: string);
|
|
97
|
+
toString(): string;
|
|
98
|
+
}
|
|
99
|
+
declare class AssignmentExpressionAST extends AST {
|
|
100
|
+
identifier: AST;
|
|
101
|
+
init: AST;
|
|
102
|
+
constructor(identifier: AST, init: AST);
|
|
103
|
+
toString(): string;
|
|
104
|
+
}
|
|
105
|
+
declare class ExpressionStatementAST extends AST {
|
|
106
|
+
expression: AST;
|
|
107
|
+
constructor(expression: AST);
|
|
108
|
+
toString(): string;
|
|
109
|
+
}
|
|
110
|
+
declare class CallExpressionAST extends AST {
|
|
111
|
+
callee: IdentifierAST;
|
|
112
|
+
arguments: AST[];
|
|
113
|
+
constructor(callee: IdentifierAST, ags: AST[]);
|
|
114
|
+
toString(): string;
|
|
115
|
+
}
|
|
116
|
+
declare class BinaryExpressionAST extends AST {
|
|
117
|
+
left: AST;
|
|
118
|
+
operation: Token;
|
|
119
|
+
right: AST;
|
|
120
|
+
constructor(left: AST, operation: Token, right: AST);
|
|
121
|
+
toString(): string;
|
|
122
|
+
}
|
|
123
|
+
declare class UnaryExpressionAST extends AST {
|
|
124
|
+
operation: Token;
|
|
125
|
+
argument: AST;
|
|
126
|
+
constructor(operation: Token, argument: AST);
|
|
127
|
+
toString(): string;
|
|
128
|
+
}
|
|
129
|
+
declare class IfStatementAST extends AST {
|
|
130
|
+
test: AST;
|
|
131
|
+
consequent: AST;
|
|
132
|
+
alternate?: AST;
|
|
133
|
+
constructor(test: AST, consequent: AST, alternate?: AST);
|
|
134
|
+
toString(): string;
|
|
135
|
+
}
|
|
136
|
+
declare class LogicalExpressionAST extends AST {
|
|
137
|
+
left: AST;
|
|
138
|
+
operator: Token;
|
|
139
|
+
right: AST;
|
|
140
|
+
constructor(left: AST, operator: Token, right: AST);
|
|
141
|
+
toString(): string;
|
|
142
|
+
}
|
|
143
|
+
declare class IndexAccessorAST extends AST {
|
|
144
|
+
owner: AST;
|
|
145
|
+
key: AST;
|
|
146
|
+
computed: boolean;
|
|
147
|
+
constructor(owner: AST, key: AST, computed?: boolean);
|
|
148
|
+
toString(): string;
|
|
149
|
+
}
|
|
150
|
+
declare class ObjectExpression extends AST {
|
|
151
|
+
properties: AST[];
|
|
152
|
+
constructor(props: AST[], startToken?: Token, endToken?: Token);
|
|
153
|
+
toString(): string;
|
|
154
|
+
}
|
|
155
|
+
declare class ArrayExpression extends AST {
|
|
156
|
+
elements: AST[];
|
|
157
|
+
constructor(props: AST[], start?: AST, end?: AST);
|
|
158
|
+
toString(): string;
|
|
159
|
+
}
|
|
160
|
+
declare class ObjectProperty extends AST {
|
|
161
|
+
key: AST;
|
|
162
|
+
value: AST;
|
|
163
|
+
constructor(key: AST, value: AST);
|
|
164
|
+
}
|
|
165
|
+
declare class WhileLoopStatement extends AST {
|
|
166
|
+
test: AST;
|
|
167
|
+
body: AST;
|
|
168
|
+
retain: boolean;
|
|
169
|
+
constructor(test: AST, body: AST, startToken?: Token, endToken?: Token, retain?: boolean);
|
|
170
|
+
}
|
|
171
|
+
declare class ForLoopStatement extends AST {
|
|
172
|
+
init: AssignmentExpressionAST;
|
|
173
|
+
test: AST;
|
|
174
|
+
update: AST;
|
|
175
|
+
direction: Token;
|
|
176
|
+
body: AST;
|
|
177
|
+
retain: boolean;
|
|
178
|
+
constructor(init: AssignmentExpressionAST, test: AST, update: AST, direction: Token, body: AST, startToken?: Token, endToken?: Token, retain?: boolean);
|
|
179
|
+
}
|
|
180
|
+
declare class ForOfStatement extends AST {
|
|
181
|
+
identifier: AST;
|
|
182
|
+
collection: AST;
|
|
183
|
+
body: AST;
|
|
184
|
+
retain: boolean;
|
|
185
|
+
constructor(identifier: AST, collection: AST, body: AST, startToken?: Token, endToken?: Token, retain?: boolean);
|
|
186
|
+
}
|
|
187
|
+
declare class TupleExpression extends AST {
|
|
188
|
+
first: AST;
|
|
189
|
+
second: AST;
|
|
190
|
+
constructor(first: AST, second: AST);
|
|
191
|
+
}
|
|
192
|
+
declare class BreakAST extends AST {
|
|
193
|
+
constructor(line?: number, col?: number);
|
|
194
|
+
}
|
|
195
|
+
declare class ReturnAST extends AST {
|
|
196
|
+
value?: AST;
|
|
197
|
+
constructor(value?: AST, line?: number, col?: number);
|
|
198
|
+
}
|
|
199
|
+
declare class ContinueAST extends AST {
|
|
200
|
+
constructor(line?: number, col?: number);
|
|
201
|
+
}
|
|
202
|
+
declare class LoopControl {
|
|
203
|
+
}
|
|
204
|
+
declare class BreakBranch extends LoopControl {
|
|
205
|
+
}
|
|
206
|
+
declare class ContinueBranch extends LoopControl {
|
|
207
|
+
}
|
|
208
|
+
declare class ReturnBranch {
|
|
209
|
+
value: any;
|
|
210
|
+
constructor(value: any);
|
|
211
|
+
}
|
|
212
|
+
type Func = (node: any) => any;
|
|
213
|
+
declare abstract class ANodeVisitor {
|
|
214
|
+
protected _nodesVisitors: {
|
|
215
|
+
[k: string]: Func;
|
|
216
|
+
};
|
|
217
|
+
protected registerVisitor(type: any, visitor: Func): void;
|
|
218
|
+
abstract visit(node: AST): any;
|
|
219
|
+
}
|
|
220
|
+
declare abstract class NodeVisitor extends ANodeVisitor {
|
|
221
|
+
constructor();
|
|
222
|
+
visit(node: AST): any;
|
|
223
|
+
protected assignProperty(owner: any, property: any, value: any): any;
|
|
224
|
+
protected abstract visitRootAST(node: RootAST): any;
|
|
225
|
+
protected abstract visitBlockStatementAST(node: BlockStatementAST): any;
|
|
226
|
+
protected abstract visitIdentifierAST(node: IdentifierAST): any;
|
|
227
|
+
protected abstract visitLiteralAST(node: AST): any;
|
|
228
|
+
protected abstract visitAssignmentExpressionAST(node: AssignmentExpressionAST): any;
|
|
229
|
+
protected abstract visitExpressionStatementAST(node: ExpressionStatementAST): any;
|
|
230
|
+
protected abstract visitCallExpressionAST(node: CallExpressionAST): any;
|
|
231
|
+
protected abstract visitBinaryExpressionAST(node: BinaryExpressionAST): any;
|
|
232
|
+
protected abstract visitUnaryExpressionAST(node: UnaryExpressionAST): any;
|
|
233
|
+
protected abstract visitIfStatementAST(node: IfStatementAST): any;
|
|
234
|
+
protected abstract visitLogicalExpressionAST(node: LogicalExpressionAST): any;
|
|
235
|
+
protected abstract visitIndexAccessorAST(node: IndexAccessorAST): any;
|
|
236
|
+
protected abstract visitObjectProperty(node: AST): any;
|
|
237
|
+
protected abstract visitObjectExpression(ast: AST): any;
|
|
238
|
+
protected abstract visitArrayExpression(ast: AST): any;
|
|
239
|
+
protected abstract visitBreakAST(node: BreakAST): any;
|
|
240
|
+
protected abstract visitWhileLoopStatement(node: WhileLoopStatement): any;
|
|
241
|
+
protected abstract visitForLoopStatement(node: ForLoopStatement): any;
|
|
242
|
+
protected abstract visitForOfStatement(node: ForOfStatement): any;
|
|
243
|
+
protected abstract visitContinueAST(node: ContinueAST): any;
|
|
244
|
+
protected abstract visitReturnAST(node: ReturnAST): any;
|
|
245
|
+
}
|
|
246
|
+
declare class MEventScope {
|
|
247
|
+
name: string;
|
|
248
|
+
parent?: MEventScope;
|
|
249
|
+
memory: {
|
|
250
|
+
[k: string]: any;
|
|
251
|
+
};
|
|
252
|
+
constructor(name: string, memory: {
|
|
253
|
+
[k: string]: any;
|
|
254
|
+
}, parent?: MEventScope);
|
|
255
|
+
resolve(key: string): any;
|
|
256
|
+
change(key: string, value: any, declare?: boolean): boolean;
|
|
257
|
+
}
|
|
258
|
+
type MEventoFBinding = (args: any[], vm: MEvento) => any;
|
|
259
|
+
declare class MEvento extends NodeVisitor {
|
|
260
|
+
rootScope: MEventScope;
|
|
261
|
+
protected currentScope?: MEventScope;
|
|
262
|
+
debug: boolean;
|
|
263
|
+
private static _globalFunctionsRegistry;
|
|
264
|
+
private static _cache;
|
|
265
|
+
protected _functionsRegistry: {
|
|
266
|
+
[k: string]: MEventoFBinding;
|
|
267
|
+
};
|
|
268
|
+
protected _functionResolver?: (name: string) => MEventoFBinding | undefined;
|
|
269
|
+
constructor();
|
|
270
|
+
protected resolve(name: string): any;
|
|
271
|
+
protected changeVariable(name: string, value: any): any;
|
|
272
|
+
protected pushScope(name: string): void;
|
|
273
|
+
protected popScope(): void;
|
|
274
|
+
protected log(message: any): void;
|
|
275
|
+
protected visitRootAST(node: RootAST): any;
|
|
276
|
+
protected visitBlockStatementAST(node: BlockStatementAST): any;
|
|
277
|
+
protected visitIdentifierAST(node: IdentifierAST): any;
|
|
278
|
+
protected visitLiteralAST(node: AST): any;
|
|
279
|
+
protected visitAssignmentExpressionAST(node: AssignmentExpressionAST): any;
|
|
280
|
+
protected visitExpressionStatementAST(node: ExpressionStatementAST): any;
|
|
281
|
+
protected visitCallExpressionAST(node: CallExpressionAST): any;
|
|
282
|
+
protected visitBinaryExpressionAST(node: BinaryExpressionAST): any;
|
|
283
|
+
protected visitUnaryExpressionAST(node: UnaryExpressionAST): any;
|
|
284
|
+
protected visitIfStatementAST(node: IfStatementAST): any;
|
|
285
|
+
protected visitLogicalExpressionAST(node: LogicalExpressionAST): any;
|
|
286
|
+
protected visitIndexAccessorAST(node: IndexAccessorAST): any;
|
|
287
|
+
protected visitObjectProperty(node: AST): any;
|
|
288
|
+
protected visitObjectExpression(ast: AST): any;
|
|
289
|
+
protected visitArrayExpression(ast: AST): any;
|
|
290
|
+
protected visitBreakAST(node: BreakAST): BreakBranch;
|
|
291
|
+
protected visitWhileLoopStatement(node: WhileLoopStatement): any;
|
|
292
|
+
protected visitForLoopStatement(node: ForLoopStatement): any;
|
|
293
|
+
protected visitForOfStatement(node: ForOfStatement): any;
|
|
294
|
+
protected visitContinueAST(node: ContinueAST): ContinueBranch;
|
|
295
|
+
protected visitReturnAST(node: ReturnAST): any;
|
|
296
|
+
protected _declareForIdentifier(node: AST, value: any): any;
|
|
297
|
+
resolveArguments(args: AST[]): any;
|
|
298
|
+
setFunctionResolver(resolver: (name: string) => MEventoFBinding | undefined): void;
|
|
299
|
+
resolveFunction(name: string): MEventoFBinding | undefined;
|
|
300
|
+
registerFunction(id: string, fn: MEventoFBinding): void;
|
|
301
|
+
unregisterFunction(id: string): void;
|
|
302
|
+
execute(source: string, cache?: boolean, input?: {
|
|
303
|
+
[k: string]: any;
|
|
304
|
+
}): any;
|
|
305
|
+
static compile(source: string, cache?: boolean): AST;
|
|
306
|
+
static register(id: string, fn: MEventoFBinding): void;
|
|
307
|
+
static unregister(id: string): void;
|
|
308
|
+
static run(source: string, cache?: boolean, input?: {
|
|
309
|
+
[k: string]: any;
|
|
310
|
+
}): any;
|
|
311
|
+
static newInstance(): MEvento;
|
|
312
|
+
clone(): MEvento;
|
|
313
|
+
newAsyncInstance(): MEventoAsync;
|
|
314
|
+
}
|
|
315
|
+
declare class MEventoAsync extends MEvento {
|
|
316
|
+
constructor();
|
|
317
|
+
protected visitRootAST(node: RootAST): Promise<any>;
|
|
318
|
+
protected visitBlockStatementAST(node: BlockStatementAST): Promise<any>;
|
|
319
|
+
protected visitIdentifierAST(node: AST): Promise<any>;
|
|
320
|
+
protected visitLiteralAST(node: LiteralAST): Promise<any>;
|
|
321
|
+
protected visitAssignmentExpressionAST(node: AssignmentExpressionAST): Promise<any>;
|
|
322
|
+
protected visitExpressionStatementAST(node: ExpressionStatementAST): Promise<any>;
|
|
323
|
+
protected visitCallExpressionAST(node: CallExpressionAST): Promise<any>;
|
|
324
|
+
protected visitBinaryExpressionAST(node: BinaryExpressionAST): Promise<string | number | boolean>;
|
|
325
|
+
protected visitUnaryExpressionAST(node: UnaryExpressionAST): Promise<number | boolean>;
|
|
326
|
+
protected visitIfStatementAST(node: IfStatementAST): Promise<any>;
|
|
327
|
+
protected visitLogicalExpressionAST(node: LogicalExpressionAST): Promise<any>;
|
|
328
|
+
protected visitIndexAccessorAST(node: IndexAccessorAST): Promise<any>;
|
|
329
|
+
protected visitObjectProperty(node: AST): Promise<void>;
|
|
330
|
+
protected visitObjectExpression(ast: AST): Promise<any>;
|
|
331
|
+
protected visitArrayExpression(ast: AST): Promise<any[]>;
|
|
332
|
+
protected visitWhileLoopStatement(node: WhileLoopStatement): Promise<any[] | ReturnBranch>;
|
|
333
|
+
protected visitForLoopStatement(node: ForLoopStatement): Promise<any[] | ReturnBranch>;
|
|
334
|
+
protected visitForOfStatement(node: ForOfStatement): Promise<any[] | ReturnBranch>;
|
|
335
|
+
protected visitReturnAST(node: ReturnAST): Promise<any>;
|
|
336
|
+
resolveArgumentsAsync(args: AST[]): Promise<any[]>;
|
|
337
|
+
registerFunction(id: string, fn: MEventoFBinding): void;
|
|
338
|
+
unregisterFunction(id: string): void;
|
|
339
|
+
execute(source: string, cache?: boolean, input?: {
|
|
340
|
+
[k: string]: any;
|
|
341
|
+
}): Promise<any>;
|
|
342
|
+
static run(source: string, cache?: boolean, input?: {
|
|
343
|
+
[k: string]: any;
|
|
344
|
+
}): Promise<any>;
|
|
345
|
+
static newInstance(): MEventoAsync;
|
|
346
|
+
clone(): MEventoAsync;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export { AST, ArrayExpression, AssignmentExpressionAST, BinaryExpressionAST, BlockStatementAST, BreakAST, BreakBranch, CallExpressionAST, ContinueAST, ContinueBranch, ExpressionStatementAST, ForLoopStatement, ForOfStatement, IdentifierAST, IfStatementAST, IndexAccessorAST, LexerDictionary, LiteralAST, LogicalExpressionAST, LoopControl, MEventScope, MEvento, MEventoAsync, type MEventoFBinding, NodeVisitor, ObjectExpression, ObjectProperty, ReturnAST, ReturnBranch, RootAST, Token, TokenType, TupleExpression, UnaryExpressionAST, WhileLoopStatement };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
function g(u,t){let e=`Invalid token ${u.type}[${u.value}] at ${u.line}, ${u.col} ${t?`: expecting ${t} token`:""}
|
|
2
|
+
`;throw Error(e)}function l(u){return typeof u=="number"}function rt(u){return typeof u=="boolean"}function T(u){return typeof u=="string"}function f(u){return!(u===null||l(u)&&u===0||T(u)&&u.length===0||rt(u)&&!u)}function it(u){let t=0,e=0,i;if(u.length===0)return t;for(e=0;e<u.length;e++)i=u.charCodeAt(e),t=(t<<5)-t+i,t|=0;return t}var r=class{};r.id=0,r.comma=1,r.semi=2,r.numberConst=3,r.stringConst=4,r.equal=5,r.lparen=6,r.rparen=7,r.eol=8,r.eof=9,r.lbrace=10,r.rbrace=11,r.lbracket=12,r.rbracket=13,r.great=14,r.greatEq=15,r.less=16,r.lessEq=17,r.eqeq=18,r.IF=19,r.ELSE=20,r.TRUE=21,r.FALSE=22,r.NULL=23,r.not=24,r.notEq=25,r.and=26,r.or=27,r.plus=28,r.minus=29,r.div=30,r.mult=31,r.mod=32,r.invalid=33,r.colon=34,r.WHILE_TILL=35,r.FOR_LOOP=36,r.up=37,r.down=38,r.with=39,r.in=40,r.TILL=41,r.BREAK=42,r.CONTINUE=43,r.nullity=44,r.RETURN=45;var h=class u{constructor(t,e,i=1,s=1){this.type=t,this.value=e,this.line=i,this.col=s}static from(t,e){return new u(t,e)}toString(){return`[${this.type.toString()}, ${this.value}]`}},c=class{};c.equal=61,c.comma=44,c.semiColon=59,c.lparen=40,c.rparen=41,c.backslash=92,c.quote=34,c.squote=39,c.plus=43,c.minus=45,c.star=42,c.slash=47,c.percent=37,c.lbrace=123,c.rbrace=125,c.lbracket=91,c.rbracket=93,c.not=33,c.great=62,c.less=60,c.and=38,c.pipe=124,c.colon=58,c.questionMark=63,c.shebang=35;var L=class{constructor(t,e){this.keywords={};this.keywords={...e},this.lang=t}},m=class m{constructor(t){this._position=0;this._line=1;this._col=1;this._currentChar=-1;this._source=t,this._currentChar=this._source[this._position].charCodeAt(0),this._resolveLanguage()}get source(){return this._source}_resolveLanguage(){var e;let t=this.nextToken();if(t.type===r.less){let i=this.nextToken();i.type!==r.id&&g(t);let s=i.value.toString();this._language=(e=m.languages.find(n=>n.lang===s))!=null?e:m._defaultLanguage,t=this.nextToken(),t.type!==r.great&&g(t)}else this._language=m._defaultLanguage,this._position=0,this._currentChar=this._source[this._position].charCodeAt(0)}_advance(){if(this._position++,this._position>=this._source.length){this._currentChar=-1;return}this._currentChar=this._source[this._position].charCodeAt(0),this._col++}_jump(t){if(this._position+=t,this._position>=this._source.length){this._currentChar=-1;return}this._currentChar=this._source[this._position].charCodeAt(0),this._col+=t}_pick(){return this._position+1>=this._source.length?-1:this._source[this._position+1].charCodeAt(0)}_isId(t){return t<48?t===36:t<58?!0:t<65?!1:t<91?!0:t<97?t===95:t<123}_isIdStart(t){return t<65?t===36:t<91?!0:t<97?t===95:t<123}_id(){var o,a;let t="",e=this._col,i=this._position,s=this._line;for(;this._isId(this._currentChar);)t+=String.fromCharCode(this._currentChar),this._advance();let n=this._language&&(o=this._language.keywords[t])!=null?o:t;return(a=m.RESERVED[n])!=null?a:new h(r.id,t,s,e)}_isLineEnd(t){return t===10||t===13||[`
|
|
3
|
+
`,"\r","\u2028","\u2029"].includes(String.fromCharCode(t))}_isWhiteSpace(t){return[" "," "].includes(String.fromCharCode(t))}_isDigit(t){return t>0&&(t^48)<=9}_skipWhiteSpace(){for(;this._isWhiteSpace(this._currentChar)===!0;)this._advance()}_number(){let t="",e=this._col,i=this._position,s=this._line,n=String.fromCharCode(this._currentChar);this._advance();let o=String.fromCharCode(this._currentChar),a=10;if(n==="0"&&["b","B","x","X","o","O"].includes(o))switch(this._advance(),o.toLowerCase()){case"b":a=2;break;case"o":a=8;break;case"x":a=16;break;default:a=10}else t+=n,a=10;for(;this._isDigit(this._currentChar)||a===16&&["A","a","B","b","C","c","D","d","E","e","F","f"].includes(String.fromCharCode(this._currentChar));)t+=String.fromCharCode(this._currentChar),this._advance();if(String.fromCharCode(this._currentChar)==="."&&this._isDigit(this._pick())===!0){for(a!==10&&g(new h(r.id,o,s,i)),t+=String.fromCharCode(this._currentChar),this._advance();this._isDigit(this._currentChar);)t+=String.fromCharCode(this._currentChar),this._advance();return new h(r.numberConst,parseFloat(t),s,e)}return new h(r.numberConst,parseInt(t,a),s,e)}_literalString(t){let e="",i=-1,s=this._position,n=this._col,o=this._line;for(;this._currentChar!==-1;){let a=String.fromCharCode(this._pick());if(this._currentChar==c.backslash){switch(a){case"\\":e+="\\";break;case"0":e+="\0";break;case"a":e+="a";break;case"b":e+="\b";break;case"f":e+="\f";break;case"n":e+=`
|
|
4
|
+
`;break;case"r":e+="\r";break;case"t":e+=" ";break;case"u":e+=String.fromCharCode(Number.parseInt(this._source.substring(this._position+2,this._position+6),16)),this._jump(4);break;case"v":e+="\v";break;case"x":e+=String.fromCharCode(Number.parseInt(this._source.substring(this._position+2,this._position+4),16)),this._jump(2);break;default:if(String.fromCharCode(t)==a)e+=String.fromCharCode(t);else{this._advance(),i=this._currentChar,e+=String.fromCharCode(this._currentChar),this._advance();continue}}this._jump(2),i=this._currentChar;continue}if(this._currentChar===t&&i!==c.backslash)break;e+=String.fromCharCode(this._currentChar),i=this._currentChar,this._advance()}return new h(r.stringConst,e,o,n)}_skipLineComment(){for(;!this._isLineEnd(this._currentChar)&&this._currentChar!=-1;)this._advance()}_skipComment(){for(;this._currentChar!==-1;){if(this._currentChar===c.star&&this._pick()===c.shebang){this._advance(),this._advance();break}this._advance()}}nextToken(){let t=this._line,e=this._col,i=this._position;for(;this._currentChar!==-1;){if(this._isLineEnd(this._currentChar))return this._line++,this._col=1,this._advance(),new h(r.eol,`
|
|
5
|
+
`,t,e);if(this._isWhiteSpace(this._currentChar)){this._skipWhiteSpace();continue}if(this._currentChar==c.shebang){this._advance(),this._currentChar===c.star?(this._advance(),this._skipComment()):this._skipLineComment();continue}if(this._isDigit(this._currentChar))return this._number();if(this._isIdStart(this._currentChar))return this._id();if(this._currentChar===c.equal)return this._advance(),this._currentChar===c.equal?(this._advance(),new h(r.eqeq,"==",t,e)):new h(r.equal,"=",t,e);if(this._currentChar===c.great)return this._advance(),this._currentChar===c.equal?(this._advance(),new h(r.greatEq,">=",t,e)):new h(r.great,">",t,e);if(this._currentChar===c.less)return this._advance(),this._currentChar===c.equal?(this._advance(),new h(r.lessEq,"<=",t,e)):new h(r.less,"<",t,e);if(this._currentChar===c.semiColon)return this._advance(),new h(r.semi,";",t,e);if(this._currentChar===c.lparen)return this._advance(),new h(r.lparen,"(",t,e);if(this._currentChar===c.rparen)return this._advance(),new h(r.rparen,")",t,e);if(this._currentChar===c.comma)return this._advance(),new h(r.comma,",",t,e);if(this._currentChar===c.lbrace)return this._advance(),new h(r.lbrace,"{",t,e);if(this._currentChar===c.rbrace)return this._advance(),new h(r.rbrace,"}",t,e);if(this._currentChar===c.lbracket)return this._advance(),new h(r.lbracket,"[",t,e);if(this._currentChar===c.rbracket)return this._advance(),new h(r.rbracket,"]",t,e);if(this._currentChar===c.plus)return this._advance(),new h(r.plus,"+",t,e);if(this._currentChar===c.minus)return this._advance(),new h(r.minus,"-",t,e);if(this._currentChar===c.slash)return this._advance(),new h(r.div,"/",t,e);if(this._currentChar===c.star)return this._advance(),new h(r.mult,"*",t,e);if(this._currentChar===c.percent)return this._advance(),new h(r.mod,"%",t,e);if(this._currentChar===c.colon)return this._advance(),new h(r.colon,":",t,e);if(this._currentChar===c.not)return this._advance(),this._currentChar===c.equal?(this._advance(),new h(r.notEq,"!=",t,e)):new h(r.not,"!",t,e);if(this._currentChar===c.and&&this._pick()===c.and)return this._advance(),this._advance(),new h(r.and,"&&",t,e);if(this._currentChar===c.pipe&&this._pick()===c.pipe)return this._advance(),this._advance(),new h(r.or,"||",t,e);if(this._currentChar===c.questionMark&&this._pick()===c.questionMark)return this._advance(),this._advance(),new h(r.nullity,"??",t,e);if(this._currentChar===c.quote||this._currentChar===c.squote){let s=this._currentChar;this._advance();let n=this._literalString(s);return this._advance(),n}return new h(r.invalid,String.fromCharCode(this._currentChar),t,e)}return new h(r.eof,"",t,e)}};m._defaultLanguage=new L("en",{if:"if",else:"else",true:"true",false:"false",null:"null",while:"while",for:"for",with:"with",up:"up",down:"down",till:"till",in:"in",break:"break",continue:"continue",return:"return"}),m.languages=[m._defaultLanguage,new L("fr",{si:"if",sinon:"else",vrai:"true",faux:"false",nul:"null",tanque:"while",pour:"for",avec:"with",mont:"up",desc:"down",jusqua:"till",dans:"in",couper:"break",continuer:"continue",returner:"return"}),new L("bm",{nii:"if",note:"else",tien:"true",galon:"false",gansan:"null",foo:"while",seginka:"for",niin:"with",kay:"up",kaj:"down",kata:"till",kono:"in",tike:"break",ipan:"continue",segin:"return"})],m.RESERVED={if:h.from(r.IF,"if"),else:h.from(r.ELSE,"else"),true:h.from(r.TRUE,!0),false:h.from(r.FALSE,!1),null:h.from(r.NULL,null),for:h.from(r.FOR_LOOP,"for"),while:h.from(r.WHILE_TILL,"while"),with:h.from(r.with,"with"),up:h.from(r.up,"up"),down:h.from(r.down,"down"),till:h.from(r.TILL,"till"),in:h.from(r.in,"in"),break:h.from(r.BREAK,"break"),continue:h.from(r.CONTINUE,"continue"),return:h.from(r.RETURN,"return")};var X=m,d=class{constructor(t,e){this.line=t,this.col=e}dump(){return this.toString()}},F=class extends d{constructor(t,e,i){super(1,1),this.body=t,this.name=e,this.source=i}dump(){let t=`Module ${this.name} Start {`;for(let e of this.body)t+=`${e.dump()}
|
|
6
|
+
`;return t+="}",t}},$=class extends d{constructor(t,e){super(e.line,e.col),this.body=t}toString(){return`{
|
|
7
|
+
${this.body.map(t=>t.toString()).join(`
|
|
8
|
+
`)}}`}},b=class extends d{constructor(t){super(t.line,t.col),this.value=t.value.toString()}toString(){return this.value}},y=class extends d{constructor(t,e){super(t.line,t.col),this.value=t.value,this.raw=e}toString(){return this.value.toString()}},E=class extends d{constructor(t,e){super(t.line,t.col),this.identifier=t,this.init=e}toString(){return`${this.identifier} = ${this.init}`}},G=class extends d{constructor(t){super(t.line,t.col),this.expression=t}toString(){return this.expression.toString()}},R=class extends d{constructor(t,e){super(t.line,t.col),this.callee=t,this.arguments=e}toString(){return`${this.callee.toString()}(...${this.arguments.length})`}},V=class extends d{constructor(t,e,i){super(t.line,t.col),this.left=t,this.operation=e,this.right=i}toString(){return`${this.left} ${this.operation} ${this.right}`}},q=class extends d{constructor(t,e){super(t.line,t.col),this.operation=t,this.argument=e}toString(){return`${this.operation} ${this.argument}`}},B=class extends d{constructor(t,e,i){super(t.line,t.col),this.test=t,this.consequent=e,this.alternate=i}toString(){return`if ${this.test} ${this.consequent} ${this.alternate?`else ${this.alternate} `:""}`}},P=class extends d{constructor(t,e,i){super(t.line,t.col),this.left=t,this.operator=e,this.right=i}toString(){return`${this.left} ${this.operator.value} ${this.right}`}},x=class extends d{constructor(e,i,s=!1){super(e.line,e.col);this.computed=!1;this.owner=e,this.key=i,this.computed=s}toString(){return`${this.owner}[${this.key}]`}},j=class extends d{constructor(t,e,i){super(e==null?void 0:e.line,e==null?void 0:e.col),this.properties=t}toString(){return"{...}"}},N=class extends d{constructor(t,e,i){super(e==null?void 0:e.line,e==null?void 0:e.col),this.elements=t}toString(){return"[...]"}},k=class extends d{constructor(t,e){super(t.line,t.col),this.value=e,this.key=t}},U=class extends d{constructor(e,i,s,n,o=!1){super(s==null?void 0:s.line,s==null?void 0:s.col);this.retain=!1;this.test=e,this.body=i,this.retain=o}},M=class extends d{constructor(e,i,s,n,o,a,p,S=!1){super(a==null?void 0:a.line,a==null?void 0:a.col);this.init=e;this.test=i;this.update=s;this.direction=n;this.body=o;this.retain=S}},W=class extends d{constructor(e,i,s,n,o,a=!1){super(n==null?void 0:n.line,n==null?void 0:n.col);this.identifier=e;this.collection=i;this.body=s;this.retain=a}},D=class extends d{constructor(e,i){super(e.line,e.col);this.first=e;this.second=i}},H=class extends d{constructor(t,e){super(t,e)}},K=class extends d{constructor(t,e,i){super(e,i),this.value=t}},z=class extends d{constructor(t,e){super(t,e)}},J=class{constructor(t=1/0){this.capacity=t;this.storage=[]}push(t){if(this.size()===this.capacity)throw Error("Stack has reached max capacity, you cannot add more items");this.storage.push(t)}pop(){return this.storage.pop()}peek(){return this.storage[this.size()-1]}size(){return this.storage.length}get isEmpty(){return this.storage.length===0}},I=class I{constructor(t){this._loopTrack=new J;this.currentToken=t.nextToken(),this.lexer=t}_eat(t){var e;((e=this.currentToken)==null?void 0:e.type)===t?this.currentToken=this.lexer.nextToken():g(this.currentToken,t)}_eatEOL(){var t;for(;((t=this.currentToken)==null?void 0:t.type)===r.eol;)this._eat(r.eol)}_eatSemiOrEOL(){var t,e;for(;((t=this.currentToken)==null?void 0:t.type)===r.eol||((e=this.currentToken)==null?void 0:e.type)===r.semi;)this._eat(this.currentToken.type)}_eatSemi(){var t;for(;((t=this.currentToken)==null?void 0:t.type)===r.semi;)this._eat(r.semi)}_variable(){let t=new b(this.currentToken);return this._eat(r.id),t}_return(){let t=this.currentToken,e;return!this._expect(r.eol)&&!this._expect(r.semi)&&(e=this._expression()),new K(e,t==null?void 0:t.line,t==null?void 0:t.col)}_factor(){let t=this.currentToken;switch(t.type){case r.plus:case r.minus:case r.not:return this._eat(this.currentToken.type),new q(t,this._term());case r.numberConst:return this._eat(r.numberConst),new y(t,t.value.toString());case r.stringConst:return this._eat(r.stringConst),new y(t,t.value.toString());case r.lparen:this._eat(r.lparen);let e=this._expression();return this._eat(r.rparen),e;case r.TRUE:case r.FALSE:return this._eat(this.currentToken.type),new y(t,t.value.toString());case r.NULL:return this._eat(r.NULL),new y(t,"null");case r.lbracket:return this._arrayExpression();case r.lbrace:return this._objectExpression();case r.IF:return this._ifStatement();case r.WHILE_TILL:return this._whileLoop(!0);case r.FOR_LOOP:return this._forLoop(!0);case r.BREAK:return this._breakExpression();case r.CONTINUE:return this._continueExpression();default:return this._variable()}}_breakExpression(){var t,e;return this._loopTrack.isEmpty&&g(this.currentToken),this._eat(r.BREAK),new H((t=this.currentToken)==null?void 0:t.line,(e=this.currentToken)==null?void 0:e.col)}_continueExpression(){var t,e;return this._loopTrack.isEmpty&&g(this.currentToken),this._eat(r.CONTINUE),new z((t=this.currentToken)==null?void 0:t.line,(e=this.currentToken)==null?void 0:e.col)}_term(){let t=this._factor();return t=this._tryParsingFunctionCall(t),t=this._tryParsingMemberExpression(t),t}_expression(){let t=this._term();for(t=this._tryBinaryExpression(0,t);[r.and,r.or,r.nullity].includes(this.currentToken.type);){let e=this.currentToken;this._eat(e.type),t=new P(t,e,this._expression())}if(this._expect(r.equal))if(t instanceof b||t instanceof x){let e=this.currentToken;this._eat(r.equal),t=new E(t,this._expression())}else throw new Error("Unexpected token");return t}_objectProperty(){var s;let t;switch((s=this.currentToken)==null?void 0:s.type){case r.stringConst:{t=new y(this.currentToken,this.currentToken.value),this._eat(r.stringConst);break}case r.lbracket:{this._eat(r.lbracket);var e=this._expression();this._eat(r.rbracket),t=e;break}case r.id:{let n=this._variable();t=new y(new h(r.id,n.value,n.line,n.col),n.value);break}default:throw`Unexpected token ${this.currentToken}`}this._eat(r.colon);var i=this._expression();return new k(t,i)}_property(){return this._objectProperty()}_objectProperties(){var e,i;let t=[];for(((e=this.currentToken)==null?void 0:e.type)!=r.rbrace&&(this._eatEOL(),t.push(this._property()),this._eatEOL());((i=this.currentToken)==null?void 0:i.type)===r.comma&&(this._eat(r.comma),this._eatEOL(),!this._expect(r.rbrace));)t.push(this._property()),this._eatEOL();return t}_objectExpression(t){var e=t!=null?t:this.currentToken;t||this._eat(r.lbrace);var i=this._objectProperties();return this._eat(r.rbrace),new j(i,e,this.currentToken)}_arrayExpression(){this._eat(r.lbracket);let t=this._expect(r.rbracket)?[]:this._expressionsList();this._eat(r.rbracket);var e=t.length!==0?t[0]:void 0,i=t.length!==0?t[t.length-1]:void 0;return new N(t,e,i)}_tryParsingMemberExpression(t){let e=t;for(;this.currentToken.type===r.lbracket;){this._eat(r.lbracket);let i=this._expression();e=new x(e,i),this._eat(r.rbracket)}return e}_tryBinaryExpression(t,e){let i=e;for(;;){let s=I._binopPrecdences[this.currentToken.type]||-1;if(s<t)return i;let n=this.currentToken;this._eat(n.type);let o=this._term(),a=I._binopPrecdences[this.currentToken.type]||-1;if(s<a){let p=this._tryBinaryExpression(s+1,o);if(p===i)return p;o=p}i=new V(i,n,o)}}_expressionsList(){var i;this._eatEOL();let t=this._expression();this._eatEOL();let e=[t];for(;((i=this.currentToken)==null?void 0:i.type)===r.comma&&(this._eat(r.comma),this._eatEOL(),!this._expect(r.rbracket));)t=this._expression(),e.push(t),this._eatEOL();return e}_callExpression(t){this._eat(r.lparen);let e=[];return this._expect(r.rparen)||(e=this._expressionsList()),this._eat(r.rparen),t instanceof b||g(this.currentToken),new R(t,e)}_tryParsingFunctionCall(t){let e=t;for(;this.currentToken.type===r.lparen;)e=this._callExpression(e);return e}_statementExpression(){let t=this._expression();return[r.semi,r.eol,r.eof].includes(this.currentToken.type)||g(this.currentToken),t}_blockStatement(t=!1){var i;if(t||this._eat(r.lbrace),this._eatEOL(),this._expect(r.rbrace))return this._eat(r.rbrace),new $([],this.currentToken);let e=[this._statement()];for(;this._eatSemiOrEOL(),!(this.currentToken.type===r.rbrace||this.currentToken.type===r.eof||(e.push(this._statement()),this._expect(r.rbrace)));)this.currentToken.type!==r.eol&&this.currentToken.type!==r.semi&&this.currentToken.type!==r.eof&&g(this.currentToken);return this._eat(r.rbrace),((i=this.currentToken)==null?void 0:i.type)===r.rbrace&&this._eat(r.rbrace),new $(e,this.currentToken)}_ifStatement(){this._eat(r.IF);let t=this.currentToken.type===r.lparen;t&&this._eat(r.lparen);let e=this._expression();t&&this._eat(r.rparen);let i;this.currentToken.type===r.lbrace?i=this._blockStatement():i=this._expression();let s;if(this.currentToken.type===r.ELSE)switch(this._eat(r.ELSE),this.currentToken.type){case r.IF:s=this._ifStatement();break;case r.lbrace:s=this._blockStatement();break;default:s=this._expression()}return new B(e,i,s)}_pushLoop(){this._loopTrack.push(!0)}_popLoop(){this._loopTrack.pop()}_whileLoop(t=!1){let e=this.currentToken;this._eat(r.WHILE_TILL),this._pushLoop();let i=this._expression(),s=this.currentToken.type===r.lbrace?this._blockStatement():this._expression();return this._popLoop(),new U(i,s,e,this.currentToken,t)}_forOfIdentifier(){switch(this.currentToken.type){case r.lparen:{this._eat(r.lparen);let t=this._variable();this._eat(r.comma);let e=this._variable();return this._eat(r.rparen),new D(t,e)}default:return this._variable()}}_forLoop(t=!1){let e=this.currentToken;this._eat(r.FOR_LOOP),this._pushLoop();let i=[r.lparen].includes(this.currentToken.type),s;if(i)s=this._forOfIdentifier();else{let n=this._expression();n instanceof E||(i=!0),s=n}if(!i&&s instanceof E){this._eat(r.TILL);let n=this._expression(),o;if(this.currentToken.type===r.up||this.currentToken.type===r.down){let S=this.currentToken;this._eat(S.type),o=S}else o=new h(r.up,"up");let a;this._expect(r.with)?(this._eat(r.with),a=this._expression()):a=new y(new h(r.numberConst,1,this.currentToken.line,this.currentToken.col),"1");let p=this.currentToken.type===r.lbrace?this._blockStatement():this._expression();s=new M(s,n,a,o,p,e,this.currentToken,t),this._popLoop()}else if(i){this._eat(r.in);let n=this._expression(),o=this.currentToken.type===r.lbrace?this._blockStatement():this._expression();s=new W(s,n,o,e,this.currentToken,t),this._popLoop()}else g(this.currentToken);return s}_statement(){switch(this.currentToken.type){case r.BREAK:return this._breakExpression();case r.CONTINUE:return this._continueExpression();case r.RETURN:return this._eat(r.RETURN),this._return();case r.semi:return this._eatSemi(),this._statement();case r.eol:return this._eatEOL(),this._statement();case r.WHILE_TILL:return this._whileLoop();case r.FOR_LOOP:return this._forLoop();default:return this._statementExpression()}}_expect(t){var e;return((e=this.currentToken)==null?void 0:e.type)===t}_definition(){if(this._eatSemiOrEOL(),this._expect(r.eof))return[];let t=[this._statement()];for(;;){if(this._eatSemiOrEOL(),this.currentToken.type===r.eof){this._eat(r.eof);break}this.currentToken.type===r.lbrace?t.push(this._blockStatement()):t.push(this._statement())}return t}_root(){let t=this.lexer.source,e="<module>",i=this._definition();return new F(i,e,t)}parse(){return this._root()}};I._binopPrecdences={[r.eqeq]:10,[r.notEq]:10,[r.great]:10,[r.greatEq]:10,[r.less]:10,[r.lessEq]:10,[r.plus]:20,[r.minus]:20,[r.mult]:40,[r.div]:40,[r.mod]:40};var Y=I,C=class{},A=class extends C{},w=class extends C{},v=class{constructor(t){this.value=t}},Z=class{constructor(){this._nodesVisitors={}}registerVisitor(t,e){let i=`visit${t.name}`;this._nodesVisitors[i]=e}},tt=class extends Z{constructor(){super(),this.registerVisitor(F,this.visitRootAST),this.registerVisitor($,this.visitBlockStatementAST),this.registerVisitor(b,this.visitIdentifierAST),this.registerVisitor(y,this.visitLiteralAST),this.registerVisitor(E,this.visitAssignmentExpressionAST),this.registerVisitor(G,this.visitExpressionStatementAST),this.registerVisitor(R,this.visitCallExpressionAST),this.registerVisitor(V,this.visitBinaryExpressionAST),this.registerVisitor(q,this.visitUnaryExpressionAST),this.registerVisitor(B,this.visitIfStatementAST),this.registerVisitor(P,this.visitLogicalExpressionAST),this.registerVisitor(x,this.visitIndexAccessorAST),this.registerVisitor(k,this.visitObjectProperty),this.registerVisitor(j,this.visitObjectExpression),this.registerVisitor(N,this.visitArrayExpression),this.registerVisitor(U,this.visitWhileLoopStatement),this.registerVisitor(M,this.visitForLoopStatement),this.registerVisitor(W,this.visitForOfStatement),this.registerVisitor(H,this.visitBreakAST),this.registerVisitor(z,this.visitContinueAST),this.registerVisitor(K,this.visitReturnAST)}visit(t){var e;try{let i=`visit${t.constructor.name}`,s=this._nodesVisitors[i];return(e=s==null?void 0:s.call(this,t))!=null?e:null}catch{return null}}assignProperty(t,e,i){(Array.isArray(t)||typeof t=="object")&&(t[e]=i)}},Q=class{constructor(t,e,i){this.memory={};this.name=t,this.memory=e,this.parent=i}resolve(t){var e,i;return Object.keys(this.memory).includes(t)?this.memory[t]:(i=(e=this.parent)==null?void 0:e.resolve(t))!=null?i:null}change(t,e,i=!0){return Object.keys(this.memory).includes(t)?(this.memory[t]=e,!0):this.parent&&this.parent.change(t,e,!1)?!0:i?(this.memory[t]=e,!0):!1}},_=class _ extends tt{constructor(){super();this.rootScope=new Q("Program",{});this.currentScope=this.rootScope;this.debug=!1;this._functionsRegistry={};this._functionsRegistry={..._._globalFunctionsRegistry}}resolve(e){var i,s;return(s=(i=this.currentScope)==null?void 0:i.resolve(e))!=null?s:null}changeVariable(e,i){var s;return(s=this.currentScope)!=null&&s.change(e,i)?i:null}pushScope(e){let i=new Q(e,{},this.currentScope);this.currentScope=i}popScope(){var e;this.currentScope=(e=this.currentScope)==null?void 0:e.parent}log(e){this.debug&&console.log(e)}visitRootAST(e){var n;let i=e.body,s;for(let o of i)if(s=this.visit(o),s instanceof v)return(n=s.value)!=null?n:null;return s!=null?s:null}visitBlockStatementAST(e){let i=e.body,s;this.pushScope("Block");for(let n of i)if(s=this.visit(n),s instanceof C||s instanceof v)break;return this.popScope(),s!=null?s:null}visitIdentifierAST(e){var s;let i=e.value;return(s=this==null?void 0:this.resolve(i))!=null?s:null}visitLiteralAST(e){return e.value}visitAssignmentExpressionAST(e){let i=e.identifier,s=e.init,n=null;if(i instanceof x){var o=this.visit(i.owner);n=this.visit(e.init);var a=this.visit(i.key);this.assignProperty(o,a,n)}else i instanceof b&&(n=this.visit(s),this.changeVariable(i.value,n));return n}visitExpressionStatementAST(e){let i=e.expression;return this.visit(i)}visitCallExpressionAST(e){let i=e.callee,s=e.arguments,n=i.value,o=this.resolveFunction(n),a=s.map(p=>this.visit(p));return o==null?void 0:o(a,this)}visitBinaryExpressionAST(e){let i=e.left,s=e.right,n=e.operation,o=this.visit(i),a=this.visit(s);switch(n.type){case r.plus:return l(o)&&l(a)?o+a:`${o}${a}`;case r.minus:if(l(o)&&l(a))return o-a;throw new Error(`Operation ${n.value} not allowed no num value`);case r.mult:if(l(o)&&l(a))return o*a;if(T(o)&&l(a))return o.repeat(a);if(l(o)&&T(a))return a.repeat(o);throw new Error(`Operation ${n.value} not allowed no num value`);case r.div:if(l(o)&&l(a)){if(a===0)throw new Error("Invalid division by 0");return o/a}throw new Error(`Operation ${n.value} not allowed no num value`);case r.mod:if(l(o)&&l(a))return o%a;throw new Error(`Operation ${n.value} not allowed no num value`);case r.great:if(l(o)&&l(a))return o>a;throw new Error(`Operation ${n.value} not allowed no num value`);case r.greatEq:if(l(o)&&l(a))return o>=a;throw new Error(`Operation ${n.value} not allowed no num value`);case r.less:if(l(o)&&l(a))return o<a;throw new Error(`Operation ${n.value} not allowed no num value`);case r.lessEq:if(l(o)&&l(a))return o<=a;throw new Error(`Operation ${n.value} not allowed no num value`);case r.eqeq:return o===a;case r.notEq:return o!==a;default:throw new Error(`Operation ${n.value} not allowed no num value`)}}visitUnaryExpressionAST(e){let i=e.argument,s=e.operation,n=this.visit(i);if(s.type===r.not)return f(n)===!1;if(!l(n))throw new Error(`Operation ${s.value} not allowed no num value`);if(s.type===r.plus)return n;if(s.type===r.minus)return-n;throw new Error(`Operation ${s.value} not allowed no num value`)}visitIfStatementAST(e){let i=e.test,s=this.visit(i);return f(s)?this.visit(e.consequent):e.alternate?this.visit(e.alternate):null}visitLogicalExpressionAST(e){let i=e.left,s=this.visit(i);if(e.operator.type===r.nullity)return s!=null?s:this.visit(e.right);let n=f(s);return e.operator.type===r.and?n?f(this.visit(e.right)):!1:e.operator.type===r.or?n?!0:f(this.visit(e.right)):null}visitIndexAccessorAST(e){var n,o;let i=e.owner,s=this.visit(i);if(s==null)return null;if(typeof s=="object"){let a=this.visit(e.key);return(n=s[a])!=null?n:null}else if(Array.isArray(s)){let a=this.visit(e.key);return l(a)&&s.length<a&&a>=0&&(o=s[a])!=null?o:null}return null}visitObjectProperty(e){}visitObjectExpression(e){let i={},s=e;for(let n of s.properties)if(n instanceof k){let o=this.visit(n.key);o=T(o)?o:o.toString(),i[o]=this.visit(n.value)}return i}visitArrayExpression(e){let i=e;return this.resolveArguments(i.elements)}visitBreakAST(e){return new A}visitWhileLoopStatement(e){this.log(`WhileLoopStatement ${e.test} ${e.body}`);let i=e.retain?[]:void 0;for(;f(this.visit(e.test));){let s=this.visit(e.body);if(s instanceof A)break;if(!(s instanceof w)){if(s instanceof v)return this.popScope(),s;i==null||i.push(s)}}return i!=null?i:null}visitForLoopStatement(e){let i=e.retain?[]:void 0,s=e.init.identifier;if(!(s instanceof b))throw new Error("Unexpected identifer found");this.pushScope("ForLoopStatement");let n=this.visit(e.init.init);this.changeVariable(s.value,n);let o=()=>{let p=this.visit(e.test);if(l(p)){let S=this.resolve(s.value);return e.direction.type===r.up?p>=S:p<=S}return f(p)},a=()=>{let p=this.visit(e.update);if(l(p)){let S=this.resolve(s.value);if(!l(S))throw Error("Cant update value");this.changeVariable(s.value,e.direction.type===r.up?S+p:S-p)}else throw Error("Update value cant be non number")};for(;o();){let p=this.visit(e.body);if(p instanceof A)break;if(p instanceof w){a();continue}if(p instanceof v)return this.popScope(),p;i==null||i.push(p),a()}return this.popScope(),i!=null?i:null}visitForOfStatement(e){let i=this.visit(e.collection);if(!Array.isArray(i))throw Error("Can iterate non array object");let s=e.retain?[]:void 0;this.pushScope("ForOfStatement");for(let n of i){this._declareForIdentifier(e.identifier,n);let o=this.visit(e.body);if(o instanceof A)break;if(!(o instanceof w)){if(o instanceof v)return this.popScope(),o;s==null||s.push(o)}}return this.popScope(),s!=null?s:null}visitContinueAST(e){return new w}visitReturnAST(e){return new v(e.value!=null?this.visit(e.value):null)}_declareForIdentifier(e,i){if(e instanceof D){if(!Array.isArray(i))throw Error("Unable to make a tuple from non Array element");this.changeVariable(e.first.value,i[0]),this.changeVariable(e.second.value,i[1])}this.changeVariable(e.value,i)}resolveArguments(e){return e.map(i=>this.visit(i))}setFunctionResolver(e){this._functionResolver=e}resolveFunction(e){var i,s;return(s=this._functionsRegistry[e])!=null?s:(i=this._functionResolver)==null?void 0:i.call(this,e)}registerFunction(e,i){this._functionsRegistry[e]=i}unregisterFunction(e){delete this._functionsRegistry[e]}execute(e,i=!0,s){let n=_.compile(e,i);return s&&Object.keys(s).forEach(o=>this.changeVariable(o,s[o])),this.visit(n)}static compile(e,i=!1){let s=it(e);if(i&&this._cache.has(s))return this._cache.get(s);let n=new X(e),a=new Y(n).parse();return i&&this._cache.set(s,a),a}static register(e,i){_._globalFunctionsRegistry[e]=i}static unregister(e){delete _._globalFunctionsRegistry[e]}static run(e,i=!1,s){let n=new _,o=_.compile(e,i);return s&&Object.keys(s).forEach(a=>n.changeVariable(a,s[a])),n.visit(o)}static newInstance(){return new _}clone(){var e=new _;return e._functionsRegistry={...this._functionsRegistry},e.rootScope.memory={...this.rootScope.memory},e}newAsyncInstance(){let e=et.newInstance();return e.rootScope.memory=this.rootScope.memory,e._functionsRegistry=this._functionsRegistry,e}};_._globalFunctionsRegistry={},_._cache=new Map;var O=_,et=class u extends O{constructor(){super()}async visitRootAST(t){var s;let e=t.body,i;for(let n of e)if(i=await this.visit(n),i instanceof v)return(s=i.value)!=null?s:null;return i!=null?i:null}async visitBlockStatementAST(t){let e=t.body,i;this.pushScope("Block");for(let s of e)if(i=await this.visit(s),i instanceof C||i instanceof v)break;return this.popScope(),i}async visitIdentifierAST(t){let e=t.value;return this.resolve(e)}async visitLiteralAST(t){return t.value}async visitAssignmentExpressionAST(t){let e=t.identifier,i=t.init,s=null;if(e instanceof x){var n=await this.visit(e.owner);s=await this.visit(t.init);var o=await this.visit(e.key);this.assignProperty(n,o,s)}else e instanceof b&&(s=await this.visit(i),this.changeVariable(e.value,s));return s}async visitExpressionStatementAST(t){let e=t.expression;return await this.visit(e)}async visitCallExpressionAST(t){let e=t.callee,i=t.arguments,s=e.value,n=this._functionsRegistry[s],o=await Promise.all(i.map(a=>this.visit(a)));return await(n==null?void 0:n(o,this))}async visitBinaryExpressionAST(t){let e=t.left,i=t.right,s=t.operation,n=await this.visit(e),o=await this.visit(i);switch(s.type){case r.plus:return l(n)&&l(o)?n+o:`${n}${o}`;case r.minus:if(l(n)&&l(o))return n-o;if(T(n)&&l(o))return n.repeat(o);if(l(n)&&T(o))return o.repeat(n);throw new Error(`Operation ${s.value} not allowed no num value`);case r.mult:if(l(n)&&l(o))return n*o;throw new Error(`Operation ${s.value} not allowed no num value`);case r.div:if(l(n)&&l(o)){if(o===0)throw new Error("Invalid division by 0");return n/o}throw new Error(`Operation ${s.value} not allowed no num value`);case r.mod:if(l(n)&&l(o))return n%o;throw new Error(`Operation ${s.value} not allowed no num value`);case r.great:if(l(n)&&l(o))return n>o;throw new Error(`Operation ${s.value} not allowed no num value`);case r.greatEq:if(l(n)&&l(o))return n>=o;throw new Error(`Operation ${s.value} not allowed no num value`);case r.less:if(l(n)&&l(o))return n<o;throw new Error(`Operation ${s.value} not allowed no num value`);case r.lessEq:if(l(n)&&l(o))return n<=o;throw new Error(`Operation ${s.value} not allowed no num value`);case r.eqeq:return n===o;case r.notEq:return n!==o;default:throw new Error(`Operation ${s.value} not allowed no num value`)}}async visitUnaryExpressionAST(t){let e=t.argument,i=t.operation,s=await this.visit(e);if(i.type===r.not)return f(s)===!1;if(!l(s))throw new Error(`Operation ${i.value} not allowed no num value`);if(i.type===r.plus)return s;if(i.type===r.minus)return-s;throw new Error(`Operation ${i.value} not allowed no num value`)}async visitIfStatementAST(t){let e=t.test,i=await this.visit(e);return f(i)?await this.visit(t.consequent):t.alternate?await this.visit(t.alternate):null}async visitLogicalExpressionAST(t){let e=t.left,i=await this.visit(e);if(t.operator.type===r.nullity)return i!=null?i:await this.visit(t.right);let s=f(i);return t.operator.type===r.and?s?f(await this.visit(t.right)):!1:t.operator.type===r.or?s?!0:f(await this.visit(t.right)):!1}async visitIndexAccessorAST(t){var s,n;let e=t.owner,i=await this.visit(e);if(i==null)return null;if(typeof i=="object"){let o=await this.visit(t.key);return(s=i[o])!=null?s:null}else if(Array.isArray(i)){let o=await this.visit(t.key);return l(o)&&i.length<o&&o>=0&&(n=i[o])!=null?n:null}return null}async visitObjectProperty(t){}async visitObjectExpression(t){let e={},i=t;for(let s of i.properties)if(s instanceof k){let n=await this.visit(s.key);n=T(n)?n:n.toString(),e[n]=await this.visit(s.value)}return e}async visitArrayExpression(t){let e=t;return await this.resolveArgumentsAsync(e.elements)}async visitWhileLoopStatement(t){this.log(`WhileLoopStatement ${t.test} ${t.body}`);let e=t.retain?[]:void 0;for(;f(await this.visit(t.test));){let i=await this.visit(t.body);if(i instanceof A)break;if(!(i instanceof w)){if(i instanceof v)return this.popScope(),i;e==null||e.push(i)}}return e}async visitForLoopStatement(t){let e=t.retain?[]:void 0,i=t.init.identifier;if(!(i instanceof b))throw new Error("Unexpected identifer found");this.pushScope("ForLoopStatement");let s=await this.visit(t.init.init);this.changeVariable(i.value,s);let n=async()=>{let a=await this.visit(t.test);if(l(a)){let p=this.resolve(i.value);return t.direction.type===r.up?a>=p:a<=p}return f(a)},o=async()=>{let a=await this.visit(t.update);if(l(a)){let p=this.resolve(i.value);if(!l(p))throw Error("Cant update value");this.changeVariable(i.value,t.direction.type===r.up?p+a:p-a)}else throw Error("Update value cant be non number")};for(;await n();){let a=await this.visit(t.body);if(a instanceof A)break;if(a instanceof w){await o();continue}if(a instanceof v)return this.popScope(),a;e==null||e.push(a),await o()}return this.popScope(),e!=null?e:null}async visitForOfStatement(t){let e=await this.visit(t.collection);if(!Array.isArray(e))throw Error("Can iterate non array object");let i=t.retain?[]:void 0;this.pushScope("ForOfStatement");for(let s of e){this._declareForIdentifier(t.identifier,s);let n=await this.visit(t.body);if(n instanceof A)break;if(!(n instanceof w)){if(n instanceof v)return this.popScope(),n;i==null||i.push(n)}}return this.popScope(),i}async visitReturnAST(t){return new v(t.value!=null?await this.visit(t.value):null)}async resolveArgumentsAsync(t){return await Promise.all(t.map(async e=>await this.visit(e)))}registerFunction(t,e){this._functionsRegistry[t]=e}unregisterFunction(t){delete this._functionsRegistry[t]}async execute(t,e=!0,i){let s=O.compile(t,e);return i&&Object.keys(i).forEach(n=>this.changeVariable(n,i[n])),await this.visit(s)}static async run(t,e=!1,i){let s=new u,n=O.compile(t,e);return i&&Object.keys(i).forEach(o=>s.changeVariable(o,i[o])),await s.visit(n)}static newInstance(){return new u}clone(){var t=new u;return t._functionsRegistry={...this._functionsRegistry},t.rootScope.memory={...this.rootScope.memory},t}};export{d as AST,N as ArrayExpression,E as AssignmentExpressionAST,V as BinaryExpressionAST,$ as BlockStatementAST,H as BreakAST,A as BreakBranch,R as CallExpressionAST,z as ContinueAST,w as ContinueBranch,G as ExpressionStatementAST,M as ForLoopStatement,W as ForOfStatement,b as IdentifierAST,B as IfStatementAST,x as IndexAccessorAST,L as LexerDictionary,y as LiteralAST,P as LogicalExpressionAST,C as LoopControl,Q as MEventScope,O as MEvento,et as MEventoAsync,tt as NodeVisitor,j as ObjectExpression,k as ObjectProperty,K as ReturnAST,v as ReturnBranch,F as RootAST,h as Token,r as TokenType,D as TupleExpression,q as UnaryExpressionAST,U as WhileLoopStatement};
|
package/package.json
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"MEvent",
|
|
10
|
-
"Event code"
|
|
11
|
-
],
|
|
12
|
-
"module": "fesm2015/mevento.mjs",
|
|
13
|
-
"es2020": "fesm2020/mevento.mjs",
|
|
14
|
-
"esm2020": "esm2020/mevento.mjs",
|
|
15
|
-
"fesm2020": "fesm2020/mevento.mjs",
|
|
16
|
-
"fesm2015": "fesm2015/mevento.mjs",
|
|
17
|
-
"typings": "index.d.ts",
|
|
18
|
-
"exports": {
|
|
19
|
-
"./package.json": {
|
|
20
|
-
"default": "./package.json"
|
|
2
|
+
"name": "mevento",
|
|
3
|
+
"version": "3.0.1",
|
|
4
|
+
"main": "dist/cjs/index.js",
|
|
5
|
+
"module": "dist/esm/index.js",
|
|
6
|
+
"types": "dist/esm/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsup"
|
|
21
9
|
},
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
10
|
+
"files": [
|
|
11
|
+
"/dist"
|
|
12
|
+
],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"tslib": "^2.0.3"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": "./dist/cjs/index.js",
|
|
19
|
+
"import": "./dist/esm/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"tsup": "^8.3.0",
|
|
24
|
+
"typescript": "^5.6.2"
|
|
29
25
|
}
|
|
30
|
-
}
|
|
31
|
-
"sideEffects": false
|
|
32
|
-
}
|
|
26
|
+
}
|
package/README.md
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
# Mevento
|
|
2
|
-
|
|
3
|
-
Mevento is a tiny VM one single file that handles `MEvento code` executions inside JS engine. `MEvento` is simple programming language that allows developers exposing an app host function, that way they have the ability to dynamically execute simple script that call host function.
|
|
4
|
-
|
|
5
|
-
The VM uses a AST Walker to execute MEvento script, so of course the performace is not its concern a lot.
|
|
6
|
-
|
|
7
|
-
## MEvento code syntax
|
|
8
|
-
Syntaxically MEvento is a c-like language, but very limited: no function declaration, no class, just assignation, function call and conditional check.
|
|
9
|
-
|
|
10
|
-
```js
|
|
11
|
-
a = 12
|
|
12
|
-
a = 23
|
|
13
|
-
b = functon1()
|
|
14
|
-
c = function2()
|
|
15
|
-
d = a + b
|
|
16
|
-
if(a == b) {
|
|
17
|
-
log('a = b')
|
|
18
|
-
} else {
|
|
19
|
-
log("a != b")
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## How to use
|
|
24
|
-
The host application can expose functions through Mevento VM that way:
|
|
25
|
-
```ts
|
|
26
|
-
import {MEvento} from 'mevento';
|
|
27
|
-
MEvento.register('log', (args) => console.log); // exposes console.log through MEvento as log function
|
|
28
|
-
MEvento.register('cos2', (args) => Math.cos);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Let's assume you want to execute a `MEvento code`:
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
import {MEvento} from 'mevento';
|
|
35
|
-
|
|
36
|
-
const mevento = MEvento.newInstance();
|
|
37
|
-
|
|
38
|
-
mevento.execute(`log("molo")`)
|
|
39
|
-
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
`MEvento` instance execution is syncrhrone, that's to say, if you wan to consume `async` function exposed through Mevento, you nee to use `MEventoAsync` instead.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
```ts
|
|
46
|
-
import {MEvento} from 'mevento';
|
|
47
|
-
|
|
48
|
-
MEvento.register('async', async (args) => await asyncF(args[0]));
|
|
49
|
-
const mevento = MEventoAsync.newInstance();
|
|
50
|
-
|
|
51
|
-
await mevento.execute(`async("molo")`)
|
|
52
|
-
|
|
53
|
-
```
|
|
54
|
-
`execute` method on `MEventoAsync` instance return a `Promise`.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
## Notes
|
|
58
|
-
MEvento does not have scope variables, all variables are visible everywhere.
|
|
59
|
-
|