hclang 0.27.0 → 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.
@@ -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
  }
@@ -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));
@@ -232,6 +235,11 @@ export function atomsOf(terms, sites = new Map()) {
232
235
  enclosingSites.set(first, scope);
233
236
  }
234
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);
235
243
  for (const element of term.asArray()) {
236
244
  noteTerms(isPlainExpr(element) ? element.asArray() : [element]);
237
245
  }
@@ -280,6 +288,8 @@ export function noteEnclosing(inner, outer) {
280
288
  if (first && !enclosingSites.has(first))
281
289
  outer.elements();
282
290
  }
291
+ /** A source aggregate walks as a body does. */
292
+ FrameList.walkSource = (terms) => atomsOf(terms, sitesAround(terms));
283
293
  /** The declarations around a closure body, as far as a walk has seen them. */
284
294
  export function sitesAround(body) {
285
295
  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;
@@ -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.
@@ -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
  });
@@ -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
@@ -1 +1 @@
1
- export const VERSION = "0.27.0";
1
+ export const VERSION = "0.28.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",