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.
- package/README.md +242 -0
- package/dist/api/__tests__/compile.test.d.ts +2 -0
- package/dist/api/__tests__/compile.test.d.ts.map +1 -0
- package/dist/api/__tests__/compile.test.js +336 -0
- package/dist/api/__tests__/runtime.test.d.ts +2 -0
- package/dist/api/__tests__/runtime.test.d.ts.map +1 -0
- package/dist/api/__tests__/runtime.test.js +275 -0
- package/dist/api/advanced.d.ts +100 -0
- package/dist/api/advanced.d.ts.map +1 -0
- package/dist/api/advanced.js +192 -0
- package/dist/api/benchmark.d.ts +87 -0
- package/dist/api/benchmark.d.ts.map +1 -0
- package/dist/api/benchmark.js +147 -0
- package/dist/api/index.d.ts +88 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +304 -0
- package/dist/ast/types.d.ts +141 -0
- package/dist/ast/types.d.ts.map +1 -0
- package/dist/ast/types.js +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +155 -0
- package/dist/cli.js +30 -0
- package/dist/generator/generator.d.ts +3 -0
- package/dist/generator/generator.d.ts.map +1 -0
- package/dist/generator/generator.js +175 -0
- package/dist/lexer/lexer.d.ts +3 -0
- package/dist/lexer/lexer.d.ts.map +1 -0
- package/dist/lexer/lexer.js +23 -0
- package/dist/lexer/tokenizer.d.ts +9 -0
- package/dist/lexer/tokenizer.d.ts.map +1 -0
- package/dist/lexer/tokenizer.js +240 -0
- package/dist/parser/grammar.d.ts +29 -0
- package/dist/parser/grammar.d.ts.map +1 -0
- package/dist/parser/grammar.js +312 -0
- package/dist/parser/parser.d.ts +4 -0
- package/dist/parser/parser.d.ts.map +1 -0
- package/dist/parser/parser.js +47 -0
- package/dist/runtime/index.d.ts +24 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +108 -0
- package/dist/transformer/transformer.d.ts +3 -0
- package/dist/transformer/transformer.d.ts.map +1 -0
- package/dist/transformer/transformer.js +318 -0
- package/package.json +54 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { compile } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Benchmark compilation performance
|
|
4
|
+
*
|
|
5
|
+
* @param source - JPS source code to compile
|
|
6
|
+
* @param options - Compilation options
|
|
7
|
+
* @param iterations - Number of iterations (default: 100)
|
|
8
|
+
* @returns Benchmark results
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const result = benchmark('print("Hello")', {}, 1000);
|
|
13
|
+
* console.log(`Average: ${result.averageTime}ms`);
|
|
14
|
+
* console.log(`Throughput: ${result.throughput} ops/sec`);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export function benchmark(source, options = {}, iterations = 100) {
|
|
18
|
+
const times = [];
|
|
19
|
+
// Warmup
|
|
20
|
+
for (let i = 0; i < 10; i++) {
|
|
21
|
+
compile(source, options);
|
|
22
|
+
}
|
|
23
|
+
// Actual benchmark
|
|
24
|
+
for (let i = 0; i < iterations; i++) {
|
|
25
|
+
const start = performance.now();
|
|
26
|
+
compile(source, options);
|
|
27
|
+
const end = performance.now();
|
|
28
|
+
times.push(end - start);
|
|
29
|
+
}
|
|
30
|
+
const totalTime = times.reduce((a, b) => a + b, 0);
|
|
31
|
+
const averageTime = totalTime / iterations;
|
|
32
|
+
const minTime = Math.min(...times);
|
|
33
|
+
const maxTime = Math.max(...times);
|
|
34
|
+
const throughput = 1000 / averageTime; // ops per second
|
|
35
|
+
return {
|
|
36
|
+
totalTime,
|
|
37
|
+
averageTime,
|
|
38
|
+
minTime,
|
|
39
|
+
maxTime,
|
|
40
|
+
iterations,
|
|
41
|
+
throughput
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Compare performance across different options
|
|
46
|
+
*
|
|
47
|
+
* @param source - JPS source code
|
|
48
|
+
* @param optionsArray - Array of options to compare
|
|
49
|
+
* @param iterations - Number of iterations per test
|
|
50
|
+
* @returns Array of benchmark results
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const results = comparePerformance(
|
|
55
|
+
* 'print("test")',
|
|
56
|
+
* [
|
|
57
|
+
* { format: 'esm', runtimeMode: 'inline' },
|
|
58
|
+
* { format: 'esm', runtimeMode: 'external' },
|
|
59
|
+
* { format: 'iife', runtimeMode: 'inline' }
|
|
60
|
+
* ]
|
|
61
|
+
* );
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function comparePerformance(source, optionsArray, iterations = 100) {
|
|
65
|
+
return optionsArray.map(options => ({
|
|
66
|
+
options,
|
|
67
|
+
result: benchmark(source, options, iterations)
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Profile compilation stages
|
|
72
|
+
*
|
|
73
|
+
* @param source - JPS source code
|
|
74
|
+
* @param options - Compilation options
|
|
75
|
+
* @returns Timing for each compilation stage
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const profile = profileCompilation('def foo(): return 42');
|
|
80
|
+
* console.log(profile);
|
|
81
|
+
* // {
|
|
82
|
+
* // tokenize: 0.5ms,
|
|
83
|
+
* // parse: 1.2ms,
|
|
84
|
+
* // transform: 0.3ms,
|
|
85
|
+
* // generate: 0.8ms,
|
|
86
|
+
* // total: 2.8ms
|
|
87
|
+
* // }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function profileCompilation(source, options = {}) {
|
|
91
|
+
const profile = {};
|
|
92
|
+
const totalStart = performance.now();
|
|
93
|
+
// Tokenize
|
|
94
|
+
const tokenizeStart = performance.now();
|
|
95
|
+
const { tokenize } = require("../lexer/tokenizer.js");
|
|
96
|
+
const tokens = tokenize(source);
|
|
97
|
+
profile.tokenize = performance.now() - tokenizeStart;
|
|
98
|
+
// Parse
|
|
99
|
+
const parseStart = performance.now();
|
|
100
|
+
const { parse } = require("../parser/parser.js");
|
|
101
|
+
const ast = parse(tokens);
|
|
102
|
+
profile.parse = performance.now() - parseStart;
|
|
103
|
+
// Transform
|
|
104
|
+
const transformStart = performance.now();
|
|
105
|
+
const { transform } = require("../transformer/transformer.js");
|
|
106
|
+
const transformed = transform(ast);
|
|
107
|
+
profile.transform = performance.now() - transformStart;
|
|
108
|
+
// Generate
|
|
109
|
+
const generateStart = performance.now();
|
|
110
|
+
const { generate } = require("../generator/generator.js");
|
|
111
|
+
generate(transformed);
|
|
112
|
+
profile.generate = performance.now() - generateStart;
|
|
113
|
+
profile.total = performance.now() - totalStart;
|
|
114
|
+
return profile;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Memory usage profiling
|
|
118
|
+
*
|
|
119
|
+
* @param source - JPS source code
|
|
120
|
+
* @param options - Compilation options
|
|
121
|
+
* @returns Memory usage information (if available)
|
|
122
|
+
*/
|
|
123
|
+
export function profileMemory(source, options = {}) {
|
|
124
|
+
if (typeof performance.memory === 'undefined') {
|
|
125
|
+
console.warn('Memory profiling not available in this environment');
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const memory = performance.memory;
|
|
129
|
+
const before = memory.usedJSHeapSize;
|
|
130
|
+
compile(source, options);
|
|
131
|
+
const after = memory.usedJSHeapSize;
|
|
132
|
+
const delta = after - before;
|
|
133
|
+
return { before, after, delta };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Format benchmark results for display
|
|
137
|
+
*/
|
|
138
|
+
export function formatBenchmarkResult(result) {
|
|
139
|
+
return [
|
|
140
|
+
`Benchmark Results (${result.iterations} iterations):`,
|
|
141
|
+
` Average: ${result.averageTime.toFixed(3)}ms`,
|
|
142
|
+
` Min: ${result.minTime.toFixed(3)}ms`,
|
|
143
|
+
` Max: ${result.maxTime.toFixed(3)}ms`,
|
|
144
|
+
` Total: ${result.totalTime.toFixed(3)}ms`,
|
|
145
|
+
` Throughput: ${result.throughput.toFixed(2)} ops/sec`
|
|
146
|
+
].join('\n');
|
|
147
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { tokenize } from "../lexer/tokenizer.js";
|
|
2
|
+
export * from "./advanced.js";
|
|
3
|
+
export { tokenize };
|
|
4
|
+
export type OutputFormat = "esm" | "cjs" | "iife" | "umd";
|
|
5
|
+
export type RuntimeMode = "inline" | "external" | "cdn" | "none";
|
|
6
|
+
export interface CompileOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Output format for the generated code
|
|
9
|
+
* - esm: ES Module (default, for modern bundlers and frameworks)
|
|
10
|
+
* - cjs: CommonJS (for Node.js)
|
|
11
|
+
* - iife: Immediately Invoked Function Expression (for direct browser use)
|
|
12
|
+
* - umd: Universal Module Definition (works everywhere)
|
|
13
|
+
*/
|
|
14
|
+
format?: OutputFormat;
|
|
15
|
+
/**
|
|
16
|
+
* How to handle the runtime library
|
|
17
|
+
* - inline: Include runtime code directly in output
|
|
18
|
+
* - external: Import from external file (default)
|
|
19
|
+
* - cdn: Import from CDN (for browser environments)
|
|
20
|
+
* - none: Don't include runtime (use when already loaded)
|
|
21
|
+
*/
|
|
22
|
+
runtimeMode?: RuntimeMode;
|
|
23
|
+
/**
|
|
24
|
+
* CDN URL for runtime (only used when runtimeMode is 'cdn')
|
|
25
|
+
*/
|
|
26
|
+
cdnUrl?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Module name for UMD/IIFE formats
|
|
29
|
+
*/
|
|
30
|
+
moduleName?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Include source map
|
|
33
|
+
*/
|
|
34
|
+
sourceMap?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Standalone mode - bundles everything without external dependencies
|
|
37
|
+
*/
|
|
38
|
+
standalone?: boolean;
|
|
39
|
+
}
|
|
40
|
+
export interface CompileResult {
|
|
41
|
+
/**
|
|
42
|
+
* Generated JavaScript code
|
|
43
|
+
*/
|
|
44
|
+
code: string;
|
|
45
|
+
/**
|
|
46
|
+
* Source map (if enabled)
|
|
47
|
+
*/
|
|
48
|
+
map?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Runtime code (if inline mode)
|
|
51
|
+
*/
|
|
52
|
+
runtime?: string;
|
|
53
|
+
/**
|
|
54
|
+
* AST for debugging
|
|
55
|
+
*/
|
|
56
|
+
ast?: any;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Compile JPS source code to JavaScript
|
|
60
|
+
*
|
|
61
|
+
* @param source - JPS source code
|
|
62
|
+
* @param options - Compilation options
|
|
63
|
+
* @returns Compiled JavaScript code and metadata
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* // For React/Vue/Svelte with bundlers
|
|
68
|
+
* const result = compile(source, { format: 'esm', runtimeMode: 'external' });
|
|
69
|
+
*
|
|
70
|
+
* // For direct browser use
|
|
71
|
+
* const result = compile(source, { format: 'iife', runtimeMode: 'inline', moduleName: 'MyApp' });
|
|
72
|
+
*
|
|
73
|
+
* // For CDN usage
|
|
74
|
+
* const result = compile(source, { format: 'esm', runtimeMode: 'cdn' });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @throws {CompilationError} When compilation fails with syntax or semantic errors
|
|
78
|
+
*/
|
|
79
|
+
export declare function compile(source: string, options?: CompileOptions): CompileResult;
|
|
80
|
+
/**
|
|
81
|
+
* Compile JPS code and return only JavaScript (simplified API)
|
|
82
|
+
*/
|
|
83
|
+
export declare function compileToJS(source: string, options?: CompileOptions): string;
|
|
84
|
+
/**
|
|
85
|
+
* Get the runtime library code
|
|
86
|
+
*/
|
|
87
|
+
export declare function getRuntime(): string;
|
|
88
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAMjD,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,CAAC;AAEpB,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,CAAC;AAC1D,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,UAAU,GAAG,KAAK,GAAG,MAAM,CAAC;AAEjE,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,YAAY,CAAC;IAEtB;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,GAAG,CAAC,EAAE,GAAG,CAAC;CACX;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,aAAa,CAmCnF;AAmND;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,MAAM,CAE5E;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAEnC"}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { tokenize } from "../lexer/tokenizer.js";
|
|
2
|
+
import { parse } from "../parser/parser.js";
|
|
3
|
+
import { transform } from "../transformer/transformer.js";
|
|
4
|
+
import { generate } from "../generator/generator.js";
|
|
5
|
+
// Re-export advanced features
|
|
6
|
+
export * from "./advanced.js";
|
|
7
|
+
export { tokenize };
|
|
8
|
+
/**
|
|
9
|
+
* Compile JPS source code to JavaScript
|
|
10
|
+
*
|
|
11
|
+
* @param source - JPS source code
|
|
12
|
+
* @param options - Compilation options
|
|
13
|
+
* @returns Compiled JavaScript code and metadata
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // For React/Vue/Svelte with bundlers
|
|
18
|
+
* const result = compile(source, { format: 'esm', runtimeMode: 'external' });
|
|
19
|
+
*
|
|
20
|
+
* // For direct browser use
|
|
21
|
+
* const result = compile(source, { format: 'iife', runtimeMode: 'inline', moduleName: 'MyApp' });
|
|
22
|
+
*
|
|
23
|
+
* // For CDN usage
|
|
24
|
+
* const result = compile(source, { format: 'esm', runtimeMode: 'cdn' });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @throws {CompilationError} When compilation fails with syntax or semantic errors
|
|
28
|
+
*/
|
|
29
|
+
export function compile(source, options = {}) {
|
|
30
|
+
const { format = "esm", runtimeMode = "external", cdnUrl = "https://cdn.jsdelivr.net/npm/jps@latest/dist/runtime/index.js", moduleName = "JPSModule", sourceMap = false, standalone = false } = options;
|
|
31
|
+
try {
|
|
32
|
+
// Validate options
|
|
33
|
+
validateOptions({ format, runtimeMode, cdnUrl, moduleName, sourceMap, standalone });
|
|
34
|
+
// Tokenize and parse
|
|
35
|
+
const tokens = tokenize(source);
|
|
36
|
+
const ast = parse(tokens);
|
|
37
|
+
const transformed = transform(ast);
|
|
38
|
+
// Generate base code
|
|
39
|
+
let code = generate(transformed);
|
|
40
|
+
// Handle different output formats and runtime modes
|
|
41
|
+
code = formatOutput(code, { format, runtimeMode, cdnUrl, moduleName, standalone });
|
|
42
|
+
const result = {
|
|
43
|
+
code,
|
|
44
|
+
ast: sourceMap ? ast : undefined
|
|
45
|
+
};
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
// Enhance error messages
|
|
50
|
+
throw createCompilationError(error, source);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function formatOutput(code, options) {
|
|
54
|
+
const { format, runtimeMode, cdnUrl, moduleName, standalone } = options;
|
|
55
|
+
// Extract runtime import and code
|
|
56
|
+
const runtimeImportMatch = code.match(/import\s+{[^}]+}\s+from\s+"[^"]+";?\n*/);
|
|
57
|
+
const runtimeImport = runtimeImportMatch ? runtimeImportMatch[0] : "";
|
|
58
|
+
const codeWithoutImport = code.replace(runtimeImportMatch?.[0] || "", "");
|
|
59
|
+
let output = "";
|
|
60
|
+
switch (format) {
|
|
61
|
+
case "esm":
|
|
62
|
+
output = formatESM(runtimeImport, codeWithoutImport, runtimeMode, cdnUrl);
|
|
63
|
+
break;
|
|
64
|
+
case "cjs":
|
|
65
|
+
output = formatCJS(runtimeImport, codeWithoutImport, runtimeMode);
|
|
66
|
+
break;
|
|
67
|
+
case "iife":
|
|
68
|
+
output = formatIIFE(runtimeImport, codeWithoutImport, runtimeMode, moduleName, cdnUrl);
|
|
69
|
+
break;
|
|
70
|
+
case "umd":
|
|
71
|
+
output = formatUMD(runtimeImport, codeWithoutImport, runtimeMode, moduleName);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
return output;
|
|
75
|
+
}
|
|
76
|
+
function formatESM(runtimeImport, code, mode, cdnUrl) {
|
|
77
|
+
switch (mode) {
|
|
78
|
+
case "external":
|
|
79
|
+
return runtimeImport.replace("./jps_runtime.js", "./jps_runtime.js") + code;
|
|
80
|
+
case "cdn":
|
|
81
|
+
return runtimeImport.replace(/from\s+"[^"]+";/, `from "${cdnUrl}";`) + code;
|
|
82
|
+
case "inline":
|
|
83
|
+
// Will be handled by bundling runtime inline
|
|
84
|
+
return `${getInlineRuntime()}\n\n${code}`;
|
|
85
|
+
case "none":
|
|
86
|
+
return code;
|
|
87
|
+
default:
|
|
88
|
+
return runtimeImport + code;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function formatCJS(runtimeImport, code, mode) {
|
|
92
|
+
// Extract imported functions
|
|
93
|
+
const match = runtimeImport.match(/import\s+{([^}]+)}/);
|
|
94
|
+
const imports = match ? match[1].trim() : "";
|
|
95
|
+
const cjsImport = mode === "none"
|
|
96
|
+
? ""
|
|
97
|
+
: `const { ${imports} } = require("./jps_runtime.js");\n\n`;
|
|
98
|
+
const cjsCode = code
|
|
99
|
+
.replace(/export\s+/g, "module.exports.")
|
|
100
|
+
.replace(/import\s+/g, "const ");
|
|
101
|
+
return cjsImport + cjsCode;
|
|
102
|
+
}
|
|
103
|
+
function formatIIFE(runtimeImport, code, mode, moduleName, cdnUrl) {
|
|
104
|
+
const match = runtimeImport.match(/import\s+{([^}]+)}/);
|
|
105
|
+
const imports = match ? match[1].split(",").map(s => s.trim()) : [];
|
|
106
|
+
if (mode === "inline") {
|
|
107
|
+
return `
|
|
108
|
+
(function(global) {
|
|
109
|
+
${getInlineRuntime()}
|
|
110
|
+
|
|
111
|
+
${code.replace(/export\s+/g, "")}
|
|
112
|
+
|
|
113
|
+
global.${moduleName} = { ${imports.join(", ")} };
|
|
114
|
+
})(typeof window !== "undefined" ? window : global);
|
|
115
|
+
`.trim();
|
|
116
|
+
}
|
|
117
|
+
return `
|
|
118
|
+
(function(global) {
|
|
119
|
+
// Import runtime from CDN or external file
|
|
120
|
+
// For browsers: <script src="${cdnUrl}"></script> should be loaded first
|
|
121
|
+
|
|
122
|
+
${code.replace(/export\s+/g, "")}
|
|
123
|
+
|
|
124
|
+
global.${moduleName} = { ${imports.join(", ")} };
|
|
125
|
+
})(typeof window !== "undefined" ? window : global);
|
|
126
|
+
`.trim();
|
|
127
|
+
}
|
|
128
|
+
function formatUMD(runtimeImport, code, mode, moduleName) {
|
|
129
|
+
const match = runtimeImport.match(/import\s+{([^}]+)}/);
|
|
130
|
+
const imports = match ? match[1].split(",").map(s => s.trim()) : [];
|
|
131
|
+
return `
|
|
132
|
+
(function (root, factory) {
|
|
133
|
+
if (typeof define === 'function' && define.amd) {
|
|
134
|
+
// AMD
|
|
135
|
+
define(['jps_runtime'], factory);
|
|
136
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
137
|
+
// CommonJS
|
|
138
|
+
module.exports = factory(require('./jps_runtime'));
|
|
139
|
+
} else {
|
|
140
|
+
// Browser globals
|
|
141
|
+
root.${moduleName} = factory(root.JPSRuntime);
|
|
142
|
+
}
|
|
143
|
+
}(typeof self !== 'undefined' ? self : this, function (runtime) {
|
|
144
|
+
const { ${imports.join(", ")} } = runtime;
|
|
145
|
+
|
|
146
|
+
${code.replace(/export\s+/g, "")}
|
|
147
|
+
|
|
148
|
+
return { ${imports.join(", ")} };
|
|
149
|
+
}));
|
|
150
|
+
`.trim();
|
|
151
|
+
}
|
|
152
|
+
function getInlineRuntime() {
|
|
153
|
+
// Inline runtime functions
|
|
154
|
+
return `
|
|
155
|
+
// JPS Runtime (inline)
|
|
156
|
+
function print(...args) {
|
|
157
|
+
console.log(...args.map(arg =>
|
|
158
|
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
159
|
+
));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function len(obj) {
|
|
163
|
+
if (Array.isArray(obj) || typeof obj === 'string') return obj.length;
|
|
164
|
+
if (typeof obj === 'object') return Object.keys(obj).length;
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function range(...args) {
|
|
169
|
+
let start = 0, stop, step = 1;
|
|
170
|
+
if (args.length === 1) stop = args[0];
|
|
171
|
+
else if (args.length === 2) [start, stop] = args;
|
|
172
|
+
else [start, stop, step] = args;
|
|
173
|
+
const result = [];
|
|
174
|
+
for (let i = start; i < stop; i += step) result.push(i);
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function sum(iterable) {
|
|
179
|
+
return Array.from(iterable).reduce((a, b) => a + b, 0);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function min(...args) {
|
|
183
|
+
const arr = args.length === 1 && Array.isArray(args[0]) ? args[0] : args;
|
|
184
|
+
return Math.min(...arr);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function max(...args) {
|
|
188
|
+
const arr = args.length === 1 && Array.isArray(args[0]) ? args[0] : args;
|
|
189
|
+
return Math.max(...arr);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function sorted(iterable, reverse = false) {
|
|
193
|
+
const arr = Array.from(iterable).sort((a, b) => a > b ? 1 : -1);
|
|
194
|
+
return reverse ? arr.reverse() : arr;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function str(val) {
|
|
198
|
+
return String(val);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function int(val) {
|
|
202
|
+
return parseInt(val, 10);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function float(val) {
|
|
206
|
+
return parseFloat(val);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function map(fn, iterable) {
|
|
210
|
+
return Array.from(iterable).map(fn);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function filter(fn, iterable) {
|
|
214
|
+
return Array.from(iterable).filter(fn);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function list(iterable) {
|
|
218
|
+
return Array.from(iterable);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function __in__(item, container) {
|
|
222
|
+
if (Array.isArray(container) || typeof container === "string") {
|
|
223
|
+
return container.includes(item);
|
|
224
|
+
}
|
|
225
|
+
if (container instanceof Set) {
|
|
226
|
+
return container.has(item);
|
|
227
|
+
}
|
|
228
|
+
if (container && typeof container === "object") {
|
|
229
|
+
return item in container;
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (!String.prototype.join) {
|
|
235
|
+
String.prototype.join = function(arr) {
|
|
236
|
+
return arr.join(this);
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
`.trim();
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Compile JPS code and return only JavaScript (simplified API)
|
|
245
|
+
*/
|
|
246
|
+
export function compileToJS(source, options) {
|
|
247
|
+
return compile(source, options).code;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Get the runtime library code
|
|
251
|
+
*/
|
|
252
|
+
export function getRuntime() {
|
|
253
|
+
return getInlineRuntime();
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Validate compilation options
|
|
257
|
+
*/
|
|
258
|
+
function validateOptions(options) {
|
|
259
|
+
const validFormats = ['esm', 'cjs', 'iife', 'umd'];
|
|
260
|
+
const validRuntimeModes = ['inline', 'external', 'cdn', 'none'];
|
|
261
|
+
if (!validFormats.includes(options.format)) {
|
|
262
|
+
throw new Error(`Invalid output format: "${options.format}". Valid options are: ${validFormats.join(', ')}`);
|
|
263
|
+
}
|
|
264
|
+
if (!validRuntimeModes.includes(options.runtimeMode)) {
|
|
265
|
+
throw new Error(`Invalid runtime mode: "${options.runtimeMode}". Valid options are: ${validRuntimeModes.join(', ')}`);
|
|
266
|
+
}
|
|
267
|
+
if (options.runtimeMode === 'cdn' && !options.cdnUrl) {
|
|
268
|
+
throw new Error('CDN URL must be provided when using runtimeMode: "cdn"');
|
|
269
|
+
}
|
|
270
|
+
if ((options.format === 'iife' || options.format === 'umd') && !options.moduleName) {
|
|
271
|
+
console.warn(`Warning: No module name specified for ${options.format} format. Using default: "JPSModule"`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Create enhanced compilation error with better messages
|
|
276
|
+
*/
|
|
277
|
+
function createCompilationError(error, source) {
|
|
278
|
+
if (error.token) {
|
|
279
|
+
// Syntax error from parser
|
|
280
|
+
const { line, col, value } = error.token;
|
|
281
|
+
const lines = source.split('\n');
|
|
282
|
+
const errorLine = lines[line - 1] || '';
|
|
283
|
+
const message = [
|
|
284
|
+
`Syntax Error at line ${line}, column ${col}:`,
|
|
285
|
+
` Unexpected token: "${value}"`,
|
|
286
|
+
'',
|
|
287
|
+
` ${line} | ${errorLine}`,
|
|
288
|
+
` ${' '.repeat(String(line).length)} | ${' '.repeat(col - 1)}^`,
|
|
289
|
+
'',
|
|
290
|
+
'Check your JPS syntax and try again.'
|
|
291
|
+
].join('\n');
|
|
292
|
+
const enhancedError = new Error(message);
|
|
293
|
+
enhancedError.token = error.token;
|
|
294
|
+
enhancedError.line = line;
|
|
295
|
+
enhancedError.column = col;
|
|
296
|
+
return enhancedError;
|
|
297
|
+
}
|
|
298
|
+
if (error.message) {
|
|
299
|
+
// General error with message
|
|
300
|
+
return new Error(`Compilation Error: ${error.message}`);
|
|
301
|
+
}
|
|
302
|
+
// Unknown error
|
|
303
|
+
return new Error(`Compilation Error: ${String(error)}`);
|
|
304
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
export type Program = {
|
|
2
|
+
type: "Program";
|
|
3
|
+
body: Statement[];
|
|
4
|
+
};
|
|
5
|
+
export type Statement = FunctionDeclaration | ForStatement | WhileStatement | IfStatement | ExpressionStatement | ReturnStatement | ClassDeclaration | TryStatement | ImportDeclaration | ExportDeclaration;
|
|
6
|
+
export type ImportDeclaration = {
|
|
7
|
+
type: "ImportDeclaration";
|
|
8
|
+
source: string;
|
|
9
|
+
specifiers: {
|
|
10
|
+
local: string;
|
|
11
|
+
imported: string;
|
|
12
|
+
}[];
|
|
13
|
+
};
|
|
14
|
+
export type ExportDeclaration = {
|
|
15
|
+
type: "ExportDeclaration";
|
|
16
|
+
declaration: Statement;
|
|
17
|
+
};
|
|
18
|
+
export type FunctionDeclaration = {
|
|
19
|
+
type: "FunctionDeclaration";
|
|
20
|
+
name: string;
|
|
21
|
+
params: string[];
|
|
22
|
+
body: Statement[];
|
|
23
|
+
};
|
|
24
|
+
export type ReturnStatement = {
|
|
25
|
+
type: "ReturnStatement";
|
|
26
|
+
argument: Expression | null;
|
|
27
|
+
};
|
|
28
|
+
export type ClassDeclaration = {
|
|
29
|
+
type: "ClassDeclaration";
|
|
30
|
+
name: string;
|
|
31
|
+
superClass: string | null;
|
|
32
|
+
body: Statement[];
|
|
33
|
+
};
|
|
34
|
+
export type TryStatement = {
|
|
35
|
+
type: "TryStatement";
|
|
36
|
+
block: Statement[];
|
|
37
|
+
handler: Statement[];
|
|
38
|
+
};
|
|
39
|
+
export type ForStatement = {
|
|
40
|
+
type: "ForStatement";
|
|
41
|
+
iterator: string;
|
|
42
|
+
start?: Expression;
|
|
43
|
+
end?: Expression;
|
|
44
|
+
collection?: Expression;
|
|
45
|
+
body: Statement[];
|
|
46
|
+
};
|
|
47
|
+
export type IfStatement = {
|
|
48
|
+
type: "IfStatement";
|
|
49
|
+
test: Expression;
|
|
50
|
+
consequent: Statement[];
|
|
51
|
+
alternate: Statement[] | null;
|
|
52
|
+
};
|
|
53
|
+
export type ExpressionStatement = {
|
|
54
|
+
type: "ExpressionStatement";
|
|
55
|
+
expression: Expression;
|
|
56
|
+
};
|
|
57
|
+
export type Expression = Identifier | StringLiteral | FStringLiteral | NumberLiteral | BooleanLiteral | ArrayLiteral | ObjectLiteral | BinaryExpression | UnaryExpression | ConditionalExpression | CallExpression | ListComprehension | MemberExpression | LambdaExpression | NewExpression;
|
|
58
|
+
export type LambdaExpression = {
|
|
59
|
+
type: "LambdaExpression";
|
|
60
|
+
params: string[];
|
|
61
|
+
body: Expression;
|
|
62
|
+
};
|
|
63
|
+
export type MemberExpression = {
|
|
64
|
+
type: "MemberExpression";
|
|
65
|
+
object: Expression;
|
|
66
|
+
property: Expression | Identifier;
|
|
67
|
+
computed: boolean;
|
|
68
|
+
};
|
|
69
|
+
export type Identifier = {
|
|
70
|
+
type: "Identifier";
|
|
71
|
+
name: string;
|
|
72
|
+
};
|
|
73
|
+
export type ListComprehension = {
|
|
74
|
+
type: "ListComprehension";
|
|
75
|
+
expression: Expression;
|
|
76
|
+
iterator: string;
|
|
77
|
+
start?: Expression;
|
|
78
|
+
end?: Expression;
|
|
79
|
+
collection?: Expression;
|
|
80
|
+
test?: Expression;
|
|
81
|
+
};
|
|
82
|
+
export type CallExpression = {
|
|
83
|
+
type: "CallExpression";
|
|
84
|
+
callee: Expression;
|
|
85
|
+
args: Expression[];
|
|
86
|
+
};
|
|
87
|
+
export type NewExpression = {
|
|
88
|
+
type: "NewExpression";
|
|
89
|
+
callee: Expression;
|
|
90
|
+
args: Expression[];
|
|
91
|
+
};
|
|
92
|
+
export type StringLiteral = {
|
|
93
|
+
type: "StringLiteral";
|
|
94
|
+
value: string;
|
|
95
|
+
};
|
|
96
|
+
export type FStringLiteral = {
|
|
97
|
+
type: "FStringLiteral";
|
|
98
|
+
value: string;
|
|
99
|
+
};
|
|
100
|
+
export type NumberLiteral = {
|
|
101
|
+
type: "NumberLiteral";
|
|
102
|
+
value: number;
|
|
103
|
+
};
|
|
104
|
+
export type BinaryExpression = {
|
|
105
|
+
type: "BinaryExpression";
|
|
106
|
+
operator: string;
|
|
107
|
+
left: Expression;
|
|
108
|
+
right: Expression;
|
|
109
|
+
};
|
|
110
|
+
export type UnaryExpression = {
|
|
111
|
+
type: "UnaryExpression";
|
|
112
|
+
operator: string;
|
|
113
|
+
argument: Expression;
|
|
114
|
+
};
|
|
115
|
+
export type ConditionalExpression = {
|
|
116
|
+
type: "ConditionalExpression";
|
|
117
|
+
test: Expression;
|
|
118
|
+
consequent: Expression;
|
|
119
|
+
alternate: Expression;
|
|
120
|
+
};
|
|
121
|
+
export type WhileStatement = {
|
|
122
|
+
type: "WhileStatement";
|
|
123
|
+
test: Expression;
|
|
124
|
+
body: Statement[];
|
|
125
|
+
};
|
|
126
|
+
export type BooleanLiteral = {
|
|
127
|
+
type: "BooleanLiteral";
|
|
128
|
+
value: boolean;
|
|
129
|
+
};
|
|
130
|
+
export type ArrayLiteral = {
|
|
131
|
+
type: "ArrayLiteral";
|
|
132
|
+
elements: Expression[];
|
|
133
|
+
};
|
|
134
|
+
export type ObjectLiteral = {
|
|
135
|
+
type: "ObjectLiteral";
|
|
136
|
+
properties: {
|
|
137
|
+
key: string;
|
|
138
|
+
value: Expression;
|
|
139
|
+
}[];
|
|
140
|
+
};
|
|
141
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/ast/types.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,EAAE,SAAS,EAAE,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,mBAAmB,GAAG,YAAY,GAAG,cAAc,GAAG,WAAW,GAAG,mBAAmB,GAAG,eAAe,GAAG,gBAAgB,GAAG,YAAY,GAAG,iBAAiB,GAAG,iBAAiB,CAAC;AAE5M,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,mBAAmB,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CACnD,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,mBAAmB,CAAC;IAC1B,WAAW,EAAE,SAAS,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,qBAAqB,CAAC;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,IAAI,EAAE,SAAS,EAAE,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,iBAAiB,CAAC;IACxB,QAAQ,EAAE,UAAU,GAAG,IAAI,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,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