hclang 0.29.1 → 0.30.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.
@@ -1,5 +1,7 @@
1
- import { Frame, FrameExpr, FrameSymbol, hasCharacterContent, } from "../frames.js";
1
+ import { Frame, FrameExpr, FrameGroup, FrameLazy, FrameResource, FrameSymbol, hasCharacterContent, } from "../frames.js";
2
2
  import { EvalPipe, makeSourcePipe } from "./eval-pipe.js";
3
+ import { LexPipe } from "./lex-pipe.js";
4
+ import { ParsePipe } from "./parse-pipe.js";
3
5
  import { sigilizer } from "./sigilizer.js";
4
6
  /** Private output state for one imported source evaluation. */
5
7
  class ImportProgress extends Frame {
@@ -164,3 +166,90 @@ function evaluateResourceSourceWithChain(resource, scope, activeImports) {
164
166
  return finished;
165
167
  return progress.firstFailure ?? progress.lastResult;
166
168
  }
169
+ /** Holds what the parser emits for a quoted source, evaluating nothing. */
170
+ class QuoteSink extends Frame {
171
+ constructor() {
172
+ super(...arguments);
173
+ Object.defineProperty(this, "parsed", {
174
+ enumerable: true,
175
+ configurable: true,
176
+ writable: true,
177
+ value: []
178
+ });
179
+ }
180
+ call(value) {
181
+ // The root pipe passes its terminal, nil, after the parsed form.
182
+ if (!value.is.immediate && value !== Frame.nil)
183
+ this.parsed.push(value);
184
+ return this;
185
+ }
186
+ }
187
+ /**
188
+ * A resource read as an unevaluated closure (#443): the import, stopped
189
+ * before evaluation.
190
+ *
191
+ * The characters pass through the import's own front end, so a testdoc's
192
+ * example lines are dropped and `<-` becomes an inert import, between `{` and
193
+ * `}`. A file's lines are therefore the comma terms of one body, as its lines
194
+ * are one comma expression at top level, and calling the closure answers what
195
+ * running the file answers. Nothing in the file runs until it is called. The
196
+ * closure is bound at `site`, where the read was written, as any closure
197
+ * written there would be.
198
+ */
199
+ export function quoteResourceSource(resource, site) {
200
+ if (!(resource instanceof FrameResource))
201
+ return sourceTypeFailure(resource);
202
+ const sink = new QuoteSink();
203
+ const lexer = new LexPipe(new ParsePipe(sink, FrameGroup, {
204
+ importSource: evaluateResourceSource,
205
+ strictStructure: true,
206
+ }));
207
+ let receiver = lexer;
208
+ const scan = (character) => {
209
+ receiver = sigilizer.scan(receiver, FrameSymbol.for(character));
210
+ return receiver.isFailedResult() ? receiver : undefined;
211
+ };
212
+ const examples = new ExampleFilter();
213
+ const opened = scan("{");
214
+ if (opened)
215
+ return opened;
216
+ for (const element of resource.elements()) {
217
+ if (element.isFailedResult())
218
+ return element;
219
+ if (!hasCharacterContent(element))
220
+ return sourceTypeFailure(element);
221
+ for (const character of element.characterContent().split("")) {
222
+ const prose = receiver.is.document === true;
223
+ for (const kept of examples.filter(character, prose)) {
224
+ const failure = scan(kept);
225
+ if (failure)
226
+ return failure;
227
+ }
228
+ }
229
+ }
230
+ for (const kept of [...examples.flush(), "\n", "}"]) {
231
+ const failure = scan(kept);
232
+ if (failure)
233
+ return failure;
234
+ }
235
+ receiver = sigilizer.finish(receiver, FrameSymbol.end());
236
+ if (receiver.isFailedResult())
237
+ return receiver;
238
+ const finished = lexer.finish(Frame.nil);
239
+ if (finished.isFailedResult())
240
+ return finished;
241
+ const closure = quotedClosure(sink.parsed);
242
+ if (!closure) {
243
+ return Frame.error(`$!.resource-quote-syntax ${resource.toString()}`);
244
+ }
245
+ return site ? closure.bind(site) : closure;
246
+ }
247
+ /** The one closure a quoted source parses to, through its wrappers. */
248
+ function quotedClosure(parsed) {
249
+ let found = parsed.length === 1 ? parsed[0] : undefined;
250
+ while (found && !(found instanceof FrameLazy) && found.asArray().length === 1) {
251
+ found = found.asArray()[0];
252
+ }
253
+ return found instanceof FrameLazy ? found : undefined;
254
+ }
255
+ FrameLazy.quote = quoteResourceSource;
@@ -30,26 +30,40 @@ export class FrameExpr extends FrameList {
30
30
  return FrameExpr.evaluateTerms(this.data, scope);
31
31
  }
32
32
  /**
33
- * Evaluates a closure body, as a sequence when the source asked for one.
33
+ * Evaluates a closure body: `;` sequences, and `,` collects, as everywhere.
34
34
  *
35
35
  * `sequence` records that the author wrote `;` between terms, so intent comes
36
- * from the parser rather than from inspecting flags on the body's children. A
37
- * body assembled programmatically therefore stays one juxtaposed expression,
38
- * which is what the Frame API has always meant by a multi-item body.
36
+ * from the parser rather than from inspecting flags on the body's children.
37
+ * Terms the source separated with `,`, or with a newline, which is the same
38
+ * separator, form one comma expression, and a comma expression is the group
39
+ * of its terms' values, as it is at top level and in a group (#443). So
40
+ * `{3, 4} ()` answers `(3, 4)`, as `3, 4` does. The group does not declare,
41
+ * so its declarations land in the call's own frame, as top level's land in
42
+ * the file: `{.a 1, .b 2; a + b} ()` is `3`.
39
43
  *
40
- * A one-term body keeps its existing value and representation either way, so
41
- * a trailing separator alone does not unwrap a single statement.
44
+ * A body assembled programmatically from bare atoms, or from statements with
45
+ * no recorded `;`, stays one juxtaposed expression, which is what the Frame
46
+ * API has always meant by a multi-item body. A one-term body keeps its
47
+ * existing value and representation either way, so a trailing separator
48
+ * alone does not unwrap a single statement.
42
49
  */
43
50
  static evaluateBody(body, input, sequence = false) {
44
51
  const scope = EvaluationScope.from(input);
45
- if (body.length === 1 || !sequence) {
52
+ if (body.length === 1)
46
53
  return FrameExpr.evaluateTerms(body, scope);
54
+ if (!sequence) {
55
+ return isCommaExpression(body)
56
+ ? FrameExpr.commaGroup(body, scope)
57
+ : FrameExpr.evaluateTerms(body, scope);
47
58
  }
48
59
  let result = Frame.nil;
49
- for (const item of body) {
50
- result = item instanceof FrameExpr
51
- ? item.evaluate(scope)
52
- : item.in(scope);
60
+ for (const run of commaRuns(body)) {
61
+ const [item] = run;
62
+ result = run.length > 1
63
+ ? FrameExpr.commaGroup(run, scope)
64
+ : item instanceof FrameExpr
65
+ ? item.evaluate(scope)
66
+ : item.in(scope);
53
67
  // A sequence stops at the first failing statement, because later
54
68
  // statements were written to run after the earlier ones succeeded.
55
69
  if (result.isFailedResult())
@@ -278,6 +292,32 @@ export function atomsOf(terms, sites = new Map(), resume) {
278
292
  }
279
293
  return atoms;
280
294
  }
295
+ /**
296
+ * Whether a body's terms are one comma expression: two or more parsed terms
297
+ * the source separated with `,`, rather than bare atoms or statements.
298
+ */
299
+ function isCommaExpression(terms) {
300
+ return terms.length > 1 &&
301
+ terms.every((term) => term.isPlainExpression() && term.is.statement !== true);
302
+ }
303
+ /**
304
+ * A sequenced body split at its `;`s: each run is the terms of one statement,
305
+ * a comma expression when it has more than one.
306
+ */
307
+ function commaRuns(body) {
308
+ const runs = [];
309
+ let run = [];
310
+ for (const term of body) {
311
+ run.push(term);
312
+ if (term.is.statement === true) {
313
+ runs.push(run);
314
+ run = [];
315
+ }
316
+ }
317
+ if (run.length > 0)
318
+ runs.push(run);
319
+ return runs;
320
+ }
281
321
  /** The atom a term begins with, through the parser's one-term wrappers. */
282
322
  function firstAtom(term) {
283
323
  let atom = term;
@@ -1,6 +1,7 @@
1
1
  // Load the FrameList cycle from FrameLazy, where it closes, so FrameList and
2
2
  // FrameExpr exist before any subclass, whichever module is imported first.
3
3
  import "./frame-lazy.js";
4
+ import { FrameExpr } from "./frame-expr.js";
4
5
  import { Frame } from "./frame.js";
5
6
  import { FrameList } from "./frame-list.js";
6
7
  import { NilContext } from "./context.js";
@@ -73,3 +74,9 @@ Object.defineProperty(FrameGroup, "SIGIL_STARTS", {
73
74
  { key: Frame.END_EXPR, mode: "pop" },
74
75
  ]
75
76
  });
77
+ /**
78
+ * A comma expression in a closure body is a group that declares nothing of
79
+ * its own, as the parser's top-level wrapper is, so its declarations land in
80
+ * the call's frame (#443).
81
+ */
82
+ FrameExpr.commaGroup = (terms, scope) => new FrameGroup([...terms], {}).in(scope);
@@ -5,6 +5,7 @@ import { FrameSymbol } from "../frames/frame-symbol.js";
5
5
  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
+ import { FrameResource } from "../frames/frame-resource.js";
8
9
  import { FrameWord } from "../frames/frame-word.js";
9
10
  import { FrameScoped } from "../frames/frame-expr.js";
10
11
  import { closureFromAtoms } from "../frames/reread.js";
@@ -69,6 +70,12 @@ export const SpliceElements = (source, receiver, receiverState) => new FrameArra
69
70
  * `{…} | (args)` is a second spelling of `{…} (args)`.
70
71
  */
71
72
  export const ReduceElements = (source, receiver, receiverState, site) => {
73
+ // A resource folded into the empty closure is read as one: parsed, not
74
+ // evaluated, so a file can be walked before anything in it runs (#443).
75
+ if (FrameLazy.quote && receiver instanceof FrameLazy &&
76
+ receiver.size() === 0 && source.referent() instanceof FrameResource) {
77
+ return FrameLazy.quote(source.referent(), site);
78
+ }
72
79
  const code = source instanceof FrameScoped
73
80
  ? source.live()
74
81
  : source.referent();
@@ -1 +1 @@
1
- export const VERSION = "0.29.1";
1
+ export const VERSION = "0.30.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.29.1",
3
+ "version": "0.30.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",