hclang 0.43.0 → 0.44.1

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.
@@ -73,19 +73,31 @@ export function demiClass() {
73
73
  return loaded;
74
74
  }
75
75
  /**
76
- * Constructs a Demi with the HC class: `Demi [count, left, right]`.
76
+ * Constructs a Demi of any arity with the HC class: `Demi [count, loads]`.
77
+ * Two loads give the binary Demi, the same instance `Demi [count, left,
78
+ * right]` builds; any other nonzero number gives the channel Demi (007.27).
77
79
  *
78
- * @param count The full application Count; the Demi derives its phase from it.
79
- * @param left The left load.
80
- * @param right The right load.
80
+ * @param count The full application Count.
81
+ * @param loads The ordered channel loads.
81
82
  * @returns The new instance, or the class's failure.
82
83
  */
83
- export function constructDemi(count, left, right) {
84
+ export function constructDemiLoads(count, loads) {
84
85
  const constructor = demiClass();
85
86
  if (constructor.isFailedResult() || constructor.observationError()) {
86
87
  return constructor;
87
88
  }
88
- const state = new FrameArray([count, left, right]);
89
+ const state = new FrameArray([count, new FrameArray([...loads])]);
89
90
  return FrameExpr.answeredValue(constructor.call(state)).referent();
90
91
  }
91
- installDemiConstructor(constructDemi);
92
+ /**
93
+ * Constructs a binary Demi with the HC class: `Demi [count, left, right]`.
94
+ *
95
+ * @param count The full application Count; the Demi derives its phase from it.
96
+ * @param left The left load.
97
+ * @param right The right load.
98
+ * @returns The new instance, or the class's failure.
99
+ */
100
+ export function constructDemi(count, left, right) {
101
+ return constructDemiLoads(count, [left, right]);
102
+ }
103
+ installDemiConstructor(constructDemiLoads);
@@ -1,6 +1,8 @@
1
1
  import { Frame, FrameCount, FrameString, FrameSymbol, } from "../frames.js";
2
2
  // Installs the Demi class that `.####` constructs (011.20).
3
3
  import "./demi-prologue.js";
4
+ // Installs the RotorRouter class that list division answers (007.27c).
5
+ import "./router-prologue.js";
4
6
  import { EvalPipe, makeSourcePipe } from "./eval-pipe.js";
5
7
  import { evaluateResourceSource } from "./resource-import.js";
6
8
  import { sigilizer } from "./sigilizer.js";
@@ -0,0 +1,153 @@
1
+ /**
2
+ * The RotorRouter class, loaded from HC for list division.
3
+ *
4
+ * Dividing a list, `xs ./ n`, deals its elements into n channels and answers
5
+ * them as a `RotorRouter`, the HC class in `hc/rotor-router.hc`, so that the
6
+ * channels stay channels and a further division reads them breadth-first
7
+ * (occurrence/figure 007.27c). The ops package deals natively but cannot run
8
+ * HC, so this module installs the constructor it calls
9
+ * (`installDivisionCarrier`).
10
+ *
11
+ * As with the Demi class (`demi-prologue.ts`), the class is read from the
12
+ * compiled-in bundle, evaluated once per process on first use, in a namespace
13
+ * of its own with no host bindings and no root, and its outcome is kept,
14
+ * failure included. A division requested while the file is still loading is
15
+ * `$!.router-prologue-reentrant`.
16
+ *
17
+ * @module
18
+ */
19
+ import { EvaluationScope, Frame, FrameArray, FrameExpr, installRouterRecognizer, } from "../frames.js";
20
+ import { installDivisionCarrier } from "../ops/math.js";
21
+ import { HC_SOURCES } from "../bundle/hc-sources.js";
22
+ import { evaluateSourceText } from "./resource-import.js";
23
+ /** The bundled file that defines the class. */
24
+ export const ROUTER_SOURCE = "hc/rotor-router.hc";
25
+ /** The name the file declares the constructor under. */
26
+ export const ROUTER_CLASS = "RotorRouter";
27
+ /**
28
+ * The builders the class file declares beside its constructor, which make an
29
+ * instance from channels already known to be admissible, without validating
30
+ * or copying them: `binary-router` for two channels, `channel-router` for any
31
+ * other number.
32
+ */
33
+ export const ROUTER_BUILDERS = ["binary-router", "channel-router"];
34
+ let loaded;
35
+ let builders;
36
+ let loading = false;
37
+ /**
38
+ * Evaluates class source in a namespace of its own, answering its
39
+ * `RotorRouter` or the failure. Uncached: `routerClass` keeps the one result.
40
+ *
41
+ * @param source The source text, or undefined when the bundle lacks it.
42
+ * @returns The constructor, or an error.
43
+ */
44
+ export function loadRouterClass(source) {
45
+ if (source === undefined) {
46
+ return Frame.error(`$!.router-prologue-missing ${ROUTER_SOURCE}`);
47
+ }
48
+ const namespace = new FrameArray([]);
49
+ const result = evaluateSourceText(source, EvaluationScope.root(namespace));
50
+ if (result.isFailedResult() || result.observationError())
51
+ return result;
52
+ const constructor = namespace.getOwn(ROUTER_CLASS);
53
+ if (constructor.is.missing) {
54
+ return Frame.error(`$!.router-prologue-missing ${ROUTER_CLASS}`);
55
+ }
56
+ const [binary, channel] = ROUTER_BUILDERS.map((key) => namespace.getOwn(key));
57
+ if (binary.is.missing || channel.is.missing) {
58
+ return Frame.error(`$!.router-prologue-missing ${ROUTER_BUILDERS}`);
59
+ }
60
+ builders = { binary, channel };
61
+ return constructor;
62
+ }
63
+ /**
64
+ * The `RotorRouter` constructor closure, evaluated from the bundle on first
65
+ * use.
66
+ *
67
+ * @returns The constructor, or the error that loading it answered.
68
+ */
69
+ export function routerClass() {
70
+ if (loaded)
71
+ return loaded;
72
+ if (loading)
73
+ return Frame.error("$!.router-prologue-reentrant");
74
+ loading = true;
75
+ try {
76
+ loaded = loadRouterClass(HC_SOURCES.get(ROUTER_SOURCE));
77
+ }
78
+ finally {
79
+ loading = false;
80
+ }
81
+ return loaded;
82
+ }
83
+ /**
84
+ * Constructs a router with the HC class: `RotorRouter [ch0, …, chk−1]`.
85
+ *
86
+ * @param channels The channel arrays, in order.
87
+ * @returns The new instance, or the class's failure.
88
+ */
89
+ export function constructRouter(channels) {
90
+ const constructor = routerClass();
91
+ if (constructor.isFailedResult() || constructor.observationError()) {
92
+ return constructor;
93
+ }
94
+ return FrameExpr.answeredValue(constructor.call(channels)).referent();
95
+ }
96
+ /**
97
+ * The channels of an instance of the shipped class with other than two
98
+ * channels, for `.####` (007.27); undefined for anything else.
99
+ *
100
+ * Recognition mirrors the Demi's (007.25e): the source's own stored
101
+ * `channels`, a list of arrays, must rebuild through the constructor into a
102
+ * router `==` to the source, fields and methods included. Nothing of the
103
+ * source runs, so a record that only spells `channels` is not a router.
104
+ *
105
+ * @param source The observed Frame.
106
+ * @returns The stored channel arrays, or undefined.
107
+ */
108
+ export function channelRouterChannels(source) {
109
+ const host = source.referent();
110
+ if (!host.ownsIdentity())
111
+ return undefined;
112
+ const stored = host.get_here("channels").referent();
113
+ if (!(stored instanceof FrameArray))
114
+ return undefined;
115
+ const channels = stored.elements().map((channel) => channel.referent());
116
+ if (channels.length === 0 || channels.length === 2)
117
+ return undefined;
118
+ if (!channels.every((channel) => channel instanceof FrameArray)) {
119
+ return undefined;
120
+ }
121
+ const candidate = constructRouter(new FrameArray(channels.map((channel) => new FrameArray([...channel.elements()]))));
122
+ if (candidate.isFailedResult() || candidate.observationError()) {
123
+ return undefined;
124
+ }
125
+ return candidate.typeEquals(host) === Frame.all ? channels : undefined;
126
+ }
127
+ /**
128
+ * The router a list division answers, from the channels it just dealt.
129
+ *
130
+ * Dealt channels are fresh arrays and a round-robin profile by construction,
131
+ * so they go straight to the class's own builder: validating and copying them
132
+ * through the public constructor costs a closure call per channel, which made
133
+ * a router's breadth-first readout (a division by its longest channel's
134
+ * length) dominate its division (PR #482 review). The instance is the one the
135
+ * constructor would build from the same channels.
136
+ *
137
+ * @param channels The dealt channel arrays, at least one.
138
+ * @returns The new router, or the class's failure.
139
+ */
140
+ export function dealtRouter(channels) {
141
+ const constructor = routerClass();
142
+ if (constructor.isFailedResult() || constructor.observationError()) {
143
+ return constructor;
144
+ }
145
+ if (!builders)
146
+ return Frame.error("$!.router-unavailable /");
147
+ const build = channels.elements().length === 2
148
+ ? builders.binary
149
+ : builders.channel;
150
+ return FrameExpr.answeredValue(build.call(channels)).referent();
151
+ }
152
+ installDivisionCarrier(dealtRouter);
153
+ installRouterRecognizer(channelRouterChannels);