hclang 0.41.0 → 0.41.1

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.
@@ -1104,6 +1104,63 @@ export const HC_SOURCES = new Map([
1104
1104
  "; [Outer.ks(), Inner.ks()]",
1105
1105
  "# [[1, 1], [5, 5]]",
1106
1106
  "",
1107
+ "`A class answers a binary arithmetic operator on its instances by declaring a",
1108
+ "method under the operator's own name, + - * or /, the way it declares apply or",
1109
+ "toText (015.02). The operator resolves against its left operand's declared",
1110
+ "method and the global built-in, and never the lexical scope the operand was",
1111
+ "read from. A declared method takes the right operand and answers the result.`",
1112
+ "; .Vec [.n 3; .+ {.a (_^ .n); .b (_ .n); [.n (a + b)]}];",
1113
+ "; (Vec + [.n 4]).n",
1114
+ "# 7",
1115
+ "",
1116
+ "`The declared operator overrides the global built-in only when the aggregate is",
1117
+ "the left operand. Ordinary numeric arithmetic keeps the global operation.`",
1118
+ "; 2 + 3",
1119
+ "# 5",
1120
+ "",
1121
+ "`A declared operator resolves through the declared-parent chain, the same chain",
1122
+ "an aggregate's other methods come from, so a child inherits it.`",
1123
+ "; .AddBase [.n 0; .+ {.a (_^ .n); .b (_ .n); [.n (a + b)]}];",
1124
+ "; .AddKid [.^ AddBase; .n 10];",
1125
+ "; (AddKid + [.n 5]).n",
1126
+ "# 15",
1127
+ "",
1128
+ "`A lexical operator-named method in the surrounding scope does not shadow global",
1129
+ "arithmetic for an ordinary numeric left operand, which carries no lexical scope",
1130
+ "into the operator.`",
1131
+ "; .+ {999};",
1132
+ "; 2 + 3",
1133
+ "# 5",
1134
+ "",
1135
+ "`Nor does a receiver's own declared + shadow the global + used inside that",
1136
+ "receiver's other methods. A numeric load read back from the receiver and used",
1137
+ "with + takes the global operation, so there is no receiver-scope recursion or",
1138
+ "stack overflow. A lexical walk would find the class's own + and recur; before",
1139
+ "015.02 this spelling overflowed the stack.`",
1140
+ "; .Ring [.base 2; .+ {_ + 100}; .inc {(_^ .base) + 3}];",
1141
+ "; Ring.inc()",
1142
+ "# 5",
1143
+ "",
1144
+ "`The same receiver still answers + when it is itself the left operand, resolving",
1145
+ "its own declared method rather than the global built-in.`",
1146
+ "; Ring + 100",
1147
+ "# 200",
1148
+ "",
1149
+ "`The declared-operator hook is scoped to the arithmetic spellings + - * / only.",
1150
+ "A built-in operator outside that set, such as membership ~, is unaffected: a",
1151
+ "class that declares .~ does not intercept it, and the global operation answers",
1152
+ "for an aggregate and for a number alike.`",
1153
+ "; .Thing [.~ {999}; .v 5];",
1154
+ "; Thing ~ ([].<>)",
1155
+ "# <>",
1156
+ "; 5 ~ (0.<>)",
1157
+ "# <>",
1158
+ "",
1159
+ "`A named arithmetic spelling, 2 .+ 2, is ordinary application and not the binary",
1160
+ "operator, so it is unchanged.`",
1161
+ "; 2 .+ 2",
1162
+ "# 4",
1163
+ "",
1107
1164
  ].join("\n"),
1108
1165
  ],
