hclang 0.26.2 → 0.27.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.
@@ -933,7 +933,7 @@ export const HC_SOURCES = new Map([
933
933
  "compares exactly as the same value written out would.`",
934
934
  "; .point [.v 1; .set_ {@v _;}];",
935
935
  "; point.set_ 2",
936
- "# [.v 2; .set_ { @v _ };]",
936
+ "# [.v 2; .set_ { @v _; };]",
937
937
  "; [(point.set_ 2) & {_}, (point.set_ 2) = [.v 2; .set_ {@v _;}]]",
938
938
  "# [[], <>]",
939
939
  "",
@@ -109,8 +109,24 @@ export class ParsePipe extends FrameArray {
109
109
  return out;
110
110
  }
111
111
  let value = this.makeFrame();
112
- if (value instanceof FrameBind && value.isEmpty()) {
113
- return out;
112
+ if (value instanceof FrameBind) {
113
+ // A bind pipe holds what followed one whitespace. One term is itself:
114
+ // wrapping it added a node with no meaning, which printed as `((x))`
115
+ // and doubled the depth of every walk. Several terms that abut, as in
116
+ // `f o.x`, are one tight expression, evaluated before the term beside
117
+ // it applies, so they stay one `FrameExpr` node. No `FrameBind` reaches
118
+ // the graph (spec a16).
119
+ if (value.isEmpty())
120
+ return out;
121
+ const [expr] = value.asArray();
122
+ const term = expr instanceof FrameExpr && expr.size() === 1
123
+ ? expr.asArray()[0]
124
+ : expr;
125
+ // Collected as a token is, by `apply`: `call` would double-dispatch,
126
+ // and a bare symbol or operator answers a call by resolving itself.
127
+ const result = out.apply(term, Frame.nil);
128
+ out.call(terminal);
129
+ return result;
114
130
  }
115
131
  if (value instanceof FrameSchema && value.isEmpty()) {
116
132
  value = Frame.all;
@@ -273,7 +273,18 @@ export class EvaluationScope {
273
273
  * receiver authority are deliberately not inherited.
274
274
  */
275
275
  namedChild(namespace) {
276
- namespace.up = new LiveLookupContext(__classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f"), namespace);
276
+ // A module imported during a construction is not that construction's
277
+ // child: its statements are its own file's, and the aggregate under
278
+ // construction is an accident of where the import was written. So the
279
+ // namespace sees the importing file before the construction, as #391
280
+ // designed, though the construction's own terms see it first (spec a16).
281
+ const frames = this.writeTargetRole === "construction"
282
+ ? [
283
+ ...__classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f").filter((frame) => frame !== this.writeTarget),
284
+ this.writeTarget,
285
+ ]
286
+ : __classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f");
287
+ namespace.up = new LiveLookupContext(frames, namespace);
277
288
  const layers = EvaluationScope.layers([namespace]);
278
289
  return new EvaluationScope({
279
290
  argument: namespace,
@@ -296,7 +307,23 @@ export class EvaluationScope {
296
307
  withLayer(frame) {
297
308
  if (__classPrivateFieldGet(this, _EvaluationScope_lookupMembers, "f").has(frame))
298
309
  return this;
299
- const layers = this.appended(frame);
310
+ return this.withLayers(this.appended(frame));
311
+ }
312
+ /**
313
+ * Adds an aggregate literal's own frame ahead of every other layer.
314
+ *
315
+ * An aggregate under construction is the current child of spec 7 §2, so
316
+ * its own properties are searched first: a name it declares shadows the
317
+ * same name in an enclosing scope, as a closure's local does. Appending it
318
+ * instead searched it last, so `.gb 1; (.gb 2; gb)` read the outer `gb`
319
+ * (Figure 004.09a).
320
+ */
321
+ withOwnLayer(frame) {
322
+ if (__classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f")[0] === frame)
323
+ return this;
324
+ return this.withLayers(this.prepended(frame));
325
+ }
326
+ withLayers(layers) {
300
327
  return new EvaluationScope({
301
328
  argument: this.argument,
302
329
  parameter: this.parameter,
@@ -315,7 +342,11 @@ export class EvaluationScope {
315
342
  }
316
343
  /** Selects a declaration target explicitly instead of scanning syntax. */
317
344
  withWriteTarget(target, role = "construction") {
318
- const layers = this.appended(target);
345
+ // A construction target is the aggregate itself, searched first; any
346
+ // other target is a destination for writes, and joins lookup last.
347
+ const layers = role === "construction"
348
+ ? this.prepended(target)
349
+ : this.appended(target);
319
350
  return new EvaluationScope({
320
351
  argument: this.argument,
321
352
  parameter: this.parameter,
@@ -429,6 +460,13 @@ export class EvaluationScope {
429
460
  ? new FrameHandle(state.receiver, state.mutable, state.copyOnWrite, state.lexicalContext)
430
461
  : state.receiver;
431
462
  }
463
+ /** Copies this scope's lookup layers with `frame` first, wherever it was. */
464
+ prepended(frame) {
465
+ const frames = [frame, ...__classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f").filter((f) => f !== frame)];
466
+ const members = new Set(__classPrivateFieldGet(this, _EvaluationScope_lookupMembers, "f"));
467
+ members.add(frame);
468
+ return { frames, members };
469
+ }
432
470
  /** Copies this scope's lookup layers with additions that are not present. */
433
471
  appended(...additions) {
434
472
  const frames = [...__classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f")];
@@ -66,10 +66,9 @@ export class FrameExpr extends FrameList {
66
66
  }
67
67
  /** A parsed expression of one atom is that atom; anything else is itself. */
68
68
  walked() {
69
- if (!isPlainExpr(this) && !(this instanceof FrameBind)) {
69
+ if (!isPlainExpr(this))
70
70
  return this;
71
- }
72
- const atoms = atomsOf([this]);
71
+ const atoms = atomsOf([this], sitesAround(this.data));
73
72
  return atoms.length === 1 ? atoms[0] : this;
74
73
  }
75
74
  /**
@@ -143,6 +142,11 @@ export class FrameExpr extends FrameList {
143
142
  : term;
144
143
  }
145
144
  }
145
+ /**
146
+ * The parser's marker for a bind pipe, what one whitespace started. It names
147
+ * the pipe only: a bind emits its one term bare, or several abutting terms as
148
+ * a tight `FrameExpr`, so no `FrameBind` is ever a node of the graph (a16).
149
+ */
146
150
  export class FrameBind extends FrameExpr {
147
151
  }
148
152
  /** A parsed expression itself, not a closure or a bind. */
@@ -152,8 +156,7 @@ const isPlainExpr = (term) => term instanceof FrameExpr &&
152
156
  * A closure literal. `FrameLazy` is the one other subclass of `FrameExpr`, and
153
157
  * it cannot be imported here, because it extends this module's class.
154
158
  */
155
- const isClosure = (term) => term instanceof FrameExpr && !isPlainExpr(term) &&
156
- !(term instanceof FrameBind);
159
+ const isClosure = (term) => term instanceof FrameExpr && !isPlainExpr(term);
157
160
  /**
158
161
  * The declarations visible to each closure body a walk has passed through.
159
162
  *
@@ -166,17 +169,18 @@ const enclosingSites = new WeakMap();
166
169
  /**
167
170
  * The atoms of parsed terms, in source order, as data (#429).
168
171
  *
169
- * The parser wraps each atom of a juxtaposition as `FrameBind` holding a
170
- * one-term `FrameExpr`, and those wrappers vanish here. A symbol, operator or
171
- * name becomes a word, so it never resolves. A term that ended in `;` is
172
- * followed by the word `;`, so the walk's text re-reads as the same source;
173
- * a `,` between terms is not kept. Any other atom, a count, a string, a
174
- * closure or an aggregate written in the body, is yielded itself, unevaluated,
175
- * and an aggregate walks in turn by the same rule.
172
+ * The terms are a body's statements. Each statement's atoms are yielded in
173
+ * order; a statement that ended in `;` is followed by the word `;`, and two
174
+ * statements the source separated with `,` get the word `,` between them, so
175
+ * the walk's text re-reads as the same source. A symbol, operator or name
176
+ * becomes a word, so it never resolves. Every other atom is yielded itself,
177
+ * unevaluated: a count, a string, a closure, an aggregate, or a tight
178
+ * expression, terms that abut as `o.x` does, which is evaluated before its
179
+ * neighbour applies and so is one atom that walks in turn (a16).
176
180
  *
177
181
  * Each word carries the declaration it resolves to, so `===` tells apart two
178
- * names with one spelling. A name at the head of an expression declares, and
179
- * a lookup resolves to the nearest declaration before it in its own body or in
182
+ * names with one spelling. A name at the head of a statement declares, and a
183
+ * lookup resolves to the nearest declaration before it in its own body or in
180
184
  * a body around it, which is the order a call evaluates them in. A closure's
181
185
  * locals come ahead of its argument, so that is static. A name declared
182
186
  * nowhere it can see is free: it resolves per call, argument first (spec 7),
@@ -185,23 +189,36 @@ const enclosingSites = new WeakMap();
185
189
  export function atomsOf(terms, sites = new Map()) {
186
190
  const scope = new Map(sites);
187
191
  const atoms = [];
188
- const walk = (term) => {
189
- if (term instanceof FrameBind) {
190
- for (const inner of term.asArray())
191
- walk(inner);
192
+ const heads = new Set();
193
+ const declaredName = (expr) => {
194
+ const terms = expr.asArray();
195
+ if (terms.length < 2)
196
+ return undefined;
197
+ const head = firstAtom(terms[0]);
198
+ if (!(head instanceof FrameName) || /^\.*$/.test(head.source)) {
199
+ return undefined;
192
200
  }
193
- else if (isPlainExpr(term)) {
194
- const declared = declaredName(term);
195
- for (const inner of term.asArray())
196
- walk(inner);
197
- if (declared)
198
- scope.set(declared.source, declared);
199
- if (term.is.statement)
200
- atoms.push(FrameWord.word(";"));
201
+ heads.add(head);
202
+ return head;
203
+ };
204
+ /** Records the declarations in view for a term that walks on its own. */
205
+ const noteTerms = (held) => {
206
+ if (held[0])
207
+ enclosingSites.set(held[0], scope);
208
+ atomsOf(held, scope);
209
+ };
210
+ const walkAtom = (term) => {
211
+ if (isPlainExpr(term)) {
212
+ const held = term.asArray();
213
+ if (held.length === 1) {
214
+ walkAtom(held[0]);
215
+ }
216
+ else {
217
+ noteTerms(held);
218
+ atoms.push(term);
219
+ }
201
220
  }
202
221
  else if (term instanceof FrameName) {
203
- // A name at the head of an expression is its own declaration. Anywhere
204
- // else it is a value or a member, `o .a`, which no local declares.
205
222
  atoms.push(FrameWord.address(term.source, heads.has(term) ? term : undefined));
206
223
  }
207
224
  else if (term instanceof FrameSymbol) {
@@ -209,32 +226,45 @@ export function atomsOf(terms, sites = new Map()) {
209
226
  atoms.push(FrameWord.word(spelling, scope.get(spelling)));
210
227
  }
211
228
  else {
212
- const first = isClosure(term) ? term.asArray()[0] : undefined;
213
- if (first)
214
- enclosingSites.set(first, scope);
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
+ }
238
+ }
215
239
  atoms.push(term);
216
240
  }
217
241
  };
218
- const heads = new Set();
219
- const declaredName = (expr) => {
220
- const terms = expr.asArray();
221
- if (terms.length < 2)
222
- return undefined;
223
- const head = firstAtom(terms[0]);
224
- if (!(head instanceof FrameName) || /^\.*$/.test(head.source)) {
225
- return undefined;
242
+ let previous;
243
+ for (const term of terms) {
244
+ if (isPlainExpr(term) && previous && isPlainExpr(previous) &&
245
+ previous.is.statement !== true) {
246
+ atoms.push(FrameWord.word(","));
226
247
  }
227
- heads.add(head);
228
- return head;
229
- };
230
- for (const term of terms)
231
- walk(term);
248
+ if (isPlainExpr(term)) {
249
+ const declared = declaredName(term);
250
+ for (const inner of term.asArray())
251
+ walkAtom(inner);
252
+ if (declared)
253
+ scope.set(declared.source, declared);
254
+ if (term.is.statement)
255
+ atoms.push(FrameWord.word(";"));
256
+ }
257
+ else {
258
+ walkAtom(term);
259
+ }
260
+ previous = term;
261
+ }
232
262
  return atoms;
233
263
  }
234
264
  /** The atom a term begins with, through the parser's one-term wrappers. */
235
265
  function firstAtom(term) {
236
266
  let atom = term;
237
- while ((atom instanceof FrameBind || isPlainExpr(atom)) && atom.size() === 1) {
267
+ while (isPlainExpr(atom) && atom.size() === 1) {
238
268
  atom = atom.asArray()[0];
239
269
  }
240
270
  return atom;
@@ -15,7 +15,7 @@ export class FrameGroup extends FrameList {
15
15
  * frame their declarations land on.
16
16
  */
17
17
  scoped(input, target) {
18
- const scope = EvaluationScope.from(input).withLayer(target);
18
+ const scope = EvaluationScope.from(input).withOwnLayer(target);
19
19
  return this.declares
20
20
  ? scope.withWriteTarget(target, "construction")
21
21
  : scope;
@@ -45,11 +45,33 @@ export class FrameLazy extends FrameExpr {
45
45
  return result;
46
46
  }
47
47
  toStringDataArray() {
48
- const stringify = (obj) => renderNested(obj, () => obj instanceof FrameExpr
49
- ? obj.asArray().map(stringify).join(" ")
48
+ // A body prints as it was written, so it reads back: `;` after a
49
+ // statement, `, ` between terms the source separated, and a tight
50
+ // expression, terms that abut as `o.x` does, in parentheses, since the
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(" ")})`
50
55
  : obj.toString());
51
- const parts = this.data.map(stringify);
52
- const body = parts.join(" ").trim();
56
+ const parts = [];
57
+ this.data.forEach((term, index) => {
58
+ const statement = term instanceof FrameExpr &&
59
+ Object.getPrototypeOf(term) ===
60
+ FrameExpr.prototype;
61
+ const text = statement
62
+ ? term.asArray().map(stringify).join(" ")
63
+ : stringify(term);
64
+ // Only two statements the source separated take a comma; a body built
65
+ // from bare terms, as a test may build one, is one juxtaposition.
66
+ const previous = this.data[index - 1];
67
+ const separated = index > 0 && statement &&
68
+ previous instanceof FrameExpr &&
69
+ Object.getPrototypeOf(previous) === FrameExpr.prototype &&
70
+ previous.is.statement !== true;
71
+ parts.push((separated ? ", " : index > 0 ? " " : "") + text +
72
+ (term.is.statement ? ";" : ""));
73
+ });
74
+ const body = parts.join("").trim();
53
75
  const display = body.length > 0 ? ` ${body} ` : body;
54
76
  return [display + ","];
55
77
  }
@@ -30,6 +30,9 @@ export function closureFromAtoms(atoms) {
30
30
  sequence = true;
31
31
  close(true);
32
32
  }
33
+ else if (atom instanceof FrameWord && atom.spelling() === ",") {
34
+ close(false);
35
+ }
33
36
  else {
34
37
  terms.push(atomOf(atom));
35
38
  }
@@ -1 +1 @@
1
- export const VERSION = "0.26.2";
1
+ export const VERSION = "0.27.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.26.2",
3
+ "version": "0.27.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",