hclang 0.27.0 → 0.29.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.
@@ -142,6 +142,11 @@ export class ParsePipe extends FrameArray {
142
142
  if (this.nested && group instanceof FrameGroup) {
143
143
  group.declares = true;
144
144
  }
145
+ // A nested aggregate is source until it is evaluated: it walks as a body
146
+ // does, statements and all, where a value's statements answer nothing
147
+ // (spec a16). Evaluation builds a value without the mark.
148
+ if (this.nested)
149
+ group.is.source = true;
145
150
  if (this.sequenced) {
146
151
  group.is.sequence = true;
147
152
  }
@@ -12,9 +12,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
12
12
  var _LiveLookupContext_lookupFrames, _LiveLookupContext_identityHandles, _EvaluationScope_lookupFrames, _EvaluationScope_lookupMembers, _EvaluationScope_argumentLayer;
13
13
  import { Frame } from "./frame.js";
14
14
  import { FrameHandle } from "./frame-handle.js";
15
+ /**
16
+ * Whether `frame` is a resumed walk's view of its closure's scope, through
17
+ * which a binding can be read and never written (spec a17).
18
+ */
19
+ export const isResumedLookup = (frame) => frame instanceof LiveLookupContext && frame.resumed;
15
20
  /** Live, read-only lookup through an ordered set of existing frame identities. */
16
21
  class LiveLookupContext extends Frame {
17
- constructor(frames, continuation) {
22
+ constructor(frames, continuation,
23
+ /** Whether a resumed walk reads through this, rather than an import. */
24
+ resumed = false) {
18
25
  super();
19
26
  Object.defineProperty(this, "continuation", {
20
27
  enumerable: true,
@@ -22,6 +29,12 @@ class LiveLookupContext extends Frame {
22
29
  writable: true,
23
30
  value: continuation
24
31
  });
32
+ Object.defineProperty(this, "resumed", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: resumed
37
+ });
25
38
  _LiveLookupContext_lookupFrames.set(this, void 0);
26
39
  _LiveLookupContext_identityHandles.set(this, new WeakMap());
27
40
  __classPrivateFieldSet(this, _LiveLookupContext_lookupFrames, frames.map((frame) => frame instanceof FrameHandle ? frame.withoutWriteAuthority() : frame), "f");
@@ -363,6 +376,45 @@ export class EvaluationScope {
363
376
  hostNamespace: this.hostNamespace,
364
377
  });
365
378
  }
379
+ /**
380
+ * This scope continued through `scope` instead of past `site`, read-only.
381
+ *
382
+ * A fold resumes a walk where the fold is written (`site`), but a term the
383
+ * walk held resumes where its own closure was bound (`scope`), as the
384
+ * closure itself would have: the resumption's own layers come first, its
385
+ * fresh local frame and the receiver, then that scope. The receiver is kept
386
+ * whatever its identity, since it may also be one of the site's layers. The
387
+ * enclosing scope is `scope` too, so `__` and deeper levels and carets read
388
+ * the closure's chain, never the site's. The scope is read through a live
389
+ * lookup that withholds write authority, and declarations land in the
390
+ * resumption's own frame, so a resumed term never writes where its closure
391
+ * was made (spec a17).
392
+ */
393
+ resumedIn(site, scope) {
394
+ // The resumed closure is not one of its own layers here: its `up` is the
395
+ // site, so reading through it would reach the site after all.
396
+ const past = new Set(site?.lookupFrames() ?? []);
397
+ if (this.closure)
398
+ past.add(this.closure);
399
+ const own = __classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f").filter((frame) => frame === __classPrivateFieldGet(this, _EvaluationScope_argumentLayer, "f") || !past.has(frame));
400
+ const live = new LiveLookupContext(scope.lookupFrames(), scope.lexicalTarget, true);
401
+ const layers = EvaluationScope.layers([...own, live]);
402
+ return new EvaluationScope({
403
+ argument: this.argument,
404
+ parameter: this.parameter,
405
+ receiverState: this.receiverState,
406
+ writeTarget: this.writeTarget,
407
+ writeTargetRole: this.writeTargetRole,
408
+ lookupFrames: layers.frames,
409
+ lookupMembers: layers.members,
410
+ argumentLayer: __classPrivateFieldGet(this, _EvaluationScope_argumentLayer, "f"),
411
+ enclosing: scope,
412
+ closure: this.closure,
413
+ lexicalTarget: this.lexicalTarget,
414
+ fileScope: this.fileScope,
415
+ hostNamespace: this.hostNamespace,
416
+ });
417
+ }
366
418
  /** Accessor whose identity grades private and protected reads. */
367
419
  accessOrigin() {
368
420
  return this.receiverState?.receiver ?? this.writeTarget;
@@ -5,7 +5,7 @@ import { FrameHandle } from "./frame-handle.js";
5
5
  import { FrameLazy } from "./frame-lazy.js";
6
6
  import { FrameSymbol } from "./frame-symbol.js";
7
7
  import { NilContext } from "./context.js";
8
- import { EvaluationScope } from "./evaluation-scope.js";
8
+ import { EvaluationScope, isResumedLookup, } from "./evaluation-scope.js";
9
9
  import { completeAtEnd, includeOrReserve } from "./atom-syntax.js";
10
10
  import { authorizedReceiverWriteTarget } from "./bound-method.js";
11
11
  export class FrameAlias extends FrameAtom {
@@ -69,6 +69,9 @@ export class FrameAlias extends FrameAtom {
69
69
  }
70
70
  return FrameNote.key(key, this);
71
71
  }
72
+ resolvesInScope() {
73
+ return true;
74
+ }
72
75
  string_prefix() {
73
76
  return FrameAlias.ALIAS_BEGIN;
74
77
  }
@@ -110,6 +113,12 @@ export class FrameAlias extends FrameAtom {
110
113
  if (copyOnWrite && !copyOnWrite.has(found.out)) {
111
114
  return Frame.error(`$!.copy-on-write-boundary .${key}`);
112
115
  }
116
+ // A resumed walk reads its closure's scope and never writes it: a walk
117
+ // can be recombined, so a resumed write would be one the closure itself
118
+ // never makes (spec a17).
119
+ if (isResumedLookup(found.out)) {
120
+ return Frame.error(`$!.resumed-write @${key}`);
121
+ }
113
122
  return FrameSymbol.for(found.key).setter(found.out);
114
123
  }
115
124
  }
@@ -60,6 +60,9 @@ export class FrameExpr extends FrameList {
60
60
  call(argument, parameter = Frame.nil) {
61
61
  return this.in(EvaluationScope.call(argument, parameter));
62
62
  }
63
+ isPlainExpression() {
64
+ return Object.getPrototypeOf(this) === FrameExpr.prototype;
65
+ }
63
66
  /** A parsed expression walks as its atoms (#429). */
64
67
  elements() {
65
68
  return atomsOf(this.data, sitesAround(this.data));
@@ -92,11 +95,11 @@ export class FrameExpr extends FrameList {
92
95
  // operand that follows it.
93
96
  if (sum.isFailedResult())
94
97
  return sum;
95
- const value = item.in(scope);
98
+ const value = item.in(resumedScope(item, scope));
96
99
  if (value.is.error === true)
97
100
  return value;
98
101
  if (index > 0 && value.is.operator === true) {
99
- return value.called_by(sum, Frame.nil);
102
+ return value.called_by(sum, Frame.nil).atSite(scope);
100
103
  }
101
104
  return sum.call(value);
102
105
  }, Frame.nil);
@@ -105,7 +108,7 @@ export class FrameExpr extends FrameList {
105
108
  if (this.data.length !== 2 || !(this.data[0] instanceof FrameSymbol) ||
106
109
  !(this.data[1] instanceof FrameName) || this.data[1].source !== "<>")
107
110
  return undefined;
108
- return this.data[0].bindingSchema(scope);
111
+ return this.data[0].bindingSchema(resumedScope(this.data[0], scope));
109
112
  }
110
113
  schemaDefinition(scope) {
111
114
  if (!this.is.statement || this.data.length !== 2)
@@ -186,7 +189,7 @@ const enclosingSites = new WeakMap();
186
189
  * nowhere it can see is free: it resolves per call, argument first (spec 7),
187
190
  * so its word carries no declaration.
188
191
  */
189
- export function atomsOf(terms, sites = new Map()) {
192
+ export function atomsOf(terms, sites = new Map(), resume) {
190
193
  const scope = new Map(sites);
191
194
  const atoms = [];
192
195
  const heads = new Set();
@@ -207,6 +210,8 @@ export function atomsOf(terms, sites = new Map()) {
207
210
  enclosingSites.set(held[0], scope);
208
211
  atomsOf(held, scope);
209
212
  };
213
+ /** A compound atom resumes where its closure was bound, as a word does. */
214
+ const scoped = (term) => resume ? new FrameScoped(term, resume) : term;
210
215
  const walkAtom = (term) => {
211
216
  if (isPlainExpr(term)) {
212
217
  const held = term.asArray();
@@ -215,7 +220,7 @@ export function atomsOf(terms, sites = new Map()) {
215
220
  }
216
221
  else {
217
222
  noteTerms(held);
218
- atoms.push(term);
223
+ atoms.push(scoped(term));
219
224
  }
220
225
  }
221
226
  else if (term instanceof FrameName) {
@@ -223,19 +228,31 @@ export function atomsOf(terms, sites = new Map()) {
223
228
  }
224
229
  else if (term instanceof FrameSymbol) {
225
230
  const spelling = term.spelling();
226
- atoms.push(FrameWord.word(spelling, scope.get(spelling)));
231
+ atoms.push(FrameWord.word(spelling, scope.get(spelling), resume));
227
232
  }
228
- else {
229
- if (isClosure(term)) {
230
- const first = term.asArray()[0];
231
- if (first)
232
- enclosingSites.set(first, scope);
233
- }
234
- else if (term instanceof FrameList) {
235
- for (const element of term.asArray()) {
236
- noteTerms(isPlainExpr(element) ? element.asArray() : [element]);
237
- }
233
+ else if (isClosure(term)) {
234
+ const first = term.asArray()[0];
235
+ if (first)
236
+ enclosingSites.set(first, scope);
237
+ atoms.push(scoped(term));
238
+ }
239
+ else if (resume && term.resolvesInScope()) {
240
+ // An alias resolves its binding where it is evaluated, so it is held
241
+ // with its closure's scope as a word is (spec a17).
242
+ atoms.push(scoped(term));
243
+ }
244
+ else if (term instanceof FrameList) {
245
+ // Keyed by its first element, which is how its own walk asks, and by
246
+ // each element's first term, which is how a walked element asks.
247
+ const [first] = term.asArray();
248
+ if (first)
249
+ enclosingSites.set(first, scope);
250
+ for (const element of term.asArray()) {
251
+ noteTerms(isPlainExpr(element) ? element.asArray() : [element]);
238
252
  }
253
+ atoms.push(scoped(term));
254
+ }
255
+ else {
239
256
  atoms.push(term);
240
257
  }
241
258
  };
@@ -277,9 +294,102 @@ function firstAtom(term) {
277
294
  */
278
295
  export function noteEnclosing(inner, outer) {
279
296
  const first = inner.asArray()[0];
280
- if (first && !enclosingSites.has(first))
281
- outer.elements();
297
+ if (first && !enclosingSites.has(first)) {
298
+ // Walked for the sites it records, so without the scope wrappers.
299
+ const body = outer.asArray();
300
+ atomsOf(body, sitesAround(body));
301
+ }
302
+ }
303
+ /**
304
+ * A compound atom of a walk, a tight expression, an aggregate or a closure
305
+ * written in a body, held with the scope its closure was bound in (spec a17).
306
+ *
307
+ * A walk is evaluation stopped before each term's last step, so what it holds
308
+ * is what evaluation holds: the term, and where it would be evaluated. The term
309
+ * is the template's own frame, so this is transparent. It prints, compares and
310
+ * counts as the term, and walks as the term does with the scope passed on, so
311
+ * a walk of a walk keeps resuming where the first closure was bound. Applying
312
+ * it, or folding it, binds a closure there first.
313
+ */
314
+ export class FrameScoped extends Frame {
315
+ constructor(term, scope) {
316
+ super();
317
+ Object.defineProperty(this, "term", {
318
+ enumerable: true,
319
+ configurable: true,
320
+ writable: true,
321
+ value: term
322
+ });
323
+ Object.defineProperty(this, "scope", {
324
+ enumerable: true,
325
+ configurable: true,
326
+ writable: true,
327
+ value: scope
328
+ });
329
+ }
330
+ /** The term, a closure bound where it would have been. */
331
+ live() {
332
+ return isClosure(this.term) ? this.term.in(this.scope) : this.term;
333
+ }
334
+ referent() {
335
+ return this.term.referent();
336
+ }
337
+ elements() {
338
+ const terms = this.term.asArray();
339
+ return atomsOf(terms, sitesAround(terms), this.scope);
340
+ }
341
+ asArray() {
342
+ return this.term.asArray();
343
+ }
344
+ walked() {
345
+ return this;
346
+ }
347
+ sizeFor(key) {
348
+ return this.term.sizeFor(key);
349
+ }
350
+ /** Counts as its term walks, `.#` and `.##`; anything else is applied. */
351
+ call(argument, parameter = Frame.nil) {
352
+ if (argument instanceof FrameSymbol && argument.is.address === true &&
353
+ (argument.spelling() === Frame.kCOUNT ||
354
+ argument.spelling() === Frame.kLEAVES)) {
355
+ return this.sizeFor(argument.spelling());
356
+ }
357
+ return this.live().call(argument, parameter);
358
+ }
359
+ equals(other) {
360
+ return this.term.equals(other.referent());
361
+ }
362
+ isPlainExpression() {
363
+ return this.term.isPlainExpression();
364
+ }
365
+ toString() {
366
+ return this.term.toString();
367
+ }
368
+ dataString() {
369
+ return this.term.dataString();
370
+ }
371
+ }
372
+ // Keyed by a term made for one resumption, a fresh symbol or a plumbing copy,
373
+ // never a shared template, so two resumptions cannot see each other's scope.
374
+ const resumptions = new WeakMap();
375
+ /**
376
+ * Marks `term`, made for one resumption, to finish evaluating in `scope` past
377
+ * `site` (spec a17). The term keeps its own class, so every structural check
378
+ * the evaluator makes on it still holds.
379
+ */
380
+ export function resumeAt(term, scope, site) {
381
+ resumptions.set(term, { scope, site });
382
+ return term;
383
+ }
384
+ /** The scope a body's term evaluates in: its resumption's, or the body's. */
385
+ function resumedScope(term, scope) {
386
+ const resumption = resumptions.get(term);
387
+ return resumption
388
+ ? scope.resumedIn(resumption.site, resumption.scope)
389
+ : scope;
282
390
  }
391
+ /** A source aggregate walks as a body does. */
392
+ FrameList.walkSource = (terms) => atomsOf(terms, sitesAround(terms));
283
393
  /** The declarations around a closure body, as far as a walk has seen them. */
284
394
  export function sitesAround(body) {
285
395
  return (body[0] && enclosingSites.get(body[0])) ?? new Map();
@@ -27,6 +27,7 @@ export class FrameGroup extends FrameList {
27
27
  */
28
28
  eval_one(input = []) {
29
29
  const target = this.copy();
30
+ target.is.source = false;
30
31
  const result = this.data[0].in(this.scoped(input, target));
31
32
  target.meta_pairs().forEach(([key, value]) => {
32
33
  result.set(key, value);
@@ -49,6 +50,7 @@ export class FrameGroup extends FrameList {
49
50
  }
50
51
  }
51
52
  const result = this.copy();
53
+ result.is.source = false;
52
54
  const scope = this.scoped(input, result);
53
55
  result.data = result.data.map((frame) => frame.in(scope));
54
56
  return result;
@@ -1,10 +1,10 @@
1
1
  import { Frame } from "./frame.js";
2
- import { FrameExpr, noteEnclosing } from "./frame-expr.js";
2
+ import { atomsOf, FrameExpr, noteEnclosing, sitesAround, } from "./frame-expr.js";
3
3
  import { FrameGroup } from "./frame-group.js";
4
4
  import { FrameSymbol } from "./frame-symbol.js";
5
5
  import { NilContext } from "./context.js";
6
6
  import { EvaluationScope } from "./evaluation-scope.js";
7
- import { renderNested } from "./stringify.js";
7
+ import { renderSourceTerm } from "./frame-list.js";
8
8
  // Both maps describe per-evaluation bound copies. The parsed/shared closure is
9
9
  // never a key, so evaluating one template in two scopes cannot rebind either
10
10
  // result or mutate the template's lexical ancestry.
@@ -49,25 +49,17 @@ export class FrameLazy extends FrameExpr {
49
49
  // statement, `, ` between terms the source separated, and a tight
50
50
  // expression, terms that abut as `o.x` does, in parentheses, since the
51
51
  // spacing that bound them cannot be restored (spec a16).
52
- const stringify = (obj) => renderNested(obj, () => obj instanceof FrameExpr && Object.getPrototypeOf(obj) ===
53
- FrameExpr.prototype
54
- ? `(${obj.asArray().map(stringify).join(" ")})`
55
- : obj.toString());
56
52
  const parts = [];
57
53
  this.data.forEach((term, index) => {
58
- const statement = term instanceof FrameExpr &&
59
- Object.getPrototypeOf(term) ===
60
- FrameExpr.prototype;
54
+ const statement = term.isPlainExpression();
61
55
  const text = statement
62
- ? term.asArray().map(stringify).join(" ")
63
- : stringify(term);
56
+ ? term.asArray().map(renderSourceTerm).join(" ")
57
+ : renderSourceTerm(term);
64
58
  // Only two statements the source separated take a comma; a body built
65
59
  // from bare terms, as a test may build one, is one juxtaposition.
66
60
  const previous = this.data[index - 1];
67
61
  const separated = index > 0 && statement &&
68
- previous instanceof FrameExpr &&
69
- Object.getPrototypeOf(previous) === FrameExpr.prototype &&
70
- previous.is.statement !== true;
62
+ previous.isPlainExpression() && previous.is.statement !== true;
71
63
  parts.push((separated ? ", " : index > 0 ? " " : "") + text +
72
64
  (term.is.statement ? ";" : ""));
73
65
  });
@@ -113,6 +105,18 @@ export class FrameLazy extends FrameExpr {
113
105
  }
114
106
  return undefined;
115
107
  }
108
+ /**
109
+ * A closure walks as the atoms of its body, each held with the scope this
110
+ * copy was bound in, so a fold resumes them where the closure would have
111
+ * run (spec a17). A template no evaluation has bound holds none.
112
+ */
113
+ elements() {
114
+ return atomsOf(this.data, sitesAround(this.data), capturedScopes.get(this));
115
+ }
116
+ /** Counting needs the atoms only, so it skips the scope wrappers. */
117
+ countedElements() {
118
+ return atomsOf(this.data, sitesAround(this.data));
119
+ }
116
120
  /** A closure is one atom of the body that holds it, never unwrapped. */
117
121
  walked() {
118
122
  return this;
@@ -4,6 +4,14 @@ import { EvaluationScope } from "./evaluation-scope.js";
4
4
  import { FrameCount } from "./frame-count.js";
5
5
  import { REPETITION_ELEMENT_LIMIT } from "./frame-numeric.js";
6
6
  import { renderNested } from "./stringify.js";
7
+ /**
8
+ * A term of a source statement, as it was written: a tight expression, terms
9
+ * that abut as `o.x` does, in parentheses, since the spacing that bound them
10
+ * cannot be restored and `(o .x)` means the same; anything else as itself.
11
+ */
12
+ export const renderSourceTerm = (term) => renderNested(term, () => term.isPlainExpression()
13
+ ? `(${term.asArray().map(renderSourceTerm).join(" ")})`
14
+ : term.toString());
7
15
  /**
8
16
  * The declaration a term recorded, when it is a declaration echo.
9
17
  *
@@ -55,7 +63,13 @@ export class FrameList extends Frame {
55
63
  toStringDataArray() {
56
64
  const result = this.renderedTerms().map((obj) => {
57
65
  const sep = (obj.is.statement) ? ";" : ",";
58
- return renderNested(obj, () => obj.toString()) + sep;
66
+ // A source aggregate prints as it was written: each statement bare,
67
+ // with a tight expression nested in it parenthesised (spec a16). A
68
+ // value's terms print as themselves.
69
+ const text = this.is.source === true && obj.isPlainExpression()
70
+ ? obj.asArray().map(renderSourceTerm).join(" ")
71
+ : renderNested(obj, () => obj.toString());
72
+ return text + sep;
59
73
  });
60
74
  return result;
61
75
  }
@@ -105,8 +119,18 @@ export class FrameList extends Frame {
105
119
  * iterate.
106
120
  */
107
121
  elements() {
122
+ if (this.is.source === true && FrameList.walkSource) {
123
+ return FrameList.walkSource(this.data);
124
+ }
108
125
  return this.data.filter((item) => this.isElement(item)).map((item) => item.walked());
109
126
  }
127
+ /**
128
+ * The elements a size operator counts: `elements()`, unless a kind can
129
+ * count them more cheaply, as a closure does without its scope (a17).
130
+ */
131
+ countedElements() {
132
+ return this.elements();
133
+ }
110
134
  /** Whether this term is one of the elements, as `elements()` reads them. */
111
135
  isElement(term) {
112
136
  return term.is.statement !== true && !this.holdsAsProperty(term);
@@ -118,7 +142,7 @@ export class FrameList extends Frame {
118
142
  */
119
143
  sizeFor(key) {
120
144
  if (key === Frame.kCOUNT) {
121
- return new FrameCount(BigInt(this.elements().length));
145
+ return new FrameCount(BigInt(this.countedElements().length));
122
146
  }
123
147
  const leaves = this.leafCount(new Set());
124
148
  return leaves === undefined
@@ -137,7 +161,7 @@ export class FrameList extends Frame {
137
161
  return undefined;
138
162
  path.add(this);
139
163
  let leaves = 0n;
140
- for (const element of this.elements()) {
164
+ for (const element of this.countedElements()) {
141
165
  const value = element.referent();
142
166
  const below = value instanceof FrameList ? value.leafCount(path) : 1n;
143
167
  if (below === undefined)
@@ -19,13 +19,17 @@ import { NilContext } from "./context.js";
19
19
  * identical when they are spelled alike.
20
20
  *
21
21
  * A word answers nothing about the value its spelling would name, and it is
22
- * not an operator, so a walked `+` is an element like any other. The one
23
- * place words turn back into code is a fold: `|` over a stream that holds a
24
- * word re-reads the stream as a closure, `closureFromAtoms`, and calls it with
25
- * the receiver, so `{x + 1} | (.x 5)` is `6`.
22
+ * not an operator, so a walked `+` is an element like any other. A walk is
23
+ * evaluation stopped before each term's last step, so a word also carries the
24
+ * scope its closure was bound in. The one place words turn back into code is
25
+ * a fold, which resumes them: `|` over a stream that holds a word calls the
26
+ * body it spells with the receiver, and each word resolves after the receiver
27
+ * where its closure would have, so `{x + 1} | (.x 5)` is `6` (spec a17).
26
28
  */
27
29
  export class FrameWord extends FrameAtom {
28
- constructor(data, dotted, site, meta = NilContext) {
30
+ constructor(data, dotted, site,
31
+ /** The scope the word's closure was bound in, where it resumes (a17). */
32
+ scope, meta = NilContext) {
29
33
  super(meta);
30
34
  Object.defineProperty(this, "data", {
31
35
  enumerable: true,
@@ -45,10 +49,16 @@ export class FrameWord extends FrameAtom {
45
49
  writable: true,
46
50
  value: site
47
51
  });
52
+ Object.defineProperty(this, "scope", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: scope
57
+ });
48
58
  }
49
59
  /** The word for a lookup, `a`, or an operator, `+`. */
50
- static word(spelling, site) {
51
- return new FrameWord(spelling, false, site);
60
+ static word(spelling, site, scope) {
61
+ return new FrameWord(spelling, false, site, scope);
52
62
  }
53
63
  /** The word for a declaration or address, `.a`. */
54
64
  static address(spelling, site) {
@@ -210,6 +210,31 @@ export class Frame extends MetaFrame {
210
210
  referent() {
211
211
  return this;
212
212
  }
213
+ /**
214
+ * Whether this is a plain parsed expression: a statement of a body or an
215
+ * aggregate, or a tight expression nested in one (spec a16). A closure is
216
+ * not, though it extends the same class. Asked here so an aggregate's
217
+ * printer can tell, without importing the expression module.
218
+ */
219
+ isPlainExpression() {
220
+ return false;
221
+ }
222
+ /**
223
+ * Records the scope an operation was written in, on the fresh value an
224
+ * operator answered for it. Only a fold reads it, to resume a walk there
225
+ * (spec a17); anything else is unchanged.
226
+ */
227
+ atSite(_site) {
228
+ return this;
229
+ }
230
+ /**
231
+ * Whether evaluating this atom resolves a binding where it is evaluated, as
232
+ * an alias does, rather than being a value or a name a walk already holds
233
+ * as a word. A walk holds such an atom with its closure's scope (spec a17).
234
+ */
235
+ resolvesInScope() {
236
+ return false;
237
+ }
213
238
  /**
214
239
  * What a walk over an aggregate yields for this element (#429). A value is
215
240
  * itself. A parsed term is syntax, and answers the atom it wraps, so the
@@ -1,19 +1,24 @@
1
1
  import { FrameArg } from "./frame-arg.js";
2
- import { FrameExpr } from "./frame-expr.js";
2
+ import { FrameExpr, FrameScoped, resumeAt } from "./frame-expr.js";
3
3
  import { FrameLazy } from "./frame-lazy.js";
4
4
  import { FrameName } from "./frame-name.js";
5
5
  import { FrameOperator, FrameSymbol } from "./frame-symbol.js";
6
6
  import { FrameWord } from "./frame-word.js";
7
7
  /**
8
- * The closure a walk re-reads as (#429): the inverse of `FrameLazy.elements`.
8
+ * The closure a walk resumes as (#429, spec a17): the inverse of
9
+ * `FrameLazy.elements`, bound where the fold is written.
9
10
  *
10
- * A word becomes the atom it spells, a lookup, an operator, a parameter or a
11
- * name, and a `;` word ends a statement, so the body is a sequence when one was
12
- * walked. Every other atom is kept as it is, a nested closure or aggregate
13
- * included. A fold over a stream that holds a word calls this closure with the
14
- * receiver, which is what `{…} | (args)` means.
11
+ * A walk is evaluation stopped before each term's last step, so this finishes
12
+ * it. A word becomes the atom it spells, a lookup, an operator, a parameter or
13
+ * a name, and a `;` word ends a statement, so the body is a sequence when one
14
+ * was walked. Every other atom is kept as it is, a nested closure or aggregate
15
+ * included. A lookup or compound atom the walk held with its closure's scope
16
+ * resolves there, after the resumption's own frame and the receiver; anything
17
+ * else resolves at `site`, as a name written there would. A fold over a
18
+ * stream that holds a word calls this closure with the receiver, which is what
19
+ * `{…} | (args)` means.
15
20
  */
16
- export function closureFromAtoms(atoms) {
21
+ export function closureFromAtoms(atoms, site) {
17
22
  const statements = [];
18
23
  let terms = [];
19
24
  let sequence = false;
@@ -34,27 +39,32 @@ export function closureFromAtoms(atoms) {
34
39
  close(false);
35
40
  }
36
41
  else {
37
- terms.push(atomOf(atom));
42
+ terms.push(atomOf(atom, site));
38
43
  }
39
44
  }
40
45
  close(false);
41
- const body = statements.length === 1 && !sequence
42
- ? statements[0].asArray()
43
- : statements;
44
- const closure = new FrameLazy(body);
46
+ // Statements stay statements, as the parser holds them, so a statement's
47
+ // own forms, such as `x .<>`, resume as they run.
48
+ const closure = new FrameLazy(statements);
45
49
  if (sequence)
46
50
  closure.is.sequence = true;
47
- return closure;
51
+ return site ? closure.bind(site) : closure;
48
52
  }
49
- function atomOf(atom) {
53
+ function atomOf(atom, site) {
54
+ if (atom instanceof FrameScoped) {
55
+ return resumeAt(atom.term.copy(), atom.scope, site);
56
+ }
50
57
  if (!(atom instanceof FrameWord))
51
58
  return atom;
52
59
  const spelling = atom.spelling();
53
60
  if (atom.isAddress())
54
61
  return new FrameName(spelling);
55
- if (/^_+$/.test(spelling))
56
- return FrameArg.level(spelling.length);
57
- return FrameOperator.Accepts(spelling[0])
58
- ? new FrameOperator(spelling)
59
- : FrameSymbol.for(spelling);
62
+ if (FrameOperator.Accepts(spelling[0]))
63
+ return new FrameOperator(spelling);
64
+ // A fresh term, never an interned one, so the resumption it is marked for
65
+ // belongs to this body alone.
66
+ const term = /^_+$/.test(spelling)
67
+ ? new FrameArg(spelling)
68
+ : new FrameSymbol(spelling);
69
+ return atom.scope ? resumeAt(term, atom.scope, site) : term;
60
70
  }
package/esm/lib/frames.js CHANGED
@@ -9,7 +9,7 @@ export { FrameBlob } from "./frames/frame-blob.js";
9
9
  export { FrameBytePayload, FrameBytes } from "./frames/frame-bytes.js";
10
10
  export { FrameComment } from "./frames/frame-comment.js";
11
11
  export { FrameDoc } from "./frames/frame-doc.js";
12
- export { FrameBind, FrameExpr } from "./frames/frame-expr.js";
12
+ export { FrameBind, FrameExpr, FrameScoped } from "./frames/frame-expr.js";
13
13
  // Evaluation scope is interpreter-internal: `EvaluationInput` appears in the
14
14
  // `Frame.in` signature, so the type is re-exported here for implementors and
15
15
  // tests, while `WriteTargetRole` stays private to the scope module.
@@ -28,8 +28,23 @@ export class FrameCurry extends Frame {
28
28
  writable: true,
29
29
  value: void 0
30
30
  });
31
+ Object.defineProperty(this, "site", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
31
37
  this.id += "." + key;
32
38
  }
39
+ /**
40
+ * Records the scope the operator was written in. The curry is fresh for
41
+ * each evaluation, so this costs a field: no copy, and the scope is held
42
+ * only as long as the curry is. A fold alone reads it (spec a17).
43
+ */
44
+ atSite(site) {
45
+ this.site = site;
46
+ return this;
47
+ }
33
48
  /** Captures method state on this fresh operation lookup for its callbacks. */
34
49
  withReceiverState(receiverState) {
35
50
  this.callbackReceiverState = receiverState;
@@ -44,7 +59,7 @@ export class FrameCurry extends Frame {
44
59
  const callbackReceiverState = receiverState && argument instanceof FrameLazy
45
60
  ? argument.receiverStateForCallback(receiverState)
46
61
  : undefined;
47
- return this.Func(this.Source, argument, callbackReceiverState);
62
+ return this.Func(this.Source, argument, callbackReceiverState, this.site);
48
63
  }
49
64
  toString() {
50
65
  return this.id; // `FrameCurry(${this.Source.id}, ${this.Func})`;
@@ -6,6 +6,7 @@ import { BoundMethod } from "../frames/bound-method.js";
6
6
  import { FrameLazy } from "../frames/frame-lazy.js";
7
7
  import { FrameList } from "../frames/frame-list.js";
8
8
  import { FrameWord } from "../frames/frame-word.js";
9
+ import { FrameScoped } from "../frames/frame-expr.js";
9
10
  import { closureFromAtoms } from "../frames/reread.js";
10
11
  /**
11
12
  * Iteration is application, twice.
@@ -67,17 +68,23 @@ export const SpliceElements = (source, receiver, receiverState) => new FrameArra
67
68
  * closure it spells and called the same way: `{x + 1} | (.x 5)` is `6`, and
68
69
  * `{…} | (args)` is a second spelling of `{…} (args)`.
69
70
  */
70
- export const ReduceElements = (source, receiver, receiverState) => {
71
- const code = source.referent();
71
+ export const ReduceElements = (source, receiver, receiverState, site) => {
72
+ const code = source instanceof FrameScoped
73
+ ? source.live()
74
+ : source.referent();
72
75
  if (code instanceof FrameLazy || code instanceof BoundMethod) {
73
76
  return code.call(receiver, Frame.nil, receiverState);
74
77
  }
75
78
  // Only an aggregate is inspected: a stream's elements are produced by
76
79
  // reading it, and the read belongs to the fold.
77
80
  if (code instanceof FrameList) {
78
- const values = code.elements();
79
- if (values.some((value) => value instanceof FrameWord)) {
80
- return closureFromAtoms(values).call(receiver, Frame.nil, receiverState);
81
+ // A held aggregate walks with its closure's scope, so its words resume
82
+ // there rather than at the site.
83
+ const values = (source instanceof FrameScoped ? source : code).elements();
84
+ // A walk is code when it holds a word or a term held with its closure's
85
+ // scope, so `{(…)} & {_}` resumes as `{x}` does.
86
+ if (values.some((value) => value instanceof FrameWord || value instanceof FrameScoped)) {
87
+ return closureFromAtoms(values, site).call(receiver, Frame.nil, receiverState);
81
88
  }
82
89
  }
83
90
  return source.reduce(receiver, receiverState);
@@ -1 +1 @@
1
- export const VERSION = "0.27.0";
1
+ export const VERSION = "0.29.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.27.0",
3
+ "version": "0.29.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",