hclang 0.24.3 → 0.26.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.
@@ -3,6 +3,7 @@ import { FrameList } from "./frame-list.js";
3
3
  import { FrameName } from "./frame-name.js";
4
4
  import { FrameSchema } from "./frame-schema.js";
5
5
  import { FrameSymbol } from "./frame-symbol.js";
6
+ import { FrameWord } from "./frame-word.js";
6
7
  import { EvaluationScope } from "./evaluation-scope.js";
7
8
  import { renderNested } from "./stringify.js";
8
9
  /**
@@ -59,6 +60,18 @@ export class FrameExpr extends FrameList {
59
60
  call(argument, parameter = Frame.nil) {
60
61
  return this.in(EvaluationScope.call(argument, parameter));
61
62
  }
63
+ /** A parsed expression walks as its atoms (#429). */
64
+ elements() {
65
+ return atomsOf(this.data, sitesAround(this.data));
66
+ }
67
+ /** A parsed expression of one atom is that atom; anything else is itself. */
68
+ walked() {
69
+ if (!isPlainExpr(this) && !(this instanceof FrameBind)) {
70
+ return this;
71
+ }
72
+ const atoms = atomsOf([this]);
73
+ return atoms.length === 1 ? atoms[0] : this;
74
+ }
62
75
  /**
63
76
  * An expression or a closure is called, not collected, so its terms are code
64
77
  * rather than elements to concatenate: repetition applies it, as before.
@@ -132,6 +145,115 @@ export class FrameExpr extends FrameList {
132
145
  }
133
146
  export class FrameBind extends FrameExpr {
134
147
  }
148
+ /** A parsed expression itself, not a closure or a bind. */
149
+ const isPlainExpr = (term) => term instanceof FrameExpr &&
150
+ Object.getPrototypeOf(term) === FrameExpr.prototype;
151
+ /**
152
+ * A closure literal. `FrameLazy` is the one other subclass of `FrameExpr`, and
153
+ * it cannot be imported here, because it extends this module's class.
154
+ */
155
+ const isClosure = (term) => term instanceof FrameExpr && !isPlainExpr(term) &&
156
+ !(term instanceof FrameBind);
157
+ /**
158
+ * The declarations visible to each closure body a walk has passed through.
159
+ *
160
+ * A nested closure walks lazily, when a program walks the atom it is, so the
161
+ * declarations around it are recorded as its enclosing body walks past it.
162
+ * The key is the body's first term: a bound copy holds a fresh body array but
163
+ * the same terms, and a body with no terms has no names to resolve.
164
+ */
165
+ const enclosingSites = new WeakMap();
166
+ /**
167
+ * The atoms of parsed terms, in source order, as data (#429).
168
+ *
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.
176
+ *
177
+ * 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
180
+ * a body around it, which is the order a call evaluates them in. A closure's
181
+ * locals come ahead of its argument, so that is static. A name declared
182
+ * nowhere it can see is free: it resolves per call, argument first (spec 7),
183
+ * so its word carries no declaration.
184
+ */
185
+ export function atomsOf(terms, sites = new Map()) {
186
+ const scope = new Map(sites);
187
+ const atoms = [];
188
+ const walk = (term) => {
189
+ if (term instanceof FrameBind) {
190
+ for (const inner of term.asArray())
191
+ walk(inner);
192
+ }
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
+ }
202
+ 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
+ atoms.push(FrameWord.address(term.source, heads.has(term) ? term : undefined));
206
+ }
207
+ else if (term instanceof FrameSymbol) {
208
+ const spelling = term.spelling();
209
+ atoms.push(FrameWord.word(spelling, scope.get(spelling)));
210
+ }
211
+ else {
212
+ const first = isClosure(term) ? term.asArray()[0] : undefined;
213
+ if (first)
214
+ enclosingSites.set(first, scope);
215
+ atoms.push(term);
216
+ }
217
+ };
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;
226
+ }
227
+ heads.add(head);
228
+ return head;
229
+ };
230
+ for (const term of terms)
231
+ walk(term);
232
+ return atoms;
233
+ }
234
+ /** The atom a term begins with, through the parser's one-term wrappers. */
235
+ function firstAtom(term) {
236
+ let atom = term;
237
+ while ((atom instanceof FrameBind || isPlainExpr(atom)) && atom.size() === 1) {
238
+ atom = atom.asArray()[0];
239
+ }
240
+ return atom;
241
+ }
242
+ /**
243
+ * Records the declarations around `inner`, a closure literal being evaluated
244
+ * while `outer` is called. Walking `outer` records them for every closure it
245
+ * holds, so a walk of `inner` sees the same names whether or not anything
246
+ * walked `outer` first. It runs once per body.
247
+ */
248
+ export function noteEnclosing(inner, outer) {
249
+ const first = inner.asArray()[0];
250
+ if (first && !enclosingSites.has(first))
251
+ outer.elements();
252
+ }
253
+ /** The declarations around a closure body, as far as a walk has seen them. */
254
+ export function sitesAround(body) {
255
+ return (body[0] && enclosingSites.get(body[0])) ?? new Map();
256
+ }
135
257
  function unwrapSingleton(frame) {
136
258
  let result = frame;
137
259
  while (result instanceof FrameList && !(result instanceof FrameSchema) &&
@@ -1,5 +1,5 @@
1
1
  import { Frame } from "./frame.js";
2
- import { FrameExpr } from "./frame-expr.js";
2
+ import { FrameExpr, noteEnclosing } 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";
@@ -58,6 +58,8 @@ export class FrameLazy extends FrameExpr {
58
58
  if (capturedScopes.has(this))
59
59
  return this;
60
60
  const scope = EvaluationScope.from(input);
61
+ if (scope.closure instanceof FrameLazy)
62
+ noteEnclosing(this, scope.closure);
61
63
  const bound = this.copy();
62
64
  bound.up = scope.lexicalTarget;
63
65
  capturedScopes.set(bound, scope);
@@ -75,8 +77,29 @@ export class FrameLazy extends FrameExpr {
75
77
  ? receiverState
76
78
  : undefined;
77
79
  }
80
+ /**
81
+ * A closure walks as the atoms of its body (#429), so `.#` counts them and
82
+ * `.##` the atoms below them, as for any aggregate (#411). The operator
83
+ * reaches a closure as its argument, where a body would otherwise run, so
84
+ * it is answered here before the body is.
85
+ */
86
+ sizeOperator(argument) {
87
+ if (argument instanceof FrameSymbol && argument.is.address === true &&
88
+ (argument.spelling() === Frame.kCOUNT ||
89
+ argument.spelling() === Frame.kLEAVES)) {
90
+ return this.sizeFor(argument.spelling());
91
+ }
92
+ return undefined;
93
+ }
94
+ /** A closure is one atom of the body that holds it, never unwrapped. */
95
+ walked() {
96
+ return this;
97
+ }
78
98
  /** Evaluates this closure with optional bound receiver capability. */
79
99
  call(argument, parameter = Frame.nil, receiverState) {
100
+ const size = this.sizeOperator(argument);
101
+ if (size)
102
+ return size;
80
103
  if (this.data.length === 0) {
81
104
  // Codify the value, not the caller's captured evaluation context.
82
105
  const codified = new FrameExpr(argument.asArray(), argument.meta_copy());
@@ -105,7 +105,7 @@ export class FrameList extends Frame {
105
105
  * iterate.
106
106
  */
107
107
  elements() {
108
- return this.data.filter((item) => this.isElement(item));
108
+ return this.data.filter((item) => this.isElement(item)).map((item) => item.walked());
109
109
  }
110
110
  /** Whether this term is one of the elements, as `elements()` reads them. */
111
111
  isElement(term) {
@@ -1,5 +1,7 @@
1
1
  import { Frame } from "./frame.js";
2
2
  import { FrameAtom } from "./frame-atom.js";
3
+ import { hasCharacterContent } from "./frame-text.js";
4
+ import { FrameWord } from "./frame-word.js";
3
5
  import { FrameNote } from "./frame-note.js";
4
6
  import { FrameHandle } from "./frame-handle.js";
5
7
  import { FrameArray } from "./frame-array.js";
@@ -277,8 +279,43 @@ export class FrameSymbol extends FrameAtom {
277
279
  (this.data === Frame.kCOUNT || this.data === Frame.kLEAVES)) {
278
280
  return context.referent().sizeFor(this.data);
279
281
  }
282
+ // An address applied by a receiver reads that member, `o .x`. Two
283
+ // receivers take the name itself instead (#429). A pending declaration
284
+ // stores it, so `.k .x` keeps the key and `o k` reads `o.x` later; the
285
+ // stored name is a fresh address, so it carries no write authority over
286
+ // the scope `.x` was written in. Text with no such member, which is all
287
+ // text but a document's `.body`, spells it: `“” .x` is `“.x”`.
288
+ if (this.is.address === true && FrameSymbol.isIdentifier(this.data)) {
289
+ if (context instanceof FrameSymbol && context.isPendingDeclaration()) {
290
+ return context.apply(FrameSymbol.address(this.data), Frame.nil);
291
+ }
292
+ if (hasCharacterContent(context) && context.get(this.data).is.missing) {
293
+ return context.apply(this, Frame.nil);
294
+ }
295
+ }
280
296
  return this.in([context]);
281
297
  }
298
+ /**
299
+ * An address is a name, and equals a walked word with its spelling (#429).
300
+ * Anything else compares as it prints, as for every frame.
301
+ */
302
+ equals(other) {
303
+ if (this.is.address === true) {
304
+ const right = other.referent();
305
+ if (right instanceof FrameWord) {
306
+ return right.spelling() === this.data ? Frame.all : Frame.nil;
307
+ }
308
+ }
309
+ return super.equals(other);
310
+ }
311
+ /** Whether this is a declaration still waiting for its value, `.k`. */
312
+ isPendingDeclaration() {
313
+ return this.is.address === true && !this.get_here(Frame.kOUT).is.missing;
314
+ }
315
+ /** Whether a spelling is an ordinary name rather than an operator or key. */
316
+ static isIdentifier(spelling) {
317
+ return /^[a-zA-Z][-\w]*$/.test(spelling);
318
+ }
282
319
  string_start() {
283
320
  return FrameSymbol.SYMBOL_BEGIN.toString();
284
321
  }
@@ -0,0 +1,96 @@
1
+ import { Frame } from "./frame.js";
2
+ import { FrameAtom } from "./frame-atom.js";
3
+ import { NilContext } from "./context.js";
4
+ /**
5
+ * A word: an atom of source held as data, which never resolves (#429).
6
+ *
7
+ * Walking a closure with `&` yields its body as syntax. A symbol in that body
8
+ * cannot be yielded as the symbol itself, because a symbol in value position
9
+ * is a pending lookup: the moment it is indexed, compared, or collected into
10
+ * an aggregate it is looked up again, in whatever scope it happens to be in.
11
+ * A word is the name instead. It prints as it was written, `a` for a lookup
12
+ * and `.a` for a declaration, so the walk's text re-reads as the same source.
13
+ *
14
+ * A word is a name, so `=` compares the name: a lookup `x`, a declaration
15
+ * `.x` and the written name `.x` are all equal. `===` also compares which
16
+ * declaration the name resolves to, so in `{.a 1; {.a 2; a}}` the inner `a`
17
+ * is identical to the inner `.a` and not to the outer one. A free name, one
18
+ * declared nowhere its body can see, resolves per call, so two free words are
19
+ * identical when they are spelled alike.
20
+ *
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`.
26
+ */
27
+ export class FrameWord extends FrameAtom {
28
+ constructor(data, dotted, site, meta = NilContext) {
29
+ super(meta);
30
+ Object.defineProperty(this, "data", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: data
35
+ });
36
+ Object.defineProperty(this, "dotted", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: dotted
41
+ });
42
+ Object.defineProperty(this, "site", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: site
47
+ });
48
+ }
49
+ /** The word for a lookup, `a`, or an operator, `+`. */
50
+ static word(spelling, site) {
51
+ return new FrameWord(spelling, false, site);
52
+ }
53
+ /** The word for a declaration or address, `.a`. */
54
+ static address(spelling, site) {
55
+ return new FrameWord(spelling, true, site);
56
+ }
57
+ /** The spelling without its dot. */
58
+ spelling() {
59
+ return this.data;
60
+ }
61
+ /** Whether this word was written with a dot, as a declaration or address. */
62
+ isAddress() {
63
+ return this.dotted;
64
+ }
65
+ string_prefix() {
66
+ return this.dotted ? "." : "";
67
+ }
68
+ toData() {
69
+ return this.data;
70
+ }
71
+ /** A word equals any name with its spelling: a word, or an address `.x`. */
72
+ equals(other) {
73
+ return nameOf(other.referent()) === this.data ? Frame.all : Frame.nil;
74
+ }
75
+ /** Two words are one name when they resolve to one declaration. */
76
+ identical(other) {
77
+ const right = other.referent();
78
+ return right instanceof FrameWord && right.data === this.data &&
79
+ right.site === this.site
80
+ ? Frame.all
81
+ : Frame.nil;
82
+ }
83
+ }
84
+ /**
85
+ * The name a frame is, if it is one: a word, or an address symbol, which
86
+ * prints with its dot. Structural, because the symbol module cannot be
87
+ * imported here without closing a cycle through its own base classes.
88
+ */
89
+ export const nameOf = (frame) => {
90
+ if (frame instanceof FrameWord)
91
+ return frame.spelling();
92
+ const named = frame;
93
+ return frame.is.address === true && typeof named.spelling === "function"
94
+ ? named.spelling()
95
+ : undefined;
96
+ };
@@ -210,6 +210,14 @@ export class Frame extends MetaFrame {
210
210
  referent() {
211
211
  return this;
212
212
  }
213
+ /**
214
+ * What a walk over an aggregate yields for this element (#429). A value is
215
+ * itself. A parsed term is syntax, and answers the atom it wraps, so the
216
+ * parser's trivial wrappers never reach a program.
217
+ */
218
+ walked() {
219
+ return this;
220
+ }
213
221
  /**
214
222
  * What this frame answers for a size operator, `.#` or `.##` (#411). Only an
215
223
  * aggregate counts and a count is its own size, so anything else is nil.
@@ -0,0 +1,57 @@
1
+ import { FrameArg } from "./frame-arg.js";
2
+ import { FrameExpr } from "./frame-expr.js";
3
+ import { FrameLazy } from "./frame-lazy.js";
4
+ import { FrameName } from "./frame-name.js";
5
+ import { FrameOperator, FrameSymbol } from "./frame-symbol.js";
6
+ import { FrameWord } from "./frame-word.js";
7
+ /**
8
+ * The closure a walk re-reads as (#429): the inverse of `FrameLazy.elements`.
9
+ *
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.
15
+ */
16
+ export function closureFromAtoms(atoms) {
17
+ const statements = [];
18
+ let terms = [];
19
+ let sequence = false;
20
+ const close = (statement) => {
21
+ if (terms.length === 0 && !statement)
22
+ return;
23
+ const expr = new FrameExpr(terms);
24
+ expr.is.statement = statement;
25
+ statements.push(expr);
26
+ terms = [];
27
+ };
28
+ for (const atom of atoms) {
29
+ if (atom instanceof FrameWord && atom.spelling() === ";") {
30
+ sequence = true;
31
+ close(true);
32
+ }
33
+ else {
34
+ terms.push(atomOf(atom));
35
+ }
36
+ }
37
+ close(false);
38
+ const body = statements.length === 1 && !sequence
39
+ ? statements[0].asArray()
40
+ : statements;
41
+ const closure = new FrameLazy(body);
42
+ if (sequence)
43
+ closure.is.sequence = true;
44
+ return closure;
45
+ }
46
+ function atomOf(atom) {
47
+ if (!(atom instanceof FrameWord))
48
+ return atom;
49
+ const spelling = atom.spelling();
50
+ if (atom.isAddress())
51
+ return new FrameName(spelling);
52
+ if (/^_+$/.test(spelling))
53
+ return FrameArg.level(spelling.length);
54
+ return FrameOperator.Accepts(spelling[0])
55
+ ? new FrameOperator(spelling)
56
+ : FrameSymbol.for(spelling);
57
+ }
package/esm/lib/frames.js CHANGED
@@ -20,6 +20,7 @@ export { FrameLazy } from "./frames/frame-lazy.js";
20
20
  export { isFrameMatcher, } from "./frames/frame-match.js";
21
21
  export { FrameList } from "./frames/frame-list.js";
22
22
  export { FrameName } from "./frames/frame-name.js";
23
+ export { FrameWord } from "./frames/frame-word.js";
23
24
  export { FrameNote } from "./frames/frame-note.js";
24
25
  export { FrameScopeAnchor, } from "./frames/frame-scope-anchor.js";
25
26
  export { FrameNumeric, NUMERIC_RANK, } from "./frames/frame-numeric.js";
@@ -2,6 +2,10 @@
2
2
  import { Frame } from "../frames/frame.js";
3
3
  import { FrameArray } from "../frames/frame-array.js";
4
4
  import { FrameSymbol } from "../frames/frame-symbol.js";
5
+ import { FrameLazy } from "../frames/frame-lazy.js";
6
+ import { FrameList } from "../frames/frame-list.js";
7
+ import { FrameWord } from "../frames/frame-word.js";
8
+ import { closureFromAtoms } from "../frames/reread.js";
5
9
  /**
6
10
  * Iteration is application, twice.
7
11
  *
@@ -51,8 +55,32 @@ export const SpliceElements = (source, receiver, receiverState) => new FrameArra
51
55
  const answer = receiver.collectingCopy().call(value, Frame.nil, receiverState);
52
56
  return answer.is.void ? [] : answer.elements();
53
57
  }));
54
- /** `|` threads each element through the receiver the last step answered. */
55
- export const ReduceElements = (source, receiver, receiverState) => source.reduce(receiver, receiverState);
58
+ /**
59
+ * `|` threads each element through the receiver the last step answered.
60
+ *
61
+ * Folding code runs it (#429). A fold applies each element to the answer so
62
+ * far, which is what the evaluator does with terms, except that the evaluator
63
+ * keeps the scope apart from the running answer: folding atoms one at a time
64
+ * would look the second name up in the first answer. So a closure is called
65
+ * with the receiver, and a stream holding a walked word is re-read as the
66
+ * closure it spells and called the same way: `{x + 1} | (.x 5)` is `6`, and
67
+ * `{…} | (args)` is a second spelling of `{…} (args)`.
68
+ */
69
+ export const ReduceElements = (source, receiver, receiverState) => {
70
+ const code = source.referent();
71
+ if (code instanceof FrameLazy) {
72
+ return code.call(receiver, Frame.nil, receiverState);
73
+ }
74
+ // Only an aggregate is inspected: a stream's elements are produced by
75
+ // reading it, and the read belongs to the fold.
76
+ if (code instanceof FrameList) {
77
+ const values = code.elements();
78
+ if (values.some((value) => value instanceof FrameWord)) {
79
+ return closureFromAtoms(values).call(receiver, Frame.nil, receiverState);
80
+ }
81
+ }
82
+ return source.reduce(receiver, receiverState);
83
+ };
56
84
  /** `&&` maps the same stream widened to `[key, value]` tuples, as `&` does. */
57
85
  export const MapMembers = (source, receiver, receiverState) => new FrameArray(members(source).map((member) => receiver.collectingCopy().call(member, Frame.nil, receiverState)));
58
86
  /** `||` reduces that widened stream, threading exactly as `|` does. */
@@ -1 +1 @@
1
- export const VERSION = "0.24.3";
1
+ export const VERSION = "0.26.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.24.3",
3
+ "version": "0.26.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",