hclang 0.26.2 → 0.28.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;
@@ -126,6 +142,11 @@ export class ParsePipe extends FrameArray {
126
142
  if (this.nested && group instanceof FrameGroup) {
127
143
  group.declares = true;
128
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;
129
150
  if (this.sequenced) {
130
151
  group.is.sequence = true;
131
152
  }
@@ -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")];
@@ -60,16 +60,18 @@ 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));
66
69
  }
67
70
  /** A parsed expression of one atom is that atom; anything else is itself. */
68
71
  walked() {
69
- if (!isPlainExpr(this) && !(this instanceof FrameBind)) {
72
+ if (!isPlainExpr(this))
70
73
  return this;
71
- }
72
- const atoms = atomsOf([this]);
74
+ const atoms = atomsOf([this], sitesAround(this.data));
73
75
  return atoms.length === 1 ? atoms[0] : this;
74
76
  }
75
77
  /**
@@ -143,6 +145,11 @@ export class FrameExpr extends FrameList {
143
145
  : term;
144
146
  }
145
147
  }
148
+ /**
149
+ * The parser's marker for a bind pipe, what one whitespace started. It names
150
+ * the pipe only: a bind emits its one term bare, or several abutting terms as
151
+ * a tight `FrameExpr`, so no `FrameBind` is ever a node of the graph (a16).
152
+ */
146
153
  export class FrameBind extends FrameExpr {
147
154
  }
148
155
  /** A parsed expression itself, not a closure or a bind. */
@@ -152,8 +159,7 @@ const isPlainExpr = (term) => term instanceof FrameExpr &&
152
159
  * A closure literal. `FrameLazy` is the one other subclass of `FrameExpr`, and
153
160
  * it cannot be imported here, because it extends this module's class.
154
161
  */
155
- const isClosure = (term) => term instanceof FrameExpr && !isPlainExpr(term) &&
156
- !(term instanceof FrameBind);
162
+ const isClosure = (term) => term instanceof FrameExpr && !isPlainExpr(term);
157
163
  /**
158
164
  * The declarations visible to each closure body a walk has passed through.
159
165
  *
@@ -166,17 +172,18 @@ const enclosingSites = new WeakMap();
166
172
  /**
167
173
  * The atoms of parsed terms, in source order, as data (#429).
168
174
  *
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.
175
+ * The terms are a body's statements. Each statement's atoms are yielded in
176
+ * order; a statement that ended in `;` is followed by the word `;`, and two
177
+ * statements the source separated with `,` get the word `,` between them, so
178
+ * the walk's text re-reads as the same source. A symbol, operator or name
179
+ * becomes a word, so it never resolves. Every other atom is yielded itself,
180
+ * unevaluated: a count, a string, a closure, an aggregate, or a tight
181
+ * expression, terms that abut as `o.x` does, which is evaluated before its
182
+ * neighbour applies and so is one atom that walks in turn (a16).
176
183
  *
177
184
  * 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
185
+ * names with one spelling. A name at the head of a statement declares, and a
186
+ * lookup resolves to the nearest declaration before it in its own body or in
180
187
  * a body around it, which is the order a call evaluates them in. A closure's
181
188
  * locals come ahead of its argument, so that is static. A name declared
182
189
  * nowhere it can see is free: it resolves per call, argument first (spec 7),
@@ -185,23 +192,36 @@ const enclosingSites = new WeakMap();
185
192
  export function atomsOf(terms, sites = new Map()) {
186
193
  const scope = new Map(sites);
187
194
  const atoms = [];
188
- const walk = (term) => {
189
- if (term instanceof FrameBind) {
190
- for (const inner of term.asArray())
191
- walk(inner);
195
+ const heads = new Set();
196
+ const declaredName = (expr) => {
197
+ const terms = expr.asArray();
198
+ if (terms.length < 2)
199
+ return undefined;
200
+ const head = firstAtom(terms[0]);
201
+ if (!(head instanceof FrameName) || /^\.*$/.test(head.source)) {
202
+ return undefined;
192
203
  }
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(";"));
204
+ heads.add(head);
205
+ return head;
206
+ };
207
+ /** Records the declarations in view for a term that walks on its own. */
208
+ const noteTerms = (held) => {
209
+ if (held[0])
210
+ enclosingSites.set(held[0], scope);
211
+ atomsOf(held, scope);
212
+ };
213
+ const walkAtom = (term) => {
214
+ if (isPlainExpr(term)) {
215
+ const held = term.asArray();
216
+ if (held.length === 1) {
217
+ walkAtom(held[0]);
218
+ }
219
+ else {
220
+ noteTerms(held);
221
+ atoms.push(term);
222
+ }
201
223
  }
202
224
  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
225
  atoms.push(FrameWord.address(term.source, heads.has(term) ? term : undefined));
206
226
  }