1109
1166
  [
@@ -1430,9 +1487,37 @@ export const HC_SOURCES = new Map([
1430
1487
  "",
1431
1488
  "**Scaling**, `d.scale_ k`: multiply both loads by a Count k, keeping the",
1432
1489
  "phase. It is the numerical shadow of Star: `(x * y).####` is",
1433
- "`x.####.scale_ (y.##)` for a nonempty host x. Scaling is a method because a",
1434
- "class cannot declare an operator, so `d * k` is ordinary Star, which refuses",
1435
- "an aggregate with properties.",
1490
+ "`x.####.scale_ (y.##)` for a nonempty host x. `scale_` stays, as the name the",
1491
+ "fourth hash and Star correspondence use; `d * k` now answers the same Demi",
1492
+ "through the operator below.",
1493
+ "",
1494
+ "**Arithmetic**, `d + x`, `d - x`, `d * k` and `d / n` (015.02). A class answers",
1495
+ "an operator by declaring a method under its name, found on the operand and its",
1496
+ "declared parents, the chain its other methods come from, so an operator in a",
1497
+ "method body keeps its global meaning rather than recurring into the class that",
1498
+ "is running. `+`, `-` and `*` **construct a fresh Demi state** from Size and",
1499
+ "Count observations of their operands, keeping Figure 008's distinction between",
1500
+ "construction and observation; `/` is the one that answers a Spin observation, a",
1501
+ "number rather than a Demi. None of the four is application: none flips the",
1502
+ "phase, and each answers a fresh value and leaves `d` as it was.",
1503
+ "",
1504
+ "- `d + x` deposits `x.##` on the **right**: `Demi [p, L, R + x.##]`. A sizeless",
1505
+ " host's failure is the answer, as it is for application.",
1506
+ "- `d - x` deposits on the **left**: `Demi [p, L + x.##, R]`. It is not",
1507
+ " subtraction: a deposit only ever adds, so a load never goes negative and",
1508
+ " stays a whole Count. Right deposition lowers the signed residual `L − R` and",
1509
+ " left deposition raises it, the reverse of ordinary signed-scalar `+` and `-`.",
1510
+ "- `d * k` scales both loads by an admissible Count k, `Demi [p, kL, kR]`, the",
1511
+ " same rule as `scale_`, refusing a non-Count with `$!!.invalid-scale`.",
1512
+ "- `d / n` observes Spin, the signed residual over a strictly positive Count,",
1513
+ " `(L − R) / n`. It answers a number, not a Demi, and keeps the typed divisor:",
1514
+ " a non-Count, including a negative, is a type error, and a zero divisor is",
1515
+ " `$!.division-by-zero`.",
1516
+ "",
1517
+ "None of these is sequential application: `d x` deposits in the active channel",
1518
+ "and flips the phase, while `+`, `-` and `*` choose a channel explicitly and",
1519
+ "keep it. So `+` and `-` reach states a run cannot, a phase-2 Demi with a load",
1520
+ "on the odd side, and `*` is the Star shadow, not a repeated deposit.",
1436
1521
  "",
1437
1522
  "**Text.** A Demi prints as what it is, an aggregate of fields and methods, and",
1438
1523
  "that reads back as the same Demi. As text it reads `Demi(<>,12,3)`, with the",
@@ -1457,6 +1542,14 @@ export const HC_SOURCES = new Map([
1457
1542
  " (s ~ (0.<>)) ? {((_^^ .phase) = 2) ? {@left (l + s); @phase 1;} : {@right (r + s); @phase 2;}; _^^} : {s}};",
1458
1543
  " .scale_ {.k _; .l (_^ .left); .r (_^ .right);",
1459
1544
  " (k ~ (0.<>)) ? {@left (l * k); @right (r * k); _^^} : {$!!.invalid-scale}};",
1545
+ " .+ {.s (_ .##); .p (_^ .phase); .l (_^ .left); .r (_^ .right);",
1546
+ " (s ~ (0.<>)) ? {Demi [p, l, (r + s)]} : {s}};",
1547
+ " .- {.s (_ .##); .p (_^ .phase); .l (_^ .left); .r (_^ .right);",
1548
+ " (s ~ (0.<>)) ? {Demi [p, (l + s), r]} : {s}};",
1549
+ " .* {.k _; .p (_^ .phase); .l (_^ .left); .r (_^ .right);",
1550
+ " (k ~ (0.<>)) ? {Demi [p, (l * k), (r * k)]} : {$!!.invalid-scale}};",
1551
+ " ./ {.n <(1).<>> _; .signed (_^ .###);",
1552
+ " (n .< 1) ? {signed / 0} : {signed / n}};",
1460
1553
  " .toText {“Demi(” (((_^ .phase) = 2) ? {“<>”} : {“()”}) “,” (_^ .left) “,” (_^ .right) “)”}",
1461
1554
  " ]} : {$!!.invalid-demi}} : {$!!.invalid-demi}};",
1462
1555
  "```",
@@ -1615,11 +1708,73 @@ export const HC_SOURCES = new Map([
1615
1708
  "; [([5, 3] * 3).#### = ([5, 3].####.scale_ 3), ([[[]], []] * [[], []]).#### = ([[[]], []].####.scale_ ([[], []].##))]",
1616
1709
  "# [<>, <>]",
1617
1710
  "```",
1618
- "No operator is overloaded: `+` is ordinary addition, which a Demi is not a",
1619
- "number for, and `*` is ordinary Star, which refuses an aggregate with fields",
1711
+ "## Arithmetic",
1712
+ "",
1713
+ "Four operators act on a Demi's state. `+`, `-` and `*` **construct a fresh",
1714
+ "Demi** from observations of their operands: `+` deposits on the right and `-`",
1715
+ "on the left, each by the operand's Size, keeping the phase, and `*` scales both",
1716
+ "loads by a Count. `/` is the one observation among them: it observes Spin and",
1717
+ "answers a number, not a Demi. Each answers a fresh value and leaves its operand",
1718
+ "as it was, so they compose by nesting",
1719
+ "```",
1720
+ "; .D (Demi [2, 5, 3]);",
1721
+ "; [“” (D + 4), “” (D - 4), “” (D * 3)]",
1722
+ "# [“Demi(<>,5,7)”, “Demi(<>,9,3)”, “Demi(<>,15,9)”]",
1723
+ "; [(D / 2), ((D + 4) / 2)]",
1724
+ "# [1, -1]",
1725
+ "; [“” ((D + 4) - 2), “” ((D - 2) + 4), “” ((D + 4) * 3)]",
1726
+ "# [“Demi(<>,7,7)”, “Demi(<>,7,7)”, “Demi(<>,15,21)”]",
1727
+ "; “” D",
1728
+ "# “Demi(<>,5,3)”",
1729
+ "```",
1730
+ "A deposit keeps the phase, including phase 1, where application would flip it,",
1731
+ "and a zero Size deposits nothing while still answering a Demi. The operand is",
1732
+ "measured by its Size, so any sized Frame deposits",
1733
+ "```",
1734
+ "; [(D + 4).#, (D - 4).#, (D * 3).#]",
1735
+ "# [2, 2, 2]",
1736
+ "; .P (Demi [1, 5, 3]);",
1737
+ "; [“” (P + 4), (P + 4).#, “” (P - 4)]",
1738
+ "# [“Demi((),5,7)”, 1, “Demi((),9,3)”]",
1739
+ "; [“” (D + 0), “” (D - 0), “” (D [3, 4])]",
1740
+ "# [“Demi(<>,5,3)”, “Demi(<>,5,3)”, “Demi((),12,3)”]",
1741
+ "```",
1742
+ "Deposit is not application and deposit is not subtraction. `D x` flips the",
1743
+ "phase and `D + x` does not, so they reach different states; `D - x` adds to the",
1744
+ "left load and never takes from the right, so a load stays a whole Count and the",
1745
+ "signed residual only rises",
1746
+ "```",
1747
+ "; [(D + 4).###, (D - 4).###, D.###]",
1748
+ "# [-2, 6, 2]",
1749
+ "; [((D 4).# = (D + 4).#), (D 4).###, (D + 4).###]",
1750
+ "# [(), 6, -2]",
1751
+ "```",
1752
+ "Order of a right then a left deposit does not matter, and scaling after a",
1753
+ "balanced deposit is the Star shadow of scaling first",
1754
+ "```",
1755
+ "; [((D + 4) - 2) = ((D - 2) + 4), ((D + 4) * 3) = ((D * 3) + 12)]",
1756
+ "# [<>, <>]",
1757
+ "```",
1758
+ "A sizeless host refuses, its failure the answer, as application refuses it.",
1759
+ "`*` keeps the `scale_` domain, refusing a non-Count, and never flips to Star on",
1760
+ "the aggregate. `/` keeps Spin's typed, strictly positive Count divisor: a",
1761
+ "non-Count, a negative among them, is a type error, and a zero divisor is",
1762
+ "division by zero, even for a zero residual",
1763
+ "```",
1764
+ "; [D + ([].nope), D - ([].nope)]",
1765
+ "# [$!.name-missing “..., $!.name-missing “...]",
1766
+ "; [D * (0 - 1), D * 2.5]",
1767
+ "# [$!!.invalid-scale, $!!.invalid-scale]",
1768
+ "; [D / 0, D / (0 - 1), D / 2.5]",
1769
+ "# [$!.division-by-zero /, $!.type-error .n <(1).<>> -1, $!.type-error .n <(1).<>> 2.5]",
1770
+ "; (Demi [2, 4, 4]) / 0",
1771
+ "# $!.division-by-zero /",
1620
1772
  "```",
1621
- "; [e + 3, e * 3]",
1622
- "# [(), $!.star-metadata *]",
1773
+ "The signed-scalar reading misleads: `+` lowers the residual and `-` raises it,",
1774
+ "so `D + 4` is not “larger” than `D`, and `D - 4` is not `D` made smaller",
1775
+ "```",
1776
+ "; [(D + 4).### .< D.###, (D - 4).### .> D.###]",
1777
+ "# [<>, <>]",
1623
1778
  "```",
1624
1779
  "## A precise regression: a load read at the wrong depth",
1625
1780
  "",
@@ -249,6 +249,25 @@ export class FrameArray extends FrameList {
249
249
  }
250
250
  return undefined;
251
251
  }
252
+ /**
253
+ * The method this aggregate declares for the operator `key`, bound to it as
254
+ * the left operand, or undefined when it declares none (015.02).
255
+ *
256
+ * Found on the aggregate or its declared parents, the same class chain its
257
+ * other methods come from, and never in the scope around it, so an operator
258
+ * written inside a method body keeps its ordinary meaning rather than
259
+ * recurring into the class that is running. The method runs immutably, like a
260
+ * declared observation: a deposit answers a fresh instance, as a functional
261
+ * update does, and leaves the left operand unchanged.
262
+ */
263
+ declaredOperator(key) {
264
+ if (!FrameArray.OPERATOR_KEYS.includes(key))
265
+ return undefined;
266
+ const method = this.declaredMethod(key);
267
+ if (!method)
268
+ return undefined;
269
+ return new BoundMethod(method, this, false, methodEffect(key));
270
+ }
252
271
  /**
253
272
  * What this aggregate contributes when text receives it (#398).
254
273
  *
@@ -455,6 +474,26 @@ Object.defineProperty(FrameArray, "APPLICATION_KEYS", {
455
474
  writable: true,
456
475
  value: ["apply_", "apply"]
457
476
  });
477
+ /**
478
+ * The operators an aggregate may declare its own answer to (015.02).
479
+ *
480
+ * These are the ordinary binary arithmetic spellings. A class declares one
481
+ * as a method under the operator's own name, `+`, `-`, `*` or `/`, the way
482
+ * it declares `apply`, `#` or `toText`. The method takes the right operand
483
+ * and answers the operation's result, so the class decides what the operator
484
+ * means on its instances without any special case in the evaluator.
485
+ */
486
+ Object.defineProperty(FrameArray, "OPERATOR_KEYS", {
487
+ enumerable: true,
488
+ configurable: true,
489
+ writable: true,
490
+ value: [
491
+ "+",
492
+ "-",
493
+ "*",
494
+ "/",
495
+ ]
496
+ });
458
497
  /** The key an aggregate declares its readable text under (#398). */
459
498
  Object.defineProperty(FrameArray, "TEXT_KEY", {
460
499
  enumerable: true,
@@ -448,7 +448,27 @@ export class FrameOperator extends FrameSymbol {
448
448
  return bound;
449
449
  }
450
450
  called_by(context) {
451
- const value = FrameSymbol.for(this.data).called_by(context);
451
+ // An operator resolves against its left operand and the global table only,
452
+ // never the lexical scope the operand was read from (015.02). A class may
453
+ // answer an operator on its own instances by declaring a method under the
454
+ // operator's name, found on the operand and its declared parents, the chain
455
+ // its other methods come from. Resolving nothing else keeps an operator
456
+ // written inside a method body meaning the global operation rather than
457
+ // recurring into the class that is running, which a lexical walk would do
458
+ // for a load read back from the receiver.
459
+ const left = context.referent();
460
+ if (left instanceof FrameArray) {
461
+ const declared = left.declaredOperator(this.data);
462
+ if (declared)
463
+ return declared;
464
+ }
465
+ const globals = Frame.globals;
466
+ const resolved = typeof globals.operation === "function"
467
+ ? globals.operation(this.data, context)
468
+ : Frame.missing;
469
+ const value = resolved.is.missing
470
+ ? FrameSymbol.for(this.data).called_by(context)
471
+ : resolved;
452
472
  return value instanceof FrameCurry && this.receiverState
453
473
  ? value.withReceiverState(this.receiverState)
454
474
  : value;
@@ -17,6 +17,19 @@ export class FrameOps extends Frame {
17
17
  }
18
18
  return Frame.missing;
19
19
  }
20
+ /**
21
+ * The built-in operation for `key`, curried to `origin` as its left operand,
22
+ * or missing when there is no such operation (015.02).
23
+ *
24
+ * Operator dispatch resolves a left operand's own declared method first, then
25
+ * this global table, and never the lexical scope the operand was read from.
26
+ * That keeps an operator written inside a method body meaning the global
27
+ * operation rather than the class's own method for the same spelling.
28
+ */
29
+ operation(key, origin) {
30
+ const func = this.OpsDict[key];
31
+ return func != null ? this.curry(func, origin, key) : Frame.missing;
32
+ }
20
33
  toString() {
21
34
  return this.OpsDict.toString();
22
35
  }
@@ -1 +1 @@
1
- export const VERSION = "0.41.0";
1
+ export const VERSION = "0.41.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.41.0",
3
+ "version": "0.41.1",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",