hclang 0.24.2 → 0.25.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);
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,43 @@ 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
+ * The atoms of parsed terms, in source order, as data (#429).
153
+ *
154
+ * The parser wraps each atom of a juxtaposition as `FrameBind` holding a
155
+ * one-term `FrameExpr`, and those wrappers vanish here. A symbol, operator or
156
+ * name becomes a word, so it never resolves. A term that ended in `;` is
157
+ * followed by the word `;`, so the walk's text re-reads as the same source;
158
+ * a `,` between terms is not kept. Any other atom, a count, a string, a
159
+ * closure or an aggregate written in the body, is yielded itself, unevaluated,
160
+ * and an aggregate walks in turn by the same rule.
161
+ */
162
+ export function atomsOf(terms) {
163
+ const atoms = [];
164
+ for (const term of terms) {
165
+ if (term instanceof FrameBind) {
166
+ atoms.push(...atomsOf(term.asArray()));
167
+ }
168
+ else if (isPlainExpr(term)) {
169
+ atoms.push(...atomsOf(term.asArray()));
170
+ if (term.is.statement)
171
+ atoms.push(FrameWord.word(";"));
172
+ }
173
+ else if (term instanceof FrameName) {
174
+ atoms.push(FrameWord.address(term.source));
175
+ }
176
+ else if (term instanceof FrameSymbol) {
177
+ atoms.push(FrameWord.word(term.spelling()));
178
+ }
179
+ else {
180
+ atoms.push(term);
181
+ }
182
+ }
183
+ return atoms;
184
+ }
135
185
  function unwrapSingleton(frame) {
136
186
  let result = frame;
137
187
  while (result instanceof FrameList && !(result instanceof FrameSchema) &&
@@ -75,8 +75,29 @@ export class FrameLazy extends FrameExpr {
75
75
  ? receiverState
76
76
  : undefined;
77
77
  }
78
+ /**
79
+ * A closure walks as the atoms of its body (#429), so `.#` counts them and
80
+ * `.##` the atoms below them, as for any aggregate (#411). The operator
81
+ * reaches a closure as its argument, where a body would otherwise run, so
82
+ * it is answered here before the body is.
83
+ */
84
+ sizeOperator(argument) {
85
+ if (argument instanceof FrameSymbol && argument.is.address === true &&
86
+ (argument.spelling() === Frame.kCOUNT ||
87
+ argument.spelling() === Frame.kLEAVES)) {
88
+ return this.sizeFor(argument.spelling());
89
+ }
90
+ return undefined;
91
+ }
92
+ /** A closure is one atom of the body that holds it, never unwrapped. */
93
+ walked() {
94
+ return this;
95
+ }
78
96
  /** Evaluates this closure with optional bound receiver capability. */
79
97
  call(argument, parameter = Frame.nil, receiverState) {
98
+ const size = this.sizeOperator(argument);
99
+ if (size)
100
+ return size;
80
101
  if (this.data.length === 0) {
81
102
  // Codify the value, not the caller's captured evaluation context.
82
103
  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) {
@@ -218,7 +218,7 @@ export class FrameList extends Frame {
218
218
  */
219
219
  holdsAsProperty(term) {
220
220
  const binding = echoedBinding(this.answeredValue(term));
221
- return binding !== undefined && binding.target.deref() === this;
221
+ return binding !== undefined && binding.target === this;
222
222
  }
223
223
  size() {
224
224
  return this.data.length;
@@ -50,7 +50,7 @@ export class FrameLiteral extends FrameAtom {
50
50
  return this;
51
51
  return new FrameLiteral(this.data, {
52
52
  ...this.binding,
53
- target: new WeakRef(owner),
53
+ target: owner,
54
54
  });
55
55
  }
56
56
  }
@@ -233,7 +233,7 @@ export class FrameSymbol extends FrameAtom {
233
233
  out.set(assignmentKey, argument);
234
234
  return previous.is.missing
235
235
  ? new FrameLiteral(`.${this.data} ${argument.toString()}`, {
236
- target: new WeakRef(out),
236
+ target: out,
237
237
  key: assignmentKey,
238
238
  value: argument,
239
239
  })
@@ -264,7 +264,7 @@ export class FrameSymbol extends FrameAtom {
264
264
  out.set(assignmentKey, schema);
265
265
  out.set(`${assignmentKey}.<>`, schema);
266
266
  return new FrameLiteral(`.${this.data} ${schema.toString()}`, {
267
- target: new WeakRef(out),
267
+ target: out,
268
268
  key: assignmentKey,
269
269
  value: schema,
270
270
  });
@@ -0,0 +1,58 @@
1
+ import { FrameAtom } from "./frame-atom.js";
2
+ import { NilContext } from "./context.js";
3
+ /**
4
+ * A word: an atom of source held as data, which never resolves (#429).
5
+ *
6
+ * Walking a closure with `&` yields its body as syntax. A symbol in that body
7
+ * cannot be yielded as the symbol itself, because a symbol in value position
8
+ * is a pending lookup: the moment it is indexed, compared, or collected into
9
+ * an aggregate it is looked up again, in whatever scope it happens to be in.
10
+ * 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.
13
+ *
14
+ * A word answers nothing about the value its spelling would name, and it is
15
+ * not an operator, so a walked `+` is an element like any other. The one
16
+ * 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`.
19
+ */
20
+ export class FrameWord extends FrameAtom {
21
+ constructor(data, dotted, meta = NilContext) {
22
+ super(meta);
23
+ Object.defineProperty(this, "data", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: data
28
+ });
29
+ Object.defineProperty(this, "dotted", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: dotted
34
+ });
35
+ }
36
+ /** The word for a lookup, `a`, or an operator, `+`. */
37
+ static word(spelling) {
38
+ return new FrameWord(spelling, false);
39
+ }
40
+ /** The word for a declaration or address, `.a`. */
41
+ static address(spelling) {
42
+ return new FrameWord(spelling, true);
43
+ }
44
+ /** The spelling without its dot. */
45
+ spelling() {
46
+ return this.data;
47
+ }
48
+ /** Whether this word was written with a dot, as a declaration or address. */
49
+ isAddress() {
50
+ return this.dotted;
51
+ }
52
+ string_prefix() {
53
+ return this.dotted ? "." : "";
54
+ }
55
+ toData() {
56
+ return this.data;
57
+ }
58
+ }
@@ -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. */
@@ -18,11 +18,8 @@ export const HasType = (source, type) => {
18
18
  export const BindType = (source, body) => {
19
19
  if (source instanceof FrameLiteral && source.binding &&
20
20
  body instanceof FrameLazy) {
21
- const target = source.binding.target.deref();
22
- if (!target)
23
- return source;
24
21
  body.bindSignature(source.binding.value);
25
- target.set(source.binding.key, body);
22
+ source.binding.target.set(source.binding.key, body);
26
23
  }
27
24
  return source;
28
25
  };
@@ -1 +1 @@
1
- export const VERSION = "0.24.2";
1
+ export const VERSION = "0.25.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.24.2",
3
+ "version": "0.25.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",