hclang 0.22.0 → 0.23.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.
@@ -996,6 +996,39 @@ export const HC_SOURCES = new Map([
996
996
  "; [(shelf.tick_ ()).count, shelf.count, (shelf.tick_ ()).item === shelf.item]",
997
997
  "# [1, 0, <>]",
998
998
  "",
999
+ "`A method reaches its receiver one caret out, because the scope a method is",
1000
+ "written in is its aggregate, and a call binds it to the aggregate it was found",
1001
+ "on. An inherited method therefore reads the descendant, and bare names, which",
1002
+ "read the argument first, are not needed to reach it.`",
1003
+ "; .Base [.x “base”; .get {_^.x}];",
1004
+ "; .Kid [.^ Base; .x “kid”];",
1005
+ "; [Base.get(), Kid.get()]",
1006
+ "# [“base”, “kid”]",
1007
+ "; .Pt [.n 1; .peek {[n, _^.n]}];",
1008
+ "; Pt.peek [.n 7]",
1009
+ "# [7, 1]",
1010
+ "",
1011
+ "`A functional copy is its own receiver, so its methods read the fields the copy",
1012
+ "holds rather than those of the value it was copied from.`",
1013
+ "; .Tally [.n 1; .read {_^.n}; .bump_ {@n (_^.n + 1);}];",
1014
+ "; .tally2 (Tally.bump_ ());",
1015
+ "; [tally2.read(), (tally2.bump_ ()).n, Tally.n]",
1016
+ "# [2, 3, 1]",
1017
+ "",
1018
+ "`The receiver keeps the authority its method was granted. A non-mutating method",
1019
+ "that calls a mutator through it gets a functional update, and a mutating method",
1020
+ "reached through an immutable handle writes only its own copy.`",
1021
+ "; .Ticker [.k 0; .bump_ {@k (k + 1);}; .sneak {(_^.bump_ ()).k}; .twice_ {_^.bump_ (); _^.bump_ ();}];",
1022
+ "; [Ticker.sneak(), (Ticker.twice_ ()).k, Ticker.k]",
1023
+ "# [1, 2, 0]",
1024
+ "",
1025
+ "`A block nested in a method counts its carets lexically, so the method's",
1026
+ "receiver is one caret further out.`",
1027
+ "; .Outer [.k 1; .ks {[1, 2] & {_^^.k}}];",
1028
+ "; .Inner [.^ Outer; .k 5];",
1029
+ "; [Outer.ks(), Inner.ks()]",
1030
+ "# [[1, 1], [5, 5]]",
1031
+ "",
999
1032
  ].join("\n"),
1000
1033
  ],
1001
1034
  [
@@ -3257,7 +3290,9 @@ export const HC_SOURCES = new Map([
3257
3290
  "You can reach past the argument to the enclosing lexical scope (one level above)",
3258
3291
  "using the `_^` identifier. It is not a `super` reference: it names the scope the",
3259
3292
  "closure was written in, not a declared parent, and each caret is one more",
3260
- "lexical scope out regardless of how the closure was called.",
3293
+ "lexical scope out regardless of how the closure was called. A method is written",
3294
+ "in its aggregate, so in a method that scope is the receiver the call is bound",
3295
+ "to, which may be a copy or a descendant of the aggregate.",
3261
3296
  "```",
3262
3297
  "; .print-arg { var };",
3263
3298
  "; .print-parent { _^.var };",
@@ -236,10 +236,7 @@ export class EvaluationScope {
236
236
  const forwarded = receiverState !== undefined &&
237
237
  receiverState === enclosing?.receiverState;
238
238
  if (receiverState && !forwarded) {
239
- const receiverLookup = receiverState.lexicalContext
240
- ? new FrameHandle(receiverState.receiver, receiverState.mutable, receiverState.copyOnWrite, receiverState.lexicalContext)
241
- : receiverState.receiver;
242
- lookupFrames.push(receiverLookup);
239
+ lookupFrames.push(EvaluationScope.receiverLayer(receiverState));
243
240
  }
244
241
  if (closure)
245
242
  lookupFrames.push(closure);
@@ -388,11 +385,39 @@ export class EvaluationScope {
388
385
  lexicalAt(level) {
389
386
  if (level <= 0)
390
387
  return this.lexicalTarget;
391
- let scope = this.enclosing;
392
- for (let current = 1; current < level; current += 1) {
393
- scope = scope?.enclosing;
394
- }
395
- return scope?.lexicalTarget;
388
+ const inner = this.outward(level - 1);
389
+ // A method's body is written in its aggregate, and a call binds it to a
390
+ // receiver: the aggregate itself, an instance copy of it, or a declared
391
+ // descendant. The level that reaches the scope the method was written in
392
+ // reaches that receiver instead, so the caret count stays lexical while a
393
+ // copy or a descendant reads its own fields rather than its source's.
394
+ const receiverState = inner?.methodReceiverState();
395
+ if (receiverState)
396
+ return EvaluationScope.receiverLayer(receiverState);
397
+ return inner?.enclosing?.lexicalTarget;
398
+ }
399
+ /** The scope `steps` enclosing scopes out from this one. */
400
+ outward(steps) {
401
+ return steps <= 0 ? this : this.enclosing?.outward(steps - 1);
402
+ }
403
+ /**
404
+ * The receiver state this scope's call bound, when this scope is a method
405
+ * body rather than a block nested in one, which runs with the receiver state
406
+ * its method forwarded.
407
+ */
408
+ methodReceiverState() {
409
+ const state = this.receiverState;
410
+ return state && state !== this.enclosing?.receiverState ? state : undefined;
411
+ }
412
+ /**
413
+ * The receiver as a method call reads it: through a handle carrying the
414
+ * call's mutability and copy provenance when the read recorded a scope, so
415
+ * lookup through it reaches the receiver and its declared parents only.
416
+ */
417
+ static receiverLayer(state) {
418
+ return state.lexicalContext
419
+ ? new FrameHandle(state.receiver, state.mutable, state.copyOnWrite, state.lexicalContext)
420
+ : state.receiver;
396
421
  }
397
422
  /** Copies this scope's lookup layers with additions that are not present. */
398
423
  appended(...additions) {
@@ -134,6 +134,14 @@ export class FrameSymbol extends FrameAtom {
134
134
  this.isReceiverPathValue(receiver, value, origin)) {
135
135
  return new BoundMethod(resolved, receiver, receiverState.mutable, methodEffect(this.data), receiverState.copyOnWrite, receiverState.lexicalContext);
136
136
  }
137
+ // A method read by dot from a value no name holds binds to that value
138
+ // as a name would, immutably: it may still be shared, so a mutating
139
+ // method answers a functional update rather than writing it.
140
+ if (resolved instanceof FrameLazy && this.is.address === true &&
141
+ !(context instanceof FrameHandle) && context.ownsIdentity() &&
142
+ this.isReceiverPathValue(context, value, origin)) {
143
+ return new BoundMethod(resolved, context, false, methodEffect(this.data));
144
+ }
137
145
  const copyOnWrite = context instanceof FrameHandle
138
146
  ? context.copyOnWriteScope() ?? receiverState?.copyOnWrite
139
147
  : receiverState?.copyOnWrite;
@@ -1 +1 @@
1
- export const VERSION = "0.22.0";
1
+ export const VERSION = "0.23.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",