jpsx 0.1.16

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.
Files changed (45) hide show
  1. package/README.md +242 -0
  2. package/dist/api/__tests__/compile.test.d.ts +2 -0
  3. package/dist/api/__tests__/compile.test.d.ts.map +1 -0
  4. package/dist/api/__tests__/compile.test.js +336 -0
  5. package/dist/api/__tests__/runtime.test.d.ts +2 -0
  6. package/dist/api/__tests__/runtime.test.d.ts.map +1 -0
  7. package/dist/api/__tests__/runtime.test.js +275 -0
  8. package/dist/api/advanced.d.ts +100 -0
  9. package/dist/api/advanced.d.ts.map +1 -0
  10. package/dist/api/advanced.js +192 -0
  11. package/dist/api/benchmark.d.ts +87 -0
  12. package/dist/api/benchmark.d.ts.map +1 -0
  13. package/dist/api/benchmark.js +147 -0
  14. package/dist/api/index.d.ts +88 -0
  15. package/dist/api/index.d.ts.map +1 -0
  16. package/dist/api/index.js +304 -0
  17. package/dist/ast/types.d.ts +141 -0
  18. package/dist/ast/types.d.ts.map +1 -0
  19. package/dist/ast/types.js +1 -0
  20. package/dist/cli/index.d.ts +3 -0
  21. package/dist/cli/index.d.ts.map +1 -0
  22. package/dist/cli/index.js +155 -0
  23. package/dist/cli.js +30 -0
  24. package/dist/generator/generator.d.ts +3 -0
  25. package/dist/generator/generator.d.ts.map +1 -0
  26. package/dist/generator/generator.js +175 -0
  27. package/dist/lexer/lexer.d.ts +3 -0
  28. package/dist/lexer/lexer.d.ts.map +1 -0
  29. package/dist/lexer/lexer.js +23 -0
  30. package/dist/lexer/tokenizer.d.ts +9 -0
  31. package/dist/lexer/tokenizer.d.ts.map +1 -0
  32. package/dist/lexer/tokenizer.js +240 -0
  33. package/dist/parser/grammar.d.ts +29 -0
  34. package/dist/parser/grammar.d.ts.map +1 -0
  35. package/dist/parser/grammar.js +312 -0
  36. package/dist/parser/parser.d.ts +4 -0
  37. package/dist/parser/parser.d.ts.map +1 -0
  38. package/dist/parser/parser.js +47 -0
  39. package/dist/runtime/index.d.ts +24 -0
  40. package/dist/runtime/index.d.ts.map +1 -0
  41. package/dist/runtime/index.js +108 -0
  42. package/dist/transformer/transformer.d.ts +3 -0
  43. package/dist/transformer/transformer.d.ts.map +1 -0
  44. package/dist/transformer/transformer.js +318 -0
  45. package/package.json +54 -0
