naider 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/naide.js +38 -5
- package/lsp/server.js +140 -0
- package/package.json +1 -1
- package/src/generator-bun.js +513 -0
- package/src/generator-python.js +1449 -0
- package/src/index.js +55 -1
package/src/index.js
CHANGED
|
@@ -4,7 +4,23 @@ import { Generator } from './generator.js';
|
|
|
4
4
|
import { preprocess } from './preprocess.js';
|
|
5
5
|
import { TypeChecker } from './typechecker.js';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
async function getGenerator(target, options) {
|
|
8
|
+
switch (target) {
|
|
9
|
+
case 'python':
|
|
10
|
+
case 'py': {
|
|
11
|
+
const { PythonGenerator } = await import('./generator-python.js');
|
|
12
|
+
return new PythonGenerator(options);
|
|
13
|
+
}
|
|
14
|
+
case 'bun': {
|
|
15
|
+
const { BunGenerator } = await import('./generator-bun.js');
|
|
16
|
+
return new BunGenerator(options);
|
|
17
|
+
}
|
|
18
|
+
default:
|
|
19
|
+
return new Generator(options);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function compile(source, { mode = 'naide', runtimePath, sourceFile, typeCheck = false, target = 'node' } = {}) {
|
|
8
24
|
let processedSource = source;
|
|
9
25
|
if (mode === 'x') {
|
|
10
26
|
processedSource = preprocess(source);
|
|
@@ -42,6 +58,44 @@ export function compile(source, { mode = 'naide', runtimePath, sourceFile, typeC
|
|
|
42
58
|
}
|
|
43
59
|
}
|
|
44
60
|
|
|
61
|
+
export async function compileAsync(source, { mode = 'naide', runtimePath, sourceFile, typeCheck = false, target = 'node' } = {}) {
|
|
62
|
+
let processedSource = source;
|
|
63
|
+
if (mode === 'x') {
|
|
64
|
+
processedSource = preprocess(source);
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const lexer = new Lexer(processedSource);
|
|
68
|
+
const tokens = lexer.tokenize();
|
|
69
|
+
const parser = new Parser(tokens);
|
|
70
|
+
const ast = parser.parse();
|
|
71
|
+
|
|
72
|
+
let typeErrors = null;
|
|
73
|
+
if (typeCheck) {
|
|
74
|
+
const checker = new TypeChecker();
|
|
75
|
+
typeErrors = checker.check(ast);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const generator = await getGenerator(target, { runtimePath, sourceFile });
|
|
79
|
+
const code = generator.generate(ast);
|
|
80
|
+
return { js: code, code, ast, tokens, sourceMap: generator.sourceMap, naide: mode === 'x' ? processedSource : null, typeErrors, target };
|
|
81
|
+
} catch (e) {
|
|
82
|
+
const lineMatch = e.message.match(/line (\d+)/);
|
|
83
|
+
if (lineMatch) {
|
|
84
|
+
const lineNum = parseInt(lineMatch[1]);
|
|
85
|
+
const lines = processedSource.split('\n');
|
|
86
|
+
const start = Math.max(0, lineNum - 3);
|
|
87
|
+
const end = Math.min(lines.length, lineNum + 2);
|
|
88
|
+
const context = lines.slice(start, end).map((l, i) => {
|
|
89
|
+
const num = start + i + 1;
|
|
90
|
+
const marker = num === lineNum ? ' >> ' : ' ';
|
|
91
|
+
return `${marker}${num} | ${l}`;
|
|
92
|
+
}).join('\n');
|
|
93
|
+
e.message += `\n\n${context}\n`;
|
|
94
|
+
}
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
45
99
|
export function transpile(source, opts) {
|
|
46
100
|
return compile(source, opts).js;
|
|
47
101
|
}
|