hclang 0.44.0 → 0.45.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/cli/hc.js +4 -1
- package/esm/lib/bundle/hc-sources.js +864 -0
- package/esm/lib/execute/denum-prologue.js +88 -0
- package/esm/lib/execute/hc-eval.js +2 -0
- package/esm/lib/frames/access-footprint.js +665 -0
- package/esm/lib/frames/bound-method.js +38 -0
- package/esm/lib/frames/denum-hash.js +166 -0
- package/esm/lib/frames/denum.js +357 -0
- package/esm/lib/frames/dispatch-host.js +118 -0
- package/esm/lib/frames/effect-registry.js +21 -2
- package/esm/lib/frames/event-dispatch.js +192 -0
- package/esm/lib/frames/frame-array.js +42 -11
- package/esm/lib/frames/frame-expr.js +40 -9
- package/esm/lib/frames/frame-lazy.js +37 -5
- package/esm/lib/frames/frame-list.js +10 -3
- package/esm/lib/frames/frame-name.js +8 -0
- package/esm/lib/frames/frame-symbol.js +1 -1
- package/esm/lib/frames/frame.js +75 -10
- package/esm/lib/frames/reference-tracking.js +275 -0
- package/esm/lib/frames.js +8 -0
- package/esm/lib/ops/denum.js +120 -0
- package/esm/lib/ops.js +3 -0
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { guardAccess, noteWrite, UncertifiedAccess, } from "./reference-tracking.js";
|
|
1
2
|
/** Subscribers registered on one managed value, in registration order. */
|
|
2
3
|
const subscribers = new WeakMap();
|
|
3
4
|
/** Managed values whose callbacks are currently running, to bound reentrancy. */
|
|
@@ -24,19 +25,34 @@ export function installEffectInvoker(invoker) {
|
|
|
24
25
|
* answer it unchanged.
|
|
25
26
|
*/
|
|
26
27
|
export function registerEffect(managed, callback) {
|
|
28
|
+
// Refused before the subscriber list changes, so a refusal registers nothing.
|
|
29
|
+
guardAccess(managed, "write");
|
|
27
30
|
const existing = subscribers.get(managed);
|
|
28
31
|
if (!existing) {
|
|
29
32
|
subscribers.set(managed, [callback]);
|
|
33
|
+
noteWrite(managed);
|
|
30
34
|
return managed;
|
|
31
35
|
}
|
|
32
|
-
if (
|
|
33
|
-
|
|
36
|
+
if (existing.includes(callback))
|
|
37
|
+
return managed;
|
|
38
|
+
existing.push(callback);
|
|
39
|
+
noteWrite(managed);
|
|
34
40
|
return managed;
|
|
35
41
|
}
|
|
36
42
|
/** Whether a value carries any effect-boundary subscribers. */
|
|
37
43
|
export function hasEffectSubscribers(managed) {
|
|
38
44
|
return subscribers.has(managed);
|
|
39
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The callbacks a committed mutation of `managed` would invoke, in order.
|
|
48
|
+
* Read by the footprint walk (`access-footprint.ts`): a write to a managed
|
|
49
|
+
* value runs its callbacks, so their effects belong to the writer's event.
|
|
50
|
+
* The list can change only by a registration, which is a write of `managed`,
|
|
51
|
+
* so a certificate that writes `managed` cannot see it change while it runs.
|
|
52
|
+
*/
|
|
53
|
+
export function effectSubscribersOf(managed) {
|
|
54
|
+
return subscribers.get(managed) ?? [];
|
|
55
|
+
}
|
|
40
56
|
/**
|
|
41
57
|
* Whether `observer` itself is registered on `managed` by an `A -> B` arrow,
|
|
42
58
|
* so a committed mutation of `managed` invokes it. Another callback on the
|
|
@@ -76,6 +92,9 @@ export function notifyEffect(managed) {
|
|
|
76
92
|
invoke(callback, managed);
|
|
77
93
|
}
|
|
78
94
|
catch (error) {
|
|
95
|
+
// A refusal ends the event here: no sibling runs after it.
|
|
96
|
+
if (error instanceof UncertifiedAccess)
|
|
97
|
+
throw error;
|
|
79
98
|
thrown ??= { error };
|
|
80
99
|
}
|
|
81
100
|
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Frame } from "./frame.js";
|
|
2
|
+
import { FrameLazy } from "./frame-lazy.js";
|
|
3
|
+
import { BoundMethod } from "./bound-method.js";
|
|
4
|
+
import { AccessRecord, UncertifiedAccess, versionOf, withAccessRecord, } from "./reference-tracking.js";
|
|
5
|
+
import { footprintsConflict, isOpaque, isState, potentialFootprint, } from "./access-footprint.js";
|
|
6
|
+
/**
|
|
7
|
+
* The guard an admitted event runs under: its certificate, the footprint
|
|
8
|
+
* admission rested on. An opaque event was serialized against everything, so
|
|
9
|
+
* nothing it does can interfere and nothing is refused.
|
|
10
|
+
*/
|
|
11
|
+
function guardFor(certificate) {
|
|
12
|
+
if (isOpaque(certificate))
|
|
13
|
+
return () => true;
|
|
14
|
+
return (frame, access) => !isState(frame) ||
|
|
15
|
+
(access === "write" ? certificate.writes : certificate.reads).has(frame);
|
|
16
|
+
}
|
|
17
|
+
/** The inferred footprint, widened by the caller's declaration if any. */
|
|
18
|
+
function widen(inferred, declared) {
|
|
19
|
+
if (!declared)
|
|
20
|
+
return inferred;
|
|
21
|
+
return {
|
|
22
|
+
reads: new Set([...inferred.reads, ...declared.reads]),
|
|
23
|
+
writes: new Set([...inferred.writes, ...declared.writes]),
|
|
24
|
+
unresolved: [...inferred.unresolved, ...declared.unresolved],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The shared state frames `record` touched that `footprint` did not allow:
|
|
29
|
+
* writes outside its writes, or reads outside its reads. Shared means not
|
|
30
|
+
* created by the event; state means what the walk would count as state.
|
|
31
|
+
*/
|
|
32
|
+
export function accessesOutside(record, footprint, access = "writes") {
|
|
33
|
+
const touched = access === "writes"
|
|
34
|
+
? record.sharedWrites()
|
|
35
|
+
: record.sharedReads();
|
|
36
|
+
const allowed = access === "writes" ? footprint.writes : footprint.reads;
|
|
37
|
+
return touched.filter((frame) => isState(frame) && !allowed.has(frame));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Runs `events` under `policy` and reports what each did and in what order.
|
|
41
|
+
*/
|
|
42
|
+
export function dispatch(events, policy = "footprint") {
|
|
43
|
+
const trace = [];
|
|
44
|
+
const infer = (event) => potentialFootprint(event.target, event.argument ?? Frame.nil);
|
|
45
|
+
const states = events.map((event) => {
|
|
46
|
+
const inferred = infer(event);
|
|
47
|
+
return {
|
|
48
|
+
event,
|
|
49
|
+
record: new AccessRecord(),
|
|
50
|
+
waitedFor: new Set(),
|
|
51
|
+
inferred,
|
|
52
|
+
potential: widen(inferred, event.declared),
|
|
53
|
+
steps: 0,
|
|
54
|
+
done: false,
|
|
55
|
+
result: Frame.nil,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
// A waiting event's names may have been rebound by an event that finished
|
|
59
|
+
// since, so its footprint is read again from its code until it starts.
|
|
60
|
+
const refresh = (state) => {
|
|
61
|
+
if (state.generator)
|
|
62
|
+
return;
|
|
63
|
+
state.inferred = infer(state.event);
|
|
64
|
+
state.potential = widen(state.inferred, state.event.declared);
|
|
65
|
+
};
|
|
66
|
+
const serial = policy === "sequential" || policy === "certified-sequential";
|
|
67
|
+
const guarded = policy === "footprint" || policy === "certified-sequential";
|
|
68
|
+
const mayStart = (index) => {
|
|
69
|
+
if (policy === "interleave")
|
|
70
|
+
return true;
|
|
71
|
+
const state = states[index];
|
|
72
|
+
refresh(state);
|
|
73
|
+
for (let other = 0; other < states.length; other++) {
|
|
74
|
+
if (other === index)
|
|
75
|
+
continue;
|
|
76
|
+
const peer = states[other];
|
|
77
|
+
if (peer.done)
|
|
78
|
+
continue;
|
|
79
|
+
// A later event matters only once it is running.
|
|
80
|
+
if (other > index && !peer.generator)
|
|
81
|
+
continue;
|
|
82
|
+
// An earlier event that has not started holds back every later one:
|
|
83
|
+
// its footprint is read when it starts and may name what a later event
|
|
84
|
+
// would already have written.
|
|
85
|
+
const conflicts = serial || !peer.generator ||
|
|
86
|
+
footprintsConflict(state.potential, peer.potential);
|
|
87
|
+
if (conflicts) {
|
|
88
|
+
state.waitedFor.add(peer.event.label);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
};
|
|
94
|
+
let remaining = states.length;
|
|
95
|
+
while (remaining > 0) {
|
|
96
|
+
let progressed = false;
|
|
97
|
+
for (let index = 0; index < states.length; index++) {
|
|
98
|
+
const state = states[index];
|
|
99
|
+
if (state.done)
|
|
100
|
+
continue;
|
|
101
|
+
if (!state.generator) {
|
|
102
|
+
if (!mayStart(index))
|
|
103
|
+
continue;
|
|
104
|
+
state.generator = stepsOf(state.event.target, state.event.argument ?? Frame.nil);
|
|
105
|
+
// The certificate is frozen with the footprint admission rested on.
|
|
106
|
+
if (guarded)
|
|
107
|
+
state.record.guard = guardFor(state.potential);
|
|
108
|
+
}
|
|
109
|
+
const generator = state.generator;
|
|
110
|
+
progressed = true;
|
|
111
|
+
let step;
|
|
112
|
+
try {
|
|
113
|
+
step = withAccessRecord(state.record, () => generator.next());
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
// Only this event's own guard ends it; a refusal by an enclosing
|
|
117
|
+
// event's guard belongs to that event's dispatcher.
|
|
118
|
+
if (!(error instanceof UncertifiedAccess) || error.record !== state.record)
|
|
119
|
+
throw error;
|
|
120
|
+
// Refused before the access happened: the event ends here, at the
|
|
121
|
+
// statement it was in, with nothing of that access committed.
|
|
122
|
+
state.refused = { frame: error.frame, access: error.access };
|
|
123
|
+
step = {
|
|
124
|
+
done: true,
|
|
125
|
+
value: Frame.error(`$!.uncertified-${error.access}`),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (step.done) {
|
|
129
|
+
state.done = true;
|
|
130
|
+
state.result = step.value;
|
|
131
|
+
remaining--;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
state.steps++;
|
|
135
|
+
trace.push(`${state.event.label}:${state.steps}`);
|
|
136
|
+
}
|
|
137
|
+
// A serial policy runs each event to completion before looking further.
|
|
138
|
+
if (serial && !state.done)
|
|
139
|
+
index--;
|
|
140
|
+
}
|
|
141
|
+
if (!progressed) {
|
|
142
|
+
throw new Error("$!.dispatch-stalled: no event could start");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
policy,
|
|
147
|
+
trace,
|
|
148
|
+
outcomes: states.map((state) => {
|
|
149
|
+
// An opaque event claimed nothing, so nothing it did escapes a claim.
|
|
150
|
+
const opaque = isOpaque(state.potential);
|
|
151
|
+
const declared = state.event.declared;
|
|
152
|
+
return {
|
|
153
|
+
label: state.event.label,
|
|
154
|
+
result: state.result,
|
|
155
|
+
steps: state.steps,
|
|
156
|
+
inferred: state.inferred,
|
|
157
|
+
potential: state.potential,
|
|
158
|
+
actual: state.record,
|
|
159
|
+
escapes: opaque ? [] : accessesOutside(state.record, state.potential),
|
|
160
|
+
readEscapes: opaque
|
|
161
|
+
? []
|
|
162
|
+
: accessesOutside(state.record, state.potential, "reads"),
|
|
163
|
+
undeclared: declared && !isOpaque(declared)
|
|
164
|
+
? accessesOutside(state.record, declared)
|
|
165
|
+
: [],
|
|
166
|
+
waitedFor: [...state.waitedFor],
|
|
167
|
+
refused: state.refused,
|
|
168
|
+
};
|
|
169
|
+
}),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** The statement steps of calling `target`; a callable without a body is one. */
|
|
173
|
+
export function stepsOf(target, argument = Frame.nil) {
|
|
174
|
+
if (target instanceof BoundMethod)
|
|
175
|
+
return target.steps(argument);
|
|
176
|
+
if (target instanceof FrameLazy)
|
|
177
|
+
return target.steps(argument);
|
|
178
|
+
return (function* () {
|
|
179
|
+
const result = target.call(argument, Frame.nil);
|
|
180
|
+
yield result;
|
|
181
|
+
return result;
|
|
182
|
+
})();
|
|
183
|
+
}
|
|
184
|
+
/** The version of `frame` an outcome read, or undefined if it never did. */
|
|
185
|
+
export function versionRead(outcome, frame) {
|
|
186
|
+
return outcome.actual.reads.get(frame);
|
|
187
|
+
}
|
|
188
|
+
/** The version of `frame` an outcome's last write produced, if it wrote it. */
|
|
189
|
+
export function versionWritten(outcome, frame) {
|
|
190
|
+
return outcome.actual.writes.get(frame);
|
|
191
|
+
}
|
|
192
|
+
export { versionOf };
|
|
@@ -8,9 +8,22 @@ import { FrameHandle } from "./frame-handle.js";
|
|
|
8
8
|
import { FrameList } from "./frame-list.js";
|
|
9
9
|
import { FrameNote } from "./frame-note.js";
|
|
10
10
|
import { NilContext } from "./context.js";
|
|
11
|
+
import { guardAccess, noteElements, noteHolding, noteRead, noteWrite, PARENT_KEY, } from "./reference-tracking.js";
|
|
11
12
|
export class FrameArray extends FrameList {
|
|
12
13
|
constructor(data, meta = NilContext) {
|
|
13
14
|
super(data, meta);
|
|
15
|
+
// An aggregate holds its elements from birth (reference-tracking.ts).
|
|
16
|
+
noteElements(this, data);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Assigning the element plane whole releases the elements held before and
|
|
20
|
+
* holds the new ones, element i under the key `i`.
|
|
21
|
+
*/
|
|
22
|
+
adoptData(data) {
|
|
23
|
+
const previous = this.data;
|
|
24
|
+
super.adoptData(data);
|
|
25
|
+
if (previous !== data)
|
|
26
|
+
noteElements(this, data, previous);
|
|
14
27
|
}
|
|
15
28
|
/** Count leaves use the same Star rule at the root and inside an array. */
|
|
16
29
|
static starCount(count, replacement) {
|
|
@@ -78,9 +91,9 @@ export class FrameArray extends FrameList {
|
|
|
78
91
|
if (!node.elements().length)
|
|
79
92
|
return replacement;
|
|
80
93
|
const result = node.copy();
|
|
81
|
-
result.
|
|
94
|
+
result.adoptData(node.itemsFor(result, (item) => node.repeatsTerm(item)
|
|
82
95
|
? go(item.walked().referent())
|
|
83
|
-
: item);
|
|
96
|
+
: item));
|
|
84
97
|
return result;
|
|
85
98
|
};
|
|
86
99
|
return go(this);
|
|
@@ -97,6 +110,7 @@ export class FrameArray extends FrameList {
|
|
|
97
110
|
// of that field rather than a flag dance over the lexical pointer.
|
|
98
111
|
if (this.hasDeclaredParent()) {
|
|
99
112
|
result.parent = this.parent;
|
|
113
|
+
noteHolding(result, PARENT_KEY, this.parent);
|
|
100
114
|
}
|
|
101
115
|
// An aggregate accepts declarations while it is under construction, and
|
|
102
116
|
// says so rather than leaving a name to infer it from the context stack.
|
|
@@ -105,7 +119,7 @@ export class FrameArray extends FrameList {
|
|
|
105
119
|
// does not make it absorb declarations.
|
|
106
120
|
result.declares = true;
|
|
107
121
|
try {
|
|
108
|
-
result.
|
|
122
|
+
result.adoptData(this.array_eval(input, result));
|
|
109
123
|
}
|
|
110
124
|
finally {
|
|
111
125
|
result.declares = false;
|
|
@@ -207,16 +221,28 @@ export class FrameArray extends FrameList {
|
|
|
207
221
|
instanceCopy() {
|
|
208
222
|
return this.copy();
|
|
209
223
|
}
|
|
224
|
+
/** Reading the element plane is a read of this aggregate's state. */
|
|
225
|
+
elements() {
|
|
226
|
+
guardAccess(this, "read");
|
|
227
|
+
noteRead(this);
|
|
228
|
+
return super.elements();
|
|
229
|
+
}
|
|
210
230
|
/** Elements are members too, so a held element can be replaced in place. */
|
|
211
231
|
replaceMember(member, replacement) {
|
|
212
|
-
|
|
213
|
-
|
|
232
|
+
const replaced = super.replaceMember(member, replacement);
|
|
233
|
+
let elementReplaced = false;
|
|
234
|
+
this.data.forEach((item, index) => {
|
|
214
235
|
if (item !== member)
|
|
215
|
-
return
|
|
216
|
-
replaced
|
|
217
|
-
|
|
236
|
+
return;
|
|
237
|
+
if (!replaced && !elementReplaced)
|
|
238
|
+
guardAccess(this, "write");
|
|
239
|
+
this.data[index] = replacement;
|
|
240
|
+
noteHolding(this, String(index), replacement, member);
|
|
241
|
+
elementReplaced = true;
|
|
218
242
|
});
|
|
219
|
-
|
|
243
|
+
if (elementReplaced && !replaced)
|
|
244
|
+
noteWrite(this);
|
|
245
|
+
return replaced || elementReplaced;
|
|
220
246
|
}
|
|
221
247
|
lookup_here(key, origin) {
|
|
222
248
|
if (Frame.isIndexKey(key)) {
|
|
@@ -347,7 +373,7 @@ export class FrameArray extends FrameList {
|
|
|
347
373
|
/** A declared Count, Size or residual first; the list's own rule otherwise. */
|
|
348
374
|
sizeFor(key) {
|
|
349
375
|
const failure = this.observationError();
|
|
350
|
-
if (failure && Frame.
|
|
376
|
+
if (failure && Frame.isSizeOperator(key))
|
|
351
377
|
return failure;
|
|
352
378
|
return this.declaredObservation(key) ?? super.sizeFor(key);
|
|
353
379
|
}
|
|
@@ -413,7 +439,10 @@ export class FrameArray extends FrameList {
|
|
|
413
439
|
/** Pushes a non-nil value, whatever application this aggregate declares. */
|
|
414
440
|
collect(value, _context = Frame.nil) {
|
|
415
441
|
if (!value.is.void) {
|
|
442
|
+
guardAccess(this, "write");
|
|
416
443
|
this.data.push(value);
|
|
444
|
+
noteHolding(this, String(this.data.length - 1), value);
|
|
445
|
+
noteWrite(this);
|
|
417
446
|
}
|
|
418
447
|
return this;
|
|
419
448
|
}
|
|
@@ -436,7 +465,9 @@ export class FrameArray extends FrameList {
|
|
|
436
465
|
return this.data.length;
|
|
437
466
|
}
|
|
438
467
|
reset() {
|
|
439
|
-
this
|
|
468
|
+
guardAccess(this, "write");
|
|
469
|
+
this.adoptData([]);
|
|
470
|
+
noteWrite(this);
|
|
440
471
|
}
|
|
441
472
|
}
|
|
442
473
|
Object.defineProperty(FrameArray, "BEGIN_ARRAY", {
|
|
@@ -55,12 +55,7 @@ export class FrameExpr extends FrameList {
|
|
|
55
55
|
}
|
|
56
56
|
let result = Frame.nil;
|
|
57
57
|
for (const run of commaRuns(body)) {
|
|
58
|
-
|
|
59
|
-
result = run.length > 1
|
|
60
|
-
? FrameExpr.commaGroup(run, scope)
|
|
61
|
-
: item instanceof FrameExpr
|
|
62
|
-
? item.evaluate(scope)
|
|
63
|
-
: item.in(scope);
|
|
58
|
+
result = FrameExpr.evaluateRun(run, scope);
|
|
64
59
|
// A sequence stops at the first failing statement, because later
|
|
65
60
|
// statements were written to run after the earlier ones succeeded.
|
|
66
61
|
if (result.isFailedResult())
|
|
@@ -68,6 +63,42 @@ export class FrameExpr extends FrameList {
|
|
|
68
63
|
}
|
|
69
64
|
return result;
|
|
70
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* `evaluateBody`, one statement at a time.
|
|
68
|
+
*
|
|
69
|
+
* The generator yields after each statement of a sequenced body and returns
|
|
70
|
+
* the body's value, with the same stops and the same scope as
|
|
71
|
+
* `evaluateBody`, so a body stepped to completion answers what a called one
|
|
72
|
+
* answers. A body that is not a sequence, or has one term, is one step. The
|
|
73
|
+
* statement is the finest boundary the language defines between two
|
|
74
|
+
* effects of one body, so it is the unit an event dispatcher may interleave
|
|
75
|
+
* at (`event-dispatch.ts`); nothing inside a statement is a boundary.
|
|
76
|
+
*/
|
|
77
|
+
static *stepBody(body, input, sequence = false) {
|
|
78
|
+
const scope = EvaluationScope.from(input);
|
|
79
|
+
if (body.length === 1 || !sequence) {
|
|
80
|
+
const result = FrameExpr.evaluateBody(body, scope, sequence);
|
|
81
|
+
yield result;
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
let result = Frame.nil;
|
|
85
|
+
for (const run of commaRuns(body)) {
|
|
86
|
+
result = FrameExpr.evaluateRun(run, scope);
|
|
87
|
+
yield result;
|
|
88
|
+
if (result.isFailedResult())
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/** One statement of a sequenced body: a comma group, or a single term. */
|
|
94
|
+
static evaluateRun(run, scope) {
|
|
95
|
+
const [item] = run;
|
|
96
|
+
return run.length > 1
|
|
97
|
+
? FrameExpr.commaGroup(run, scope)
|
|
98
|
+
: item instanceof FrameExpr
|
|
99
|
+
? item.evaluate(scope)
|
|
100
|
+
: item.in(scope);
|
|
101
|
+
}
|
|
71
102
|
call(argument, parameter = Frame.nil) {
|
|
72
103
|
return this.in(EvaluationScope.call(argument, parameter));
|
|
73
104
|
}
|
|
@@ -146,7 +177,7 @@ export class FrameExpr extends FrameList {
|
|
|
146
177
|
continue;
|
|
147
178
|
}
|
|
148
179
|
if (index === 0 && value.isSymbol() && value.is.address &&
|
|
149
|
-
Frame.
|
|
180
|
+
Frame.isSizeOperator(value.spelling())) {
|
|
150
181
|
sum = value;
|
|
151
182
|
continue;
|
|
152
183
|
}
|
|
@@ -154,7 +185,7 @@ export class FrameExpr extends FrameList {
|
|
|
154
185
|
// is not a receiver, so a leading .## remains an address (#411).
|
|
155
186
|
if (index > 0 && sum.is.void && value.isSymbol() &&
|
|
156
187
|
value.is.address &&
|
|
157
|
-
Frame.
|
|
188
|
+
Frame.isSizeOperator(value.spelling())) {
|
|
158
189
|
sum = value.called_by(sum);
|
|
159
190
|
continue;
|
|
160
191
|
}
|
|
@@ -522,7 +553,7 @@ export class FrameScoped extends Frame {
|
|
|
522
553
|
/** Sizes as its term does, `.#`, `.##` and `.###`; anything else is applied. */
|
|
523
554
|
call(argument, parameter = Frame.nil) {
|
|
524
555
|
if (argument.isSymbol() && argument.is.address === true &&
|
|
525
|
-
Frame.
|
|
556
|
+
Frame.isSizeOperator(argument.spelling())) {
|
|
526
557
|
return this.sizeFor(argument.spelling());
|
|
527
558
|
}
|
|
528
559
|
return this.live().call(argument, parameter);
|
|
@@ -132,6 +132,18 @@ export class FrameLazy extends FrameExpr {
|
|
|
132
132
|
in(input = []) {
|
|
133
133
|
return this.bind(input);
|
|
134
134
|
}
|
|
135
|
+
/** The scope this bound copy captured; a template no read bound has none. */
|
|
136
|
+
capturedScope() {
|
|
137
|
+
return capturedScopes.get(this) ?? this.legacyCapture();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The scope a call of this closure would evaluate its body in, built as
|
|
141
|
+
* `call` builds it, without running the body. A static walk of the body
|
|
142
|
+
* resolves names against this scope's lookup frames (`access-footprint.ts`).
|
|
143
|
+
*/
|
|
144
|
+
scopeForCall(argument, parameter = Frame.nil, receiverState) {
|
|
145
|
+
return EvaluationScope.call(argument, parameter, this, this.capturedScope(), receiverState);
|
|
146
|
+
}
|
|
135
147
|
/** Returns state only when this literal was evaluated in that invocation. */
|
|
136
148
|
receiverStateForCallback(receiverState) {
|
|
137
149
|
return inlineCallbackReceiverStates.get(this) === receiverState
|
|
@@ -147,7 +159,7 @@ export class FrameLazy extends FrameExpr {
|
|
|
147
159
|
*/
|
|
148
160
|
sizeOperator(argument) {
|
|
149
161
|
if (argument instanceof FrameSymbol && argument.is.address === true &&
|
|
150
|
-
Frame.
|
|
162
|
+
Frame.isSizeOperator(argument.spelling())) {
|
|
151
163
|
return this.sizeFor(argument.spelling());
|
|
152
164
|
}
|
|
153
165
|
return undefined;
|
|
@@ -170,6 +182,29 @@ export class FrameLazy extends FrameExpr {
|
|
|
170
182
|
}
|
|
171
183
|
/** Evaluates this closure with optional bound receiver capability. */
|
|
172
184
|
call(argument, parameter = Frame.nil, receiverState) {
|
|
185
|
+
const scope = this.prelude(argument, parameter, receiverState);
|
|
186
|
+
if (scope instanceof Frame)
|
|
187
|
+
return scope;
|
|
188
|
+
const result = FrameExpr.evaluateBody(this.data, scope, this.is.sequence === true);
|
|
189
|
+
return this.checkReturn(result);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* `call`, one statement at a time (`FrameExpr.stepBody`). The generator
|
|
193
|
+
* yields after each statement and returns what `call` would have answered.
|
|
194
|
+
* A call that never reaches the body, a size operator, an empty body, or a
|
|
195
|
+
* refused argument, is one step.
|
|
196
|
+
*/
|
|
197
|
+
*steps(argument, parameter = Frame.nil, receiverState) {
|
|
198
|
+
const scope = this.prelude(argument, parameter, receiverState);
|
|
199
|
+
if (scope instanceof Frame) {
|
|
200
|
+
yield scope;
|
|
201
|
+
return scope;
|
|
202
|
+
}
|
|
203
|
+
const result = yield* FrameExpr.stepBody(this.data, scope, this.is.sequence === true);
|
|
204
|
+
return this.checkReturn(result);
|
|
205
|
+
}
|
|
206
|
+
/** Everything `call` does before the body: an answer, or the body's scope. */
|
|
207
|
+
prelude(argument, parameter, receiverState) {
|
|
173
208
|
if (argument instanceof FrameSymbol && argument.is.address === true &&
|
|
174
209
|
argument.spelling() === "<>")
|
|
175
210
|
return FrameType.of(this);
|
|
@@ -185,10 +220,7 @@ export class FrameLazy extends FrameExpr {
|
|
|
185
220
|
const prepared = this.prepareArgument(argument);
|
|
186
221
|
if (prepared.is.error)
|
|
187
222
|
return prepared;
|
|
188
|
-
|
|
189
|
-
const scope = EvaluationScope.call(prepared, parameter, this, enclosing, receiverState);
|
|
190
|
-
const result = FrameExpr.evaluateBody(this.data, scope, this.is.sequence === true);
|
|
191
|
-
return this.checkReturn(result);
|
|
223
|
+
return this.scopeForCall(prepared, parameter, receiverState);
|
|
192
224
|
}
|
|
193
225
|
legacyCapture() {
|
|
194
226
|
return this.up && !this.up.is.missing
|
|
@@ -145,7 +145,7 @@ export class FrameList extends Frame {
|
|
|
145
145
|
/** Immediate count is unchanged; Size and residual share native child sizes. */
|
|
146
146
|
sizeFor(key) {
|
|
147
147
|
const failure = this.observationError();
|
|
148
|
-
if (failure && Frame.
|
|
148
|
+
if (failure && Frame.isSizeOperator(key))
|
|
149
149
|
return failure;
|
|
150
150
|
if (key === Frame.kCOUNT) {
|
|
151
151
|
const elements = this.sizeElements();
|
|
@@ -236,7 +236,7 @@ export class FrameList extends Frame {
|
|
|
236
236
|
for (const term of repeated)
|
|
237
237
|
terms.push(term);
|
|
238
238
|
}
|
|
239
|
-
result.
|
|
239
|
+
result.adoptData(terms);
|
|
240
240
|
return result.finishRepetition();
|
|
241
241
|
}
|
|
242
242
|
/**
|
|
@@ -283,9 +283,16 @@ export class FrameList extends Frame {
|
|
|
283
283
|
}
|
|
284
284
|
copy() {
|
|
285
285
|
const clone = super.copy();
|
|
286
|
-
clone.
|
|
286
|
+
clone.adoptData(this.itemsFor(clone, (item) => item));
|
|
287
287
|
return clone;
|
|
288
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Replaces the whole data plane. The one assignment point, so an aggregate
|
|
291
|
+
* can observe the elements it stops and starts holding (`FrameArray`).
|
|
292
|
+
*/
|
|
293
|
+
adoptData(data) {
|
|
294
|
+
this.data = data;
|
|
295
|
+
}
|
|
289
296
|
/**
|
|
290
297
|
* This frame's items as a copy holds them.
|
|
291
298
|
*
|
|
@@ -32,6 +32,14 @@ const recognizeName = (symbol, source = "") => {
|
|
|
32
32
|
if (Frame.SIZE_OPERATORS.includes(source + char)) {
|
|
33
33
|
return { disposition: ScanDisposition.Consume };
|
|
34
34
|
}
|
|
35
|
+
// A single hash followed by decimal digits is the ranked hash `.#n`
|
|
36
|
+
// (occurrence/figure 007.32). Only the lone `#` takes digits: `.##2` is
|
|
37
|
+
// `.##` and then the count 2, as before, and `.#2x` is `.#2` then `x`.
|
|
38
|
+
// The spelling is canonical decimal, one name per rung: a zero ends the name,
|
|
39
|
+
// so `.#05` is `.#0` and then the number 5.
|
|
40
|
+
if (/^#(|[1-9][0-9]*)$/.test(source) && /^[0-9]$/.test(char)) {
|
|
41
|
+
return { disposition: ScanDisposition.Consume };
|
|
42
|
+
}
|
|
35
43
|
if (source.startsWith(Frame.kCOUNT)) {
|
|
36
44
|
return { disposition: ScanDisposition.CompleteRedispatch };
|
|
37
45
|
}
|
|
@@ -289,7 +289,7 @@ export class FrameSymbol extends FrameAtom {
|
|
|
289
289
|
// A size operator answers for the receiver itself rather than looking a
|
|
290
290
|
// name up, so the scope a value was read from never answers for it (#411).
|
|
291
291
|
// Only an address is one, since the lexer looks `#` up as the comment.
|
|
292
|
-
if (this.is.address === true && Frame.
|
|
292
|
+
if (this.is.address === true && Frame.isSizeOperator(this.data)) {
|
|
293
293
|
return context.referent().sizeFor(this.data);
|
|
294
294
|
}
|
|
295
295
|
// A dotted arithmetic operator is the operator itself: `xs ./ 3` is
|