hclang 0.28.0 → 0.29.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.
- package/esm/lib/frames/evaluation-scope.js +53 -1
- package/esm/lib/frames/frame-alias.js +10 -1
- package/esm/lib/frames/frame-expr.js +123 -23
- package/esm/lib/frames/frame-lazy.js +13 -1
- package/esm/lib/frames/frame-list.js +9 -2
- package/esm/lib/frames/frame-word.js +17 -7
- package/esm/lib/frames/frame.js +16 -0
- package/esm/lib/frames/reread.js +30 -20
- package/esm/lib/frames.js +1 -1
- package/esm/lib/ops/frame-curry.js +16 -1
- package/esm/lib/ops/iterators.js +12 -5
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
|
@@ -12,9 +12,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
var _LiveLookupContext_lookupFrames, _LiveLookupContext_identityHandles, _EvaluationScope_lookupFrames, _EvaluationScope_lookupMembers, _EvaluationScope_argumentLayer;
|
|
13
13
|
import { Frame } from "./frame.js";
|
|
14
14
|
import { FrameHandle } from "./frame-handle.js";
|
|
15
|
+
/**
|
|
16
|
+
* Whether `frame` is a resumed walk's view of its closure's scope, through
|
|
17
|
+
* which a binding can be read and never written (spec a17).
|
|
18
|
+
*/
|
|
19
|
+
export const isResumedLookup = (frame) => frame instanceof LiveLookupContext && frame.resumed;
|
|
15
20
|
/** Live, read-only lookup through an ordered set of existing frame identities. */
|
|
16
21
|
class LiveLookupContext extends Frame {
|
|
17
|
-
constructor(frames, continuation
|
|
22
|
+
constructor(frames, continuation,
|
|
23
|
+
/** Whether a resumed walk reads through this, rather than an import. */
|
|
24
|
+
resumed = false) {
|
|
18
25
|
super();
|
|
19
26
|
Object.defineProperty(this, "continuation", {
|
|
20
27
|
enumerable: true,
|
|
@@ -22,6 +29,12 @@ class LiveLookupContext extends Frame {
|
|
|
22
29
|
writable: true,
|
|
23
30
|
value: continuation
|
|
24
31
|
});
|
|
32
|
+
Object.defineProperty(this, "resumed", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: resumed
|
|
37
|
+
});
|
|
25
38
|
_LiveLookupContext_lookupFrames.set(this, void 0);
|
|
26
39
|
_LiveLookupContext_identityHandles.set(this, new WeakMap());
|
|
27
40
|
__classPrivateFieldSet(this, _LiveLookupContext_lookupFrames, frames.map((frame) => frame instanceof FrameHandle ? frame.withoutWriteAuthority() : frame), "f");
|
|
@@ -363,6 +376,45 @@ export class EvaluationScope {
|
|
|
363
376
|
hostNamespace: this.hostNamespace,
|
|
364
377
|
});
|
|
365
378
|
}
|
|
379
|
+
/**
|
|
380
|
+
* This scope continued through `scope` instead of past `site`, read-only.
|
|
381
|
+
*
|
|
382
|
+
* A fold resumes a walk where the fold is written (`site`), but a term the
|
|
383
|
+
* walk held resumes where its own closure was bound (`scope`), as the
|
|
384
|
+
* closure itself would have: the resumption's own layers come first, its
|
|
385
|
+
* fresh local frame and the receiver, then that scope. The receiver is kept
|
|
386
|
+
* whatever its identity, since it may also be one of the site's layers. The
|
|
387
|
+
* enclosing scope is `scope` too, so `__` and deeper levels and carets read
|
|
388
|
+
* the closure's chain, never the site's. The scope is read through a live
|
|
389
|
+
* lookup that withholds write authority, and declarations land in the
|
|
390
|
+
* resumption's own frame, so a resumed term never writes where its closure
|
|
391
|
+
* was made (spec a17).
|
|
392
|
+
*/
|
|
393
|
+
resumedIn(site, scope) {
|
|
394
|
+
// The resumed closure is not one of its own layers here: its `up` is the
|
|
395
|
+
// site, so reading through it would reach the site after all.
|
|
396
|
+
const past = new Set(site?.lookupFrames() ?? []);
|
|
397
|
+
if (this.closure)
|
|
398
|
+
past.add(this.closure);
|
|
399
|
+
const own = __classPrivateFieldGet(this, _EvaluationScope_lookupFrames, "f").filter((frame) => frame === __classPrivateFieldGet(this, _EvaluationScope_argumentLayer, "f") || !past.has(frame));
|
|
400
|
+
const live = new LiveLookupContext(scope.lookupFrames(), scope.lexicalTarget, true);
|
|
401
|
+
const layers = EvaluationScope.layers([...own, live]);
|
|
402
|
+
return new EvaluationScope({
|
|
403
|
+
argument: this.argument,
|
|
404
|
+
parameter: this.parameter,
|
|
405
|
+
receiverState: this.receiverState,
|
|
406
|
+
writeTarget: this.writeTarget,
|
|
407
|
+
writeTargetRole: this.writeTargetRole,
|
|
408
|
+
lookupFrames: layers.frames,
|
|
409
|
+
lookupMembers: layers.members,
|
|
410
|
+
argumentLayer: __classPrivateFieldGet(this, _EvaluationScope_argumentLayer, "f"),
|
|
411
|
+
enclosing: scope,
|
|
412
|
+
closure: this.closure,
|
|
413
|
+
lexicalTarget: this.lexicalTarget,
|
|
414
|
+
fileScope: this.fileScope,
|
|
415
|
+
hostNamespace: this.hostNamespace,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
366
418
|
/** Accessor whose identity grades private and protected reads. */
|
|
367
419
|
accessOrigin() {
|
|
368
420
|
return this.receiverState?.receiver ?? this.writeTarget;
|
|
@@ -5,7 +5,7 @@ import { FrameHandle } from "./frame-handle.js";
|
|
|
5
5
|
import { FrameLazy } from "./frame-lazy.js";
|
|
6
6
|
import { FrameSymbol } from "./frame-symbol.js";
|
|
7
7
|
import { NilContext } from "./context.js";
|
|
8
|
-
import { EvaluationScope } from "./evaluation-scope.js";
|
|
8
|
+
import { EvaluationScope, isResumedLookup, } from "./evaluation-scope.js";
|
|
9
9
|
import { completeAtEnd, includeOrReserve } from "./atom-syntax.js";
|
|
10
10
|
import { authorizedReceiverWriteTarget } from "./bound-method.js";
|
|
11
11
|
export class FrameAlias extends FrameAtom {
|
|
@@ -69,6 +69,9 @@ export class FrameAlias extends FrameAtom {
|
|
|
69
69
|
}
|
|
70
70
|
return FrameNote.key(key, this);
|
|
71
71
|
}
|
|
72
|
+
resolvesInScope() {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
72
75
|
string_prefix() {
|
|
73
76
|
return FrameAlias.ALIAS_BEGIN;
|
|
74
77
|
}
|
|
@@ -110,6 +113,12 @@ export class FrameAlias extends FrameAtom {
|
|
|
110
113
|
if (copyOnWrite && !copyOnWrite.has(found.out)) {
|
|
111
114
|
return Frame.error(`$!.copy-on-write-boundary .${key}`);
|
|
112
115
|
}
|
|
116
|
+
// A resumed walk reads its closure's scope and never writes it: a walk
|
|
117
|
+
// can be recombined, so a resumed write would be one the closure itself
|
|
118
|
+
// never makes (spec a17).
|
|
119
|
+
if (isResumedLookup(found.out)) {
|
|
120
|
+
return Frame.error(`$!.resumed-write @${key}`);
|
|
121
|
+
}
|
|
113
122
|
return FrameSymbol.for(found.key).setter(found.out);
|
|
114
123
|
}
|
|
115
124
|
}
|
|
@@ -95,11 +95,11 @@ export class FrameExpr extends FrameList {
|
|
|
95
95
|
// operand that follows it.
|
|
96
96
|
if (sum.isFailedResult())
|
|
97
97
|
return sum;
|
|
98
|
-
const value = item.in(scope);
|
|
98
|
+
const value = item.in(resumedScope(item, scope));
|
|
99
99
|
if (value.is.error === true)
|
|
100
100
|
return value;
|
|
101
101
|
if (index > 0 && value.is.operator === true) {
|
|
102
|
-
return value.called_by(sum, Frame.nil);
|
|
102
|
+
return value.called_by(sum, Frame.nil).atSite(scope);
|
|
103
103
|
}
|
|
104
104
|
return sum.call(value);
|
|
105
105
|
}, Frame.nil);
|
|
@@ -108,7 +108,7 @@ export class FrameExpr extends FrameList {
|
|
|
108
108
|
if (this.data.length !== 2 || !(this.data[0] instanceof FrameSymbol) ||
|
|
109
109
|
!(this.data[1] instanceof FrameName) || this.data[1].source !== "<>")
|
|
110
110
|
return undefined;
|
|
111
|
-
return this.data[0].bindingSchema(scope);
|
|
111
|
+
return this.data[0].bindingSchema(resumedScope(this.data[0], scope));
|
|
112
112
|
}
|
|
113
113
|
schemaDefinition(scope) {
|
|
114
114
|
if (!this.is.statement || this.data.length !== 2)
|
|
@@ -189,7 +189,7 @@ const enclosingSites = new WeakMap();
|
|
|
189
189
|
* nowhere it can see is free: it resolves per call, argument first (spec 7),
|
|
190
190
|
* so its word carries no declaration.
|
|
191
191
|
*/
|
|
192
|
-
export function atomsOf(terms, sites = new Map()) {
|
|
192
|
+
export function atomsOf(terms, sites = new Map(), resume) {
|
|
193
193
|
const scope = new Map(sites);
|
|
194
194
|
const atoms = [];
|
|
195
195
|
const heads = new Set();
|
|
@@ -210,6 +210,8 @@ export function atomsOf(terms, sites = new Map()) {
|
|
|
210
210
|
enclosingSites.set(held[0], scope);
|
|
211
211
|
atomsOf(held, scope);
|
|
212
212
|
};
|
|
213
|
+
/** A compound atom resumes where its closure was bound, as a word does. */
|
|
214
|
+
const scoped = (term) => resume ? new FrameScoped(term, resume) : term;
|
|
213
215
|
const walkAtom = (term) => {
|
|
214
216
|
if (isPlainExpr(term)) {
|
|
215
217
|
const held = term.asArray();
|
|
@@ -218,7 +220,7 @@ export function atomsOf(terms, sites = new Map()) {
|
|
|
218
220
|
}
|
|
219
221
|
else {
|
|
220
222
|
noteTerms(held);
|
|
221
|
-
atoms.push(term);
|
|
223
|
+
atoms.push(scoped(term));
|
|
222
224
|
}
|
|
223
225
|
}
|
|
224
226
|
else if (term instanceof FrameName) {
|
|
@@ -226,24 +228,31 @@ export function atomsOf(terms, sites = new Map()) {
|
|
|
226
228
|
}
|
|
227
229
|
else if (term instanceof FrameSymbol) {
|
|
228
230
|
const spelling = term.spelling();
|
|
229
|
-
atoms.push(FrameWord.word(spelling, scope.get(spelling)));
|
|
231
|
+
atoms.push(FrameWord.word(spelling, scope.get(spelling), resume));
|
|
230
232
|
}
|
|
231
|
-
else {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
233
|
+
else if (isClosure(term)) {
|
|
234
|
+
const first = term.asArray()[0];
|
|
235
|
+
if (first)
|
|
236
|
+
enclosingSites.set(first, scope);
|
|
237
|
+
atoms.push(scoped(term));
|
|
238
|
+
}
|
|
239
|
+
else if (resume && term.resolvesInScope()) {
|
|
240
|
+
// An alias resolves its binding where it is evaluated, so it is held
|
|
241
|
+
// with its closure's scope as a word is (spec a17).
|
|
242
|
+
atoms.push(scoped(term));
|
|
243
|
+
}
|
|
244
|
+
else if (term instanceof FrameList) {
|
|
245
|
+
// Keyed by its first element, which is how its own walk asks, and by
|
|
246
|
+
// each element's first term, which is how a walked element asks.
|
|
247
|
+
const [first] = term.asArray();
|
|
248
|
+
if (first)
|
|
249
|
+
enclosingSites.set(first, scope);
|
|
250
|
+
for (const element of term.asArray()) {
|
|
251
|
+
noteTerms(isPlainExpr(element) ? element.asArray() : [element]);
|
|
246
252
|
}
|
|
253
|
+
atoms.push(scoped(term));
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
247
256
|
atoms.push(term);
|
|
248
257
|
}
|
|
249
258
|
};
|
|
@@ -285,8 +294,99 @@ function firstAtom(term) {
|
|
|
285
294
|
*/
|
|
286
295
|
export function noteEnclosing(inner, outer) {
|
|
287
296
|
const first = inner.asArray()[0];
|
|
288
|
-
if (first && !enclosingSites.has(first))
|
|
289
|
-
|
|
297
|
+
if (first && !enclosingSites.has(first)) {
|
|
298
|
+
// Walked for the sites it records, so without the scope wrappers.
|
|
299
|
+
const body = outer.asArray();
|
|
300
|
+
atomsOf(body, sitesAround(body));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* A compound atom of a walk, a tight expression, an aggregate or a closure
|
|
305
|
+
* written in a body, held with the scope its closure was bound in (spec a17).
|
|
306
|
+
*
|
|
307
|
+
* A walk is evaluation stopped before each term's last step, so what it holds
|
|
308
|
+
* is what evaluation holds: the term, and where it would be evaluated. The term
|
|
309
|
+
* is the template's own frame, so this is transparent. It prints, compares and
|
|
310
|
+
* counts as the term, and walks as the term does with the scope passed on, so
|
|
311
|
+
* a walk of a walk keeps resuming where the first closure was bound. Applying
|
|
312
|
+
* it, or folding it, binds a closure there first.
|
|
313
|
+
*/
|
|
314
|
+
export class FrameScoped extends Frame {
|
|
315
|
+
constructor(term, scope) {
|
|
316
|
+
super();
|
|
317
|
+
Object.defineProperty(this, "term", {
|
|
318
|
+
enumerable: true,
|
|
319
|
+
configurable: true,
|
|
320
|
+
writable: true,
|
|
321
|
+
value: term
|
|
322
|
+
});
|
|
323
|
+
Object.defineProperty(this, "scope", {
|
|
324
|
+
enumerable: true,
|
|
325
|
+
configurable: true,
|
|
326
|
+
writable: true,
|
|
327
|
+
value: scope
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
/** The term, a closure bound where it would have been. */
|
|
331
|
+
live() {
|
|
332
|
+
return isClosure(this.term) ? this.term.in(this.scope) : this.term;
|
|
333
|
+
}
|
|
334
|
+
referent() {
|
|
335
|
+
return this.term.referent();
|
|
336
|
+
}
|
|
337
|
+
elements() {
|
|
338
|
+
const terms = this.term.asArray();
|
|
339
|
+
return atomsOf(terms, sitesAround(terms), this.scope);
|
|
340
|
+
}
|
|
341
|
+
asArray() {
|
|
342
|
+
return this.term.asArray();
|
|
343
|
+
}
|
|
344
|
+
walked() {
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
sizeFor(key) {
|
|
348
|
+
return this.term.sizeFor(key);
|
|
349
|
+
}
|
|
350
|
+
/** Counts as its term walks, `.#` and `.##`; anything else is applied. */
|
|
351
|
+
call(argument, parameter = Frame.nil) {
|
|
352
|
+
if (argument instanceof FrameSymbol && argument.is.address === true &&
|
|
353
|
+
(argument.spelling() === Frame.kCOUNT ||
|
|
354
|
+
argument.spelling() === Frame.kLEAVES)) {
|
|
355
|
+
return this.sizeFor(argument.spelling());
|
|
356
|
+
}
|
|
357
|
+
return this.live().call(argument, parameter);
|
|
358
|
+
}
|
|
359
|
+
equals(other) {
|
|
360
|
+
return this.term.equals(other.referent());
|
|
361
|
+
}
|
|
362
|
+
isPlainExpression() {
|
|
363
|
+
return this.term.isPlainExpression();
|
|
364
|
+
}
|
|
365
|
+
toString() {
|
|
366
|
+
return this.term.toString();
|
|
367
|
+
}
|
|
368
|
+
dataString() {
|
|
369
|
+
return this.term.dataString();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
// Keyed by a term made for one resumption, a fresh symbol or a plumbing copy,
|
|
373
|
+
// never a shared template, so two resumptions cannot see each other's scope.
|
|
374
|
+
const resumptions = new WeakMap();
|
|
375
|
+
/**
|
|
376
|
+
* Marks `term`, made for one resumption, to finish evaluating in `scope` past
|
|
377
|
+
* `site` (spec a17). The term keeps its own class, so every structural check
|
|
378
|
+
* the evaluator makes on it still holds.
|
|
379
|
+
*/
|
|
380
|
+
export function resumeAt(term, scope, site) {
|
|
381
|
+
resumptions.set(term, { scope, site });
|
|
382
|
+
return term;
|
|
383
|
+
}
|
|
384
|
+
/** The scope a body's term evaluates in: its resumption's, or the body's. */
|
|
385
|
+
function resumedScope(term, scope) {
|
|
386
|
+
const resumption = resumptions.get(term);
|
|
387
|
+
return resumption
|
|
388
|
+
? scope.resumedIn(resumption.site, resumption.scope)
|
|
389
|
+
: scope;
|
|
290
390
|
}
|
|
291
391
|
/** A source aggregate walks as a body does. */
|
|
292
392
|
FrameList.walkSource = (terms) => atomsOf(terms, sitesAround(terms));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Frame } from "./frame.js";
|
|
2
|
-
import { FrameExpr, noteEnclosing } from "./frame-expr.js";
|
|
2
|
+
import { atomsOf, FrameExpr, noteEnclosing, sitesAround, } 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";
|
|
@@ -105,6 +105,18 @@ export class FrameLazy extends FrameExpr {
|
|
|
105
105
|
}
|
|
106
106
|
return undefined;
|
|
107
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* A closure walks as the atoms of its body, each held with the scope this
|
|
110
|
+
* copy was bound in, so a fold resumes them where the closure would have
|
|
111
|
+
* run (spec a17). A template no evaluation has bound holds none.
|
|
112
|
+
*/
|
|
113
|
+
elements() {
|
|
114
|
+
return atomsOf(this.data, sitesAround(this.data), capturedScopes.get(this));
|
|
115
|
+
}
|
|
116
|
+
/** Counting needs the atoms only, so it skips the scope wrappers. */
|
|
117
|
+
countedElements() {
|
|
118
|
+
return atomsOf(this.data, sitesAround(this.data));
|
|
119
|
+
}
|
|
108
120
|
/** A closure is one atom of the body that holds it, never unwrapped. */
|
|
109
121
|
walked() {
|
|
110
122
|
return this;
|
|
@@ -124,6 +124,13 @@ export class FrameList extends Frame {
|
|
|
124
124
|
}
|
|
125
125
|
return this.data.filter((item) => this.isElement(item)).map((item) => item.walked());
|
|
126
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* The elements a size operator counts: `elements()`, unless a kind can
|
|
129
|
+
* count them more cheaply, as a closure does without its scope (a17).
|
|
130
|
+
*/
|
|
131
|
+
countedElements() {
|
|
132
|
+
return this.elements();
|
|
133
|
+
}
|
|
127
134
|
/** Whether this term is one of the elements, as `elements()` reads them. */
|
|
128
135
|
isElement(term) {
|
|
129
136
|
return term.is.statement !== true && !this.holdsAsProperty(term);
|
|
@@ -135,7 +142,7 @@ export class FrameList extends Frame {
|
|
|
135
142
|
*/
|
|
136
143
|
sizeFor(key) {
|
|
137
144
|
if (key === Frame.kCOUNT) {
|
|
138
|
-
return new FrameCount(BigInt(this.
|
|
145
|
+
return new FrameCount(BigInt(this.countedElements().length));
|
|
139
146
|
}
|
|
140
147
|
const leaves = this.leafCount(new Set());
|
|
141
148
|
return leaves === undefined
|
|
@@ -154,7 +161,7 @@ export class FrameList extends Frame {
|
|
|
154
161
|
return undefined;
|
|
155
162
|
path.add(this);
|
|
156
163
|
let leaves = 0n;
|
|
157
|
-
for (const element of this.
|
|
164
|
+
for (const element of this.countedElements()) {
|
|
158
165
|
const value = element.referent();
|
|
159
166
|
const below = value instanceof FrameList ? value.leafCount(path) : 1n;
|
|
160
167
|
if (below === undefined)
|
|
@@ -19,13 +19,17 @@ import { NilContext } from "./context.js";
|
|
|
19
19
|
* identical when they are spelled alike.
|
|
20
20
|
*
|
|
21
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.
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
22
|
+
* not an operator, so a walked `+` is an element like any other. A walk is
|
|
23
|
+
* evaluation stopped before each term's last step, so a word also carries the
|
|
24
|
+
* scope its closure was bound in. The one place words turn back into code is
|
|
25
|
+
* a fold, which resumes them: `|` over a stream that holds a word calls the
|
|
26
|
+
* body it spells with the receiver, and each word resolves after the receiver
|
|
27
|
+
* where its closure would have, so `{x + 1} | (.x 5)` is `6` (spec a17).
|
|
26
28
|
*/
|
|
27
29
|
export class FrameWord extends FrameAtom {
|
|
28
|
-
constructor(data, dotted, site,
|
|
30
|
+
constructor(data, dotted, site,
|
|
31
|
+
/** The scope the word's closure was bound in, where it resumes (a17). */
|
|
32
|
+
scope, meta = NilContext) {
|
|
29
33
|
super(meta);
|
|
30
34
|
Object.defineProperty(this, "data", {
|
|
31
35
|
enumerable: true,
|
|
@@ -45,10 +49,16 @@ export class FrameWord extends FrameAtom {
|
|
|
45
49
|
writable: true,
|
|
46
50
|
value: site
|
|
47
51
|
});
|
|
52
|
+
Object.defineProperty(this, "scope", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: scope
|
|
57
|
+
});
|
|
48
58
|
}
|
|
49
59
|
/** The word for a lookup, `a`, or an operator, `+`. */
|
|
50
|
-
static word(spelling, site) {
|
|
51
|
-
return new FrameWord(spelling, false, site);
|
|
60
|
+
static word(spelling, site, scope) {
|
|
61
|
+
return new FrameWord(spelling, false, site, scope);
|
|
52
62
|
}
|
|
53
63
|
/** The word for a declaration or address, `.a`. */
|
|
54
64
|
static address(spelling, site) {
|
package/esm/lib/frames/frame.js
CHANGED
|
@@ -219,6 +219,22 @@ export class Frame extends MetaFrame {
|
|
|
219
219
|
isPlainExpression() {
|
|
220
220
|
return false;
|
|
221
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Records the scope an operation was written in, on the fresh value an
|
|
224
|
+
* operator answered for it. Only a fold reads it, to resume a walk there
|
|
225
|
+
* (spec a17); anything else is unchanged.
|
|
226
|
+
*/
|
|
227
|
+
atSite(_site) {
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Whether evaluating this atom resolves a binding where it is evaluated, as
|
|
232
|
+
* an alias does, rather than being a value or a name a walk already holds
|
|
233
|
+
* as a word. A walk holds such an atom with its closure's scope (spec a17).
|
|
234
|
+
*/
|
|
235
|
+
resolvesInScope() {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
222
238
|
/**
|
|
223
239
|
* What a walk over an aggregate yields for this element (#429). A value is
|
|
224
240
|
* itself. A parsed term is syntax, and answers the atom it wraps, so the
|
package/esm/lib/frames/reread.js
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import { FrameArg } from "./frame-arg.js";
|
|
2
|
-
import { FrameExpr } from "./frame-expr.js";
|
|
2
|
+
import { FrameExpr, FrameScoped, resumeAt } from "./frame-expr.js";
|
|
3
3
|
import { FrameLazy } from "./frame-lazy.js";
|
|
4
4
|
import { FrameName } from "./frame-name.js";
|
|
5
5
|
import { FrameOperator, FrameSymbol } from "./frame-symbol.js";
|
|
6
6
|
import { FrameWord } from "./frame-word.js";
|
|
7
7
|
/**
|
|
8
|
-
* The closure a walk
|
|
8
|
+
* The closure a walk resumes as (#429, spec a17): the inverse of
|
|
9
|
+
* `FrameLazy.elements`, bound where the fold is written.
|
|
9
10
|
*
|
|
10
|
-
* A
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* A walk is evaluation stopped before each term's last step, so this finishes
|
|
12
|
+
* it. A word becomes the atom it spells, a lookup, an operator, a parameter or
|
|
13
|
+
* a name, and a `;` word ends a statement, so the body is a sequence when one
|
|
14
|
+
* was walked. Every other atom is kept as it is, a nested closure or aggregate
|
|
15
|
+
* included. A lookup or compound atom the walk held with its closure's scope
|
|
16
|
+
* resolves there, after the resumption's own frame and the receiver; anything
|
|
17
|
+
* else resolves at `site`, as a name written there would. A fold over a
|
|
18
|
+
* stream that holds a word calls this closure with the receiver, which is what
|
|
19
|
+
* `{…} | (args)` means.
|
|
15
20
|
*/
|
|
16
|
-
export function closureFromAtoms(atoms) {
|
|
21
|
+
export function closureFromAtoms(atoms, site) {
|
|
17
22
|
const statements = [];
|
|
18
23
|
let terms = [];
|
|
19
24
|
let sequence = false;
|
|
@@ -34,27 +39,32 @@ export function closureFromAtoms(atoms) {
|
|
|
34
39
|
close(false);
|
|
35
40
|
}
|
|
36
41
|
else {
|
|
37
|
-
terms.push(atomOf(atom));
|
|
42
|
+
terms.push(atomOf(atom, site));
|
|
38
43
|
}
|
|
39
44
|
}
|
|
40
45
|
close(false);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const closure = new FrameLazy(body);
|
|
46
|
+
// Statements stay statements, as the parser holds them, so a statement's
|
|
47
|
+
// own forms, such as `x .<>`, resume as they run.
|
|
48
|
+
const closure = new FrameLazy(statements);
|
|
45
49
|
if (sequence)
|
|
46
50
|
closure.is.sequence = true;
|
|
47
|
-
return closure;
|
|
51
|
+
return site ? closure.bind(site) : closure;
|
|
48
52
|
}
|
|
49
|
-
function atomOf(atom) {
|
|
53
|
+
function atomOf(atom, site) {
|
|
54
|
+
if (atom instanceof FrameScoped) {
|
|
55
|
+
return resumeAt(atom.term.copy(), atom.scope, site);
|
|
56
|
+
}
|
|
50
57
|
if (!(atom instanceof FrameWord))
|
|
51
58
|
return atom;
|
|
52
59
|
const spelling = atom.spelling();
|
|
53
60
|
if (atom.isAddress())
|
|
54
61
|
return new FrameName(spelling);
|
|
55
|
-
if (
|
|
56
|
-
return
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
if (FrameOperator.Accepts(spelling[0]))
|
|
63
|
+
return new FrameOperator(spelling);
|
|
64
|
+
// A fresh term, never an interned one, so the resumption it is marked for
|
|
65
|
+
// belongs to this body alone.
|
|
66
|
+
const term = /^_+$/.test(spelling)
|
|
67
|
+
? new FrameArg(spelling)
|
|
68
|
+
: new FrameSymbol(spelling);
|
|
69
|
+
return atom.scope ? resumeAt(term, atom.scope, site) : term;
|
|
60
70
|
}
|
package/esm/lib/frames.js
CHANGED
|
@@ -9,7 +9,7 @@ export { FrameBlob } from "./frames/frame-blob.js";
|
|
|
9
9
|
export { FrameBytePayload, FrameBytes } from "./frames/frame-bytes.js";
|
|
10
10
|
export { FrameComment } from "./frames/frame-comment.js";
|
|
11
11
|
export { FrameDoc } from "./frames/frame-doc.js";
|
|
12
|
-
export { FrameBind, FrameExpr } from "./frames/frame-expr.js";
|
|
12
|
+
export { FrameBind, FrameExpr, FrameScoped } from "./frames/frame-expr.js";
|
|
13
13
|
// Evaluation scope is interpreter-internal: `EvaluationInput` appears in the
|
|
14
14
|
// `Frame.in` signature, so the type is re-exported here for implementors and
|
|
15
15
|
// tests, while `WriteTargetRole` stays private to the scope module.
|
|
@@ -28,8 +28,23 @@ export class FrameCurry extends Frame {
|
|
|
28
28
|
writable: true,
|
|
29
29
|
value: void 0
|
|
30
30
|
});
|
|
31
|
+
Object.defineProperty(this, "site", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
31
37
|
this.id += "." + key;
|
|
32
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Records the scope the operator was written in. The curry is fresh for
|
|
41
|
+
* each evaluation, so this costs a field: no copy, and the scope is held
|
|
42
|
+
* only as long as the curry is. A fold alone reads it (spec a17).
|
|
43
|
+
*/
|
|
44
|
+
atSite(site) {
|
|
45
|
+
this.site = site;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
33
48
|
/** Captures method state on this fresh operation lookup for its callbacks. */
|
|
34
49
|
withReceiverState(receiverState) {
|
|
35
50
|
this.callbackReceiverState = receiverState;
|
|
@@ -44,7 +59,7 @@ export class FrameCurry extends Frame {
|
|
|
44
59
|
const callbackReceiverState = receiverState && argument instanceof FrameLazy
|
|
45
60
|
? argument.receiverStateForCallback(receiverState)
|
|
46
61
|
: undefined;
|
|
47
|
-
return this.Func(this.Source, argument, callbackReceiverState);
|
|
62
|
+
return this.Func(this.Source, argument, callbackReceiverState, this.site);
|
|
48
63
|
}
|
|
49
64
|
toString() {
|
|
50
65
|
return this.id; // `FrameCurry(${this.Source.id}, ${this.Func})`;
|
package/esm/lib/ops/iterators.js
CHANGED
|
@@ -6,6 +6,7 @@ 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
8
|
import { FrameWord } from "../frames/frame-word.js";
|
|
9
|
+
import { FrameScoped } from "../frames/frame-expr.js";
|
|
9
10
|
import { closureFromAtoms } from "../frames/reread.js";
|
|
10
11
|
/**
|
|
11
12
|
* Iteration is application, twice.
|
|
@@ -67,17 +68,23 @@ export const SpliceElements = (source, receiver, receiverState) => new FrameArra
|
|
|
67
68
|
* closure it spells and called the same way: `{x + 1} | (.x 5)` is `6`, and
|
|
68
69
|
* `{…} | (args)` is a second spelling of `{…} (args)`.
|
|
69
70
|
*/
|
|
70
|
-
export const ReduceElements = (source, receiver, receiverState) => {
|
|
71
|
-
const code = source
|
|
71
|
+
export const ReduceElements = (source, receiver, receiverState, site) => {
|
|
72
|
+
const code = source instanceof FrameScoped
|
|
73
|
+
? source.live()
|
|
74
|
+
: source.referent();
|
|
72
75
|
if (code instanceof FrameLazy || code instanceof BoundMethod) {
|
|
73
76
|
return code.call(receiver, Frame.nil, receiverState);
|
|
74
77
|
}
|
|
75
78
|
// Only an aggregate is inspected: a stream's elements are produced by
|
|
76
79
|
// reading it, and the read belongs to the fold.
|
|
77
80
|
if (code instanceof FrameList) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
// A held aggregate walks with its closure's scope, so its words resume
|
|
82
|
+
// there rather than at the site.
|
|
83
|
+
const values = (source instanceof FrameScoped ? source : code).elements();
|
|
84
|
+
// A walk is code when it holds a word or a term held with its closure's
|
|
85
|
+
// scope, so `{(…)} & {_}` resumes as `{x}` does.
|
|
86
|
+
if (values.some((value) => value instanceof FrameWord || value instanceof FrameScoped)) {
|
|
87
|
+
return closureFromAtoms(values, site).call(receiver, Frame.nil, receiverState);
|
|
81
88
|
}
|
|
82
89
|
}
|
|
83
90
|
return source.reduce(receiver, receiverState);
|
package/esm/lib/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "0.
|
|
1
|
+
export const VERSION = "0.29.0";
|