@elaraai/east 1.0.67 → 1.0.69
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/dist/src/ast.d.ts +2 -0
- package/dist/src/ast.d.ts.map +1 -1
- package/dist/src/ast_to_ir.d.ts +7 -3
- package/dist/src/ast_to_ir.d.ts.map +1 -1
- package/dist/src/ast_to_ir.js +62 -15
- package/dist/src/ast_to_ir.js.map +1 -1
- package/dist/src/codegen/codegen.spec.d.ts +6 -0
- package/dist/src/codegen/codegen.spec.d.ts.map +1 -0
- package/dist/src/codegen/codegen.spec.js +763 -0
- package/dist/src/codegen/codegen.spec.js.map +1 -0
- package/dist/src/codegen/doc.d.ts +111 -0
- package/dist/src/codegen/doc.d.ts.map +1 -0
- package/dist/src/codegen/doc.js +350 -0
- package/dist/src/codegen/doc.js.map +1 -0
- package/dist/src/codegen/doc.spec.d.ts +6 -0
- package/dist/src/codegen/doc.spec.d.ts.map +1 -0
- package/dist/src/codegen/doc.spec.js +86 -0
- package/dist/src/codegen/doc.spec.js.map +1 -0
- package/dist/src/codegen/index.d.ts +15 -0
- package/dist/src/codegen/index.d.ts.map +1 -0
- package/dist/src/codegen/index.js +15 -0
- package/dist/src/codegen/index.js.map +1 -0
- package/dist/src/codegen/printer.d.ts +63 -0
- package/dist/src/codegen/printer.d.ts.map +1 -0
- package/dist/src/codegen/printer.js +1529 -0
- package/dist/src/codegen/printer.js.map +1 -0
- package/dist/src/codegen/spellings.d.ts +73 -0
- package/dist/src/codegen/spellings.d.ts.map +1 -0
- package/dist/src/codegen/spellings.js +298 -0
- package/dist/src/codegen/spellings.js.map +1 -0
- package/dist/src/codegen/spellings.spec.d.ts +6 -0
- package/dist/src/codegen/spellings.spec.d.ts.map +1 -0
- package/dist/src/codegen/spellings.spec.js +191 -0
- package/dist/src/codegen/spellings.spec.js.map +1 -0
- package/dist/src/codegen/types.d.ts +67 -0
- package/dist/src/codegen/types.d.ts.map +1 -0
- package/dist/src/codegen/types.js +173 -0
- package/dist/src/codegen/types.js.map +1 -0
- package/dist/src/compile.js +10 -10
- package/dist/src/compile.js.map +1 -1
- package/dist/src/expr/block.d.ts +148 -0
- package/dist/src/expr/block.d.ts.map +1 -1
- package/dist/src/expr/block.js +168 -4
- package/dist/src/expr/block.js.map +1 -1
- package/dist/src/expr/index.d.ts +155 -2
- package/dist/src/expr/index.d.ts.map +1 -1
- package/dist/src/expr/index.js +155 -2
- package/dist/src/expr/index.js.map +1 -1
- package/dist/src/functions.crossimport.spec.d.ts +6 -0
- package/dist/src/functions.crossimport.spec.d.ts.map +1 -0
- package/dist/src/functions.crossimport.spec.js +45 -0
- package/dist/src/functions.crossimport.spec.js.map +1 -0
- package/dist/src/functions.d.ts +4103 -0
- package/dist/src/functions.d.ts.map +1 -0
- package/dist/src/functions.js +443 -0
- package/dist/src/functions.js.map +1 -0
- package/dist/src/functions.spec.d.ts +6 -0
- package/dist/src/functions.spec.d.ts.map +1 -0
- package/dist/src/functions.spec.js +123 -0
- package/dist/src/functions.spec.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/naming.d.ts +50 -0
- package/dist/src/naming.d.ts.map +1 -0
- package/dist/src/naming.js +194 -0
- package/dist/src/naming.js.map +1 -0
- package/dist/src/naming.spec.d.ts +6 -0
- package/dist/src/naming.spec.d.ts.map +1 -0
- package/dist/src/naming.spec.js +154 -0
- package/dist/src/naming.spec.js.map +1 -0
- package/dist/src/serialization/beast2/index.spec.js +25 -0
- package/dist/src/serialization/beast2/index.spec.js.map +1 -1
- package/dist/src/serialization/beast2/shared.d.ts +1 -1
- package/dist/src/serialization/beast2/shared.d.ts.map +1 -1
- package/dist/src/serialization/beast2/shared.js +9 -1
- package/dist/src/serialization/beast2/shared.js.map +1 -1
- package/package.json +11 -2
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The spelling table against the surface it spells (#645).
|
|
7
|
+
*
|
|
8
|
+
* A row's `exprs` names the argument slots the surface types `Expr`-only —
|
|
9
|
+
* where a literal must print through `East.value(v, T)`; every other slot
|
|
10
|
+
* is typed `SubtypeExprOrValue` (or a host value union) and a construction
|
|
11
|
+
* prints there as the host literal. The TypeScript signatures are the
|
|
12
|
+
* source of truth for that distinction, so this spec reads them — the
|
|
13
|
+
* expression classes and the `East.*` helpers under `src/expr/`, parsed by
|
|
14
|
+
* the TypeScript compiler — and checks every slot of every row: a slot in
|
|
15
|
+
* `exprs` is `Expr`-only in the signature, a slot in `inferred` is an
|
|
16
|
+
* unconstrained type parameter (the East type comes from the argument), and
|
|
17
|
+
* every other slot accepts a value under a type the surface supplies. A
|
|
18
|
+
* slot the signatures do not settle is an error, not a skip.
|
|
19
|
+
*/
|
|
20
|
+
import { test } from "node:test";
|
|
21
|
+
import assert from "node:assert/strict";
|
|
22
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
23
|
+
import { join } from "node:path";
|
|
24
|
+
import { fileURLToPath } from "node:url";
|
|
25
|
+
import ts from "typescript";
|
|
26
|
+
import { SPELLINGS } from "./spellings.js";
|
|
27
|
+
const EXPR_DIR = fileURLToPath(new URL("../../../src/expr/", import.meta.url));
|
|
28
|
+
/** A bare type-parameter name (the signature's own, unconstrained) — the East type is inferred from the argument. */
|
|
29
|
+
const INFERRED = "<inferred>";
|
|
30
|
+
function kindOf(type) {
|
|
31
|
+
if (type === INFERRED)
|
|
32
|
+
return "inferred";
|
|
33
|
+
if (/=>/.test(type))
|
|
34
|
+
return "callback";
|
|
35
|
+
if (/SubtypeExprOrValue|ExprOrValue|ValueTypeOf|\b(?:bigint|number|string|boolean|Date|Uint8Array|Map|Set|Float64Array|BigInt64Array|Uint8ClampedArray|null|RegExp)\b/.test(type))
|
|
36
|
+
return "value";
|
|
37
|
+
if (/Expr(?:Type)?(?:\b|<)/.test(type))
|
|
38
|
+
return "expr";
|
|
39
|
+
return "unknown";
|
|
40
|
+
}
|
|
41
|
+
/** Every signature (overloads and implementation) of `name` in `node`'s function-like members. */
|
|
42
|
+
class Surface {
|
|
43
|
+
files = new Map();
|
|
44
|
+
constructor() {
|
|
45
|
+
for (const dir of [EXPR_DIR, join(EXPR_DIR, "libs")]) {
|
|
46
|
+
for (const f of readdirSync(dir)) {
|
|
47
|
+
if (!f.endsWith(".ts") || f.endsWith(".spec.ts"))
|
|
48
|
+
continue;
|
|
49
|
+
const path = join(dir, f);
|
|
50
|
+
this.files.set(path, ts.createSourceFile(path, readFileSync(path, "utf-8"), ts.ScriptTarget.Latest, true));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
file(name) {
|
|
55
|
+
const hit = this.files.get(join(EXPR_DIR, name));
|
|
56
|
+
if (hit === undefined)
|
|
57
|
+
throw new Error(`no surface file ${name}`);
|
|
58
|
+
return hit;
|
|
59
|
+
}
|
|
60
|
+
/** The parameters of a signature: a type parameter (also under `NoInfer`) reads as its constraint, or as {@link INFERRED} when it has none. */
|
|
61
|
+
static params(sig, sf) {
|
|
62
|
+
const constraints = new Map();
|
|
63
|
+
for (const tp of sig.typeParameters ?? [])
|
|
64
|
+
constraints.set(tp.name.text, tp.constraint?.getText(sf) ?? INFERRED);
|
|
65
|
+
return sig.parameters.map(p => {
|
|
66
|
+
const text = (p.type?.getText(sf) ?? "").replace(/^NoInfer<(.*)>$/, "$1");
|
|
67
|
+
return { type: constraints.get(text) ?? text, rest: p.dotDotDotToken !== undefined };
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** The signatures of method `name` on class `className` (its own file). */
|
|
71
|
+
method(className, name) {
|
|
72
|
+
const sf = this.file(`${className.slice(0, -4).toLowerCase()}.ts`);
|
|
73
|
+
const out = [];
|
|
74
|
+
const visit = (n) => {
|
|
75
|
+
if (ts.isClassDeclaration(n) && n.name?.text === className) {
|
|
76
|
+
for (const m of n.members) {
|
|
77
|
+
if (ts.isMethodDeclaration(m) && ts.isIdentifier(m.name) && m.name.text === name)
|
|
78
|
+
out.push(Surface.params(m, sf));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
ts.forEachChild(n, visit);
|
|
82
|
+
};
|
|
83
|
+
visit(sf);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/** The signatures of the top-level function `name` in `file`. */
|
|
87
|
+
fn(file, name) {
|
|
88
|
+
const sf = this.file(file);
|
|
89
|
+
const out = [];
|
|
90
|
+
for (const s of sf.statements) {
|
|
91
|
+
if (ts.isFunctionDeclaration(s) && s.name?.text === name)
|
|
92
|
+
out.push(Surface.params(s, sf));
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
/** The signatures of member `name` of the default-exported object of `libs/<ns>.ts` — a method, or an imported function. */
|
|
97
|
+
lib(ns, name) {
|
|
98
|
+
const file = `libs/${ns.toLowerCase()}.ts`;
|
|
99
|
+
const sf = this.file(file);
|
|
100
|
+
const out = [];
|
|
101
|
+
const visit = (n) => {
|
|
102
|
+
if (ts.isObjectLiteralExpression(n)) {
|
|
103
|
+
for (const m of n.properties) {
|
|
104
|
+
if (ts.isMethodDeclaration(m) && ts.isIdentifier(m.name) && m.name.text === name)
|
|
105
|
+
out.push(Surface.params(m, sf));
|
|
106
|
+
if (ts.isShorthandPropertyAssignment(m) && m.name.text === name)
|
|
107
|
+
out.push(...this.fn(`${ns.toLowerCase()}.ts`, name));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
ts.forEachChild(n, visit);
|
|
111
|
+
};
|
|
112
|
+
visit(sf);
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** The receiver class of a builtin by its name prefix; `null` for a namespace helper. */
|
|
117
|
+
function receiverClass(builtin) {
|
|
118
|
+
if (builtin === "Parse" || builtin.startsWith("Regex"))
|
|
119
|
+
return "StringExpr";
|
|
120
|
+
for (const prefix of ["Array", "Blob", "Boolean", "DateTime", "Dict", "Float", "Integer", "Matrix", "Ref", "Set", "String", "Vector"]) {
|
|
121
|
+
if (builtin.startsWith(prefix))
|
|
122
|
+
return `${prefix}Expr`;
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`no receiver class for ${builtin}`);
|
|
125
|
+
}
|
|
126
|
+
/** The top-level comma-separated argument templates of `head(args)`. */
|
|
127
|
+
function argTemplates(template) {
|
|
128
|
+
let depth = 0;
|
|
129
|
+
for (let i = template.length - 1; i >= 0; i--) {
|
|
130
|
+
if (template[i] === ")")
|
|
131
|
+
depth += 1;
|
|
132
|
+
else if (template[i] === "(") {
|
|
133
|
+
depth -= 1;
|
|
134
|
+
if (depth === 0) {
|
|
135
|
+
const inner = template.slice(i + 1, -1).trim();
|
|
136
|
+
return { head: template.slice(0, i), args: inner === "" ? [] : inner.split(",").map(a => a.trim()) };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
throw new Error(`not a call template: ${template}`);
|
|
141
|
+
}
|
|
142
|
+
test("every slot of the spelling table agrees with the surface's signatures: `exprs` are the Expr-only slots", () => {
|
|
143
|
+
const surface = new Surface();
|
|
144
|
+
const problems = [];
|
|
145
|
+
for (const [builtin, row] of Object.entries(SPELLINGS)) {
|
|
146
|
+
const { head, args } = argTemplates(row.template);
|
|
147
|
+
const method = /^\{\d+\}\.(\w+)$/.exec(head);
|
|
148
|
+
const namespaced = /^East\.(\w+)\.(\w+)$/.exec(head);
|
|
149
|
+
const root = /^East\.(\w+)$/.exec(head);
|
|
150
|
+
const signatures = method !== null
|
|
151
|
+
? surface.method(receiverClass(builtin), method[1])
|
|
152
|
+
: namespaced !== null
|
|
153
|
+
? surface.lib(namespaced[1], namespaced[2])
|
|
154
|
+
: root !== null
|
|
155
|
+
? surface.fn("block.ts", root[1])
|
|
156
|
+
: [];
|
|
157
|
+
if (signatures.length === 0) {
|
|
158
|
+
problems.push(`${builtin}: no signature found for ${head}`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const callbacks = new Set(row.callbacks ?? []);
|
|
162
|
+
const exprs = new Set(row.exprs ?? []);
|
|
163
|
+
const inferred = new Set(row.inferred ?? []);
|
|
164
|
+
args.forEach((arg, position) => {
|
|
165
|
+
const slot = /^\{(\d+)\}$/.exec(arg);
|
|
166
|
+
if (slot === null)
|
|
167
|
+
return; // a type parameter, the RegExp or the CSV options
|
|
168
|
+
const k = Number(slot[1]);
|
|
169
|
+
if (k === 0 || callbacks.has(k))
|
|
170
|
+
return; // the receiver is always an Expr; callbacks print as bodies
|
|
171
|
+
// the parameter at this position in every overload (a rest parameter covers the tail)
|
|
172
|
+
const kinds = new Set();
|
|
173
|
+
for (const sig of signatures) {
|
|
174
|
+
const p = sig[position] ?? (sig.length > 0 && sig[sig.length - 1].rest ? sig[sig.length - 1] : undefined);
|
|
175
|
+
if (p !== undefined)
|
|
176
|
+
kinds.add(kindOf(p.type));
|
|
177
|
+
}
|
|
178
|
+
const where = `${builtin}: slot ${k} (${arg} of ${row.template})`;
|
|
179
|
+
// what the surface says: a typed value slot in any overload wins; else inferred; else Expr-only
|
|
180
|
+
const surface = kinds.has("value") ? "value" : kinds.has("inferred") ? "inferred" : kinds.has("expr") && !kinds.has("unknown") ? "expr" : "unknown";
|
|
181
|
+
// what the table says
|
|
182
|
+
const table = exprs.has(k) ? "expr" : inferred.has(k) ? "inferred" : "value";
|
|
183
|
+
if (surface === "unknown")
|
|
184
|
+
problems.push(`${where} has an unsettled type (${[...kinds].join(", ")})`);
|
|
185
|
+
else if (surface !== table)
|
|
186
|
+
problems.push(`${where} is ${surface} in the surface but ${table} in the table`);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
assert.deepEqual(problems, [], `\n ${problems.join("\n ")}`);
|
|
190
|
+
});
|
|
191
|
+
//# sourceMappingURL=spellings.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spellings.spec.js","sourceRoot":"","sources":["../../../src/codegen/spellings.spec.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,GAAG,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAS/E,qHAAqH;AACrH,MAAM,QAAQ,GAAG,YAAY,CAAC;AAE9B,SAAS,MAAM,CAAC,IAAY;IAC1B,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,UAAU,CAAC;IACzC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,UAAU,CAAC;IACvC,IAAI,kKAAkK,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,OAAO,CAAC;IAClM,IAAI,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,MAAM,CAAC;IACtD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,kGAAkG;AAClG,MAAM,OAAO;IACF,KAAK,GAAG,IAAI,GAAG,EAAyB,CAAC;IAElD;QACE,KAAK,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC;YACrD,KAAK,M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Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Printing East type VALUES as TypeScript type-constructor source (#628).
|
|
7
|
+
*
|
|
8
|
+
* The IR carries every type as an `EastTypeValue` — the homoiconic value of
|
|
9
|
+
* `EastTypeType` — and the printer needs the TypeScript expression that
|
|
10
|
+
* rebuilds it: `ArrayType(IntegerType)`, `StructType({ a: IntegerType })`,
|
|
11
|
+
* `OptionType(T)` for the exact Option shape, `RecursiveType(self => ...)`
|
|
12
|
+
* for a recursive wrapper (nested wrappers name their self-references
|
|
13
|
+
* `self`, `self2`, ...). A recursive `ref` resolves to the enclosing
|
|
14
|
+
* wrapper's lambda parameter; a free ref (a fragment lifted out of its
|
|
15
|
+
* wrapper) is an error. The python twin is `east/codegen/types.py`.
|
|
16
|
+
*/
|
|
17
|
+
import type { EastTypeValue } from "../type_of_type.js";
|
|
18
|
+
import { type Doc } from "./doc.js";
|
|
19
|
+
/** The names a printed module imports from `@elaraai/east` for type source. */
|
|
20
|
+
export declare const TYPE_IMPORTS: readonly ["NullType", "NeverType", "BooleanType", "IntegerType", "FloatType", "StringType", "DateTimeType", "BlobType", "ArrayType", "SetType", "DictType", "StructType", "VariantType", "OptionType", "RefType", "VectorType", "MatrixType", "FunctionType", "AsyncFunctionType", "RecursiveType"];
|
|
21
|
+
/** Whether `t` is the exact Option shape: `Variant{ none: Null, some: T }`. */
|
|
22
|
+
export declare function isOptionValue(t: EastTypeValue): boolean;
|
|
23
|
+
/** A struct field / variant case name as a JavaScript object key. */
|
|
24
|
+
export declare function objectKey(name: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* An East type value as the layout document of the TypeScript source that
|
|
27
|
+
* rebuilds it: a struct, variant or parameter list breaks one entry per
|
|
28
|
+
* line when the line it sits on would pass the width (`codegen/doc`).
|
|
29
|
+
*
|
|
30
|
+
* @param t - The type value (the `type` field of an IR node, a type parameter)
|
|
31
|
+
* @param scope - The stack of enclosing recursive wrappers as `[id, parameter name]`
|
|
32
|
+
* @returns The document; {@link typeSource} is its one-line rendering
|
|
33
|
+
* @throws {Error} When a recursive `ref` has no enclosing wrapper in `scope`
|
|
34
|
+
*/
|
|
35
|
+
export declare function typeDoc(t: EastTypeValue, scope?: [bigint, string][]): Doc;
|
|
36
|
+
/**
|
|
37
|
+
* Prints an East type value as the TypeScript source that rebuilds it, on
|
|
38
|
+
* one line.
|
|
39
|
+
*
|
|
40
|
+
* @param t - The type value (the `type` field of an IR node, a type parameter)
|
|
41
|
+
* @param scope - The stack of enclosing recursive wrappers as `[id, parameter name]`
|
|
42
|
+
* @returns TypeScript source such as `DictType(StringType, ArrayType(IntegerType))`
|
|
43
|
+
* @throws {Error} When a recursive `ref` has no enclosing wrapper in `scope`
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* typeSource(toEastTypeValue(ArrayType(OptionType(IntegerType))));
|
|
48
|
+
* // "ArrayType(OptionType(IntegerType))"
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function typeSource(t: EastTypeValue, scope?: [bigint, string][]): string;
|
|
52
|
+
/**
|
|
53
|
+
* A structural key for deduplicating hoisted type constants: two type values
|
|
54
|
+
* that print to the same source rebuild the same type, whatever recursive
|
|
55
|
+
* ids they were minted with.
|
|
56
|
+
*/
|
|
57
|
+
export declare function typeKey(t: EastTypeValue): string;
|
|
58
|
+
/**
|
|
59
|
+
* Adds to `into` the constructor names `typeSource(t)` spells — what a
|
|
60
|
+
* printed module must import for the type. A walk over the type, case for
|
|
61
|
+
* case with {@link typeSource}.
|
|
62
|
+
*
|
|
63
|
+
* @param t - The type value
|
|
64
|
+
* @param into - The set of imported names being collected
|
|
65
|
+
*/
|
|
66
|
+
export declare function typeConstructors(t: EastTypeValue, into: Set<string>): void;
|
|
67
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/codegen/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,KAAK,GAAG,EAAiB,MAAM,UAAU,CAAC;AAEnD,+EAA+E;AAC/E,eAAO,MAAM,YAAY,qSAKf,CAAC;AAOX,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,CAAC,EAAE,aAAa,GAAG,OAAO,CAKvD;AAID,qEAAqE;AACrE,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,GAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAO,GAAG,GAAG,CAwC7E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,GAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAO,GAAG,MAAM,CAEnF;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,aAAa,GAAG,MAAM,CAEhD;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAmD1E"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
import { bracket, flat } from "./doc.js";
|
|
6
|
+
/** The names a printed module imports from `@elaraai/east` for type source. */
|
|
7
|
+
export const TYPE_IMPORTS = [
|
|
8
|
+
"NullType", "NeverType", "BooleanType", "IntegerType", "FloatType", "StringType",
|
|
9
|
+
"DateTimeType", "BlobType", "ArrayType", "SetType", "DictType", "StructType",
|
|
10
|
+
"VariantType", "OptionType", "RefType", "VectorType", "MatrixType", "FunctionType",
|
|
11
|
+
"AsyncFunctionType", "RecursiveType",
|
|
12
|
+
];
|
|
13
|
+
const PRIMITIVES = {
|
|
14
|
+
Null: "NullType", Never: "NeverType", Boolean: "BooleanType", Integer: "IntegerType",
|
|
15
|
+
Float: "FloatType", String: "StringType", DateTime: "DateTimeType", Blob: "BlobType",
|
|
16
|
+
};
|
|
17
|
+
/** Whether `t` is the exact Option shape: `Variant{ none: Null, some: T }`. */
|
|
18
|
+
export function isOptionValue(t) {
|
|
19
|
+
if (t.type !== "Variant")
|
|
20
|
+
return false;
|
|
21
|
+
const cases = t.value;
|
|
22
|
+
return cases.length === 2 && cases[0].name === "none" && cases[0].type.type === "Null"
|
|
23
|
+
&& cases[1].name === "some";
|
|
24
|
+
}
|
|
25
|
+
const IDENT = /^[A-Za-z_$][\w$]*$/;
|
|
26
|
+
/** A struct field / variant case name as a JavaScript object key. */
|
|
27
|
+
export function objectKey(name) {
|
|
28
|
+
return IDENT.test(name) ? name : JSON.stringify(name);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* An East type value as the layout document of the TypeScript source that
|
|
32
|
+
* rebuilds it: a struct, variant or parameter list breaks one entry per
|
|
33
|
+
* line when the line it sits on would pass the width (`codegen/doc`).
|
|
34
|
+
*
|
|
35
|
+
* @param t - The type value (the `type` field of an IR node, a type parameter)
|
|
36
|
+
* @param scope - The stack of enclosing recursive wrappers as `[id, parameter name]`
|
|
37
|
+
* @returns The document; {@link typeSource} is its one-line rendering
|
|
38
|
+
* @throws {Error} When a recursive `ref` has no enclosing wrapper in `scope`
|
|
39
|
+
*/
|
|
40
|
+
export function typeDoc(t, scope = []) {
|
|
41
|
+
const kind = t.type;
|
|
42
|
+
const primitive = PRIMITIVES[kind];
|
|
43
|
+
if (primitive !== undefined)
|
|
44
|
+
return primitive;
|
|
45
|
+
if (kind === "Array" || kind === "Set" || kind === "Ref" || kind === "Vector" || kind === "Matrix") {
|
|
46
|
+
return [`${kind}Type(`, typeDoc(t.value, scope), ")"];
|
|
47
|
+
}
|
|
48
|
+
if (kind === "Dict") {
|
|
49
|
+
const d = t.value;
|
|
50
|
+
return ["DictType(", typeDoc(d.key, scope), ", ", typeDoc(d.value, scope), ")"];
|
|
51
|
+
}
|
|
52
|
+
if (kind === "Struct") {
|
|
53
|
+
const fields = t.value
|
|
54
|
+
.map((f) => [objectKey(f.name), ": ", typeDoc(f.type, scope)]);
|
|
55
|
+
return ["StructType(", bracket("{", fields, "}", " "), ")"];
|
|
56
|
+
}
|
|
57
|
+
if (kind === "Variant") {
|
|
58
|
+
const cases = t.value;
|
|
59
|
+
if (isOptionValue(t))
|
|
60
|
+
return ["OptionType(", typeDoc(cases[1].type, scope), ")"];
|
|
61
|
+
const parts = cases.map((c) => [objectKey(c.name), ": ", typeDoc(c.type, scope)]);
|
|
62
|
+
return ["VariantType(", bracket("{", parts, "}", " "), ")"];
|
|
63
|
+
}
|
|
64
|
+
if (kind === "Function" || kind === "AsyncFunction") {
|
|
65
|
+
const f = t.value;
|
|
66
|
+
const inputs = bracket("[", f.inputs.map(i => typeDoc(i, scope)), "]");
|
|
67
|
+
return [`${kind}Type(`, inputs, ", ", typeDoc(f.output, scope), ")"];
|
|
68
|
+
}
|
|
69
|
+
if (kind === "Recursive") {
|
|
70
|
+
const payload = t.value;
|
|
71
|
+
if (payload.type === "ref") {
|
|
72
|
+
for (let i = scope.length - 1; i >= 0; i--) {
|
|
73
|
+
if (scope[i][0] === payload.value)
|
|
74
|
+
return scope[i][1];
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`recursive ref ${payload.value} outside its wrapper`);
|
|
77
|
+
}
|
|
78
|
+
const name = scope.length === 0 ? "self" : `self${scope.length + 1}`;
|
|
79
|
+
const body = typeDoc(payload.value.inner, [...scope, [payload.value.id, name]]);
|
|
80
|
+
return [`RecursiveType(${name} => `, body, ")"];
|
|
81
|
+
}
|
|
82
|
+
throw new Error(`unknown type kind ${kind}`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Prints an East type value as the TypeScript source that rebuilds it, on
|
|
86
|
+
* one line.
|
|
87
|
+
*
|
|
88
|
+
* @param t - The type value (the `type` field of an IR node, a type parameter)
|
|
89
|
+
* @param scope - The stack of enclosing recursive wrappers as `[id, parameter name]`
|
|
90
|
+
* @returns TypeScript source such as `DictType(StringType, ArrayType(IntegerType))`
|
|
91
|
+
* @throws {Error} When a recursive `ref` has no enclosing wrapper in `scope`
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* typeSource(toEastTypeValue(ArrayType(OptionType(IntegerType))));
|
|
96
|
+
* // "ArrayType(OptionType(IntegerType))"
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function typeSource(t, scope = []) {
|
|
100
|
+
return flat(typeDoc(t, scope));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A structural key for deduplicating hoisted type constants: two type values
|
|
104
|
+
* that print to the same source rebuild the same type, whatever recursive
|
|
105
|
+
* ids they were minted with.
|
|
106
|
+
*/
|
|
107
|
+
export function typeKey(t) {
|
|
108
|
+
return typeSource(t);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Adds to `into` the constructor names `typeSource(t)` spells — what a
|
|
112
|
+
* printed module must import for the type. A walk over the type, case for
|
|
113
|
+
* case with {@link typeSource}.
|
|
114
|
+
*
|
|
115
|
+
* @param t - The type value
|
|
116
|
+
* @param into - The set of imported names being collected
|
|
117
|
+
*/
|
|
118
|
+
export function typeConstructors(t, into) {
|
|
119
|
+
const kind = t.type;
|
|
120
|
+
const primitive = PRIMITIVES[kind];
|
|
121
|
+
if (primitive !== undefined) {
|
|
122
|
+
into.add(primitive);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (kind === "Array" || kind === "Set" || kind === "Ref" || kind === "Vector" || kind === "Matrix") {
|
|
126
|
+
into.add(`${kind}Type`);
|
|
127
|
+
typeConstructors(t.value, into);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (kind === "Dict") {
|
|
131
|
+
const d = t.value;
|
|
132
|
+
into.add("DictType");
|
|
133
|
+
typeConstructors(d.key, into);
|
|
134
|
+
typeConstructors(d.value, into);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (kind === "Struct") {
|
|
138
|
+
into.add("StructType");
|
|
139
|
+
for (const f of t.value)
|
|
140
|
+
typeConstructors(f.type, into);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (kind === "Variant") {
|
|
144
|
+
const cases = t.value;
|
|
145
|
+
if (isOptionValue(t)) {
|
|
146
|
+
into.add("OptionType");
|
|
147
|
+
typeConstructors(cases[1].type, into);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
into.add("VariantType");
|
|
151
|
+
for (const c of cases)
|
|
152
|
+
typeConstructors(c.type, into);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (kind === "Function" || kind === "AsyncFunction") {
|
|
156
|
+
const f = t.value;
|
|
157
|
+
into.add(`${kind}Type`);
|
|
158
|
+
for (const i of f.inputs)
|
|
159
|
+
typeConstructors(i, into);
|
|
160
|
+
typeConstructors(f.output, into);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (kind === "Recursive") {
|
|
164
|
+
const payload = t.value;
|
|
165
|
+
if (payload.type === "wrapper") {
|
|
166
|
+
into.add("RecursiveType");
|
|
167
|
+
typeConstructors(payload.value.inner, into);
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
throw new Error(`unknown type kind ${kind}`);
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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xB,KAAK,MAAM,CAAC,IAAI,KAAK;YAAE,gBAAgB,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtD,OAAO;IACT,CAAC;IACD,IAAI,IAAI,KAAK,UAAU,IAAI,IAAI,KAAK,eAAe,EAAE,CAAC;QACpD,MAAM,CAAC,GAAG,CAAC,CAAC,KAA2D,CAAC;QACxE,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,MAAM,CAAC,CAAC;QACxB,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM;YAAE,gBAAgB,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;QACpD,gBAAgB,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACjC,OAAO;IACT,CAAC;IACD,IAAI,IAAI,KAAK,WAAW,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,CAAC,CAAC,KAA0G,CAAC;QAC7H,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC/B,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;YAC1B,gBAAgB,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO;IACT,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,qBAAqB,IAAI,EAAE,CAAC,CAAC;AAC/C,CAAC"}
|
package/dist/src/compile.js
CHANGED
|
@@ -3501,12 +3501,12 @@ const builtin_evaluators = {
|
|
|
3501
3501
|
}
|
|
3502
3502
|
return matrix(vec.slice(), r, c);
|
|
3503
3503
|
},
|
|
3504
|
-
VectorZeros: (_loc_id, _source_map, _platformDef,
|
|
3505
|
-
return
|
|
3504
|
+
VectorZeros: (_loc_id, _source_map, _platformDef, T) => (len) => {
|
|
3505
|
+
return allocateTypedArray(T, Number(len));
|
|
3506
3506
|
},
|
|
3507
|
-
VectorOnes: (_loc_id, _source_map, _platformDef,
|
|
3508
|
-
const arr =
|
|
3509
|
-
arr.fill(1
|
|
3507
|
+
VectorOnes: (_loc_id, _source_map, _platformDef, T) => (len) => {
|
|
3508
|
+
const arr = allocateTypedArray(T, Number(len));
|
|
3509
|
+
arr.fill((arr instanceof BigInt64Array ? 1n : 1));
|
|
3510
3510
|
return arr;
|
|
3511
3511
|
},
|
|
3512
3512
|
VectorFill: (loc_id, source_map, _platformDef, T) => (len, value) => {
|
|
@@ -4109,12 +4109,12 @@ const builtin_evaluators = {
|
|
|
4109
4109
|
}
|
|
4110
4110
|
return matrix(result, m.cols, m.rows);
|
|
4111
4111
|
},
|
|
4112
|
-
MatrixZeros: (_loc_id, _source_map, _platformDef,
|
|
4113
|
-
return matrix(
|
|
4112
|
+
MatrixZeros: (_loc_id, _source_map, _platformDef, T) => (rows, cols) => {
|
|
4113
|
+
return matrix(allocateTypedArray(T, Number(rows) * Number(cols)), Number(rows), Number(cols));
|
|
4114
4114
|
},
|
|
4115
|
-
MatrixOnes: (_loc_id, _source_map, _platformDef,
|
|
4116
|
-
const data =
|
|
4117
|
-
data.fill(1
|
|
4115
|
+
MatrixOnes: (_loc_id, _source_map, _platformDef, T) => (rows, cols) => {
|
|
4116
|
+
const data = allocateTypedArray(T, Number(rows) * Number(cols));
|
|
4117
|
+
data.fill((data instanceof BigInt64Array ? 1n : 1));
|
|
4118
4118
|
return matrix(data, Number(rows), Number(cols));
|
|
4119
4119
|
},
|
|
4120
4120
|
MatrixFill: (loc_id, source_map, _platformDef, T) => (rows, cols, value) => {
|