hclang 0.47.0 → 0.49.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.
@@ -1,238 +0,0 @@
1
- // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
- import { Frame } from "../frames/frame.js";
3
- import { FrameArray } from "../frames/frame-array.js";
4
- import { FrameCount } from "../frames/frame-count.js";
5
- import { FrameString } from "../frames/frame-string.js";
6
- import { add, addDenum, arity, cdRank, cdViewOf as deriveCDView, conjugate, coordinates, denum, DenumError, isCDArity, multiply, opposite, promote, rankOf, residual, schemaOf, signatureName, subtract, } from "../frames/denum.js";
7
- /**
8
- * The native capability registries (occurrence/figure 007.36). A retained Denum
9
- * state and a CD view each cross the engine as an ordinary `[count, loads]`
10
- * array, but the engine records the arrays it makes in these tables, keyed by
11
- * the array's identity, and trusts only those. A program cannot add to them: an
12
- * array it writes, copies or builds from `d.count` and `d.loads` is not in the
13
- * table, so a raw `[count, loads]` payload, a lookalike or a bare `2ⁿ`-load
14
- * state has no CD capability. The `view` op is the one way into the view table,
15
- * and it accepts only an entry of the state table. (The arrays stay plain
16
- * `FrameArray`s so that `~ ([].<>)` and type equality see lists.)
17
- */
18
- const denumStates = new WeakMap();
19
- const cdViews = new WeakMap();
20
- const pairFrame = (state) => new FrameArray([
21
- Frame.count(state.count),
22
- new FrameArray(state.loads.map((load) => Frame.count(load))),
23
- ]);
24
- /** A native Denum state Frame, registered as one. */
25
- export function denumStateFrame(state) {
26
- const frame = pairFrame(state);
27
- denumStates.set(frame, state);
28
- return frame;
29
- }
30
- /** A native CD view Frame, registered as one. */
31
- export function cdViewFrame(view) {
32
- const frame = pairFrame(view);
33
- cdViews.set(frame, view);
34
- return frame;
35
- }
36
- /** The Denum state a Frame carries by registration, or undefined. */
37
- export function denumStateOf(value) {
38
- const frame = value?.referent();
39
- return frame === undefined ? undefined : denumStates.get(frame);
40
- }
41
- /** The CD view a Frame carries by registration, or undefined. */
42
- export function cdViewOf(value) {
43
- const frame = value?.referent();
44
- return frame === undefined ? undefined : cdViews.get(frame);
45
- }
46
- /**
47
- * The operations `denum-engine` performs, selected by the first element of its
48
- * argument. A generic op takes `[op, count, loads]` (a second `count, loads`
49
- * for `addDenum`) and never a view. A CD op takes `[op, view, view]`: its
50
- * operands are registered view frames, not payloads (occurrence/figure 007.36).
51
- * There is no rank slot, so a legacy `[rank, count, loads]` payload is refused,
52
- * not reinterpreted (occurrence/figure 007.35).
53
- *
54
- * The engine receives data, so the public type is chosen by the *op*, not
55
- * inferred from the arity. Ops `build`, `rank`, `addDenum` and `denumSchema`
56
- * are generic and accept any positive arity. `view` is the one way to a CD
57
- * capability: it takes a a registered Denum state. Every other op is a CD op and
58
- * refuses an operand that is not a view with `$!.denum-not-view`. Which HC
59
- * class may call which op, and which operands it accepts, is the HC class's
60
- * identity check in `hc/denum.hc`.
61
- */
62
- export const DENUM_OPERATIONS = {
63
- /** Validate a generic state, any positive arity, and answer a Denum state. */
64
- build: 0,
65
- add: 1,
66
- subtract: 2,
67
- multiply: 3,
68
- opposite: 4,
69
- conjugate: 5,
70
- /** Promote the left view to the target rung in the third slot. */
71
- promote: 6,
72
- /**
73
- * The integer coordinate of each direction, an observation. The third slot
74
- * is the mode: 0 signed `W₊ − W₋`, 1 unsigned `W₊ + W₋`.
75
- */
76
- coordinates: 7,
77
- /** Legacy name of {@link DENUM_OPERATIONS.coordinates} (the signed mode). */
78
- observe: 7,
79
- /** The native signature names of the left view's channels, as text. */
80
- names: 8,
81
- /** The CD schema description `CD[n]` of the left view. */
82
- schema: 9,
83
- /** Positive minus negative load total: the declared residual, an integer. */
84
- residual: 10,
85
- /** The derived rank `⌊log₂ A⌋` of the left state's loads, an observation. */
86
- rank: 11,
87
- /** Generic equal-arity componentwise addition: no CD arity is required. */
88
- addDenum: 12,
89
- /** The generic schema description `Arity[A]`, never `CD[n]`. */
90
- denumSchema: 13,
91
- /**
92
- * The CD view of a Denum state (`d.cd ()`): the state must be a
93
- * a registered Denum state, and its arity 1, 2, 4, 8, 16 or 32.
94
- */
95
- view: 14,
96
- /**
97
- * Refusals the HC classes cannot raise themselves, because source code can
98
- * write `$!.reason` but cannot raise a system error with it. They take no
99
- * state: `refuseNotCD` answers `$!.denum-not-cd` (a CD operation on a
100
- * generic Denum) and `refuseMixed` answers `$!.denum-mixed` (a generic and a
101
- * view operand in one operation).
102
- */
103
- refuseNotCD: 15,
104
- refuseMixed: 16,
105
- /**
106
- * Whether a Denum state could be viewed: 1 or 0, never an error and never a
107
- * capability. It lets the HC classes avoid deriving a view that cannot exist
108
- * when they classify an operand. It takes a registered Denum state; anything
109
- * else is 0.
110
- */
111
- viewable: 17,
112
- };
113
- const counts = (frame) => {
114
- const value = frame.referent();
115
- return value instanceof FrameCount && typeof value.data === "bigint"
116
- ? value.data
117
- : undefined;
118
- };
119
- /** The state a pair of Frames holds, or the contract failure. */
120
- function readState(count, loads) {
121
- const c = count === undefined ? undefined : counts(count);
122
- // Base Frame.elements() answers [this] for any non-aggregate, so a bare Count
123
- // would read as a one-element list: require an aggregate, as the hash reader
124
- // `storedState` does.
125
- const list = loads?.referent();
126
- const elements = list?.ownsIdentity() ? list.elements() : undefined;
127
- const l = elements?.map(counts);
128
- if (c === undefined || !l || l.some((x) => x === undefined)) {
129
- throw new DenumError("$!!.invalid-denum");
130
- }
131
- return denum(c, l);
132
- }
133
- /** The CD view an operand must be, or the refusal that names why. */
134
- function readView(operand) {
135
- const view = cdViewOf(operand);
136
- if (view === undefined)
137
- throw new DenumError("$!.denum-not-view");
138
- return view;
139
- }
140
- const stateFrame = denumStateFrame;
141
- const viewFrame = cdViewFrame;
142
- /**
143
- * `denum-engine`, the one native seam under the HC class in `hc/denum.hc`.
144
- *
145
- * It is pure arithmetic over the typed reference in `lib/frames/denum.ts`: it
146
- * reads Counts and answers fresh data, touches no scope and holds no state,
147
- * and it refuses anything malformed with a named error rather than guessing.
148
- * The Denum itself is an HC class; this is only how that class reaches the
149
- * Cayley–Dickson recurrence without a hand-written table in HC.
150
- */
151
- export const DenumEngine = (_source, block) => {
152
- const args = block.referent().elements();
153
- const failure = block.referent().observationError();
154
- if (failure)
155
- return failure;
156
- const op = args[0] === undefined ? undefined : counts(args[0]);
157
- if (op === undefined)
158
- return Frame.error("$!!.invalid-denum");
159
- if (Number(op) === DENUM_OPERATIONS.refuseNotCD) {
160
- return Frame.error("$!.denum-not-cd");
161
- }
162
- if (Number(op) === DENUM_OPERATIONS.refuseMixed) {
163
- return Frame.error("$!.denum-mixed");
164
- }
165
- if (Number(op) === DENUM_OPERATIONS.viewable) {
166
- const source = denumStateOf(args[1]);
167
- return Frame.count(source !== undefined && isCDArity(source.loads.length) ? 1n : 0n);
168
- }
169
- try {
170
- switch (Number(op)) {
171
- case DENUM_OPERATIONS.build:
172
- return stateFrame(readState(args[1], args[2]));
173
- case DENUM_OPERATIONS.rank:
174
- return Frame.count(BigInt(rankOf(readState(args[1], args[2]))));
175
- case DENUM_OPERATIONS.denumSchema:
176
- return new FrameString(schemaOf(readState(args[1], args[2])));
177
- case DENUM_OPERATIONS.addDenum:
178
- return stateFrame(addDenum(readState(args[1], args[2]), readState(args[3], args[4])));
179
- case DENUM_OPERATIONS.view: {
180
- const source = denumStateOf(args[1]);
181
- if (source === undefined)
182
- return Frame.error("$!!.invalid-denum");
183
- return viewFrame(deriveCDView(source));
184
- }
185
- }
186
- // Everything below is a CD op on views; anything else is not an op.
187
- if (Number(op) < DENUM_OPERATIONS.add ||
188
- Number(op) > DENUM_OPERATIONS.residual) {
189
- return Frame.error("$!!.invalid-denum");
190
- }
191
- const left = readView(args[1]);
192
- switch (Number(op)) {
193
- case DENUM_OPERATIONS.opposite:
194
- return viewFrame(opposite(left));
195
- case DENUM_OPERATIONS.conjugate:
196
- return viewFrame(conjugate(left));
197
- case DENUM_OPERATIONS.coordinates: {
198
- // Absent (the v0.45.0 call shape) is signed; otherwise exactly 0 or 1.
199
- const mode = args[2] === undefined ? 0n : counts(args[2]);
200
- if (mode !== 0n && mode !== 1n) {
201
- return Frame.error("$!!.invalid-denum");
202
- }
203
- return new FrameArray(coordinates(left, mode === 1n).map((c) => Frame.signed(c)));
204
- }
205
- case DENUM_OPERATIONS.names:
206
- return new FrameArray(Array.from({ length: arity(cdRank(left)) }, (_, channel) => new FrameString(signatureName(cdRank(left), channel))));
207
- case DENUM_OPERATIONS.residual:
208
- return Frame.signed(residual(left));
209
- case DENUM_OPERATIONS.schema:
210
- return new FrameString(schemaOf(left));
211
- case DENUM_OPERATIONS.promote: {
212
- const target = args[2] === undefined ? undefined : counts(args[2]);
213
- if (target === undefined || target > 5n) {
214
- return Frame.error("$!.denum-rank");
215
- }
216
- return viewFrame(promote(left, Number(target)));
217
- }
218
- case DENUM_OPERATIONS.add:
219
- case DENUM_OPERATIONS.subtract:
220
- case DENUM_OPERATIONS.multiply: {
221
- const right = readView(args[2]);
222
- const operate = Number(op) === DENUM_OPERATIONS.add
223
- ? add
224
- : Number(op) === DENUM_OPERATIONS.subtract
225
- ? subtract
226
- : multiply;
227
- return viewFrame(operate(left, right));
228
- }
229
- default:
230
- return Frame.error("$!!.invalid-denum");
231
- }
232
- }
233
- catch (error) {
234
- if (error instanceof DenumError)
235
- return Frame.error(error.kind);
236
- throw error;
237
- }
238
- };