eliscript 0.0.2 → 0.0.4
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 +13 -11
- package/bootstrap/README.md +13 -8
- package/bootstrap/host/build.eli +2 -0
- package/bootstrap/host/bun.eli +2 -0
- package/bootstrap/host/check-cli-legacy.eli +2 -0
- package/bootstrap/host/evaluation-cli.eli +2 -0
- package/bootstrap/host/evaluation.eli +2 -0
- package/bootstrap/host/format-cli.eli +2 -0
- package/bootstrap/host/project-cli-legacy.eli +2 -0
- package/bootstrap/host/project-legacy.eli +2 -0
- package/bootstrap/host/watch-cli.eli +2 -0
- package/bootstrap/host/watch.eli +2 -0
- package/browser/README.md +33 -0
- package/browser/host.eli +2 -0
- package/browser/host.mjs +643 -11
- package/browser/worker.eli +2 -0
- package/browser/worker.mjs +6 -3
- package/dist/browser/host.mjs +643 -11
- package/dist/browser/worker.mjs +6 -3
- package/dist/compiler-artifact.json +1 -1
- package/dist/literal-sources/runtime/core/collection.eli +76 -16
- package/dist/literal-sources/runtime/core/metadata.eli +25 -8
- package/dist/literal-sources/runtime/core/vector.eli +7 -2
- package/dist/literal-sources/stdlib/managed-literal-protocols.eli +18 -7
- package/dist/literal-sources/stdlib/managed-literals.eli +121 -66
- package/dist/stage3-release.json +1 -1
- package/package.json +11 -2
- package/platform/README.md +33 -12
- package/platform/browser.eli +2 -0
- package/platform/http.eli +2 -0
- package/platform/worker.eli +2 -0
- package/runtime/README.md +34 -14
- package/runtime/array-text.eli +2 -0
- package/runtime/core/collection-internals.eli +2 -0
- package/runtime/core/collection.eli +2 -0
- package/runtime/core/collection.mjs +99 -17
- package/runtime/core/data-text.eli +2 -0
- package/runtime/core/data.eli +2 -0
- package/runtime/core/data.mjs +7 -5
- package/runtime/core/identifier.eli +2 -0
- package/runtime/core/lazy-sequence.eli +2 -0
- package/runtime/core/list-internals.eli +2 -0
- package/runtime/core/list.eli +2 -0
- package/runtime/core/list.mjs +3 -0
- package/runtime/core/map-internals.eli +2 -0
- package/runtime/core/map.eli +2 -0
- package/runtime/core/map.mjs +5 -0
- package/runtime/core/metadata-internals.eli +2 -0
- package/runtime/core/metadata.eli +2 -0
- package/runtime/core/metadata.mjs +14 -0
- package/runtime/core/object.eli +2 -0
- package/runtime/core/order.eli +2 -0
- package/runtime/core/order.mjs +35 -6
- package/runtime/core/persistent-kind.eli +2 -0
- package/runtime/core/persistent-kind.mjs +6 -0
- package/runtime/core/protocol-error.eli +2 -0
- package/runtime/core/protocol.eli +2 -0
- package/runtime/core/queue-internals.eli +2 -0
- package/runtime/core/queue.eli +2 -0
- package/runtime/core/queue.mjs +3 -0
- package/runtime/core/record.eli +2 -0
- package/runtime/core/regex.eli +2 -0
- package/runtime/core/sequence.eli +2 -0
- package/runtime/core/sequence.mjs +75 -13
- package/runtime/core/set-algebra.eli +2 -0
- package/runtime/core/set-internals.eli +2 -0
- package/runtime/core/set.eli +2 -0
- package/runtime/core/sorted-internals.eli +2 -0
- package/runtime/core/sorted.eli +2 -0
- package/runtime/core/text.eli +2 -0
- package/runtime/core/transducer.eli +2 -0
- package/runtime/core/transient-internals.eli +2 -0
- package/runtime/core/transient.eli +2 -0
- package/runtime/core/transient.mjs +59 -10
- package/runtime/core/truth.eli +2 -0
- package/runtime/core/type.eli +2 -0
- package/runtime/core/value-internals.eli +2 -0
- package/runtime/core/value.eli +2 -0
- package/runtime/core/vector-internals.eli +2 -0
- package/runtime/core/vector-internals.mjs +3 -6
- package/runtime/core/vector.eli +2 -0
- package/runtime/core/vector.mjs +125 -7
- package/runtime/literals.eli +2 -0
- package/runtime/number-radix.eli +2 -0
- package/runtime/source-mapping.eli +2 -0
- package/runtime/testing/list.eli +2 -0
- package/runtime/testing/map.eli +2 -0
- package/runtime/testing/queue.eli +2 -0
- package/runtime/testing/set.eli +2 -0
- package/runtime/testing/sorted.eli +2 -0
- package/runtime/testing/value.eli +2 -0
- package/runtime/testing/vector.eli +2 -0
- package/runtime/wire/http-rpc.eli +2 -0
- package/runtime/wire/markdown.eli +2 -0
- package/runtime/wire/schema.eli +2 -0
- package/runtime/worker-value-codec.eli +2 -0
- package/runtime/worker-value-stream.eli +2 -0
- package/runtime/worker.eli +2 -0
- package/stdlib/README.md +2 -2
- package/stdlib/core/order.eli +36 -10
- package/stdlib/core/seq.eli +104 -75
- package/stdlib/core/zipper.eli +274 -101
- package/stdlib/managed-literal-protocols.eli +18 -7
- package/stdlib/managed-literals.eli +121 -66
- package/stdlib/managed-runtime-collection.eli +76 -16
- package/stdlib/managed-runtime-metadata.eli +25 -8
- package/stdlib/managed-runtime-order.eli +32 -10
- package/stdlib/managed-runtime-sequence.eli +69 -28
- package/stdlib/managed-runtime-vector.eli +7 -2
- package/tools/acceptance/check.eli +13 -3
- package/tools/backends/compiler-build.mjs +140 -140
- package/tools/backends/dist/release-cli.mjs +2 -0
- package/tools/backends/dist/release-cli.mjs.map +1 -1
- package/tools/backends/stage3-targets.mjs +121 -114
- package/bootstrap/host/eliscript-cli-legacy.mjs +0 -37
package/browser/host.mjs
CHANGED
|
@@ -108,6 +108,607 @@ function walkProgram(program, visit) {
|
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
function isPlainLiteralNode(node) {
|
|
112
|
+
return (
|
|
113
|
+
node !== null &&
|
|
114
|
+
typeof node === "object" &&
|
|
115
|
+
node.kind === "literal" &&
|
|
116
|
+
node.properties?.literalKind === undefined
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isConstantLiteralNode(node) {
|
|
121
|
+
if (node === null || typeof node !== "object" || node.kind !== "literal") {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const literalKind = node.properties?.literalKind;
|
|
125
|
+
return (
|
|
126
|
+
literalKind === undefined ||
|
|
127
|
+
literalKind === "undefined" ||
|
|
128
|
+
literalKind === "keyword"
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function cloneLiteralNode(literal, span) {
|
|
133
|
+
return {
|
|
134
|
+
kind: "literal",
|
|
135
|
+
span: span ?? literal.span,
|
|
136
|
+
value: literal.value,
|
|
137
|
+
children: [],
|
|
138
|
+
...(literal.properties ? { properties: { ...literal.properties } } : {}),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function literalRuntimeValue(node) {
|
|
143
|
+
const literalKind = node.properties?.literalKind;
|
|
144
|
+
if (literalKind === "undefined") return undefined;
|
|
145
|
+
if (literalKind === "keyword") return `:${node.value}`;
|
|
146
|
+
return node.value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function literalTruthiness(node) {
|
|
150
|
+
if (!isConstantLiteralNode(node)) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
const literalKind = node.properties?.literalKind;
|
|
154
|
+
if (literalKind === "undefined") return false;
|
|
155
|
+
if (literalKind === "keyword") return true;
|
|
156
|
+
return node.value !== false && node.value !== null && node.value !== undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function collectMutatedNames(program) {
|
|
160
|
+
const mutated = new Set();
|
|
161
|
+
walkProgram(program, (node) => {
|
|
162
|
+
if (node.kind === "assignment") {
|
|
163
|
+
for (const pair of node.children ?? []) {
|
|
164
|
+
if (typeof pair?.value === "string") mutated.add(pair.value);
|
|
165
|
+
}
|
|
166
|
+
} else if (
|
|
167
|
+
node.kind === "variable-declaration" &&
|
|
168
|
+
node.properties?.mutable === true &&
|
|
169
|
+
typeof node.value === "string"
|
|
170
|
+
) {
|
|
171
|
+
mutated.add(node.value);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return mutated;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function foldIrNode(node, env = new Map(), mutated = new Set()) {
|
|
178
|
+
if (node === null || typeof node !== "object") return node;
|
|
179
|
+
if (node.kind === "export-declaration" || node.kind === "import-declaration") {
|
|
180
|
+
return node;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (node.kind === "reference" && typeof node.value === "string") {
|
|
184
|
+
if (!mutated.has(node.value) && env.has(node.value)) {
|
|
185
|
+
return cloneLiteralNode(env.get(node.value), node.span);
|
|
186
|
+
}
|
|
187
|
+
return node;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (node.kind === "module-declaration" && Array.isArray(node.children)) {
|
|
191
|
+
const scope = new Map(env);
|
|
192
|
+
let changed = false;
|
|
193
|
+
const nextChildren = node.children.map((child) => {
|
|
194
|
+
const next = foldIrNode(child, scope, mutated);
|
|
195
|
+
if (next !== child) changed = true;
|
|
196
|
+
if (
|
|
197
|
+
next.kind === "variable-declaration" &&
|
|
198
|
+
next.properties?.mutable !== true &&
|
|
199
|
+
typeof next.value === "string" &&
|
|
200
|
+
!mutated.has(next.value) &&
|
|
201
|
+
Array.isArray(next.children) &&
|
|
202
|
+
next.children.length === 1 &&
|
|
203
|
+
isConstantLiteralNode(next.children[0])
|
|
204
|
+
) {
|
|
205
|
+
scope.set(next.value, next.children[0]);
|
|
206
|
+
}
|
|
207
|
+
return next;
|
|
208
|
+
});
|
|
209
|
+
return changed ? { ...node, children: nextChildren } : node;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (
|
|
213
|
+
(node.kind === "function-declaration" || node.kind === "function-expression") &&
|
|
214
|
+
Array.isArray(node.children)
|
|
215
|
+
) {
|
|
216
|
+
const paramCount = node.properties?.parameterCount ?? 0;
|
|
217
|
+
const scope = new Map(env);
|
|
218
|
+
for (let index = 0; index < paramCount; index += 1) {
|
|
219
|
+
const param = node.children[index];
|
|
220
|
+
if (typeof param?.value === "string") scope.delete(param.value);
|
|
221
|
+
}
|
|
222
|
+
let changed = false;
|
|
223
|
+
const nextChildren = node.children.map((child, index) => {
|
|
224
|
+
if (index < paramCount) return child;
|
|
225
|
+
const next = foldIrNode(child, scope, mutated);
|
|
226
|
+
if (next !== child) changed = true;
|
|
227
|
+
return next;
|
|
228
|
+
});
|
|
229
|
+
return changed ? { ...node, children: nextChildren } : node;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (node.kind === "lexical-bindings" && Array.isArray(node.children)) {
|
|
233
|
+
const bindingCount = node.properties?.bindingCount ?? 0;
|
|
234
|
+
const sequential = node.properties?.sequential === true;
|
|
235
|
+
const outerScope = new Map(env);
|
|
236
|
+
const bodyScope = new Map(env);
|
|
237
|
+
let changed = false;
|
|
238
|
+
const nextChildren = node.children.map((child, index) => {
|
|
239
|
+
if (index < bindingCount && child.kind === "lexical-binding") {
|
|
240
|
+
const initScope = sequential ? bodyScope : outerScope;
|
|
241
|
+
let nextBinding = child;
|
|
242
|
+
if (Array.isArray(child.children) && child.children.length > 0) {
|
|
243
|
+
let bindingChanged = false;
|
|
244
|
+
const nextBindingChildren = child.children.map((expr) => {
|
|
245
|
+
const folded = foldIrNode(expr, initScope, mutated);
|
|
246
|
+
if (folded !== expr) bindingChanged = true;
|
|
247
|
+
return folded;
|
|
248
|
+
});
|
|
249
|
+
if (bindingChanged) {
|
|
250
|
+
changed = true;
|
|
251
|
+
nextBinding = { ...child, children: nextBindingChildren };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (
|
|
255
|
+
typeof nextBinding.value === "string" &&
|
|
256
|
+
nextBinding.properties?.pattern !== true
|
|
257
|
+
) {
|
|
258
|
+
if (
|
|
259
|
+
!mutated.has(nextBinding.value) &&
|
|
260
|
+
Array.isArray(nextBinding.children) &&
|
|
261
|
+
nextBinding.children.length === 1 &&
|
|
262
|
+
isConstantLiteralNode(nextBinding.children[0])
|
|
263
|
+
) {
|
|
264
|
+
bodyScope.set(nextBinding.value, nextBinding.children[0]);
|
|
265
|
+
} else {
|
|
266
|
+
bodyScope.delete(nextBinding.value);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return nextBinding;
|
|
270
|
+
}
|
|
271
|
+
const next = foldIrNode(child, bodyScope, mutated);
|
|
272
|
+
if (next !== child) changed = true;
|
|
273
|
+
return next;
|
|
274
|
+
});
|
|
275
|
+
const updated = changed ? { ...node, children: nextChildren } : node;
|
|
276
|
+
const bodyNodes = updated.children.slice(bindingCount);
|
|
277
|
+
if (bodyNodes.length === 1 && isConstantLiteralNode(bodyNodes[0])) {
|
|
278
|
+
const allBindingsPureConstants = updated.children
|
|
279
|
+
.slice(0, bindingCount)
|
|
280
|
+
.every(
|
|
281
|
+
(binding) =>
|
|
282
|
+
binding.kind === "lexical-binding" &&
|
|
283
|
+
binding.properties?.pattern !== true &&
|
|
284
|
+
Array.isArray(binding.children) &&
|
|
285
|
+
binding.children.length === 1 &&
|
|
286
|
+
isConstantLiteralNode(binding.children[0]),
|
|
287
|
+
);
|
|
288
|
+
if (allBindingsPureConstants) {
|
|
289
|
+
return bodyNodes[0];
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return updated;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (Array.isArray(node.children) && node.children.length > 0) {
|
|
296
|
+
let changed = false;
|
|
297
|
+
const optimizedChildren = node.children.map((child) => {
|
|
298
|
+
const next = foldIrNode(child, env, mutated);
|
|
299
|
+
if (next !== child) changed = true;
|
|
300
|
+
return next;
|
|
301
|
+
});
|
|
302
|
+
if (changed) {
|
|
303
|
+
node = { ...node, children: optimizedChildren };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const children = node.children ?? [];
|
|
308
|
+
if (node.kind === "conditional" && children.length >= 2) {
|
|
309
|
+
const truthy = literalTruthiness(children[0]);
|
|
310
|
+
if (truthy === true) return children[1];
|
|
311
|
+
if (truthy === false) {
|
|
312
|
+
return (
|
|
313
|
+
children[2] ?? {
|
|
314
|
+
kind: "literal",
|
|
315
|
+
span: node.span,
|
|
316
|
+
value: null,
|
|
317
|
+
children: [],
|
|
318
|
+
}
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (node.kind === "short-circuit" && children.length > 0) {
|
|
324
|
+
if (node.value === "and") {
|
|
325
|
+
const remaining = [];
|
|
326
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
327
|
+
const child = children[index];
|
|
328
|
+
const truthy = literalTruthiness(child);
|
|
329
|
+
if (truthy === false && remaining.length === 0) {
|
|
330
|
+
return child;
|
|
331
|
+
}
|
|
332
|
+
if (truthy === true && index < children.length - 1) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
remaining.push(child);
|
|
336
|
+
}
|
|
337
|
+
if (remaining.length === 1) return remaining[0];
|
|
338
|
+
if (remaining.length !== children.length) {
|
|
339
|
+
return { ...node, children: remaining };
|
|
340
|
+
}
|
|
341
|
+
} else if (node.value === "or") {
|
|
342
|
+
const remaining = [];
|
|
343
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
344
|
+
const child = children[index];
|
|
345
|
+
const truthy = literalTruthiness(child);
|
|
346
|
+
if (truthy === true && remaining.length === 0) {
|
|
347
|
+
return child;
|
|
348
|
+
}
|
|
349
|
+
if (truthy === false && index < children.length - 1) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
remaining.push(child);
|
|
353
|
+
}
|
|
354
|
+
if (remaining.length === 1) return remaining[0];
|
|
355
|
+
if (remaining.length !== children.length) {
|
|
356
|
+
return { ...node, children: remaining };
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (node.kind === "intrinsic") {
|
|
362
|
+
if (node.value === "not" && children.length === 1) {
|
|
363
|
+
const truthy = literalTruthiness(children[0]);
|
|
364
|
+
if (truthy !== null) {
|
|
365
|
+
return {
|
|
366
|
+
kind: "literal",
|
|
367
|
+
span: node.span,
|
|
368
|
+
value: !truthy,
|
|
369
|
+
children: [],
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (
|
|
375
|
+
(node.value === "null" ||
|
|
376
|
+
node.value === "nil?" ||
|
|
377
|
+
node.value === "undefined?" ||
|
|
378
|
+
node.value === "nullish?") &&
|
|
379
|
+
children.length === 1 &&
|
|
380
|
+
isConstantLiteralNode(children[0])
|
|
381
|
+
) {
|
|
382
|
+
const raw = literalRuntimeValue(children[0]);
|
|
383
|
+
const result =
|
|
384
|
+
node.value === "undefined?"
|
|
385
|
+
? raw === undefined
|
|
386
|
+
: node.value === "nullish?"
|
|
387
|
+
? raw === null || raw === undefined
|
|
388
|
+
: raw === null;
|
|
389
|
+
return {
|
|
390
|
+
kind: "literal",
|
|
391
|
+
span: node.span,
|
|
392
|
+
value: result,
|
|
393
|
+
children: [],
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (
|
|
398
|
+
(node.value === "=" ||
|
|
399
|
+
node.value === "/=" ||
|
|
400
|
+
node.value === "not=" ||
|
|
401
|
+
node.value === "eq" ||
|
|
402
|
+
node.value === "equal") &&
|
|
403
|
+
children.length === 2 &&
|
|
404
|
+
isConstantLiteralNode(children[0]) &&
|
|
405
|
+
isConstantLiteralNode(children[1])
|
|
406
|
+
) {
|
|
407
|
+
const left = literalRuntimeValue(children[0]);
|
|
408
|
+
const right = literalRuntimeValue(children[1]);
|
|
409
|
+
const same = left === right;
|
|
410
|
+
return {
|
|
411
|
+
kind: "literal",
|
|
412
|
+
span: node.span,
|
|
413
|
+
value: node.value === "/=" || node.value === "not=" ? !same : same,
|
|
414
|
+
children: [],
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (
|
|
419
|
+
(node.value === "<" ||
|
|
420
|
+
node.value === "<=" ||
|
|
421
|
+
node.value === ">" ||
|
|
422
|
+
node.value === ">=") &&
|
|
423
|
+
children.length === 2 &&
|
|
424
|
+
isPlainLiteralNode(children[0]) &&
|
|
425
|
+
isPlainLiteralNode(children[1]) &&
|
|
426
|
+
typeof children[0].value === "number" &&
|
|
427
|
+
typeof children[1].value === "number"
|
|
428
|
+
) {
|
|
429
|
+
const left = children[0].value;
|
|
430
|
+
const right = children[1].value;
|
|
431
|
+
const compared =
|
|
432
|
+
node.value === "<"
|
|
433
|
+
? left < right
|
|
434
|
+
: node.value === "<="
|
|
435
|
+
? left <= right
|
|
436
|
+
: node.value === ">"
|
|
437
|
+
? left > right
|
|
438
|
+
: left >= right;
|
|
439
|
+
return {
|
|
440
|
+
kind: "literal",
|
|
441
|
+
span: node.span,
|
|
442
|
+
value: compared,
|
|
443
|
+
children: [],
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (
|
|
448
|
+
children.every(
|
|
449
|
+
(child) => isPlainLiteralNode(child) && typeof child.value === "number",
|
|
450
|
+
)
|
|
451
|
+
) {
|
|
452
|
+
const values = children.map((child) => child.value);
|
|
453
|
+
let folded;
|
|
454
|
+
switch (node.value) {
|
|
455
|
+
case "+":
|
|
456
|
+
folded = values.reduce((sum, item) => sum + item, 0);
|
|
457
|
+
break;
|
|
458
|
+
case "*":
|
|
459
|
+
folded = values.reduce((product, item) => product * item, 1);
|
|
460
|
+
break;
|
|
461
|
+
case "-":
|
|
462
|
+
if (values.length === 1) folded = -values[0];
|
|
463
|
+
else if (values.length >= 2) {
|
|
464
|
+
folded = values.slice(1).reduce((acc, item) => acc - item, values[0]);
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
case "/":
|
|
468
|
+
if (values.length === 2 && values[1] !== 0) {
|
|
469
|
+
folded = values[0] / values[1];
|
|
470
|
+
}
|
|
471
|
+
break;
|
|
472
|
+
case "%":
|
|
473
|
+
if (values.length === 2 && values[1] !== 0) {
|
|
474
|
+
folded = values[0] % values[1];
|
|
475
|
+
}
|
|
476
|
+
break;
|
|
477
|
+
case "1+":
|
|
478
|
+
if (values.length === 1) folded = values[0] + 1;
|
|
479
|
+
break;
|
|
480
|
+
case "1-":
|
|
481
|
+
if (values.length === 1) folded = values[0] - 1;
|
|
482
|
+
break;
|
|
483
|
+
default:
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
if (typeof folded === "number" && Number.isFinite(folded)) {
|
|
487
|
+
return {
|
|
488
|
+
kind: "literal",
|
|
489
|
+
span: node.span,
|
|
490
|
+
value: folded,
|
|
491
|
+
children: [],
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (node.value === "+" && children.length >= 2) {
|
|
497
|
+
const nonZero = children.filter(
|
|
498
|
+
(child) => !(isPlainLiteralNode(child) && child.value === 0),
|
|
499
|
+
);
|
|
500
|
+
if (nonZero.length > 0 && nonZero.length < children.length) {
|
|
501
|
+
return nonZero.length === 1
|
|
502
|
+
? nonZero[0]
|
|
503
|
+
: { ...node, children: nonZero };
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (node.value === "*" && children.length >= 2) {
|
|
508
|
+
const nonOne = children.filter(
|
|
509
|
+
(child) => !(isPlainLiteralNode(child) && child.value === 1),
|
|
510
|
+
);
|
|
511
|
+
if (nonOne.length > 0 && nonOne.length < children.length) {
|
|
512
|
+
return nonOne.length === 1 ? nonOne[0] : { ...node, children: nonOne };
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (
|
|
518
|
+
node.kind === "string-concat" &&
|
|
519
|
+
children.every(
|
|
520
|
+
(child) =>
|
|
521
|
+
isPlainLiteralNode(child) &&
|
|
522
|
+
(typeof child.value === "string" ||
|
|
523
|
+
typeof child.value === "number" ||
|
|
524
|
+
typeof child.value === "boolean" ||
|
|
525
|
+
child.value === null),
|
|
526
|
+
)
|
|
527
|
+
) {
|
|
528
|
+
return {
|
|
529
|
+
kind: "literal",
|
|
530
|
+
span: node.span,
|
|
531
|
+
value: children
|
|
532
|
+
.map((child) => (child.value === null ? "" : String(child.value)))
|
|
533
|
+
.join(""),
|
|
534
|
+
children: [],
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return node;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Performs a Closure-style IR optimization pass (constant propagation,
|
|
543
|
+
* constant folding, algebraic identity simplification, and dead conditional
|
|
544
|
+
* branch elimination) over a compiled Eliscript IR program.
|
|
545
|
+
*/
|
|
546
|
+
export function optimizeProgramIr(program) {
|
|
547
|
+
if (program === null || typeof program !== "object" || !Array.isArray(program.body)) {
|
|
548
|
+
return program;
|
|
549
|
+
}
|
|
550
|
+
const mutated = collectMutatedNames(program);
|
|
551
|
+
const topScope = new Map();
|
|
552
|
+
program.body = program.body.map((node) => {
|
|
553
|
+
const next = foldIrNode(node, topScope, mutated);
|
|
554
|
+
if (
|
|
555
|
+
next.kind === "variable-declaration" &&
|
|
556
|
+
next.properties?.mutable !== true &&
|
|
557
|
+
typeof next.value === "string" &&
|
|
558
|
+
!mutated.has(next.value) &&
|
|
559
|
+
Array.isArray(next.children) &&
|
|
560
|
+
next.children.length === 1 &&
|
|
561
|
+
isConstantLiteralNode(next.children[0])
|
|
562
|
+
) {
|
|
563
|
+
topScope.set(next.value, next.children[0]);
|
|
564
|
+
}
|
|
565
|
+
return next;
|
|
566
|
+
});
|
|
567
|
+
return program;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function isIdentifierChar(ch) {
|
|
571
|
+
if (!ch) return false;
|
|
572
|
+
const code = ch.charCodeAt(0);
|
|
573
|
+
return (
|
|
574
|
+
(code >= 48 && code <= 57) ||
|
|
575
|
+
(code >= 65 && code <= 90) ||
|
|
576
|
+
(code >= 97 && code <= 122) ||
|
|
577
|
+
code === 36 ||
|
|
578
|
+
code === 95 ||
|
|
579
|
+
code > 127
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function compactLineWhitespace(line) {
|
|
584
|
+
if (line.startsWith("//# sourceMappingURL=")) return line;
|
|
585
|
+
let result = "";
|
|
586
|
+
let index = 0;
|
|
587
|
+
const length = line.length;
|
|
588
|
+
while (index < length) {
|
|
589
|
+
const ch = line[index];
|
|
590
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
591
|
+
const quote = ch;
|
|
592
|
+
result += ch;
|
|
593
|
+
index += 1;
|
|
594
|
+
while (index < length) {
|
|
595
|
+
const cur = line[index];
|
|
596
|
+
result += cur;
|
|
597
|
+
index += 1;
|
|
598
|
+
if (cur === "\\" && index < length) {
|
|
599
|
+
result += line[index];
|
|
600
|
+
index += 1;
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
if (cur === quote) break;
|
|
604
|
+
}
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
if (ch === " " || ch === "\t") {
|
|
608
|
+
while (index < length && (line[index] === " " || line[index] === "\t")) {
|
|
609
|
+
index += 1;
|
|
610
|
+
}
|
|
611
|
+
const prev = result[result.length - 1] ?? "";
|
|
612
|
+
const next = line[index] ?? "";
|
|
613
|
+
if (
|
|
614
|
+
(isIdentifierChar(prev) && isIdentifierChar(next)) ||
|
|
615
|
+
(prev === "+" && next === "+") ||
|
|
616
|
+
(prev === "-" && next === "-") ||
|
|
617
|
+
(prev === "/" && (next === "/" || next === "*"))
|
|
618
|
+
) {
|
|
619
|
+
result += " ";
|
|
620
|
+
}
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
result += ch;
|
|
624
|
+
index += 1;
|
|
625
|
+
}
|
|
626
|
+
return result;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Strips comments and compacts whitespace in emitted JavaScript while
|
|
631
|
+
* preserving string/template literals and sourceMappingURL directives.
|
|
632
|
+
*/
|
|
633
|
+
export function optimizeJavaScript(code) {
|
|
634
|
+
if (typeof code !== "string" || code.length === 0) return code;
|
|
635
|
+
let out = "";
|
|
636
|
+
let index = 0;
|
|
637
|
+
const length = code.length;
|
|
638
|
+
|
|
639
|
+
while (index < length) {
|
|
640
|
+
const ch = code[index];
|
|
641
|
+
const next = code[index + 1];
|
|
642
|
+
|
|
643
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
644
|
+
const quote = ch;
|
|
645
|
+
out += ch;
|
|
646
|
+
index += 1;
|
|
647
|
+
while (index < length) {
|
|
648
|
+
const current = code[index];
|
|
649
|
+
out += current;
|
|
650
|
+
index += 1;
|
|
651
|
+
if (current === "\\" && index < length) {
|
|
652
|
+
out += code[index];
|
|
653
|
+
index += 1;
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
if (current === quote) break;
|
|
657
|
+
}
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (ch === "/" && next === "/") {
|
|
662
|
+
const lineEnd = code.indexOf("\n", index + 2);
|
|
663
|
+
const comment = lineEnd < 0 ? code.slice(index) : code.slice(index, lineEnd);
|
|
664
|
+
if (comment.startsWith("//# sourceMappingURL=")) {
|
|
665
|
+
out += comment;
|
|
666
|
+
}
|
|
667
|
+
index = lineEnd < 0 ? length : lineEnd;
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (ch === "/" && next === "*") {
|
|
672
|
+
const closeIndex = code.indexOf("*/", index + 2);
|
|
673
|
+
if (closeIndex < 0) break;
|
|
674
|
+
const block = code.slice(index + 2, closeIndex);
|
|
675
|
+
out += block.includes("\n") ? "\n" : " ";
|
|
676
|
+
index = closeIndex + 2;
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
out += ch;
|
|
681
|
+
index += 1;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const lines = out
|
|
685
|
+
.split("\n")
|
|
686
|
+
.map((line) => compactLineWhitespace(line.trim()))
|
|
687
|
+
.filter((line) => line.length > 0);
|
|
688
|
+
return lines.length === 0 ? "" : `${lines.join("\n")}\n`;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
function applyPostCompileOptimization(javascript, sourceMap, meta, options = {}) {
|
|
692
|
+
let code =
|
|
693
|
+
options.minify === true || options.optimize === "compact"
|
|
694
|
+
? optimizeJavaScript(javascript)
|
|
695
|
+
: javascript;
|
|
696
|
+
let map = sourceMap;
|
|
697
|
+
if (typeof options.closureCompiler === "function") {
|
|
698
|
+
const transformed = options.closureCompiler(code, {
|
|
699
|
+
...meta,
|
|
700
|
+
sourceMap: map,
|
|
701
|
+
});
|
|
702
|
+
if (typeof transformed === "string") {
|
|
703
|
+
code = transformed;
|
|
704
|
+
} else if (transformed !== null && typeof transformed === "object") {
|
|
705
|
+
if (typeof transformed.javascript === "string") code = transformed.javascript;
|
|
706
|
+
if (typeof transformed.sourceMap === "string") map = transformed.sourceMap;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return { javascript: code, sourceMap: map };
|
|
710
|
+
}
|
|
711
|
+
|
|
111
712
|
function localSourceSpecifier(specifier) {
|
|
112
713
|
return specifier.startsWith(".") && specifier.endsWith(SOURCE_EXTENSION);
|
|
113
714
|
}
|
|
@@ -189,6 +790,16 @@ function failure(code, message, filename) {
|
|
|
189
790
|
// point the runtime at a served directory without an import map;
|
|
190
791
|
// - anything `resolveExternal` declines stays as written, which is what a
|
|
191
792
|
// third-party package import needs.
|
|
793
|
+
function normalizePackageSubpath(specifier) {
|
|
794
|
+
if (
|
|
795
|
+
/^eliscript\/(?:runtime|platform|dist\/stdlib|stdlib)\//u.test(specifier) &&
|
|
796
|
+
!/\.[a-z0-9]+$/iu.test(specifier)
|
|
797
|
+
) {
|
|
798
|
+
return `${specifier}${OUTPUT_EXTENSION}`;
|
|
799
|
+
}
|
|
800
|
+
return specifier;
|
|
801
|
+
}
|
|
802
|
+
|
|
192
803
|
function rewriteSpecifiers(program, source, resolveExternal) {
|
|
193
804
|
for (const node of importSpecifiers(program)) {
|
|
194
805
|
const specifier = node.value;
|
|
@@ -200,7 +811,9 @@ function rewriteSpecifiers(program, source, resolveExternal) {
|
|
|
200
811
|
continue;
|
|
201
812
|
}
|
|
202
813
|
if (specifier.startsWith(".") || specifier.startsWith("/")) continue;
|
|
203
|
-
const
|
|
814
|
+
const normalized = normalizePackageSubpath(specifier);
|
|
815
|
+
node.value = normalized;
|
|
816
|
+
const external = resolveExternal?.(normalized, source);
|
|
204
817
|
if (typeof external === "string" && external.length > 0) node.value = external;
|
|
205
818
|
}
|
|
206
819
|
}
|
|
@@ -216,6 +829,9 @@ export function compileSource(
|
|
|
216
829
|
options = {},
|
|
217
830
|
) {
|
|
218
831
|
const program = compile_ir_string(source, filename, options.macroContext ?? null);
|
|
832
|
+
if (options.optimize !== false) {
|
|
833
|
+
optimizeProgramIr(program);
|
|
834
|
+
}
|
|
219
835
|
rewriteSpecifiers(program, filename, options.resolveExternal);
|
|
220
836
|
const output = outputPathFor(filename);
|
|
221
837
|
const emission = emit_ir_string_with_source_map(
|
|
@@ -224,14 +840,17 @@ export function compileSource(
|
|
|
224
840
|
output,
|
|
225
841
|
mapSourcePath(output, filename),
|
|
226
842
|
);
|
|
227
|
-
|
|
228
|
-
javascript
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
843
|
+
const rewritten = rewriteEmittedExternals(
|
|
844
|
+
emission.javascript,
|
|
845
|
+
filename,
|
|
846
|
+
options.resolveExternal,
|
|
847
|
+
);
|
|
848
|
+
return applyPostCompileOptimization(
|
|
849
|
+
rewritten,
|
|
850
|
+
emission.sourceMap,
|
|
851
|
+
{ filename, output },
|
|
852
|
+
options,
|
|
853
|
+
);
|
|
235
854
|
}
|
|
236
855
|
|
|
237
856
|
/**
|
|
@@ -247,6 +866,9 @@ export function compileProject({
|
|
|
247
866
|
entry,
|
|
248
867
|
macroContext = null,
|
|
249
868
|
resolveExternal,
|
|
869
|
+
optimize = true,
|
|
870
|
+
minify = false,
|
|
871
|
+
closureCompiler,
|
|
250
872
|
}) {
|
|
251
873
|
for (const path of Object.keys(files)) outputPathFor(path);
|
|
252
874
|
if (files[entry] === undefined) {
|
|
@@ -262,6 +884,9 @@ export function compileProject({
|
|
|
262
884
|
let program;
|
|
263
885
|
try {
|
|
264
886
|
program = compile_ir_string(files[id], id, macroContext);
|
|
887
|
+
if (optimize !== false) {
|
|
888
|
+
optimizeProgramIr(program);
|
|
889
|
+
}
|
|
265
890
|
} catch (error) {
|
|
266
891
|
throw new BrowserCompileError([diagnosticOf(error, id)]);
|
|
267
892
|
}
|
|
@@ -284,11 +909,18 @@ export function compileProject({
|
|
|
284
909
|
output,
|
|
285
910
|
mapSourcePath(output, id),
|
|
286
911
|
);
|
|
912
|
+
const rewritten = rewriteEmittedExternals(emission.javascript, id, resolveExternal);
|
|
913
|
+
const optimized = applyPostCompileOptimization(
|
|
914
|
+
rewritten,
|
|
915
|
+
emission.sourceMap,
|
|
916
|
+
{ filename: id, output },
|
|
917
|
+
{ optimize, minify, closureCompiler },
|
|
918
|
+
);
|
|
287
919
|
modules[output] = {
|
|
288
920
|
id,
|
|
289
921
|
source: files[id],
|
|
290
|
-
javascript:
|
|
291
|
-
sourceMap:
|
|
922
|
+
javascript: optimized.javascript,
|
|
923
|
+
sourceMap: optimized.sourceMap,
|
|
292
924
|
};
|
|
293
925
|
}
|
|
294
926
|
return { plan, modules };
|