eliscript 0.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/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtempSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
symlinkSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { SourceMap } from "node:module";
|
|
14
|
+
import { format } from "node:util";
|
|
15
|
+
import {
|
|
16
|
+
basename,
|
|
17
|
+
dirname,
|
|
18
|
+
isAbsolute,
|
|
19
|
+
join,
|
|
20
|
+
relative,
|
|
21
|
+
resolve,
|
|
22
|
+
sep,
|
|
23
|
+
} from "node:path";
|
|
24
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
25
|
+
|
|
26
|
+
import { printValue } from "../../runtime/core/data-text.mjs";
|
|
27
|
+
import { loadCompiler } from "./bun.mjs";
|
|
28
|
+
import { buildProject } from "./project.mjs";
|
|
29
|
+
|
|
30
|
+
export const evaluationResultFormat = "eliscript-evaluation-result";
|
|
31
|
+
export const evaluationResultVersion = 1;
|
|
32
|
+
|
|
33
|
+
const projectDirectory = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
|
|
34
|
+
const evaluationCompilerDigest = createHash("sha256")
|
|
35
|
+
.update("eliscript-evaluation-session-v1")
|
|
36
|
+
.digest("hex");
|
|
37
|
+
|
|
38
|
+
function frozen(value) {
|
|
39
|
+
return Object.freeze(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function diagnosticError(message, filename = undefined) {
|
|
43
|
+
const error = new Error(message);
|
|
44
|
+
error.eliscriptDiagnostic = {
|
|
45
|
+
format: "eliscript-diagnostic",
|
|
46
|
+
version: 1,
|
|
47
|
+
code: "ELI-E0001",
|
|
48
|
+
severity: "error",
|
|
49
|
+
phase: "evaluation-request",
|
|
50
|
+
message,
|
|
51
|
+
...(filename ? { location: { file: filename } } : {}),
|
|
52
|
+
};
|
|
53
|
+
return error;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function containedPath(path, root) {
|
|
57
|
+
const pathFromRoot = relative(root, path);
|
|
58
|
+
return pathFromRoot === "" ||
|
|
59
|
+
(!pathFromRoot.startsWith(`..${sep}`) && pathFromRoot !== ".." &&
|
|
60
|
+
!isAbsolute(pathFromRoot));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function nearestProjectRoot(filename) {
|
|
64
|
+
let directory = dirname(filename);
|
|
65
|
+
while (true) {
|
|
66
|
+
if (existsSync(join(directory, "eliscript.json"))) return directory;
|
|
67
|
+
const parent = dirname(directory);
|
|
68
|
+
if (parent === directory) return dirname(filename);
|
|
69
|
+
directory = parent;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function requestFilename(filename) {
|
|
74
|
+
return isAbsolute(filename) ? resolve(filename) : resolve(process.cwd(), filename);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function runtimeCategory(value) {
|
|
78
|
+
if (value === null) return "nil";
|
|
79
|
+
if (value === undefined) return "undefined";
|
|
80
|
+
return typeof value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function stackLocation(stack) {
|
|
84
|
+
if (typeof stack !== "string") return null;
|
|
85
|
+
const match = stack.match(
|
|
86
|
+
/(?:at\s+)?(?:file:\/\/)?([^\s()]+\.eli):(\d+):(\d+)/u,
|
|
87
|
+
);
|
|
88
|
+
if (!match) return null;
|
|
89
|
+
return {
|
|
90
|
+
file: match[1].trim(),
|
|
91
|
+
line: Number(match[2]),
|
|
92
|
+
column: Number(match[3]),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function generatedStackLocation(stack) {
|
|
97
|
+
if (typeof stack !== "string") return null;
|
|
98
|
+
const match = stack.match(
|
|
99
|
+
/(?:at\s+)?(?:file:\/\/)?([^\s()?]+\.mjs)(?:[?#][^\s():]*)?:(\d+):(\d+)/u,
|
|
100
|
+
);
|
|
101
|
+
if (!match) return null;
|
|
102
|
+
const generatedFile = decodeURIComponent(match[1].trim());
|
|
103
|
+
const mapFile = `${generatedFile}.map`;
|
|
104
|
+
if (!existsSync(mapFile)) return null;
|
|
105
|
+
try {
|
|
106
|
+
const sourceMap = new SourceMap(JSON.parse(readFileSync(mapFile, "utf8")));
|
|
107
|
+
const entry = sourceMap.findEntry(Number(match[2]) - 1, Number(match[3]) - 1);
|
|
108
|
+
if (!entry?.originalSource) return null;
|
|
109
|
+
const originalSource = entry.originalSource.startsWith("file://")
|
|
110
|
+
? fileURLToPath(entry.originalSource)
|
|
111
|
+
: entry.originalSource;
|
|
112
|
+
return {
|
|
113
|
+
file: isAbsolute(originalSource)
|
|
114
|
+
? originalSource
|
|
115
|
+
: resolve(dirname(mapFile), originalSource),
|
|
116
|
+
line: entry.originalLine + 1,
|
|
117
|
+
column: entry.originalColumn + 1,
|
|
118
|
+
};
|
|
119
|
+
} catch {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function remapPoint(point, adjustment) {
|
|
125
|
+
const relativeLine = point.line - adjustment.expressionLine + 1;
|
|
126
|
+
if (relativeLine < 1) return point;
|
|
127
|
+
return {
|
|
128
|
+
...point,
|
|
129
|
+
line: adjustment.line + relativeLine - 1,
|
|
130
|
+
column: relativeLine === 1
|
|
131
|
+
? adjustment.column + Math.max(0, point.column - adjustment.expressionColumn)
|
|
132
|
+
: point.column,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function remapLocation(location, adjustment) {
|
|
137
|
+
if (!location || !adjustment || location.file !== adjustment.syntheticFilename) {
|
|
138
|
+
return location;
|
|
139
|
+
}
|
|
140
|
+
if (location.start && typeof location.start === "object") {
|
|
141
|
+
return {
|
|
142
|
+
...location,
|
|
143
|
+
file: adjustment.filename,
|
|
144
|
+
start: remapPoint(location.start, adjustment),
|
|
145
|
+
...(location.end
|
|
146
|
+
? { end: remapPoint(location.end, adjustment) }
|
|
147
|
+
: {}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return { ...remapPoint(location, adjustment), file: adjustment.filename };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function normalizeDiagnostic(error, adjustment = undefined) {
|
|
154
|
+
const existing = error?.eliscriptDiagnostic;
|
|
155
|
+
if (existing && typeof existing === "object") {
|
|
156
|
+
const location = remapLocation(existing.location, adjustment);
|
|
157
|
+
return frozen({
|
|
158
|
+
...existing,
|
|
159
|
+
...(location ? { location: frozen({ ...location }) } : {}),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const location = remapLocation(
|
|
163
|
+
stackLocation(error?.stack) ?? generatedStackLocation(error?.stack),
|
|
164
|
+
adjustment,
|
|
165
|
+
);
|
|
166
|
+
return frozen({
|
|
167
|
+
format: "eliscript-diagnostic",
|
|
168
|
+
version: 1,
|
|
169
|
+
code: "ELI-E0002",
|
|
170
|
+
severity: "error",
|
|
171
|
+
phase: "evaluation-runtime",
|
|
172
|
+
message: error instanceof Error ? error.message : String(error),
|
|
173
|
+
...(location ? { location: frozen(location) } : {}),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function resultBase(request, revision) {
|
|
178
|
+
return {
|
|
179
|
+
format: evaluationResultFormat,
|
|
180
|
+
version: evaluationResultVersion,
|
|
181
|
+
id: request?.id ?? null,
|
|
182
|
+
operation: request?.operation ?? "unknown",
|
|
183
|
+
revision,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function success(request, revision, details) {
|
|
188
|
+
return frozen({
|
|
189
|
+
...resultBase(request, revision),
|
|
190
|
+
status: "ok",
|
|
191
|
+
...details,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function appendCapturedOutput(result, stdout) {
|
|
196
|
+
if (stdout === "") return result;
|
|
197
|
+
return frozen({ ...result, stdout });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function captureStandardOutput(action) {
|
|
201
|
+
const write = process.stdout.write;
|
|
202
|
+
const consoleMethods = new Map();
|
|
203
|
+
let stdout = "";
|
|
204
|
+
process.stdout.write = function capture(chunk, encoding, callback) {
|
|
205
|
+
stdout += Buffer.isBuffer(chunk)
|
|
206
|
+
? chunk.toString(typeof encoding === "string" ? encoding : "utf8")
|
|
207
|
+
: String(chunk);
|
|
208
|
+
const done = typeof encoding === "function" ? encoding : callback;
|
|
209
|
+
if (typeof done === "function") queueMicrotask(done);
|
|
210
|
+
return true;
|
|
211
|
+
};
|
|
212
|
+
for (const name of ["debug", "info", "log"]) {
|
|
213
|
+
consoleMethods.set(name, console[name]);
|
|
214
|
+
console[name] = (...values) => {
|
|
215
|
+
stdout += `${format(...values)}\n`;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
return { value: await action(), stdout };
|
|
220
|
+
} catch (error) {
|
|
221
|
+
error.eliscriptCapturedStdout = stdout;
|
|
222
|
+
throw error;
|
|
223
|
+
} finally {
|
|
224
|
+
process.stdout.write = write;
|
|
225
|
+
for (const [name, method] of consoleMethods) console[name] = method;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function failure(rawRequest, request, revision, error, adjustment = undefined) {
|
|
230
|
+
const rawId = rawRequest && typeof rawRequest === "object" ? rawRequest.id : null;
|
|
231
|
+
const id = request?.id ?? (
|
|
232
|
+
(typeof rawId === "string" && rawId.length > 0) ||
|
|
233
|
+
(Number.isSafeInteger(rawId) && rawId >= 0)
|
|
234
|
+
? rawId
|
|
235
|
+
: null
|
|
236
|
+
);
|
|
237
|
+
return frozen({
|
|
238
|
+
...resultBase({
|
|
239
|
+
id,
|
|
240
|
+
operation: request?.operation ?? (
|
|
241
|
+
typeof rawRequest?.operation === "string" ? rawRequest.operation : "unknown"
|
|
242
|
+
),
|
|
243
|
+
}, revision),
|
|
244
|
+
status: "error",
|
|
245
|
+
diagnostic: normalizeDiagnostic(error, adjustment),
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function namespaceSource(source, description) {
|
|
250
|
+
const exported = new Set(description.exports);
|
|
251
|
+
const missing = description.bindings.filter((name) => !exported.has(name));
|
|
252
|
+
if (missing.length === 0) return source;
|
|
253
|
+
return `${source}\n(export ${missing.join(" ")})\n`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function expressionSource(request, namespace) {
|
|
257
|
+
const prelude = [...(namespace?.macroSources ?? [])];
|
|
258
|
+
if (namespace?.bindings.length > 0) {
|
|
259
|
+
prelude.push(`(import ${JSON.stringify(namespace.url)} ${
|
|
260
|
+
namespace.bindings.join(" ")})`);
|
|
261
|
+
}
|
|
262
|
+
const prefix = "(export-default ";
|
|
263
|
+
const expressionLine = prelude.reduce(
|
|
264
|
+
(lines, source) => lines + source.split("\n").length,
|
|
265
|
+
1,
|
|
266
|
+
);
|
|
267
|
+
const syntheticFilename = `${requestFilename(request.filename)}.eval.eli`;
|
|
268
|
+
return {
|
|
269
|
+
source: `${prelude.length > 0 ? `${prelude.join("\n")}\n` : ""}${
|
|
270
|
+
prefix}${request.source}\n)`,
|
|
271
|
+
adjustment: {
|
|
272
|
+
syntheticFilename,
|
|
273
|
+
filename: request.filename,
|
|
274
|
+
line: request.line,
|
|
275
|
+
column: request.column,
|
|
276
|
+
expressionLine,
|
|
277
|
+
expressionColumn: prefix.length + 1,
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export class EvaluationSession {
|
|
283
|
+
constructor(options) {
|
|
284
|
+
this.compiler = options.compiler;
|
|
285
|
+
this.directory = options.directory;
|
|
286
|
+
this.revision = 0;
|
|
287
|
+
this.artifact = 0;
|
|
288
|
+
this.namespace = null;
|
|
289
|
+
this.mode = "empty";
|
|
290
|
+
this.definitions = new Map();
|
|
291
|
+
this.definitionFilename = null;
|
|
292
|
+
this.closed = false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
static async create(options = {}) {
|
|
296
|
+
const compiler = options.compiler ?? await loadCompiler(options.moduleDirectory);
|
|
297
|
+
const directory = mkdtempSync(join(tmpdir(), "eliscript-evaluation-"));
|
|
298
|
+
try {
|
|
299
|
+
mkdirSync(join(directory, "node_modules"), { recursive: true });
|
|
300
|
+
symlinkSync(projectDirectory, join(directory, "node_modules", "eliscript"),
|
|
301
|
+
"dir");
|
|
302
|
+
return new EvaluationSession({ compiler, directory });
|
|
303
|
+
} catch (error) {
|
|
304
|
+
rmSync(directory, { recursive: true, force: true });
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
ensureOpen() {
|
|
310
|
+
if (this.closed) throw diagnosticError("evaluation session is closed");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
nextArtifactDirectory(label) {
|
|
314
|
+
this.artifact += 1;
|
|
315
|
+
const directory = join(this.directory, `${String(this.artifact).padStart(6, "0")}-${label}`);
|
|
316
|
+
mkdirSync(directory, { recursive: true });
|
|
317
|
+
return directory;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async compileSingle(source, filename, label) {
|
|
321
|
+
const directory = this.nextArtifactDirectory(label);
|
|
322
|
+
const generatedName = `${label}.mjs`;
|
|
323
|
+
const modulePath = join(directory, generatedName);
|
|
324
|
+
const emission = this.compiler.compile_string_with_source_map(
|
|
325
|
+
source,
|
|
326
|
+
filename,
|
|
327
|
+
generatedName,
|
|
328
|
+
filename,
|
|
329
|
+
);
|
|
330
|
+
writeFileSync(
|
|
331
|
+
modulePath,
|
|
332
|
+
`${emission.javascript}\n`,
|
|
333
|
+
"utf8",
|
|
334
|
+
);
|
|
335
|
+
writeFileSync(`${modulePath}.map`, emission.sourceMap, "utf8");
|
|
336
|
+
const url = `${pathToFileURL(modulePath).href}?artifact=${this.artifact}`;
|
|
337
|
+
return { module: await import(url), modulePath, url };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async compileProject(source, filename, root, label) {
|
|
341
|
+
const directory = this.nextArtifactDirectory(label);
|
|
342
|
+
const result = await buildProject({
|
|
343
|
+
entry: filename,
|
|
344
|
+
outDir: directory,
|
|
345
|
+
root,
|
|
346
|
+
portableEntries: [],
|
|
347
|
+
useCache: false,
|
|
348
|
+
sourceOverrides: new Map([[filename, source]]),
|
|
349
|
+
compiler: this.compiler,
|
|
350
|
+
compilerDigest: evaluationCompilerDigest,
|
|
351
|
+
sourceMapReference: false,
|
|
352
|
+
});
|
|
353
|
+
const modulePath = result.entryOutput;
|
|
354
|
+
const url = `${pathToFileURL(modulePath).href}?artifact=${this.artifact}`;
|
|
355
|
+
return { module: await import(url), modulePath, url };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async buildNamespace(source, filename, root, label) {
|
|
359
|
+
const description = this.compiler.evaluation_module_description(source, filename);
|
|
360
|
+
const compiledSource = namespaceSource(source, description);
|
|
361
|
+
const absoluteFilename = requestFilename(filename);
|
|
362
|
+
let artifact;
|
|
363
|
+
if (existsSync(absoluteFilename) && absoluteFilename.endsWith(".eli")) {
|
|
364
|
+
const canonicalFilename = realpathSync(absoluteFilename);
|
|
365
|
+
const requestedRoot = root === null ? nearestProjectRoot(canonicalFilename) : resolve(root);
|
|
366
|
+
const canonicalRoot = realpathSync(requestedRoot);
|
|
367
|
+
if (!containedPath(canonicalFilename, canonicalRoot)) {
|
|
368
|
+
throw diagnosticError("evaluation file escapes project root", filename);
|
|
369
|
+
}
|
|
370
|
+
artifact = await this.compileProject(
|
|
371
|
+
compiledSource,
|
|
372
|
+
canonicalFilename,
|
|
373
|
+
canonicalRoot,
|
|
374
|
+
label,
|
|
375
|
+
);
|
|
376
|
+
} else {
|
|
377
|
+
artifact = await this.compileSingle(compiledSource, filename, label);
|
|
378
|
+
}
|
|
379
|
+
return frozen({
|
|
380
|
+
...artifact,
|
|
381
|
+
filename,
|
|
382
|
+
source,
|
|
383
|
+
bindings: description.bindings,
|
|
384
|
+
exports: description.exports,
|
|
385
|
+
macroSources: description.macroSources,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
commitNamespace(namespace, mode) {
|
|
390
|
+
this.namespace = namespace;
|
|
391
|
+
this.mode = mode;
|
|
392
|
+
this.revision += 1;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async describe(request) {
|
|
396
|
+
return success(request, this.revision, {
|
|
397
|
+
kind: "capabilities",
|
|
398
|
+
capabilities: frozen({
|
|
399
|
+
operations: frozen(["describe", "evaluate", "load", "reset"]),
|
|
400
|
+
persistentSession: true,
|
|
401
|
+
sourceMaps: true,
|
|
402
|
+
valuePrinter: "eliscript-data-text",
|
|
403
|
+
}),
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async load(request) {
|
|
408
|
+
const namespace = await this.buildNamespace(
|
|
409
|
+
request.source,
|
|
410
|
+
request.filename,
|
|
411
|
+
request.root,
|
|
412
|
+
`namespace-${this.revision + 1}`,
|
|
413
|
+
);
|
|
414
|
+
this.definitions = new Map();
|
|
415
|
+
this.definitionFilename = null;
|
|
416
|
+
this.commitNamespace(namespace, "loaded");
|
|
417
|
+
return success(request, this.revision, {
|
|
418
|
+
kind: "module",
|
|
419
|
+
bindings: namespace.bindings,
|
|
420
|
+
value: null,
|
|
421
|
+
valueCategory: "module",
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async evaluateDefinition(request, description) {
|
|
426
|
+
if (this.mode === "loaded") {
|
|
427
|
+
throw diagnosticError(
|
|
428
|
+
"evaluate a complete updated source to replace definitions from a loaded module",
|
|
429
|
+
request.filename,
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
if (this.definitionFilename !== null &&
|
|
433
|
+
this.definitionFilename !== request.filename) {
|
|
434
|
+
throw diagnosticError(
|
|
435
|
+
"one definition journal cannot mix logical filenames",
|
|
436
|
+
request.filename,
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
const definitions = new Map(this.definitions);
|
|
440
|
+
definitions.set(description.name, request.source);
|
|
441
|
+
const source = [...definitions.values()].join("\n");
|
|
442
|
+
const namespace = await this.buildNamespace(
|
|
443
|
+
source,
|
|
444
|
+
request.filename,
|
|
445
|
+
request.root,
|
|
446
|
+
`namespace-${this.revision + 1}`,
|
|
447
|
+
);
|
|
448
|
+
this.definitions = definitions;
|
|
449
|
+
this.definitionFilename = request.filename;
|
|
450
|
+
this.commitNamespace(namespace, "journal");
|
|
451
|
+
const hasValue = description.runtimeBinding &&
|
|
452
|
+
Object.hasOwn(namespace.module, description.name);
|
|
453
|
+
const value = hasValue ? namespace.module[description.name] : undefined;
|
|
454
|
+
return success(request, this.revision, {
|
|
455
|
+
kind: "definition",
|
|
456
|
+
binding: description.name,
|
|
457
|
+
hasValue,
|
|
458
|
+
value: hasValue ? printValue(value) : null,
|
|
459
|
+
valueCategory: hasValue ? runtimeCategory(value) : "macro",
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
async evaluateExpression(request) {
|
|
464
|
+
const prepared = expressionSource(request, this.namespace);
|
|
465
|
+
let artifact;
|
|
466
|
+
try {
|
|
467
|
+
artifact = await this.compileSingle(
|
|
468
|
+
prepared.source,
|
|
469
|
+
prepared.adjustment.syntheticFilename,
|
|
470
|
+
`expression-${this.artifact + 1}`,
|
|
471
|
+
);
|
|
472
|
+
} catch (error) {
|
|
473
|
+
error.eliscriptEvaluationAdjustment = prepared.adjustment;
|
|
474
|
+
throw error;
|
|
475
|
+
}
|
|
476
|
+
const value = await artifact.module.default;
|
|
477
|
+
return success(request, this.revision, {
|
|
478
|
+
kind: "expression",
|
|
479
|
+
hasValue: true,
|
|
480
|
+
value: printValue(value),
|
|
481
|
+
valueCategory: runtimeCategory(value),
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async evaluate(request) {
|
|
486
|
+
const description = this.compiler.evaluation_form_description(
|
|
487
|
+
request.source,
|
|
488
|
+
request.filename,
|
|
489
|
+
);
|
|
490
|
+
if (description.kind === "module") {
|
|
491
|
+
throw diagnosticError(
|
|
492
|
+
"module forms require a complete-source load operation",
|
|
493
|
+
request.filename,
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
return description.kind === "definition"
|
|
497
|
+
? this.evaluateDefinition(request, description)
|
|
498
|
+
: this.evaluateExpression(request);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
async reset(request) {
|
|
502
|
+
this.namespace = null;
|
|
503
|
+
this.mode = "empty";
|
|
504
|
+
this.definitions = new Map();
|
|
505
|
+
this.definitionFilename = null;
|
|
506
|
+
this.revision += 1;
|
|
507
|
+
return success(request, this.revision, {
|
|
508
|
+
kind: "reset",
|
|
509
|
+
hasValue: false,
|
|
510
|
+
value: null,
|
|
511
|
+
valueCategory: "none",
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
async execute(rawRequest) {
|
|
516
|
+
this.ensureOpen();
|
|
517
|
+
let request;
|
|
518
|
+
try {
|
|
519
|
+
request = this.compiler.evaluation_operation_request(rawRequest);
|
|
520
|
+
const executed = await captureStandardOutput(async () => {
|
|
521
|
+
switch (request.operation) {
|
|
522
|
+
case "describe": return this.describe(request);
|
|
523
|
+
case "evaluate": return this.evaluate(request);
|
|
524
|
+
case "load": return this.load(request);
|
|
525
|
+
case "reset": return this.reset(request);
|
|
526
|
+
default: throw diagnosticError("unsupported evaluation operation");
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
return appendCapturedOutput(executed.value, executed.stdout);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
return appendCapturedOutput(failure(
|
|
532
|
+
rawRequest,
|
|
533
|
+
request,
|
|
534
|
+
this.revision,
|
|
535
|
+
error,
|
|
536
|
+
error?.eliscriptEvaluationAdjustment,
|
|
537
|
+
), error?.eliscriptCapturedStdout ?? "");
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
close() {
|
|
542
|
+
if (this.closed) return;
|
|
543
|
+
this.closed = true;
|
|
544
|
+
rmSync(this.directory, { recursive: true, force: true });
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export async function evaluateOnce(request, options = {}) {
|
|
549
|
+
const session = await EvaluationSession.create(options);
|
|
550
|
+
try {
|
|
551
|
+
return await session.execute(request);
|
|
552
|
+
} finally {
|
|
553
|
+
session.close();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
export function readEvaluationFile(filename) {
|
|
558
|
+
const absolute = requestFilename(filename);
|
|
559
|
+
return { filename: absolute, source: readFileSync(absolute, "utf8") };
|
|
560
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import {
|
|
5
|
+
readFile,
|
|
6
|
+
rename,
|
|
7
|
+
stat,
|
|
8
|
+
unlink,
|
|
9
|
+
writeFile,
|
|
10
|
+
} from "node:fs/promises";
|
|
11
|
+
import { basename, dirname, resolve } from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
diagnosticFromError,
|
|
16
|
+
loadCompiler,
|
|
17
|
+
requestedDiagnosticFormat,
|
|
18
|
+
} from "./bun.mjs";
|
|
19
|
+
|
|
20
|
+
const usage = `Usage: eliscript-format [OPTIONS] INPUT
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
--write Atomically replace INPUT when formatting changes
|
|
24
|
+
--check Fail without writing when INPUT is not canonical
|
|
25
|
+
--diagnostic-format human|json
|
|
26
|
+
Select human-readable or JSON failures
|
|
27
|
+
-h, --help Show this help
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
export function parseArguments(arguments_) {
|
|
31
|
+
const argumentsList = [...arguments_];
|
|
32
|
+
if (argumentsList[0] === "--") argumentsList.shift();
|
|
33
|
+
let input;
|
|
34
|
+
let write = false;
|
|
35
|
+
let check = false;
|
|
36
|
+
let diagnosticFormat = "human";
|
|
37
|
+
|
|
38
|
+
while (argumentsList.length > 0) {
|
|
39
|
+
const argument = argumentsList.shift();
|
|
40
|
+
if (argument === "-h" || argument === "--help") {
|
|
41
|
+
return { help: true };
|
|
42
|
+
}
|
|
43
|
+
if (argument === "--write") {
|
|
44
|
+
write = true;
|
|
45
|
+
} else if (argument === "--check") {
|
|
46
|
+
check = true;
|
|
47
|
+
} else if (argument === "--diagnostic-format") {
|
|
48
|
+
if (argumentsList.length === 0) {
|
|
49
|
+
throw new Error(`${argument} requires human or json`);
|
|
50
|
+
}
|
|
51
|
+
diagnosticFormat = argumentsList.shift();
|
|
52
|
+
if (diagnosticFormat !== "human" && diagnosticFormat !== "json") {
|
|
53
|
+
throw new Error(`unsupported diagnostic format: ${diagnosticFormat}`);
|
|
54
|
+
}
|
|
55
|
+
} else if (argument.startsWith("-")) {
|
|
56
|
+
throw new Error(`unknown option: ${argument}`);
|
|
57
|
+
} else if (input !== undefined) {
|
|
58
|
+
throw new Error("multiple input files are not supported");
|
|
59
|
+
} else {
|
|
60
|
+
input = argument;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (input === undefined) throw new Error("missing input file");
|
|
65
|
+
if (write && check) throw new Error("--write and --check are mutually exclusive");
|
|
66
|
+
return { input, write, check, diagnosticFormat };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function unformattedError(filename) {
|
|
70
|
+
const error = new Error(`${filename} is not formatted`);
|
|
71
|
+
error.eliscriptDiagnostic = {
|
|
72
|
+
format: "eliscript-diagnostic",
|
|
73
|
+
version: 1,
|
|
74
|
+
code: "ELI-F0002",
|
|
75
|
+
severity: "error",
|
|
76
|
+
phase: "formatter",
|
|
77
|
+
message: "file is not formatted",
|
|
78
|
+
location: { file: filename },
|
|
79
|
+
};
|
|
80
|
+
return error;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function atomicWrite(filename, source, mode) {
|
|
84
|
+
const temporary = resolve(
|
|
85
|
+
dirname(filename),
|
|
86
|
+
`.${basename(filename)}.eliscript-format-${process.pid}-${randomUUID()}`,
|
|
87
|
+
);
|
|
88
|
+
try {
|
|
89
|
+
await writeFile(temporary, source, { encoding: "utf8", mode });
|
|
90
|
+
await rename(temporary, filename);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
await unlink(temporary).catch(() => {});
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function formatFile(options) {
|
|
98
|
+
if (!options || typeof options !== "object") {
|
|
99
|
+
throw new Error("formatter options must be an object");
|
|
100
|
+
}
|
|
101
|
+
const filename = resolve(options.input);
|
|
102
|
+
const compiler = options.compiler ?? await loadCompiler(options.moduleDirectory);
|
|
103
|
+
const [source, attributes] = await Promise.all([
|
|
104
|
+
readFile(filename, "utf8"),
|
|
105
|
+
stat(filename),
|
|
106
|
+
]);
|
|
107
|
+
const formatted = compiler.format_source(source, filename);
|
|
108
|
+
const changed = formatted !== source;
|
|
109
|
+
|
|
110
|
+
if (options.check && changed) throw unformattedError(filename);
|
|
111
|
+
if (options.write && changed) {
|
|
112
|
+
await atomicWrite(filename, formatted, attributes.mode);
|
|
113
|
+
}
|
|
114
|
+
return Object.freeze({ filename, formatted, changed });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function main(arguments_ = process.argv.slice(2)) {
|
|
118
|
+
const options = parseArguments(arguments_);
|
|
119
|
+
if (options.help) {
|
|
120
|
+
process.stdout.write(usage);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const result = await formatFile(options);
|
|
124
|
+
if (!options.write && !options.check) process.stdout.write(result.formatted);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const isMain = process.argv[1] !== undefined &&
|
|
128
|
+
resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
129
|
+
if (isMain) {
|
|
130
|
+
const outputFormat = requestedDiagnosticFormat(process.argv.slice(2));
|
|
131
|
+
main().catch((error) => {
|
|
132
|
+
if (outputFormat === "json") {
|
|
133
|
+
console.error(JSON.stringify(diagnosticFromError(error)));
|
|
134
|
+
} else {
|
|
135
|
+
console.error(`eliscript-format: ${error.message}`);
|
|
136
|
+
}
|
|
137
|
+
process.exitCode = 1;
|
|
138
|
+
});
|
|
139
|
+
}
|