@@ -0,0 +1,29 @@
1
+ interface NearleyToken {
2
+ value: any;
3
+ [key: string]: any;
4
+ }
5
+ interface NearleyLexer {
6
+ reset: (chunk: string, info: any) => void;
7
+ next: () => NearleyToken | undefined;
8
+ save: () => any;
9
+ formatError: (token: never) => string;
10
+ has: (tokenType: string) => boolean;
11
+ }
12
+ interface NearleyRule {
13
+ name: string;
14
+ symbols: NearleySymbol[];
15
+ postprocess?: (d: any[], loc?: number, reject?: {}) => any;
16
+ }
17
+ type NearleySymbol = string | {
18
+ literal: any;
19
+ } | {
20
+ test: (token: any) => boolean;
21
+ };
22
+ interface Grammar {
23
+ Lexer: NearleyLexer | undefined;
24
+ ParserRules: NearleyRule[];
25
+ ParserStart: string;
26
+ }
27
+ declare const grammar: Grammar;
28
+ export default grammar;
29
+ //# sourceMappingURL=grammar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grammar.d.ts","sourceRoot":"","sources":["../../src/parser/grammar.ts"],"names":[],"mappings":"AAwDA,UAAU,YAAY;IACpB,KAAK,EAAE,GAAG,CAAC;IACX,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,UAAU,YAAY;IACpB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;IAC1C,IAAI,EAAE,MAAM,YAAY,GAAG,SAAS,CAAC;IACrC,IAAI,EAAE,MAAM,GAAG,CAAC;IAChB,WAAW,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC;IACtC,GAAG,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC;CACrC;AAED,UAAU,WAAW;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,WAAW,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,KAAK,GAAG,CAAC;CAC5D;AAED,KAAK,aAAa,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,GAAG,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,OAAO,CAAA;CAAE,CAAC;AAEnF,UAAU,OAAO;IACf,KAAK,EAAE,YAAY,GAAG,SAAS,CAAC;IAChC,WAAW,EAAE,WAAW,EAAE,CAAC;IAC3B,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,QAAA,MAAM,OAAO,EAAE,OAgQd,CAAC;AAEF,eAAe,OAAO,CAAC"}
@@ -0,0 +1,312 @@
1
+ // Generated automatically by nearley, version 2.20.1
2
+ // http://github.com/Hardmath123/nearley
3
+ // Bypasses TS6133. Allow declared but unused functions.
4
+ // @ts-ignore
5
+ function id(d) { return d[0]; }
6
+ // Helper to extract value from token
7
+ const val = (d) => d[0].value;
8
+ // Define tokens as predicates for robust matching
9
+ const tokenCheck = (type) => ({ test: (t) => t.type === type });
10
+ const NEWLINE = tokenCheck("NEWLINE");
11
+ const DEF = tokenCheck("DEF");
12
+ const INDENT = tokenCheck("INDENT");
13
+ const DEDENT = tokenCheck("DEDENT");
14
+ const IDENT = tokenCheck("IDENT");
15
+ const LPAREN = tokenCheck("LPAREN");
16
+ const RPAREN = tokenCheck("RPAREN");
17
+ const COLON = tokenCheck("COLON");
18
+ const COMMA = tokenCheck("COMMA");
19
+ const IF = tokenCheck("IF");
20
+ const ELSE = tokenCheck("ELSE");
21
+ const FOR = tokenCheck("FOR");
22
+ const IN = tokenCheck("IN");
23
+ const RANGE = tokenCheck("RANGE");
24
+ const RETURN = tokenCheck("RETURN");
25
+ const CLASS = tokenCheck("CLASS");
26
+ const TRY = tokenCheck("TRY");
27
+ const EXCEPT = tokenCheck("EXCEPT");
28
+ const LAMBDA = tokenCheck("LAMBDA");
29
+ const LBRACKET = tokenCheck("LBRACKET");
30
+ const RBRACKET = tokenCheck("RBRACKET");
31
+ const STRING = tokenCheck("STRING");
32
+ const FSTRING = tokenCheck("FSTRING");
33
+ const NUMBER = tokenCheck("NUMBER");
34
+ const TRUE = tokenCheck("TRUE");
35
+ const FALSE = tokenCheck("FALSE");
36
+ const WHILE = tokenCheck("WHILE");
37
+ const LBRACE = tokenCheck("LBRACE");
38
+ const RBRACE = tokenCheck("RBRACE");
39
+ const DOT = tokenCheck("DOT");
40
+ const IMPORT = tokenCheck("IMPORT");
41
+ const FROM = tokenCheck("FROM");
42
+ const NOT = tokenCheck("NOT");
43
+ const AND = tokenCheck("AND");
44
+ const OR = tokenCheck("OR");
45
+ const EXPORT = tokenCheck("EXPORT");
46
+ const AS = tokenCheck("AS");
47
+ // Custom token checks for operators
48
+ const OP = { test: (t) => t.type === "OP" && t.value !== "=" && !["+=", "-=", "*=", "/="].includes(t.value) };
49
+ const ASSIGN = { test: (t) => t.type === "OP" && t.value === "=" };
50
+ const AUG_ASSIGN = { test: (t) => t.type === "OP" && ["+=", "-=", "*=", "/="].includes(t.value) };
51
+ ;
52
+ ;
53
+ ;
54
+ ;
55
+ const grammar = {
56
+ Lexer: undefined,
57
+ ParserRules: [
58
+ { "name": "program", "symbols": ["statements"], "postprocess": (d) => ({ type: "Program", body: d[0] }) },
59
+ { "name": "lambda_expr", "symbols": [LAMBDA, "_", "func_params", "_", COLON, "_", "expression"], "postprocess": (d) => ({
60
+ type: "LambdaExpression",
61
+ params: d[2],
62
+ body: d[6]
63
+ }) },
64
+ { "name": "statements", "symbols": ["statement"], "postprocess": (d) => [d[0]] },
65
+ { "name": "statements", "symbols": ["statement", "_", "statements"], "postprocess": (d) => [d[0], ...d[2]] },
66
+ { "name": "statement", "symbols": ["function_def"], "postprocess": id },
67
+ { "name": "statement", "symbols": ["class_def"], "postprocess": id },
68
+ { "name": "statement", "symbols": ["if_stmt"], "postprocess": id },
69
+ { "name": "statement", "symbols": ["while_stmt"], "postprocess": id },
70
+ { "name": "statement", "symbols": ["for_stmt"], "postprocess": id },
71
+ { "name": "statement", "symbols": ["try_stmt"], "postprocess": id },
72
+ { "name": "statement", "symbols": ["return_stmt"], "postprocess": id },
73
+ { "name": "statement", "symbols": ["import_stmt"], "postprocess": id },
74
+ { "name": "statement", "symbols": ["export_stmt"], "postprocess": id },
75
+ { "name": "statement", "symbols": ["expr_stmt"], "postprocess": id },
76
+ { "name": "import_stmt", "symbols": [IMPORT, "_", IDENT, "_", NEWLINE], "postprocess": (d) => ({
77
+ type: "ImportDeclaration",
78
+ source: d[2].value,
79
+ specifiers: [{ local: d[2].value, imported: d[2].value }]
80
+ }) },
81
+ { "name": "import_stmt", "symbols": [FROM, "_", IDENT, "_", IMPORT, "_", IDENT, "_", NEWLINE], "postprocess": (d) => ({
82
+ type: "ImportDeclaration",
83
+ source: d[2].value,
84
+ specifiers: [{ local: d[6].value, imported: d[6].value }]
85
+ }) },
86
+ { "name": "import_stmt", "symbols": [FROM, "_", IDENT, "_", IMPORT, "_", IDENT, "_", AS, "_", IDENT, "_", NEWLINE], "postprocess": (d) => ({
87
+ type: "ImportDeclaration",
88
+ source: d[2].value,
89
+ specifiers: [{ local: d[10].value, imported: d[6].value }]
90
+ }) },
91
+ { "name": "export_stmt", "symbols": [EXPORT, "_", "statement"], "postprocess": (d) => ({
92
+ type: "ExportDeclaration",
93
+ declaration: d[2]
94
+ }) },
95
+ { "name": "return_stmt", "symbols": [RETURN, "_", "expression_list", "_", NEWLINE], "postprocess": (d) => ({
96
+ type: "ReturnStatement",
97
+ argument: d[2].length > 1 ? { type: "ArrayLiteral", elements: d[2] } : d[2][0]
98
+ }) },
99
+ { "name": "return_stmt", "symbols": [RETURN, "_", NEWLINE], "postprocess": () => ({ type: "ReturnStatement", argument: null }) },
100
+ { "name": "expression_list", "symbols": ["expression"], "postprocess": (d) => [d[0]] },
101
+ { "name": "expression_list", "symbols": ["expression", "_", COMMA, "_", "expression_list"], "postprocess": (d) => [d[0], ...d[4]] },
102
+ { "name": "expr_stmt", "symbols": ["expression", "_", NEWLINE], "postprocess": (d) => ({ type: "ExpressionStatement", expression: d[0] }) },
103
+ { "name": "expression", "symbols": ["assignment_expr"], "postprocess": id },
104
+ { "name": "expression", "symbols": ["conditional_expr"], "postprocess": id },
105
+ { "name": "expression", "symbols": ["binary_expr"], "postprocess": id },
106
+ { "name": "assignment_expr", "symbols": ["target_expr", "_", ASSIGN, "_", "expression"], "postprocess": (d) => ({
107
+ type: "BinaryExpression",
108
+ operator: d[2].value,
109
+ left: d[0],
110
+ right: d[4]
111
+ }) },
112
+ { "name": "assignment_expr", "symbols": ["target_expr", "_", AUG_ASSIGN, "_", "expression"], "postprocess": (d) => ({
113
+ type: "BinaryExpression",
114
+ operator: d[2].value,
115
+ left: d[0],
116
+ right: d[4]
117
+ }) },
118
+ { "name": "target_expr", "symbols": [IDENT], "postprocess": (d) => ({ type: "Identifier", name: d[0].value }) },
119
+ { "name": "target_expr", "symbols": ["member_expr"], "postprocess": id },
120
+ { "name": "target_expr", "symbols": [IDENT, "_", COMMA, "_", "target_expr_list"], "postprocess": (d) => ({
121
+ type: "ArrayLiteral",
122
+ elements: [{ type: "Identifier", name: d[0].value }, ...d[4]]
123
+ }) },
124
+ { "name": "target_expr_list", "symbols": [IDENT], "postprocess": (d) => [{ type: "Identifier", name: d[0].value }] },
125
+ { "name": "target_expr_list", "symbols": [IDENT, "_", COMMA, "_", "target_expr_list"], "postprocess": (d) => [{ type: "Identifier", name: d[0].value }, ...d[4]] },
126
+ { "name": "function_def", "symbols": [DEF, "_", IDENT, "_", LPAREN, "_", "func_params", "_", RPAREN, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
127
+ type: "FunctionDeclaration",
128
+ name: d[2].value,
129
+ params: d[6],
130
+ body: d[16].filter((s) => s !== null)
131
+ }) },
132
+ { "name": "func_params", "symbols": [IDENT], "postprocess": (d) => [d[0].value] },
133
+ { "name": "func_params", "symbols": [IDENT, "_", COMMA, "_", "func_params"], "postprocess": (d) => [d[0].value, ...d[4]] },
134
+ { "name": "func_params", "symbols": [IDENT, "_", ASSIGN, "_", "param_value"], "postprocess": (d) => [d[0].value + "=" + d[4].value] },
135
+ { "name": "func_params", "symbols": [IDENT, "_", ASSIGN, "_", "param_value", "_", COMMA, "_", "func_params"], "postprocess": (d) => [d[0].value + "=" + d[4].value, ...d[8]] },
136
+ { "name": "func_params", "symbols": [], "postprocess": () => [] },
137
+ { "name": "param_value", "symbols": ["number_literal"], "postprocess": id },
138
+ { "name": "param_value", "symbols": ["string_literal"], "postprocess": id },
139
+ { "name": "param_value", "symbols": ["boolean_literal"], "postprocess": id },
140
+ { "name": "if_stmt", "symbols": [IF, "_", "expression", "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
141
+ type: "IfStatement",
142
+ test: d[2],
143
+ consequent: d[10].filter((s) => s !== null),
144
+ alternate: null
145
+ }) },
146
+ { "name": "if_stmt", "symbols": [IF, "_", "expression", "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT, "_", ELSE, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
147
+ type: "IfStatement",
148
+ test: d[2],
149
+ consequent: d[10].filter((s) => s !== null),
150
+ alternate: d[22].filter((s) => s !== null)
151
+ }) },
152
+ { "name": "for_stmt", "symbols": [FOR, "_", "loop_vars", "_", IN, "_", RANGE, "_", LPAREN, "_", "expression", "_", COMMA, "_", "expression", "_", RPAREN, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
153
+ type: "ForStatement",
154
+ iterator: d[2],
155
+ start: d[10],
156
+ end: d[14],
157
+ body: d[24].filter((s) => s !== null)
158
+ }) },
159
+ { "name": "for_stmt", "symbols": [FOR, "_", "loop_vars", "_", IN, "_", "expression", "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
160
+ type: "ForStatement",
161
+ iterator: d[2],
162
+ collection: d[6],
163
+ body: d[14].filter((s) => s !== null)
164
+ }) },
165
+ { "name": "loop_vars", "symbols": [IDENT], "postprocess": (d) => d[0].value },
166
+ { "name": "loop_vars", "symbols": [IDENT, "_", COMMA, "_", "loop_vars"], "postprocess": (d) => "[" + d[0].value + ", " + d[4].replace(/^\[|\]$/g, "") + "]" },
167
+ { "name": "try_stmt", "symbols": [TRY, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT, "_", EXCEPT, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
168
+ type: "TryStatement",
169
+ block: d[8].filter((s) => s !== null),
170
+ handler: d[20].filter((s) => s !== null)
171
+ }) },
172
+ { "name": "class_def", "symbols": [CLASS, "_", IDENT, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
173
+ type: "ClassDeclaration",
174
+ name: d[2].value,
175
+ superClass: null,
176
+ body: d[10].filter((s) => s !== null)
177
+ }) },
178
+ { "name": "class_def", "symbols": [CLASS, "_", IDENT, "_", LPAREN, "_", IDENT, "_", RPAREN, "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
179
+ type: "ClassDeclaration",
180
+ name: d[2].value,
181
+ superClass: d[6].value,
182
+ body: d[16].filter((s) => s !== null)
183
+ }) },
184
+ { "name": "conditional_expr", "symbols": ["binary_expr", "_", IF, "_", "expression", "_", ELSE, "_", "expression"], "postprocess": (d) => ({
185
+ type: "ConditionalExpression",
186
+ test: d[4],
187
+ consequent: d[0],
188
+ alternate: d[8]
189
+ }) },
190
+ { "name": "binary_expr", "symbols": ["binary_expr", "_", OP, "_", "unary_expr"], "postprocess": (d) => ({
191
+ type: "BinaryExpression",
192
+ operator: d[2].value,
193
+ left: d[0],
194
+ right: d[4]
195
+ }) },
196
+ { "name": "binary_expr", "symbols": ["binary_expr", "_", AND, "_", "unary_expr"], "postprocess": (d) => ({
197
+ type: "BinaryExpression",
198
+ operator: "and",
199
+ left: d[0],
200
+ right: d[4]
201
+ }) },
202
+ { "name": "binary_expr", "symbols": ["binary_expr", "_", OR, "_", "unary_expr"], "postprocess": (d) => ({
203
+ type: "BinaryExpression",
204
+ operator: "or",
205
+ left: d[0],
206
+ right: d[4]
207
+ }) },
208
+ { "name": "binary_expr", "symbols": ["binary_expr", "_", IN, "_", "unary_expr"], "postprocess": (d) => ({
209
+ type: "BinaryExpression",
210
+ operator: "in", // We might want a runtime helper for this
211
+ left: d[0],
212
+ right: d[4]
213
+ }) },
214
+ { "name": "binary_expr", "symbols": ["binary_expr", "_", NOT, "_", IN, "_", "unary_expr"], "postprocess": (d) => ({
215
+ type: "UnaryExpression", // Treat not in as !(in)
216
+ operator: "!",
217
+ argument: {
218
+ type: "BinaryExpression",
219
+ operator: "in",
220
+ left: d[0],
221
+ right: d[6]
222
+ }
223
+ }) },
224
+ { "name": "binary_expr", "symbols": ["unary_expr"], "postprocess": id },
225
+ { "name": "unary_expr", "symbols": [OP, "_", "unary_expr"], "postprocess": (d) => {
226
+ if (d[0].value === "-") {
227
+ return {
228
+ type: "BinaryExpression",
229
+ operator: "-",
230
+ left: { type: "NumberLiteral", value: 0 },
231
+ right: d[2]
232
+ };
233
+ }
234
+ return d[2];
235
+ } },
236
+ { "name": "unary_expr", "symbols": [NOT, "_", "unary_expr"], "postprocess": (d) => ({
237
+ type: "UnaryExpression",
238
+ operator: "!",
239
+ argument: d[2]
240
+ }) },
241
+ { "name": "unary_expr", "symbols": ["operand"], "postprocess": id },
242
+ { "name": "operand", "symbols": ["call_expr"], "postprocess": id },
243
+ { "name": "operand", "symbols": ["list_comp"], "postprocess": id },
244
+ { "name": "operand", "symbols": ["lambda_expr"], "postprocess": id },
245
+ { "name": "operand", "symbols": ["identifier"], "postprocess": id },
246
+ { "name": "operand", "symbols": ["number_literal"], "postprocess": id },
247
+ { "name": "operand", "symbols": ["string_literal"], "postprocess": id },
248
+ { "name": "operand", "symbols": ["boolean_literal"], "postprocess": id },
249
+ { "name": "operand", "symbols": ["array_literal"], "postprocess": id },
250
+ { "name": "operand", "symbols": ["object_literal"], "postprocess": id },
251
+ { "name": "operand", "symbols": ["member_expr"], "postprocess": id },
252
+ { "name": "operand", "symbols": [LPAREN, "_", "expression", "_", RPAREN], "postprocess": (d) => d[2] },
253
+ { "name": "member_expr", "symbols": ["operand", "_", DOT, "_", IDENT], "postprocess": (d) => ({
254
+ type: "MemberExpression",
255
+ object: d[0],
256
+ property: { type: "Identifier", name: d[4].value },
257
+ computed: false
258
+ }) },
259
+ { "name": "member_expr", "symbols": ["operand", "_", LBRACKET, "_", "expression", "_", RBRACKET], "postprocess": (d) => ({
260
+ type: "MemberExpression",
261
+ object: d[0],
262
+ property: d[4], // For computed access, we likely need to handle this in generator if we treat property as expression directly?
263
+ // Wait, my MemberExpression type in AST might expect Identifier for property but for computed it is Expression.
264
+ // Let's check AST type.
265
+ computed: true
266
+ }) },
267
+ { "name": "call_expr", "symbols": ["operand", "_", LPAREN, "_", "args", "_", RPAREN], "postprocess": (d) => ({
268
+ type: "CallExpression",
269
+ callee: d[0],
270
+ args: d[4]
271
+ }) },
272
+ { "name": "list_comp", "symbols": [LBRACKET, "_", "expression", "_", FOR, "_", IDENT, "_", IN, "_", "expression", "_", "opt_if_clause", "_", RBRACKET], "postprocess": (d) => ({
273
+ type: "ListComprehension",
274
+ expression: d[2],
275
+ iterator: d[6].value,
276
+ collection: d[10],
277
+ test: d[12]
278
+ }) },
279
+ { "name": "opt_if_clause", "symbols": [IF, "_", "expression"], "postprocess": (d) => d[2] },
280
+ { "name": "opt_if_clause", "symbols": [], "postprocess": () => null },
281
+ { "name": "args", "symbols": ["expression"], "postprocess": (d) => [d[0]] },
282
+ { "name": "args", "symbols": ["args", "_", COMMA, "_", "expression"], "postprocess": (d) => [...d[0], d[4]] },
283
+ { "name": "args", "symbols": [], "postprocess": () => [] },
284
+ { "name": "identifier", "symbols": [IDENT], "postprocess": (d) => ({ type: "Identifier", name: d[0].value }) },
285
+ { "name": "string_literal", "symbols": [STRING], "postprocess": (d) => ({ type: "StringLiteral", value: d[0].value }) },
286
+ { "name": "string_literal", "symbols": [FSTRING], "postprocess": (d) => ({ type: "FStringLiteral", value: d[0].value }) },
287
+ { "name": "number_literal", "symbols": [NUMBER], "postprocess": (d) => ({ type: "NumberLiteral", value: Number(d[0].value) }) },
288
+ { "name": "_", "symbols": [] },
289
+ { "name": "while_stmt", "symbols": [WHILE, "_", "expression", "_", COLON, "_", NEWLINE, "_", INDENT, "_", "statements", "_", DEDENT], "postprocess": (d) => ({
290
+ type: "WhileStatement",
291
+ test: d[2],
292
+ body: d[10].filter((s) => s !== null)
293
+ }) },
294
+ { "name": "sws", "symbols": [NEWLINE, "sws"] },
295
+ { "name": "sws", "symbols": [INDENT, "sws"] },
296
+ { "name": "sws", "symbols": [DEDENT, "sws"] },
297
+ { "name": "sws", "symbols": [] },
298
+ { "name": "boolean_literal", "symbols": [TRUE], "postprocess": () => ({ type: "BooleanLiteral", value: true }) },
299
+ { "name": "boolean_literal", "symbols": [FALSE], "postprocess": () => ({ type: "BooleanLiteral", value: false }) },
300
+ { "name": "array_literal", "symbols": [LBRACKET, "sws", "elements", "sws", RBRACKET], "postprocess": (d) => ({ type: "ArrayLiteral", elements: d[2] }) },
301
+ { "name": "array_literal", "symbols": [LBRACKET, "sws", RBRACKET], "postprocess": () => ({ type: "ArrayLiteral", elements: [] }) },
302
+ { "name": "elements", "symbols": ["expression"], "postprocess": (d) => [d[0]] },
303
+ { "name": "elements", "symbols": ["elements", "sws", COMMA, "sws", "expression"], "postprocess": (d) => [...d[0], d[4]] },
304
+ { "name": "object_literal", "symbols": [LBRACE, "sws", "properties", "sws", RBRACE], "postprocess": (d) => ({ type: "ObjectLiteral", properties: d[2] }) },
305
+ { "name": "object_literal", "symbols": [LBRACE, "sws", RBRACE], "postprocess": () => ({ type: "ObjectLiteral", properties: [] }) },
306
+ { "name": "properties", "symbols": ["property"], "postprocess": (d) => [d[0]] },
307
+ { "name": "properties", "symbols": ["properties", "sws", COMMA, "sws", "property"], "postprocess": (d) => [...d[0], d[4]] },
308
+ { "name": "property", "symbols": [STRING, "sws", COLON, "sws", "expression"], "postprocess": (d) => ({ key: d[0].value, value: d[4] }) }
309
+ ],
310
+ ParserStart: "program",
311
+ };
312
+ export default grammar;
@@ -0,0 +1,4 @@
1
+ import { Token } from "../lexer/tokenizer.js";
2
+ import { Program } from "../ast/types.js";
3
+ export declare function parse(tokens: Token[]): Program;
4
+ //# sourceMappingURL=parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser/parser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EACL,OAAO,EAYR,MAAM,iBAAiB,CAAC;AA4CzB,wBAAgB,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAa9C"}
@@ -0,0 +1,47 @@
1
+ import nearley from "nearley";
2
+ import grammar from "./grammar.js";
3
+ // Adapt our token array to Nearley's lexer interface.
4
+ function createTokenLexer(tokens) {
5
+ let state = { index: 0, tokens };
6
+ return {
7
+ reset: (_data, savedState) => {
8
+ if (savedState) {
9
+ state = savedState;
10
+ }
11
+ else {
12
+ state = { index: 0, tokens };
13
+ }
14
+ },
15
+ next: () => {
16
+ if (state.index >= state.tokens.length) {
17
+ return undefined;
18
+ }
19
+ const token = state.tokens[state.index];
20
+ state.index += 1;
21
+ return token;
22
+ },
23
+ save: () => ({ ...state }),
24
+ formatError: (_token) => "Unexpected token",
25
+ has: (name) => {
26
+ if (state.index >= state.tokens.length)
27
+ return false;
28
+ const token = state.tokens[state.index];
29
+ const match = token.type === name;
30
+ return match;
31
+ }
32
+ };
33
+ }
34
+ // Parse tokens into a Program AST using nearley.
35
+ export function parse(tokens) {
36
+ const lexer = createTokenLexer(tokens);
37
+ const grammarWithLexer = { ...grammar, Lexer: lexer };
38
+ const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammarWithLexer));
39
+ parser.feed("");
40
+ if (parser.results.length === 0) {
41
+ throw new Error("Parser produced no results.");
42
+ }
43
+ if (parser.results.length > 1) {
44
+ console.warn("Parser produced ambiguous results. Using the first one.");
45
+ }
46
+ return parser.results[0];
47
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * JPS Standard Runtime Library
3
+ * This file provides the implementations for JPS builtin functions.
4
+ */
5
+ export declare function print(...args: any[]): void;
6
+ export declare function len(obj: any): number;
7
+ export declare function range(start: number, end?: number, step?: number): number[];
8
+ export declare function sum(iterable: number[], start?: number): number;
9
+ export declare function map(fn: (x: any) => any, iterable: any[]): any[];
10
+ export declare function filter(fn: (x: any) => boolean, iterable: any[]): any[];
11
+ export declare function sorted(iterable: any[], key?: (x: any) => any, reverse?: boolean): any[];
12
+ export declare function min(iterable: any[] | any, ...args: any[]): any;
13
+ export declare function max(iterable: any[] | any, ...args: any[]): any;
14
+ export declare function str(obj: any): string;
15
+ export declare function int(obj: any): number;
16
+ export declare function float(obj: any): number;
17
+ export declare function list(iterable: any): any[];
18
+ export declare function __in__(item: any, container: any): boolean;
19
+ declare global {
20
+ interface String {
21
+ join(arr: any[]): string;
22
+ }
23
+ }
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,wBAAgB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAE1C;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAWpC;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,GAAE,MAAU,GAAG,MAAM,EAAE,CAgB7E;AAED,wBAAgB,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,KAAK,GAAE,MAAU,GAAG,MAAM,CAEjE;AAED,wBAAgB,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,CAE/D;AAED,wBAAgB,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,CAEtE;AAED,wBAAgB,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,GAAG,EAAE,OAAO,GAAE,OAAe,GAAG,GAAG,EAAE,CAU9F;AAED,wBAAgB,GAAG,CAAC,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAU9D;AAED,wBAAgB,GAAG,CAAC,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAQ9D;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAEpC;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAEpC;AAED,wBAAgB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAEtC;AAED,wBAAgB,IAAI,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,EAAE,CAEzC;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,GAAG,OAAO,CAWzD;AAED,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,MAAM;QACZ,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;KAC5B;CACJ"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * JPS Standard Runtime Library
3
+ * This file provides the implementations for JPS builtin functions.
4
+ */
5
+ export function print(...args) {
6
+ console.log(...args);
7
+ }
8
+ export function len(obj) {
9
+ if (Array.isArray(obj) || typeof obj === "string") {
10
+ return obj.length;
11
+ }
12
+ if (obj instanceof Set || obj instanceof Map) {
13
+ return obj.size;
14
+ }
15
+ if (obj && typeof obj === "object") {
16
+ return Object.keys(obj).length;
17
+ }
18
+ throw new Error(`TypeError: object of type '${typeof obj}' has no len()`);
19
+ }
20
+ export function range(start, end, step = 1) {
21
+ if (end === undefined) {
22
+ end = start;
23
+ start = 0;
24
+ }
25
+ const result = [];
26
+ if (step > 0) {
27
+ for (let i = start; i < end; i += step) {
28
+ result.push(i);
29
+ }
30
+ }
31
+ else {
32
+ for (let i = start; i > end; i += step) {
33
+ result.push(i);
34
+ }
35
+ }
36
+ return result;
37
+ }
38
+ export function sum(iterable, start = 0) {
39
+ return iterable.reduce((a, b) => a + b, start);
40
+ }
41
+ export function map(fn, iterable) {
42
+ return iterable.map(fn);
43
+ }
44
+ export function filter(fn, iterable) {
45
+ return iterable.filter(fn);
46
+ }
47
+ export function sorted(iterable, key, reverse = false) {
48
+ const result = [...iterable];
49
+ result.sort((a, b) => {
50
+ const valA = key ? key(a) : a;
51
+ const valB = key ? key(b) : b;
52
+ if (valA < valB)
53
+ return reverse ? 1 : -1;
54
+ if (valA > valB)
55
+ return reverse ? -1 : 1;
56
+ return 0;
57
+ });
58
+ return result;
59
+ }
60
+ export function min(iterable, ...args) {
61
+ if (Array.isArray(iterable)) {
62
+ return Math.min(...iterable);
63
+ }
64
+ // Handle varargs logic if needed, but JPS usually calls min([list]) or min(1,2)
65
+ // If min(1, 2, 3) -> iterable=1, args=[2,3]
66
+ if (args.length > 0) {
67
+ return Math.min(iterable, ...args);
68
+ }
69
+ return Math.min(iterable);
70
+ }
71
+ export function max(iterable, ...args) {
72
+ if (Array.isArray(iterable)) {
73
+ return Math.max(...iterable);
74
+ }
75
+ if (args.length > 0) {
76
+ return Math.max(iterable, ...args);
77
+ }
78
+ return Math.max(iterable);
79
+ }
80
+ export function str(obj) {
81
+ return String(obj);
82
+ }
83
+ export function int(obj) {
84
+ return parseInt(obj, 10);
85
+ }
86
+ export function float(obj) {
87
+ return parseFloat(obj);
88
+ }
89
+ export function list(iterable) {
90
+ return Array.from(iterable);
91
+ }
92
+ export function __in__(item, container) {
93
+ if (Array.isArray(container) || typeof container === "string") {
94
+ return container.includes(item);
95
+ }
96
+ if (container instanceof Set) {
97
+ return container.has(item);
98
+ }
99
+ if (container && typeof container === "object") {
100
+ return item in container;
101
+ }
102
+ return false;
103
+ }
104
+ if (!String.prototype.join) {
105
+ String.prototype.join = function (arr) {
106
+ return arr.join(this);
107
+ };
108
+ }
@@ -0,0 +1,3 @@
1
+ import { Program } from "../ast/types.js";
2
+ export declare function transform(program: Program): Program;
3
+ //# sourceMappingURL=transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/transformer/transformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAqD,MAAM,iBAAiB,CAAC;AAE7F,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAGnD"}