@mark1russell7/vex-compiler 0.17.2 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memo-08WGBHWI.js","names":["FREE"],"sources":["../src/facts.ts","../src/unshared.ts","../src/memo.ts"],"sourcesContent":["/**\n * The facts of the options that the printer uses (docs/COMPILER.md §13.2). For each op site, they give the domain\n * whose op the interpreter takes first, and the kinds and the lifts of its parameters. The facts come from the op\n * tables, so the same options always give the same facts, and the same source.\n */\nimport { isListOp, isOpName, liftsAt, listItemKind, type AnyDomain, type ExtHandler, type FreeFn, type ListOp, type OpSpec, type ParamKind, type PrimitiveKind, type ReturnKind } from \"@mark1russell7/vex\";\n\n/**\n * The most bodies of functions that tier 2 puts at the calls of one program. A function that calls another function\n * twice doubles its body, so a chain of such functions grows fast. A module prints each function once.\n */\nexport const MAX_CALLS = 10_000;\n\n/** The list ops that are one loop with an `each`. Each other list op takes the list as a value (COMPILE.LIST.VALUE). */\nexport type FusedOp = Exclude<ListOp, \"sortBy\" | \"take\" | \"nth\" | \"setNth\" | \"drop\" | \"append\" | \"flat\" | \"without\" | \"get\" | \"indexOf\" | \"distinct\" | \"group\" | \"join\">;\n\nconst FUSED_OPS: ReadonlySet<string> = new Set<FusedOp>([\"count\", \"sum\", \"min\", \"max\", \"mean\", \"any\", \"all\", \"none\", \"values\", \"keys\", \"reduce\", \"first\", \"errors\"]);\n\n/** This function tells if a list op over an `each` is one loop in tier 2. The lowering gives the same answer to the memo. */\nexport const isFusedOp = (op: string): op is FusedOp => FUSED_OPS.has(op);\n\n/** The function of an op of a domain, or a method. */\nexport type Method = (...args: unknown[]) => unknown;\n\n/**\n * The kind of a value that the compiler knows: a primitive type, or `domain:Name`. The second kind is for a value of\n * the domain `Name` without a primitive type (KIND.DOMAIN).\n */\nexport type Kind = PrimitiveKind | `domain:${string}`;\n\n/** This function gives the kind of the values of a domain. */\nexport const kindOfDomain = (d: AnyDomain): Kind => d.primitive ?? `domain:${d.name}`;\n\n/** This function gives a kind if it is a primitive type, else `undefined`. A kind of a list is not a primitive type. */\nexport const primitiveOf = (k: Kind | ListKind | undefined): PrimitiveKind | undefined => (k === \"number\" || k === \"string\" || k === \"boolean\" ? k : undefined);\n\n/**\n * The kind of a list: the kind of its items, which can be a list too. A column of lists of a module of a sheet has such a\n * kind (COMPILE.MODULE.SHEET.LIST). With `texts`, a key of an item can be a key of a list literal or of `fill`, not the\n * key of a record. A field of the schema can have such a kind too (COMPILE.MODULE.LIST.INPUT), and a variable of the\n * schema can have it (COMPILE.MODULE.LIST.VAR).\n */\nexport interface ListKind {\n readonly list: Kind | ListKind;\n readonly texts?: boolean;\n}\n\n/**\n * The kinds that a host declares (docs/COMPILER.md §3.2): the kind of each field of the records, and of each variable of\n * the run. A read of a declared field or variable checks the kind. When the check fails, tier 1 evaluates the program.\n * A field and a variable can hold a list (COMPILE.MODULE.LIST.INPUT, COMPILE.MODULE.LIST.VAR). Tier 2 does not check\n * the kind of such a field or variable.\n */\nexport interface Schema {\n readonly fields?: Readonly<Record<string, Kind | ListKind>>;\n readonly vars?: Readonly<Record<string, Kind | ListKind>>;\n}\n\n/** The options of a program: the domains, the free functions and the extension handlers, and the schema of the host. */\nexport interface Options {\n readonly domains?: readonly AnyDomain[];\n readonly fns?: Readonly<Record<string, FreeFn>>;\n readonly extensions?: Readonly<Record<string, ExtHandler>>;\n readonly schema?: Schema;\n}\n\n/**\n * The op of one name in one domain, as `resolveOp` of the core finds it. It is the function of the spec, or else a\n * method of the receiver.\n */\nexport interface Entry {\n readonly domain: AnyDomain;\n readonly spec: OpSpec<unknown>;\n readonly fn: Method | undefined;\n}\n\nconst NO_SPEC: OpSpec<unknown> = Object.freeze({});\n\n/** This function gives the op of a name in a domain, or `undefined` for a domain without the op. */\nexport function entryOf(domain: AnyDomain, name: string): Entry | undefined {\n if (!isOpName(name)) return undefined;\n const declared = Object.hasOwn(domain.ops, name) ? (domain.ops[name] as OpSpec<unknown>) : undefined;\n if (declared === undefined) return domain.methods === \"all\" ? { domain, spec: NO_SPEC, fn: undefined } : undefined;\n return { domain, spec: declared, fn: declared.fn as Method | undefined };\n}\n\n/**\n * The facts of an op site:\n * - `op`: the op of the domain `domain` comes first. It is a method of the receiver, or else the function of the spec.\n * `params` are the kinds of the parameters, and `lift` lifts numbers (`liftScalar`, if the domain has `fromScalar`).\n * - `free`: no domain has the op, and a free function has the name.\n *\n * Without facts, the site uses the dispatch of the runtime only.\n */\nexport type SiteFacts =\n | {\n readonly t: \"op\";\n readonly domain: number;\n readonly method: boolean;\n readonly params: readonly ParamKind[] | undefined;\n readonly lift: OpSpec<unknown>[\"liftScalar\"];\n }\n | { readonly t: \"free\" };\n\nconst FREE: SiteFacts = Object.freeze({ t: \"free\" });\n\n/**\n * The op of a site whose values have known kinds (KIND.STATIC):\n * - `fn`: the function of the op of the domain `domain`. `bad` is true when an argument has a kind that the op does\n * not accept, so the interpreter gives an error. `returns` is the kind of the result, if the op declares it. `item`\n * is the kind of the items of a result that is a list (KIND.RETURNS.LIST). `lifted` has the positions after the\n * receiver of the numbers that `fromScalar` lifts, if the caller asks for them.\n * - `free`: no domain that accepts the receiver has the op, and a free function has the name.\n */\nexport type StaticSite =\n | { readonly t: \"fn\"; readonly domain: number; readonly bad: boolean; readonly returns: Kind | undefined; readonly item?: PrimitiveKind; readonly lifted?: readonly number[] }\n | { readonly t: \"free\" };\n\nconst STATIC_FREE: StaticSite = Object.freeze({ t: \"free\" });\n\n/** The positions of the lists of a site without lists. */\nconst NO_LISTS: ReadonlySet<number> = new Set();\n\n/** This function gives the kind of the result of an op of a domain, if the op declares it. */\nfunction returnsOf(domain: AnyDomain, kind: ReturnKind | undefined): Kind | undefined {\n if (kind === \"domain\") return kindOfDomain(domain);\n return kind === \"any\" || listItemKind(kind) !== undefined ? undefined : (kind as Kind | undefined);\n}\n\n/**\n * This function tells if an argument of a kind fits a parameter, or `undefined` if the kind of the argument is unknown.\n * A list fits only `any`, and the test `is` of a domain without a primitive type can accept it (COMPILE.MODULE.LIST.ARG).\n */\nfunction fits(domain: AnyDomain, param: ParamKind | undefined, arg: Kind | undefined, list: boolean): boolean | undefined {\n if (param === undefined || param === \"any\") return true;\n if (list) return param === \"domain\" && domain.primitive === undefined ? undefined : false;\n if (arg === undefined) return undefined;\n return arg === (param === \"domain\" ? kindOfDomain(domain) : param);\n}\n\n/** This function tells if two domains declare different field names, so they have no common value (KIND.FIELDS). */\nfunction disjoint(a: AnyDomain, b: AnyDomain | undefined): boolean {\n const x = a.fields;\n const y = b?.fields;\n return x !== undefined && y !== undefined && (x.length !== y.length || x.some((name) => !y.includes(name)));\n}\n\n/**\n * This function tells if a domain accepts a value of a kind. A domain with a primitive type accepts its type, and a\n * domain without one accepts the values of its kind. It does not accept a value of a domain with other fields. For\n * another value, it gives `undefined`: its `is` can accept it.\n */\nfunction accepts(domains: readonly AnyDomain[], d: AnyDomain, kind: Kind): boolean | undefined {\n if (d.primitive !== undefined) return d.primitive === kind;\n if (kind === kindOfDomain(d)) return true;\n return disjoint(d, domains.find((x) => kindOfDomain(x) === kind)) ? false : undefined;\n}\n\n/** The facts of the options. */\nexport interface Facts {\n /** This function gives the facts of the site of the op `name` with `arity` values, the receiver first, if it has facts. */\n site(name: string, arity: number): SiteFacts | undefined;\n /**\n * The checks of the result of an op, as `#checkResult` of the interpreter: for each domain, `true` if it has `valid`.\n * The list ends at the last domain with `valid`, because the domains after it check nothing.\n */\n readonly checks: readonly boolean[];\n /**\n * This function gives the op of a site with the kinds of its values, the receiver first. When the kinds do not decide\n * the op, it gives `undefined`. Each domain before the op must declare a primitive type. Then the interpreter takes\n * the same op at each run. `lists` has the positions of the values that are lists, after the receiver. A number that\n * the op lifts gives `undefined`, unless `lift` is true: then the caller lifts the numbers at the positions `lifted`.\n */\n staticSite(name: string, kinds: readonly (Kind | undefined)[], lists?: ReadonlySet<number>, lift?: boolean): StaticSite | undefined;\n /**\n * This function gives the index of the domain that checks a result of a kind. When no domain accepts such a value,\n * it gives -1. When a domain without a primitive type comes first, it gives `undefined`.\n */\n checkOf(kind: Kind): number | undefined;\n /**\n * This function tells if no domain and no function of the host has the op `name`. Then the interpreter takes its own\n * op for each receiver, for example `fill` (LIST.FILL).\n */\n own(name: string): boolean;\n /** This function tells if a domain declares a list as the result of the op `name` (KIND.RETURNS.LIST). */\n givesList(name: string): boolean;\n /** This function gives the number of domains with an identity of the op `name`, as `#reduce` of the interpreter counts them (LIST.EMPTY). */\n identities(name: string): number;\n}\n\n/** This function gives the facts of a set of options. */\nexport function factsOf(options: Options): Facts {\n const domains = options.domains ?? [];\n const fns = options.fns;\n const last = domains.findLastIndex((d) => d.valid !== undefined);\n return {\n checks: domains.slice(0, last + 1).map((d) => d.valid !== undefined),\n staticSite(name, kinds, lists = NO_LISTS, lift = false) {\n const [self, ...args] = kinds;\n // A primitive value is not a list, so a list op name is an op of a domain here, as in the interpreter.\n if (self === undefined) return undefined;\n for (const [i, d] of domains.entries()) {\n const takes = accepts(domains, d, self);\n if (takes === undefined) return undefined;\n const e = takes ? entryOf(d, name) : undefined;\n // A primitive value has no methods, so only the function of a spec is an op for it. The domain of a value without\n // a primitive type can have methods, and a later domain can accept the value too.\n if (e?.fn === undefined) {\n if (takes && d.primitive === undefined) return undefined;\n continue;\n }\n // A lifted number goes through fromScalar: the site keeps the dispatch of the runtime, or the caller lifts it. A\n // list is not a number, and neither is a value of another kind, so the interpreter does not lift them.\n const lifted = d.fromScalar === undefined ? [] : args.flatMap((a, j) => ((a === undefined || a === \"number\") && !lists.has(j + 1) && liftsAt(e.spec.liftScalar, j) ? [j] : []));\n if (lifted.some((j) => !lift || args[j] === undefined)) return undefined;\n const ok = args.map((a, j) => fits(d, e.spec.params?.[j], lifted.includes(j) ? kindOfDomain(d) : a, lists.has(j + 1)));\n if (ok.includes(undefined)) return undefined;\n const item = listItemKind(e.spec.returns);\n return { t: \"fn\", domain: i, bad: ok.includes(false), returns: returnsOf(d, e.spec.returns), ...(item === undefined ? {} : { item }), ...(lifted.length === 0 ? {} : { lifted }) };\n }\n return fns !== undefined && Object.hasOwn(fns, name) ? STATIC_FREE : undefined;\n },\n checkOf(kind) {\n for (const [i, d] of domains.entries()) {\n const takes = accepts(domains, d, kind);\n if (takes === undefined) return undefined;\n if (takes) return i;\n }\n return -1;\n },\n own(name) {\n return domains.every((d) => entryOf(d, name) === undefined) && !(fns !== undefined && Object.hasOwn(fns, name));\n },\n givesList(name) {\n return domains.some((d) => Object.hasOwn(d.ops, name) && listItemKind((d.ops[name] as OpSpec<unknown>).returns) !== undefined);\n },\n identities(name) {\n return domains.filter((d) => Object.hasOwn(d.ops, name) && (d.ops[name] as OpSpec<unknown>).identity !== undefined).length;\n },\n site(name, arity) {\n // Without values, the interpreter takes no op of a domain. A list op can have a list as its receiver.\n if (arity === 0 || isListOp(name)) return undefined;\n const index = domains.findIndex((d) => entryOf(d, name) !== undefined);\n if (index < 0) return fns !== undefined && Object.hasOwn(fns, name) ? FREE : undefined;\n const e = entryOf(domains[index] as AnyDomain, name) as Entry;\n return { t: \"op\", domain: index, method: e.fn === undefined, params: e.spec.params, lift: e.domain.fromScalar === undefined ? undefined : e.spec.liftScalar };\n },\n };\n}\n","/**\n * A program can use one object at two places, and a name can have two meanings at those places. The printer and the\n * lowering hold facts of a place by the object of its node. Thus this file first gives each place an object of its own.\n * A program from JSON is a tree, so it stays the same objects. The root and `./module` of this package do not export this file.\n */\nimport { children, type Expr, MAX_NESTING, nestingOf } from \"@mark1russell7/vex\";\n\n/** The original of each copy that `unsharedAll` made. */\nconst ORIGINALS = new WeakMap<Expr, Expr>();\n\n/**\n * The most places that a program can have for a compile (COMPILE.PLACES). A place is a node at one position of the tree\n * of the program, so a shared object counts once for each place. The compile takes time at each place.\n *\n * The rule: the largest program of each doubling shape under this limit stays under 1,024 MB, half of a heap of 2,048 MB.\n * At 4 times the places, at least one shape goes over that line. The workflow `places-gate.yml` holds this rule.\n *\n * The worst of 5 fresh processes in tier 2 at the limit is 560 MB (plain), 566 MB (constant) and 489 MB (loop).\n * At 4 times the places it is 1,661 MB, 1,699 MB and 1,469 MB. The limit is 15 times the largest module of `ireko`.\n * `docs/COMPILER.md` has the measures.\n */\nexport const MAX_PLACES: number = 1 << 15;\n\n/** This function gives the parts of a node. A node that is not valid has no parts here, and the check of the shape gives its reason. */\nfunction partsOf(x: Expr): readonly Expr[] {\n try {\n return children(x).filter((part) => typeof part === \"object\" && (part as unknown) !== null);\n } catch {\n return [];\n }\n}\n\n/** This function gives the count of the objects of the expressions when each object has one place. Otherwise it gives `undefined`. */\nfunction treeSize(exprs: readonly Expr[]): number | undefined {\n const seen = new Set<Expr>();\n const todo = [...exprs];\n for (let x = todo.pop(); x !== undefined; x = todo.pop()) {\n if (seen.has(x)) return undefined;\n seen.add(x);\n todo.push(...partsOf(x));\n }\n return seen.size;\n}\n\n/**\n * This function gives the number of places of expressions with a shared object, with a walk that visits each object once.\n * So it takes the time of the objects, not of the places. An object that holds itself has no end, and its count is infinite.\n */\nfunction sharedPlaces(exprs: readonly Expr[]): number {\n const counts = new Map<Expr, number>();\n const open = new Map<Expr, readonly Expr[]>();\n const todo = [...exprs];\n for (let x = todo.at(-1); x !== undefined; x = todo.at(-1)) {\n const parts = open.get(x);\n if (counts.has(x)) {\n todo.pop();\n } else if (parts !== undefined) {\n todo.pop();\n counts.set(x, parts.reduce((sum, part) => sum + (counts.get(part) ?? Infinity), 1));\n } else {\n const own = partsOf(x);\n open.set(x, own);\n todo.push(...own.filter((part) => !counts.has(part)));\n }\n }\n return exprs.reduce((sum, e) => sum + (counts.get(e) as number), 0);\n}\n\n/** This function gives the number of places of the expressions. */\nexport const placesOf = (exprs: readonly Expr[]): number => treeSize(exprs) ?? sharedPlaces(exprs);\n\n/** The result of one walk of a program: the reason that a compile refuses it (COMPILE.PLACES), and if two places hold one object. */\nexport interface PlacesRead {\n readonly error: string | undefined;\n readonly shares: boolean;\n}\n\n/** This function gives the result of one walk of the expressions. So a program with no shared object needs no second walk. */\nexport function placesRead(exprs: readonly Expr[]): PlacesRead {\n const size = treeSize(exprs);\n const places = size ?? sharedPlaces(exprs);\n return {\n error: places > MAX_PLACES ? `COMPILE.PLACES: the program has ${String(places)} places, and the limit is ${String(MAX_PLACES)}` : undefined,\n shares: size === undefined,\n };\n}\n\n/** This function gives the reason that a compile refuses the expressions, or `undefined` (COMPILE.PLACES). */\nexport const placesError = (exprs: readonly Expr[]): string | undefined => placesRead(exprs).error;\n\n/** This function gives a copy of a part of an expression, with a new object for each place. A value of a literal and the data of an extension are not nodes, so they stay. */\nfunction copied(x: unknown): unknown {\n if (Array.isArray(x)) return x.map(copied);\n if (typeof x !== \"object\" || x === null) return x;\n // A copy keeps each own property, also a property with the name `__proto__`.\n const out: Record<string, unknown> = { ...x };\n const tag = out[\"tag\"];\n if (tag === \"lit\" || tag === \"ext\") return noted(out, x);\n for (const name of Object.keys(out)) {\n const part = out[name];\n if (typeof part === \"object\") out[name] = copied(part);\n }\n return typeof tag === \"string\" ? noted(out, x) : out;\n}\n\n/** This function notes the original of a copy of a node, and gives the copy. */\nfunction noted(copy: unknown, original: unknown): unknown {\n ORIGINALS.set(copy as Expr, original as Expr);\n return copy;\n}\n\n/**\n * This function gives the expressions with one object at each place. Expressions without a shared object stay the same\n * objects. So does an expression that is nested too deep: the printer and the lowering refuse it. A caller with the\n * result of `placesRead` gives its `shares`, and this function makes no walk.\n */\nexport function unsharedAll(exprs: readonly Expr[], shares: boolean = treeSize(exprs) === undefined): readonly Expr[] {\n return !shares || exprs.some((e) => nestingOf(e) > MAX_NESTING) ? exprs : (exprs.map(copied) as readonly Expr[]);\n}\n\n/** This function gives the expression with one object at each place, as `unsharedAll` does. */\nexport const unshared = (e: Expr, shares?: boolean): Expr => unsharedAll([e], shares)[0] as Expr;\n\n/** This function gives the original of a copy, and an expression that is not a copy gives itself. */\nexport const originalOf = (e: Expr): Expr => ORIGINALS.get(e) ?? e;\n","/**\n * The memo of a program (docs/COMPILER.md §7.2 and §7.3). The value of some nodes depends only on the space and on a\n * few values that the node reads from outside: its keys. A program keeps the value of such a node for each space and\n * each value of its keys. Thus a run at each key of a space evaluates the node once for each value of its keys.\n *\n * - `AVERAGE(all.Score)` has no keys, so it evaluates once for each space.\n * - \"The largest width in the column of the origin\" has one key, the column of the origin. It evaluates once for each\n * column, not once for each origin.\n *\n * A key reads the origin or a name from outside the node, and nothing that the node changes. Inside the node, the\n * value of the key replaces the key. Thus the value of the node depends only on the space and the values of the keys.\n */\nimport { addrExprs, children, isListOp, listOf, type Addr, type Axis, type Expr } from \"@mark1russell7/vex\";\nimport { originalOf } from \"./unshared.ts\";\n\n/** A read of the focus of the expression, outside its own loops. */\nconst FOCUS = 1;\n/** A move to the origin. */\nconst ORIGIN = 2;\n/** A name that the node does not bind: a variable of the run, or a binding outside the node. */\nconst FREE = 4;\n/** A name that the node binds, outside the expression. */\nconst INTERNAL = 8;\n/** An extension or an axis table. An extension can read each part of the evaluation, and a table is another space. */\nconst OPAQUE = 16;\n/** A move to the origin or a free name inside a loop of the expression. Such a loop is a part of a node, not a key. */\nconst DEEP = 32;\n\n/** This function gives the axis inside the `where` axes of an axis, and the tests of the `where` axes. */\nfunction parts(a: Axis): { readonly base: Exclude<Axis, { readonly t: \"where\" }>; readonly tests: readonly Expr[] } {\n // Stryker disable next-line ArrayDeclaration: a test that is not an expression reads nothing, and it has no keys\n const tests: Expr[] = [];\n let x: Axis = a;\n while (x.t === \"where\") {\n tests.push(x.test);\n x = x.axis;\n }\n return { base: x, tests };\n}\n\n/** This function adds `DEEP` to a read of the origin or of a free name inside a loop. */\nconst inLoop = (r: number, top: boolean): number => (top || (r & (ORIGIN | FREE)) === 0 ? r : r | DEEP);\n\n/**\n * This function gives the reads of an address: the focus at the top of the expression, or the origin, or nothing. A\n * move from the focus or the origin can fail, so the address reads its start also before a fixed key (V-052).\n */\nfunction addressReads(at: Addr | undefined, top: boolean): number {\n let start = top ? FOCUS : 0;\n let found = 0;\n at?.forEach((m) => {\n if (m.t === \"key\" || m.t === \"index\") start = 0;\n else if (m.t === \"origin\") start = ORIGIN;\n else found |= start;\n });\n return inLoop(found | start, top);\n}\n\n/** The facts of an expression with no names around it. A set of names has one bit for each name. */\ninterface Facts {\n /** The reads of the expression outside each loop around it, with no read of a name. */\n readonly top: number;\n /** The reads of the expression in a loop around it, with no read of a name. */\n readonly inner: number;\n /** The names that the expression reads and does not bind, outside its own loops. */\n readonly near: bigint;\n /** The names that the expression reads and does not bind, in its own loops. */\n readonly far: bigint;\n /** This value is true if the expression has a loop. A node with no loop is not a node of the memo. */\n readonly loop: boolean;\n}\n\n/** The facts of an expression that reads nothing. */\nconst NOTHING: Facts = { top: 0, inner: 0, near: 0n, far: 0n, loop: false };\n\n/** The facts of an expression that the analysis does not follow. */\nconst OPAQUE_FACTS: Facts = { ...NOTHING, top: OPAQUE, inner: OPAQUE };\n\n/** The keys of a part, in the order of the parts. A list of keys holds the lists of its parts, so a join does not copy them. */\ntype Keys = readonly (Expr | Keys)[];\n\n/** This function joins the keys of the parts of an expression. A part that cannot be in the memo makes the expression the same. */\nfunction all(xs: readonly (Keys | undefined)[]): Keys | undefined {\n const known = xs.filter((x) => x !== undefined);\n if (known.length < xs.length) return undefined;\n const some = known.filter((x) => x.length > 0);\n return some.length === 1 ? some[0] : some;\n}\n\n/** This function adds each key of a list to `out`, in the order of the parts. */\nfunction flatten(keys: Keys, out: Expr[]): readonly Expr[] {\n for (const x of keys) {\n if (Array.isArray(x)) flatten(x as Keys, out);\n else out.push(x as Expr);\n }\n return out;\n}\n\n/**\n * The analysis of one program. Each node has one record of facts, so a read of a part does not walk that part again. A\n * part that cannot be in the memo stays known, so the next node above it does not walk it again. The walk of the keys\n * starts at each node of the memo. Thus the work on the key lists grows with the nodes of the memo times the levels of\n * `origin` around them. The gate COMPILE.GROWTH holds this growth at the largest legal sizes.\n */\nclass Analysis {\n readonly #bits = new Map<string, bigint>();\n readonly #facts = new Map<Expr, Facts>();\n readonly #dead = new Map<Expr, Set<bigint>>();\n\n /** This method gives the set of some names. */\n namesOf(names: readonly string[]): bigint {\n return names.reduce((set, name) => {\n const bit = this.#bits.get(name) ?? 1n << BigInt(this.#bits.size);\n this.#bits.set(name, bit);\n return set | bit;\n }, 0n);\n }\n\n /** This method gives the facts of an expression. */\n factsOf(e: Expr): Facts {\n const known = this.#facts.get(e);\n if (known !== undefined) return known;\n const facts = this.#read(e);\n this.#facts.set(e, facts);\n return facts;\n }\n\n /** This method joins the facts of some expressions. */\n #joined(xs: readonly Expr[]): Facts {\n return xs.reduce((a, x) => {\n const b = this.factsOf(x);\n return { top: a.top | b.top, inner: a.inner | b.inner, near: a.near | b.near, far: a.far | b.far, loop: a.loop || b.loop };\n }, NOTHING);\n }\n\n /** This method gives the facts of an expression from the facts of its parts. */\n #read(e: Expr): Facts {\n switch (e.tag) {\n case \"lit\":\n return NOTHING;\n case \"var\":\n return { ...NOTHING, near: this.namesOf([e.name]) };\n case \"fold\":\n case \"iterate\":\n case \"fix\":\n return { ...OPAQUE_FACTS, loop: true };\n case \"ext\":\n // A loop of names is not in the memo yet, so a node with one is not either. A call reads what its body reads, and a\n // function reads nothing where it is bound, but the memo does not follow them yet (FN.CALL).\n case \"last\":\n // A let with a function and no call has the value of its body, and a call is opaque, so this case only keeps the\n // memo simple.\n case \"fn\":\n case \"call\":\n return OPAQUE_FACTS;\n case \"ref\": {\n // The expressions of computed moves evaluate at the position of the node (NAV.COMPUTED).\n const at = this.#joined(addrExprs(e.at));\n return { ...at, top: at.top | addressReads(e.at, true), inner: at.inner | addressReads(e.at, false) };\n }\n case \"app\":\n return this.#joined(e.args);\n case \"rec\":\n return this.#joined(Object.values(e.fields));\n case \"let\": {\n const binds = this.#joined(Object.values(e.bind));\n const body = this.factsOf(e.body);\n const free = ~this.namesOf(Object.keys(e.bind));\n return { top: binds.top | body.top, inner: binds.inner | body.inner, near: binds.near | (body.near & free), far: binds.far | (body.far & free), loop: binds.loop || body.loop };\n }\n case \"each\": {\n const { base, tests } = parts(e.axis);\n // The targets of `all` do not depend on the focus, but its address can fail (V-051). Each other axis starts at its\n // address. The targets of the axis list are the items of a value, and the memo does not follow them yet (AXIS.LIST).\n const targets = (top: boolean): number => (base.t === \"table\" || listOf(e.axis) !== undefined ? OPAQUE : base.t === \"all\" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top));\n const at = this.#joined(addrExprs(e.at));\n const body = this.#joined([e.body, ...tests]);\n return { top: targets(true) | at.top | body.inner, inner: targets(false) | at.inner | body.inner, near: at.near, far: at.far | body.near | body.far, loop: true };\n }\n }\n }\n\n /**\n * This method gives the reads of `e`. `outer` has the names that the node binds outside `e`. `top` is true outside the\n * loops of the node.\n */\n #reads(e: Expr, outer: bigint, top: boolean): number {\n const { near, far, top: atTop, inner } = this.factsOf(e);\n const internal = ((near | far) & outer) === 0n ? 0 : INTERNAL;\n const free = (near & ~outer) === 0n ? 0 : inLoop(FREE, top);\n const deep = (far & ~outer) === 0n ? 0 : inLoop(FREE, false);\n return (top ? atTop : inner) | internal | free | deep;\n }\n\n /**\n * This method gives the keys of the part `e` of a node, or `undefined` if the part cannot be in the memo. `top` is\n * true outside the loops of the node, and `outer` has the names that the node binds above `e`.\n */\n keysOf(e: Expr, top: boolean, outer: bigint, pure: Set<Expr>): Keys | undefined {\n const mark = (outer << 1n) | (top ? 1n : 0n);\n const dead = this.#dead.get(e);\n if (dead?.has(mark) === true) return undefined;\n const keys = this.#partsOf(e, top, outer, pure, false);\n if (keys === undefined) this.#dead.set(e, (dead ?? new Set()).add(mark));\n return keys;\n }\n\n /** This method gives the keys of a node of the program. The node itself is not a key of itself. */\n keysOfNode(e: Expr, pure: Set<Expr>): readonly Expr[] | undefined {\n const keys = this.#partsOf(e, true, 0n, pure, true);\n return keys === undefined ? undefined : flatten(keys, []);\n }\n\n /** This method gives the keys of an expression from the keys of its parts. An undefined value means no memo. */\n #partsOf(e: Expr, top: boolean, outer: bigint, pure: Set<Expr>, root: boolean): Keys | undefined {\n const r = this.#reads(e, outer, true);\n // In a loop of the node, the focus is a target of the node, and the node can read it. A part that depends only on\n // the space has a memo of its own, if it has a loop.\n if ((r & (ORIGIN | FREE | OPAQUE | (top ? FOCUS : 0))) === 0) {\n if (!root) pure.add(e);\n return [];\n }\n // A key evaluates once, outside the loops of the node. A loop that reads the origin is a part of the node, not a key.\n if (!root && (r & (FOCUS | INTERNAL | OPAQUE | DEEP)) === 0) return [e];\n switch (e.tag) {\n case \"app\":\n return all(e.args.map((x) => this.keysOf(x, top, outer, pure)));\n case \"rec\":\n return all(Object.values(e.fields).map((x) => this.keysOf(x, top, outer, pure)));\n case \"let\": {\n const inner = outer | this.namesOf(Object.keys(e.bind));\n return all([...Object.values(e.bind).map((x) => this.keysOf(x, top, outer, pure)), this.keysOf(e.body, top, inner, pure)]);\n }\n case \"each\": {\n const { base, tests } = parts(e.axis);\n // The targets of a loop must not depend on the origin: a set of targets is not a value of a key. The address of\n // `all` can fail, so it must not depend on the origin either. The targets of the axis list are the items of a\n // value, and the memo does not follow them yet (AXIS.LIST).\n if (base.t === \"table\" || listOf(e.axis) !== undefined || ((base.t !== \"all\" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0)) return undefined;\n // The expressions of computed moves give the targets too, so they must read nothing.\n if (addrExprs(e.at).some((x) => this.#reads(x, outer, top) !== 0)) return undefined;\n return all([e.body, ...tests].map((x) => this.keysOf(x, false, outer, pure)));\n }\n // A leaf here reads the focus or a name of the node, so it is not a key, and the part cannot be in the memo. A\n // missing case gives undefined too.\n // Stryker disable StringLiteral,ConditionalExpression: see the comment above\n case \"lit\":\n case \"ref\":\n case \"var\":\n case \"ext\":\n case \"fold\":\n case \"iterate\":\n case \"fix\":\n case \"last\":\n case \"fn\":\n case \"call\":\n return undefined;\n // Stryker restore StringLiteral,ConditionalExpression\n }\n }\n}\n\n/** This function tells if an expression has a loop outside its keys and outside the parts that have a memo of their own. */\nfunction loopOutside(e: Expr, keys: ReadonlySet<Expr>): boolean {\n if (keys.has(e)) return false;\n switch (e.tag) {\n case \"each\":\n case \"fold\":\n case \"iterate\":\n case \"fix\":\n return true;\n case \"app\":\n return e.args.some((x) => loopOutside(x, keys));\n case \"rec\":\n return Object.values(e.fields).some((x) => loopOutside(x, keys));\n case \"let\":\n return [...Object.values(e.bind), e.body].some((x) => loopOutside(x, keys));\n case \"ref\":\n return addrExprs(e.at).some((x) => loopOutside(x, keys));\n // A missing case gives undefined, which is false too.\n // Stryker disable StringLiteral,ConditionalExpression: see the comment above\n case \"last\":\n case \"lit\":\n case \"var\":\n case \"ext\":\n // A node with a function or a call is opaque (reads), so it is not a node of the memo.\n case \"fn\":\n case \"call\":\n return false;\n // Stryker restore StringLiteral,ConditionalExpression\n }\n}\n\n/** This function gives the `let` expressions above an `each`, and the `each`, or `undefined` for another expression. */\nfunction eachUnderLets(e: Expr | undefined): { readonly lets: readonly Extract<Expr, { readonly tag: \"let\" }>[]; readonly each: Extract<Expr, { readonly tag: \"each\" }> } | undefined {\n const lets: Extract<Expr, { readonly tag: \"let\" }>[] = [];\n let x = e;\n while (x?.tag === \"let\") {\n lets.push(x);\n x = x.body;\n }\n return x?.tag === \"each\" ? { lets, each: x } : undefined;\n}\n\n/**\n * This function gives the nodes of `expr` that a program keeps in its memo, each with its keys. Each such node has a\n * loop outside its keys. A node inside such a node can be in the memo too. Thus the first evaluation of the outer\n * node evaluates the inner node once. The function `loops` tells if a list op is one loop with an `each`. In a module,\n * each list op is one. In tier 2, the ops of `isFusedOp` are.\n *\n * A node at two places of the program has one object in the source, and the compile gives each place a copy. A node of\n * the memo at one place is a node of the memo at each place, so the second pass finds the other copies.\n */\nexport function memoNodes(expr: Expr, loops: (op: string) => boolean = isListOp): ReadonlyMap<Expr, readonly Expr[]> {\n const out = new Map<Expr, readonly Expr[]>();\n const analysis = new Analysis();\n const note = (e: Expr): void => {\n if (!analysis.factsOf(e).loop) return;\n const pure = new Set<Expr>();\n const keys = analysis.keysOfNode(e, pure);\n if (keys !== undefined && loopOutside(e, new Set([...keys, ...pure]))) out.set(e, keys);\n };\n const visit = (e: Expr): void => {\n note(e);\n if (e.tag === \"app\") {\n // A list op over an `each` under `let` expressions is one loop, so that `each` and those `let` expressions have\n // no value of their own. Their parts can be in the memo.\n const [first, ...rest] = e.args;\n const fused = loops(e.op) ? eachUnderLets(first) : undefined;\n if (fused === undefined) {\n if (first !== undefined) visit(first);\n } else {\n for (const l of fused.lets) Object.values(l.bind).forEach(visit);\n [fused.each.body, ...parts(fused.each.axis).tests, ...addrExprs(fused.each.at)].forEach(visit);\n }\n rest.forEach(visit);\n } else if (e.tag === \"rec\") {\n Object.values(e.fields).forEach(visit);\n } else if (e.tag === \"let\") {\n Object.values(e.bind).forEach(visit);\n visit(e.body);\n } else if (e.tag === \"each\") {\n [e.body, ...parts(e.axis).tests, ...addrExprs(e.at)].forEach(visit);\n } else if (e.tag === \"ref\") {\n addrExprs(e.at).forEach(visit);\n }\n };\n visit(expr);\n const found = new Set([...out.keys()].map(originalOf));\n const copies = (e: Expr): void => {\n if (!out.has(e) && found.has(originalOf(e))) note(e);\n children(e).forEach(copies);\n };\n copies(expr);\n return out;\n}\n"],"mappings":";;;;;;;;;;;AAWA,MAAa,YAAY;AAKzB,MAAM,4BAAiC,IAAI,IAAa;CAAC;CAAS;CAAO;CAAO;CAAO;CAAQ;CAAO;CAAO;CAAQ;CAAU;CAAQ;CAAU;CAAS;AAAQ,CAAC;;AAGnK,MAAa,aAAa,OAA8B,UAAU,IAAI,EAAE;;AAYxE,MAAa,gBAAgB,MAAuB,EAAE,aAAa,UAAU,EAAE;;AAG/E,MAAa,eAAe,MAA+D,MAAM,YAAY,MAAM,YAAY,MAAM,YAAY,IAAI,KAAA;AA0CrJ,MAAM,UAA2B,OAAO,OAAO,CAAC,CAAC;;AAGjD,SAAgB,QAAQ,QAAmB,MAAiC;CAC1E,IAAI,CAAC,SAAS,IAAI,GAAG,OAAO,KAAA;CAC5B,MAAM,WAAW,OAAO,OAAO,OAAO,KAAK,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This function gives the text of a function, in one form for each runner. A text that does not parse as one expression
5
+ * stays as it is, for example a method or a native function. A module refuses such a text when it tries the copy.
6
+ */
7
+ export declare function functionText(f: (...a: never[]) => unknown): string;
8
+ /** The options of a standalone module. */
9
+ interface ModuleOptions {
10
+ readonly domains?: readonly AnyDomain[];
11
+ /** The kinds of the fields of the records and of the variables. A module reads only the names of its schema. */
12
+ readonly schema: Schema;
13
+ /**
14
+ * The name of the function that the module exports. The default is `run`. A reserved word, `VEX_ERROR` and
15
+ * `VexContractError` are not names of the run (V-060). Each other name hides nothing from the code (COMPILE.MODULE.NAME).
16
+ */
17
+ readonly name?: string;
18
+ /**
19
+ * The space of the records. In each space, the key of a record is the text of its position, unless the option
20
+ * `keys` is true. The default `array`
21
+ * has no parents. A `tree` takes the parents as the third argument of the run (COMPILE.MODULE.TREE). Each entry
22
+ * is the position of the parent of its record, or -1 for a root. A `graph` takes the edges as the third argument
23
+ * (COMPILE.MODULE.GRAPH). Each entry is an array of the positions of the ends of the edges from its record, in order.
24
+ */
25
+ readonly space?: "array" | "tree" | "graph";
26
+ /**
27
+ * The highest step of the language that the host accepts (STEP.MAX). The default is B. A program that needs a higher
28
+ * step gives a reason that names its step.
29
+ */
30
+ readonly maxStep?: VexStep;
31
+ /**
32
+ * True to fuse the chains of list ops into loops (`COMPILER.md` §7.6), and false for the code without fusion
33
+ * (COMPILE.MODULE.FUSE.OFF). The default is true. In this version, the two values print the same code.
34
+ */
35
+ readonly fuse?: boolean;
36
+ /**
37
+ * The form of the lists of the input and of the result (COMPILE.MODULE.LIST.PLAIN). The default `envelope` gives each
38
+ * list as a `vex.list`. With `plain`, a list is an array of the values of its items, and an error item is `VEX_ERROR`.
39
+ * The key of an item of the input is the text of its position, and the result has no keys. A list in a record or in
40
+ * an argument of an op stays a `vex.list`.
41
+ */
42
+ readonly lists?: "envelope" | "plain";
43
+ /**
44
+ * True when the run takes the keys of the records as its last argument (COMPILE.MODULE.KEYS). The argument is an array
45
+ * with one text for each record, and each text is different. A program that reads no key ignores it and prints the
46
+ * same text as without the option. The default is false.
47
+ */
48
+ readonly keys?: boolean;
49
+ /**
50
+ * What a catch of the module does with an exception that no function of a domain gives (COMPILE.MODULE.FAULT). The
51
+ * default `value` gives the error of Vex for each exception. With `throw`, the exception leaves the run, so a fault
52
+ * of the printed code does not become a value. An exception of a function of a domain stays an error of Vex.
53
+ */
54
+ readonly faults?: "value" | "throw";
55
+ }
56
+ /** The options of a module of a sheet (COMPILE.MODULE.SHEET): the options of a module, and the parts of a sheet. */
57
+ interface SheetModuleOptions extends ModuleOptions {
58
+ /** The cells with their own formula (SHEET.CELL.FORMULA). The formula of a cell replaces the formula of its column at its key. */
59
+ readonly cells?: CellFormulas;
60
+ /**
61
+ * The kind of the values of each column. The printer prints each column after the columns that it reads. A column
62
+ * that a formula reads before its own formula prints needs its kind here, for example a column that reads itself.
63
+ * A column of lists has a `ListKind` (COMPILE.MODULE.SHEET.LIST).
64
+ */
65
+ readonly kinds?: Readonly<Record<string, Kind | ListKind>>;
66
+ /** The budget of the stack of the search of a run, in estimated frames, as in `SheetRun`. The default is 3000. */
67
+ readonly budget?: number;
68
+ }
69
+ /** The source of a standalone module, or the reason that the program cannot be one. */
70
+ type Emitted = {
71
+ readonly ok: true;
72
+ readonly source: string;
73
+ } | {
74
+ readonly ok: false;
75
+ readonly reason: string;
76
+ };
77
+ /**
78
+ * This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
79
+ * function that takes the records and the variables and gives the value at each key. A module of a tree space takes
80
+ * the parents too. The function is pure, so a build can use it. The program can be an expression or a document
81
+ * (DOC.ENTRY).
82
+ *
83
+ * This function is a tool of a build. A print of one module of ireko takes 16 to 172 ms on a CI runner, at 0.833 to
84
+ * 1.149 times the fixed base of COMPILE.MODULE.COST. `docs/COMPILER.md` gives the ms and the ratio of each module. A
85
+ * limit of that gate holds the cost of today. The target of 1.10 for each module is not proven. A caller on a request
86
+ * path keeps the printed module, and it does not print for each request.
87
+ */
88
+ export declare function emitModule(source: Expr | Envelope, options: ModuleOptions): Emitted;
89
+ /**
90
+ * This function prints the formula columns of a sheet as a standalone ES module (COMPILE.MODULE.SHEET). The function of
91
+ * the module takes the records and the variables, and the parents for a tree. It gives an object with an array for
92
+ * each column: the result of the column at each key, or `VEX_ERROR`. The results are the results of `SheetRun`.
93
+ *
94
+ * The run fills the columns in the order of `SheetRun.fill`. A read of a finished cell is the read of an array. A read
95
+ * of a cell that is not finished starts the search of `SheetRun`, with the same budget of the stack. Each cell on a
96
+ * cycle of reads gives `VEX_ERROR`.
97
+ */
98
+ export declare function emitSheetModule(columns: ReadonlyMap<string, Expr>, options: SheetModuleOptions): Emitted;
99
+ export type { Emitted, ListKind, ModuleOptions, SheetModuleOptions };
100
+ //# sourceMappingURL=module-entry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module-entry.d.ts","names":[],"sources":["../src/fn-text.ts","../src/module.ts"],"mappings":";;;;;;wBAgDgB,aAAa,OAAO;;UCHnB;WACN,mBAAmB;;WAEnB,QAAQ;;;;;WAKR;;;;;;;;WAQA;;;;;WAKA,UAAU;;;;;WAKV;;;;;;;WAOA;;;;;;WAMA;;;;;;WAMA;;;UAIM,2BAA2B;;WAEjC,QAAQ;;;;;;WAMR,QAAQ,SAAS,eAAe,OAAO;;WAEvC;;;KAMC;WAAqB;WAAmB;;WAA8B;WAAoB;;;;;;;;;;;;;wBA4tKtF,WAAW,QAAQ,OAAO,UAAU,SAAS,gBAAgB;;;;;;;;;;wBA+E7D,gBAAgB,SAAS,oBAAoB,OAAO,SAAS,qBAAqB"}