hclang 0.44.1 → 0.46.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.
@@ -452,6 +452,17 @@ export function inspectFlags(flags) {
452
452
  * manipulating and interacting with frames.
453
453
  */
454
454
  export class Frame extends MetaFrame {
455
+ /**
456
+ * Whether `key` names an observer a Frame answers for itself: one of the four
457
+ * fixed hashes `#`, `##`, `###`, `####`, or a ranked hash `#n`.
458
+ */
459
+ static isSizeOperator(key) {
460
+ return Frame.SIZE_OPERATORS.includes(key) || Frame.RANKED_HASH.test(key);
461
+ }
462
+ /** The rung n of a ranked hash `#n`, or undefined for any other key. */
463
+ static hashRank(key) {
464
+ return Frame.RANKED_HASH.test(key) ? Number(key.slice(1)) : undefined;
465
+ }
455
466
  /** Creates a runtime error value without coupling MetaFrame to FrameNote. */
456
467
  static error(source) {
457
468
  const error = new (class extends Frame {
@@ -724,7 +735,7 @@ export class Frame extends MetaFrame {
724
735
  * Nil has no Count, so no residual or Demi.
725
736
  */
726
737
  sizeFor(key) {
727
- if (!Frame.SIZE_OPERATORS.includes(key))
738
+ if (!Frame.isSizeOperator(key))
728
739
  return Frame.nil;
729
740
  const failure = this.observationError();
730
741
  if (failure)
@@ -751,8 +762,10 @@ export class Frame extends MetaFrame {
751
762
  }
752
763
  case Frame.kDEMI:
753
764
  return Frame.demi(this);
754
- default:
755
- return Frame.nil;
765
+ default: {
766
+ const rank = Frame.hashRank(key);
767
+ return rank === undefined ? Frame.nil : Frame.denum(this, rank);
768
+ }
756
769
  }
757
770
  }
758
771
  /** Nonnegative represented quantity; path tracks recursive containment only. */
@@ -1070,7 +1083,7 @@ export class Frame extends MetaFrame {
1070
1083
  return undefined;
1071
1084
  }
1072
1085
  const key = argument.spelling();
1073
- return Frame.SIZE_OPERATORS.includes(key) ? key : undefined;
1086
+ return Frame.isSizeOperator(key) ? key : undefined;
1074
1087
  }
1075
1088
  /**
1076
1089
  * Negation is decided by the symbol's name, never by how it prints.
@@ -1155,6 +1168,19 @@ Object.defineProperty(Frame, "SIZE_OPERATORS", {
1155
1168
  Frame.kDEMI,
1156
1169
  ]
1157
1170
  });
1171
+ /**
1172
+ * The ranked hash `.#n` (occurrence/figure 007.32): `#` and decimal digits,
1173
+ * as `.#2`, in canonical decimal (no leading zero, so each rung has one
1174
+ * spelling). Its n is the Cayley–Dickson rung and not a hash count, so it is
1175
+ * not one of {@link SIZE_OPERATORS}, whose spellings are the four fixed
1176
+ * hashes. Bare `.#` stays the Count.
1177
+ */
1178
+ Object.defineProperty(Frame, "RANKED_HASH", {
1179
+ enumerable: true,
1180
+ configurable: true,
1181
+ writable: true,
1182
+ value: /^#(0|[1-9][0-9]*)$/
1183
+ });
1158
1184
  /**
1159
1185
  * BEGIN_EXPR is the symbol for the beginning of an expression.
1160
1186
  */
package/esm/lib/frames.js CHANGED
@@ -28,6 +28,8 @@ export { FrameNumeric, NUMERIC_RANK, } from "./frames/frame-numeric.js";
28
28
  export { FrameInt } from "./frames/frame-int.js";
29
29
  export { FrameCount } from "./frames/frame-count.js";
30
30
  export { installDemiConstructor, installRouterRecognizer, observeDemi, } from "./frames/fourth-hash.js";
31
+ export { installDenumConstructor, observeDenum, } from "./frames/denum-hash.js";
32
+ export * as denum from "./frames/denum.js";
31
33
  export { FrameDecimal } from "./frames/frame-decimal.js";
32
34
  export { FrameSequence } from "./frames/frame-sequence.js";
33
35
  export { FrameTypedNumber } from "./frames/frame-typed-number.js";
@@ -0,0 +1,131 @@
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, arity, conjugate, coordinates, denum, DenumError, multiply, opposite, promote, residual, schemaName, signatureName, subtract, } from "../frames/denum.js";
7
+ /**
8
+ * 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.
11
+ */
12
+ export const DENUM_OPERATIONS = {
13
+ /** Validate a state and answer it as built. */
14
+ build: 0,
15
+ add: 1,
16
+ subtract: 2,
17
+ multiply: 3,
18
+ opposite: 4,
19
+ conjugate: 5,
20
+ /** Promote the left state to the rank of the right state's first slot. */
21
+ promote: 6,
22
+ /**
23
+ * The integer coordinate of each direction, an observation. The fifth slot
24
+ * is the mode: 0 signed `W₊ − W₋`, 1 unsigned `W₊ + W₋`.
25
+ */
26
+ coordinates: 7,
27
+ /** Legacy name of {@link DENUM_OPERATIONS.coordinates} (the signed mode). */
28
+ observe: 7,
29
+ /** The native signature names of the left state's channels, as text. */
30
+ names: 8,
31
+ /** The schema name `CD[n]` of the left state's rank, as text. */
32
+ schema: 9,
33
+ /** Positive minus negative load total: the declared residual, an integer. */
34
+ residual: 10,
35
+ };
36
+ const counts = (frame) => {
37
+ const value = frame.referent();
38
+ return value instanceof FrameCount && typeof value.data === "bigint"
39
+ ? value.data
40
+ : undefined;
41
+ };
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);
45
+ const c = count === undefined ? undefined : counts(count);
46
+ // Base Frame.elements() answers [this] for any non-aggregate, so a bare Count
47
+ // would read as a one-element list: require an aggregate, as the hash reader
48
+ // `storedState` does.
49
+ const list = loads?.referent();
50
+ const elements = list?.ownsIdentity() ? list.elements() : undefined;
51
+ const l = elements?.map(counts);
52
+ if (r === undefined || c === undefined || !l || l.some((x) => x === undefined)) {
53
+ throw new DenumError("$!!.invalid-denum");
54
+ }
55
+ if (r > 5n)
56
+ throw new DenumError("$!!.invalid-denum", `rank ${r}`);
57
+ return denum(Number(r), c, l);
58
+ }
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
+ ]);
64
+ /**
65
+ * `denum-engine`, the one native seam under the HC class in `hc/denum.hc`.
66
+ *
67
+ * It is pure arithmetic over the typed reference in `lib/frames/denum.ts`: it
68
+ * reads Counts and answers fresh data, touches no scope and holds no state,
69
+ * and it refuses anything malformed with a named error rather than guessing.
70
+ * The Denum itself is an HC class; this is only how that class reaches the
71
+ * Cayley–Dickson recurrence without a hand-written table in HC.
72
+ */
73
+ export const DenumEngine = (_source, block) => {
74
+ const failure = block.referent().observationError();
75
+ if (failure)
76
+ return failure;
77
+ const args = block.referent().elements();
78
+ const op = args[0] === undefined ? undefined : counts(args[0]);
79
+ if (op === undefined)
80
+ return Frame.error("$!!.invalid-denum");
81
+ try {
82
+ const left = readState(args[1], args[2], args[3]);
83
+ switch (Number(op)) {
84
+ case DENUM_OPERATIONS.build:
85
+ return stateFrame(left);
86
+ case DENUM_OPERATIONS.opposite:
87
+ return stateFrame(opposite(left));
88
+ case DENUM_OPERATIONS.conjugate:
89
+ return stateFrame(conjugate(left));
90
+ case DENUM_OPERATIONS.coordinates: {
91
+ // Absent (the v0.45.0 call shape) is signed; otherwise exactly 0 or 1.
92
+ const mode = args[4] === undefined ? 0n : counts(args[4]);
93
+ if (mode !== 0n && mode !== 1n) {
94
+ return Frame.error("$!!.invalid-denum");
95
+ }
96
+ return new FrameArray(coordinates(left, mode === 1n).map((c) => Frame.signed(c)));
97
+ }
98
+ case DENUM_OPERATIONS.names:
99
+ return new FrameArray(Array.from({ length: arity(left.rank) }, (_, channel) => new FrameString(signatureName(left.rank, channel))));
100
+ case DENUM_OPERATIONS.residual:
101
+ return Frame.signed(residual(left));
102
+ case DENUM_OPERATIONS.schema:
103
+ return new FrameString(schemaName(left.rank));
104
+ case DENUM_OPERATIONS.promote: {
105
+ const target = args[4] === undefined ? undefined : counts(args[4]);
106
+ if (target === undefined || target > 5n) {
107
+ return Frame.error("$!.denum-rank");
108
+ }
109
+ return stateFrame(promote(left, Number(target)));
110
+ }
111
+ case DENUM_OPERATIONS.add:
112
+ case DENUM_OPERATIONS.subtract:
113
+ case DENUM_OPERATIONS.multiply: {
114
+ const right = readState(args[4], args[5], args[6]);
115
+ const operate = Number(op) === DENUM_OPERATIONS.add
116
+ ? add
117
+ : Number(op) === DENUM_OPERATIONS.subtract
118
+ ? subtract
119
+ : multiply;
120
+ return stateFrame(operate(left, right));
121
+ }
122
+ default:
123
+ return Frame.error("$!!.invalid-denum");
124
+ }
125
+ }
126
+ catch (error) {
127
+ if (error instanceof DenumError)
128
+ return Frame.error(error.kind);
129
+ throw error;
130
+ }
131
+ };
package/esm/lib/ops.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { IfElse, IfThen } from "./ops/conditionals.js";
2
+ import { DenumEngine } from "./ops/denum.js";
2
3
  import { FrameOps } from "./ops/frame-ops.js";
3
4
  import { MapElements, MapMembers, ReduceElements, ReduceMembers, SpliceElements, } from "./ops/iterators.js";
4
5
  import { Add, Divide, Equals, GreaterThan, GreaterThanOrEqual, Identical, LessThan, LessThanOrEqual, Modulo, Multiply, Power, Subtract, TypeEquals, } from "./ops/math.js";
@@ -34,4 +35,6 @@ export const Ops = new FrameOps({
34
35
  "^": BindType,
35
36
  "^^": BindReturn,
36
37
  "->": Arrow,
38
+ // The one native seam under `hc/denum.hc`: pure Cayley–Dickson arithmetic.
39
+ "denum-engine": DenumEngine,
37
40
  });
@@ -1 +1 @@
1
- export const VERSION = "0.44.1";
1
+ export const VERSION = "0.46.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.44.1",
3
+ "version": "0.46.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",