hclang 0.25.0 → 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.
@@ -62,7 +62,7 @@ export class FrameExpr extends FrameList {
62
62
  }
63
63
  /** A parsed expression walks as its atoms (#429). */
64
64
  elements() {
65
- return atomsOf(this.data);
65
+ return atomsOf(this.data, sitesAround(this.data));
66
66
  }
67
67
  /** A parsed expression of one atom is that atom; anything else is itself. */
68
68
  walked() {
@@ -148,6 +148,21 @@ export class FrameBind extends FrameExpr {
148
148
  /** A parsed expression itself, not a closure or a bind. */
149
149
  const isPlainExpr = (term) => term instanceof FrameExpr &&
150
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();
151
166
  /**
152
167
  * The atoms of parsed terms, in source order, as data (#429).
153
168
  *
@@ -158,30 +173,87 @@ const isPlainExpr = (term) => term instanceof FrameExpr &&
158
173
  * a `,` between terms is not kept. Any other atom, a count, a string, a
159
174
  * closure or an aggregate written in the body, is yielded itself, unevaluated,
160
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.
161
184
  */
162
- export function atomsOf(terms) {
185
+ export function atomsOf(terms, sites = new Map()) {
186
+ const scope = new Map(sites);
163
187
  const atoms = [];
164
- for (const term of terms) {
188
+ const walk = (term) => {
165
189
  if (term instanceof FrameBind) {
166
- atoms.push(...atomsOf(term.asArray()));
190
+ for (const inner of term.asArray())
191
+ walk(inner);
167
192
  }
168
193
  else if (isPlainExpr(term)) {
169
- atoms.push(...atomsOf(term.asArray()));
194
+ const declared = declaredName(term);
195
+ for (const inner of term.asArray())
196
+ walk(inner);
197
+ if (declared)
198
+ scope.set(declared.source, declared);
170
199
  if (term.is.statement)
171
200
  atoms.push(FrameWord.word(";"));
172
201
  }
173
202
  else if (term instanceof FrameName) {
174
- atoms.push(FrameWord.address(term.source));
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));
175
206
  }
176
207
  else if (term instanceof FrameSymbol) {
177
- atoms.push(FrameWord.word(term.spelling()));
208
+ const spelling = term.spelling();
209
+ atoms.push(FrameWord.word(spelling, scope.get(spelling)));
178
210
  }
179
211
  else {
212
+ const first = isClosure(term) ? term.asArray()[0] : undefined;
213
+ if (first)
214
+ enclosingSites.set(first, scope);
180
215
  atoms.push(term);
181
216
  }
182
- }
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);
183
232
  return atoms;
184
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
+ }
185
257
  function unwrapSingleton(frame) {
186
258
  let result = frame;
187
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);
@@ -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
  }
@@ -1,3 +1,4 @@
1
+ import { Frame } from "./frame.js";
1
2
  import { FrameAtom } from "./frame-atom.js";
2
3
  import { NilContext } from "./context.js";
3
4
  /**
@@ -8,17 +9,23 @@ import { NilContext } from "./context.js";
8
9
  * is a pending lookup: the moment it is indexed, compared, or collected into
9
10
  * an aggregate it is looked up again, in whatever scope it happens to be in.
10
11
  * A word is the name instead. It prints as it was written, `a` for a lookup
11
- * and `.a` for a declaration, so the walk's text re-reads as the same source,
12
- * and `=` compares that spelling.
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.
13
20
  *
14
21
  * A word answers nothing about the value its spelling would name, and it is
15
22
  * not an operator, so a walked `+` is an element like any other. The one
16
23
  * place words turn back into code is a fold: `|` over a stream that holds a
17
- * word re-reads the stream as a closure, `FrameLazy.fromAtoms`, and calls it
18
- * with the receiver, so `{x + 1} | (.x 5)` is `6`.
24
+ * word re-reads the stream as a closure, `closureFromAtoms`, and calls it with
25
+ * the receiver, so `{x + 1} | (.x 5)` is `6`.
19
26
  */
20
27
  export class FrameWord extends FrameAtom {
21
- constructor(data, dotted, meta = NilContext) {
28
+ constructor(data, dotted, site, meta = NilContext) {
22
29
  super(meta);
23
30
  Object.defineProperty(this, "data", {
24
31
  enumerable: true,
@@ -32,14 +39,20 @@ export class FrameWord extends FrameAtom {
32
39
  writable: true,
33
40
  value: dotted
34
41
  });
42
+ Object.defineProperty(this, "site", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: site
47
+ });
35
48
  }
36
49
  /** The word for a lookup, `a`, or an operator, `+`. */
37
- static word(spelling) {
38
- return new FrameWord(spelling, false);
50
+ static word(spelling, site) {
51
+ return new FrameWord(spelling, false, site);
39
52
  }
40
53
  /** The word for a declaration or address, `.a`. */
41
- static address(spelling) {
42
- return new FrameWord(spelling, true);
54
+ static address(spelling, site) {
55
+ return new FrameWord(spelling, true, site);
43
56
  }
44
57
  /** The spelling without its dot. */
45
58
  spelling() {
@@ -55,4 +68,29 @@ export class FrameWord extends FrameAtom {
55
68
  toData() {
56
69
  return this.data;
57
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
+ }
58
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
+ };
@@ -1 +1 @@
1
- export const VERSION = "0.25.0";
1
+ export const VERSION = "0.26.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",