207
227
  else if (term instanceof FrameSymbol) {
@@ -209,32 +229,50 @@ export function atomsOf(terms, sites = new Map()) {
209
229
  atoms.push(FrameWord.word(spelling, scope.get(spelling)));
210
230
  }
211
231
  else {
212
- const first = isClosure(term) ? term.asArray()[0] : undefined;
213
- if (first)
214
- enclosingSites.set(first, scope);
232
+ if (isClosure(term)) {
233
+ const first = term.asArray()[0];
234
+ if (first)
235
+ enclosingSites.set(first, scope);
236
+ }
237
+ else if (term instanceof FrameList) {
238
+ // Keyed by its first element, which is how its own walk asks, and by
239
+ // each element's first term, which is how a walked element asks.
240
+ const [first] = term.asArray();
241
+ if (first)
242
+ enclosingSites.set(first, scope);
243
+ for (const element of term.asArray()) {
244
+ noteTerms(isPlainExpr(element) ? element.asArray() : [element]);
245
+ }
246
+ }
215
247
  atoms.push(term);
216
248
  }
217
249
  };
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;
250
+ let previous;
251
+ for (const term of terms) {
252
+ if (isPlainExpr(term) && previous && isPlainExpr(previous) &&
253
+ previous.is.statement !== true) {
254
+ atoms.push(FrameWord.word(","));
226
255
  }
227
- heads.add(head);
228
- return head;
229
- };
230
- for (const term of terms)
231
- walk(term);
256
+ if (isPlainExpr(term)) {
257
+ const declared = declaredName(term);
258
+ for (const inner of term.asArray())
259
+ walkAtom(inner);
260
+ if (declared)
261
+ scope.set(declared.source, declared);
262
+ if (term.is.statement)
263
+ atoms.push(FrameWord.word(";"));
264
+ }
265
+ else {
266
+ walkAtom(term);
267
+ }
268
+ previous = term;
269
+ }
232
270
  return atoms;
233
271
  }
234
272
  /** The atom a term begins with, through the parser's one-term wrappers. */
235
273
  function firstAtom(term) {
236
274
  let atom = term;
237
- while ((atom instanceof FrameBind || isPlainExpr(atom)) && atom.size() === 1) {
275
+ while (isPlainExpr(atom) && atom.size() === 1) {
238
276
  atom = atom.asArray()[0];
239
277
  }
240
278
  return atom;
@@ -250,6 +288,8 @@ export function noteEnclosing(inner, outer) {
250
288
  if (first && !enclosingSites.has(first))
251
289
  outer.elements();
252
290
  }
291
+ /** A source aggregate walks as a body does. */
292
+ FrameList.walkSource = (terms) => atomsOf(terms, sitesAround(terms));
253
293
  /** The declarations around a closure body, as far as a walk has seen them. */
254
294
  export function sitesAround(body) {
255
295
  return (body[0] && enclosingSites.get(body[0])) ?? new Map();
@@ -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;
@@ -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;
@@ -4,7 +4,7 @@ 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.
@@ -45,11 +45,25 @@ 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(" ")
50
- : obj.toString());
51
- const parts = this.data.map(stringify);
52
- const body = parts.join(" ").trim();
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 parts = [];
53
+ this.data.forEach((term, index) => {
54
+ const statement = term.isPlainExpression();
55
+ const text = statement
56
+ ? term.asArray().map(renderSourceTerm).join(" ")
57
+ : renderSourceTerm(term);
58
+ // Only two statements the source separated take a comma; a body built
59
+ // from bare terms, as a test may build one, is one juxtaposition.
60
+ const previous = this.data[index - 1];
61
+ const separated = index > 0 && statement &&
62
+ previous.isPlainExpression() && previous.is.statement !== true;
63
+ parts.push((separated ? ", " : index > 0 ? " " : "") + text +
64
+ (term.is.statement ? ";" : ""));
65
+ });
66
+ const body = parts.join("").trim();
53
67
  const display = body.length > 0 ? ` ${body} ` : body;
54
68
  return [display + ","];
55
69
  }
@@ -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,6 +119,9 @@ 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
  }
110
127
  /** Whether this term is one of the elements, as `elements()` reads them. */
@@ -210,6 +210,15 @@ 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
+ }
213
222
  /**
214
223
  * What a walk over an aggregate yields for this element (#429). A value is
215
224
  * itself. A parsed term is syntax, and answers the atom it wraps, so the
@@ -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.28.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.26.2",
3
+ "version": "0.28.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",