hclang 0.46.0 → 0.47.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.
@@ -3,35 +3,112 @@ import { Frame } from "../frames/frame.js";
3
3
  import { FrameArray } from "../frames/frame-array.js";
4
4
  import { FrameCount } from "../frames/frame-count.js";
5
5
  import { FrameString } from "../frames/frame-string.js";
6
- import { add, arity, conjugate, coordinates, denum, DenumError, multiply, opposite, promote, residual, schemaName, signatureName, subtract, } from "../frames/denum.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
+ }
7
46
  /**
8
47
  * The operations `denum-engine` performs, selected by the first element of its
9
- * argument. The argument is `[op, rank, count, loads, rank, count, loads]`: the
10
- * left Denum state, then the right one for the binary operations.
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`.
11
61
  */
12
62
  export const DENUM_OPERATIONS = {
13
- /** Validate a state and answer it as built. */
63
+ /** Validate a generic state, any positive arity, and answer a Denum state. */
14
64
  build: 0,
15
65
  add: 1,
16
66
  subtract: 2,
17
67
  multiply: 3,
18
68
  opposite: 4,
19
69
  conjugate: 5,
20
- /** Promote the left state to the rank of the right state's first slot. */
70
+ /** Promote the left view to the target rung in the third slot. */
21
71
  promote: 6,
22
72
  /**
23
- * The integer coordinate of each direction, an observation. The fifth slot
73
+ * The integer coordinate of each direction, an observation. The third slot
24
74
  * is the mode: 0 signed `W₊ − W₋`, 1 unsigned `W₊ + W₋`.
25
75
  */
26
76
  coordinates: 7,
27
77
  /** Legacy name of {@link DENUM_OPERATIONS.coordinates} (the signed mode). */
28
78
  observe: 7,
29
- /** The native signature names of the left state's channels, as text. */
79
+ /** The native signature names of the left view's channels, as text. */
30
80
  names: 8,
31
- /** The schema name `CD[n]` of the left state's rank, as text. */
81
+ /** The CD schema description `CD[n]` of the left view. */
32
82
  schema: 9,
33
83
  /** Positive minus negative load total: the declared residual, an integer. */
34
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,
35
112
  };
36
113
  const counts = (frame) => {
37
114
  const value = frame.referent();
@@ -39,9 +116,8 @@ const counts = (frame) => {
39
116
  ? value.data
40
117
  : undefined;
41
118
  };
42
- /** The state a triple of Frames holds, or the contract failure. */
43
- function readState(rank, count, loads) {
44
- const r = rank === undefined ? undefined : counts(rank);
119
+ /** The state a pair of Frames holds, or the contract failure. */
120
+ function readState(count, loads) {
45
121
  const c = count === undefined ? undefined : counts(count);
46
122
  // Base Frame.elements() answers [this] for any non-aggregate, so a bare Count
47
123
  // would read as a one-element list: require an aggregate, as the hash reader
@@ -49,18 +125,20 @@ function readState(rank, count, loads) {
49
125
  const list = loads?.referent();
50
126
  const elements = list?.ownsIdentity() ? list.elements() : undefined;
51
127
  const l = elements?.map(counts);
52
- if (r === undefined || c === undefined || !l || l.some((x) => x === undefined)) {
128
+ if (c === undefined || !l || l.some((x) => x === undefined)) {
53
129
  throw new DenumError("$!!.invalid-denum");
54
130
  }
55
- if (r > 5n)
56
- throw new DenumError("$!!.invalid-denum", `rank ${r}`);
57
- return denum(Number(r), c, l);
131
+ return denum(c, l);
58
132
  }
59
- const stateFrame = (state) => new FrameArray([
60
- Frame.count(BigInt(state.rank)),
61
- Frame.count(state.count),
62
- new FrameArray(state.loads.map((load) => Frame.count(load))),
63
- ]);
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;
64
142
  /**
65
143
  * `denum-engine`, the one native seam under the HC class in `hc/denum.hc`.
66
144
  *
@@ -71,53 +149,82 @@ const stateFrame = (state) => new FrameArray([
71
149
  * Cayley–Dickson recurrence without a hand-written table in HC.
72
150
  */
73
151
  export const DenumEngine = (_source, block) => {
152
+ const args = block.referent().elements();
74
153
  const failure = block.referent().observationError();
75
154
  if (failure)
76
155
  return failure;
77
- const args = block.referent().elements();
78
156
  const op = args[0] === undefined ? undefined : counts(args[0]);
79
157
  if (op === undefined)
80
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
+ }
81
169
  try {
82
- const left = readState(args[1], args[2], args[3]);
83
170
  switch (Number(op)) {
84
171
  case DENUM_OPERATIONS.build:
85
- return stateFrame(left);
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)) {
86
193
  case DENUM_OPERATIONS.opposite:
87
- return stateFrame(opposite(left));
194
+ return viewFrame(opposite(left));
88
195
  case DENUM_OPERATIONS.conjugate:
89
- return stateFrame(conjugate(left));
196
+ return viewFrame(conjugate(left));
90
197
  case DENUM_OPERATIONS.coordinates: {
91
198
  // Absent (the v0.45.0 call shape) is signed; otherwise exactly 0 or 1.
92
- const mode = args[4] === undefined ? 0n : counts(args[4]);
199
+ const mode = args[2] === undefined ? 0n : counts(args[2]);
93
200
  if (mode !== 0n && mode !== 1n) {
94
201
  return Frame.error("$!!.invalid-denum");
95
202
  }
96
203
  return new FrameArray(coordinates(left, mode === 1n).map((c) => Frame.signed(c)));
97
204
  }
98
205
  case DENUM_OPERATIONS.names:
99
- return new FrameArray(Array.from({ length: arity(left.rank) }, (_, channel) => new FrameString(signatureName(left.rank, channel))));
206
+ return new FrameArray(Array.from({ length: arity(cdRank(left)) }, (_, channel) => new FrameString(signatureName(cdRank(left), channel))));
100
207
  case DENUM_OPERATIONS.residual:
101
208
  return Frame.signed(residual(left));
102
209
  case DENUM_OPERATIONS.schema:
103
- return new FrameString(schemaName(left.rank));
210
+ return new FrameString(schemaOf(left));
104
211
  case DENUM_OPERATIONS.promote: {
105
- const target = args[4] === undefined ? undefined : counts(args[4]);
212
+ const target = args[2] === undefined ? undefined : counts(args[2]);
106
213
  if (target === undefined || target > 5n) {
107
214
  return Frame.error("$!.denum-rank");
108
215
  }
109
- return stateFrame(promote(left, Number(target)));
216
+ return viewFrame(promote(left, Number(target)));
110
217
  }
111
218
  case DENUM_OPERATIONS.add:
112
219
  case DENUM_OPERATIONS.subtract:
113
220
  case DENUM_OPERATIONS.multiply: {
114
- const right = readState(args[4], args[5], args[6]);
221
+ const right = readView(args[2]);
115
222
  const operate = Number(op) === DENUM_OPERATIONS.add
116
223
  ? add
117
224
  : Number(op) === DENUM_OPERATIONS.subtract
118
225
  ? subtract
119
226
  : multiply;
120
- return stateFrame(operate(left, right));
227
+ return viewFrame(operate(left, right));
121
228
  }
122
229
  default:
123
230
  return Frame.error("$!!.invalid-denum");
@@ -1 +1 @@
1
- export const VERSION = "0.46.0";
1
+ export const VERSION = "0.47.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",