eliscript 0.0.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.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MemoizedSequenceView,
|
|
3
|
+
memoizedSequenceView,
|
|
4
|
+
seq,
|
|
5
|
+
} from "./collection.mjs";
|
|
6
|
+
import { memoizedSequenceStatus } from "./collection-internals.mjs";
|
|
7
|
+
import {
|
|
8
|
+
deduping,
|
|
9
|
+
distincting,
|
|
10
|
+
dropping,
|
|
11
|
+
droppingWhile,
|
|
12
|
+
filtering,
|
|
13
|
+
interposing,
|
|
14
|
+
keeping,
|
|
15
|
+
keepingIndexed,
|
|
16
|
+
mapcatting,
|
|
17
|
+
mapping,
|
|
18
|
+
mappingIndexed,
|
|
19
|
+
partitioningAll,
|
|
20
|
+
partitioningBy,
|
|
21
|
+
removing,
|
|
22
|
+
sequence as transducerSequence,
|
|
23
|
+
taking,
|
|
24
|
+
takingNth,
|
|
25
|
+
takingWhile,
|
|
26
|
+
} from "./transducer.mjs";
|
|
27
|
+
|
|
28
|
+
const lazyConsStates = new WeakMap();
|
|
29
|
+
const NO_LAZY_CONS_FAILURE = Symbol("eliscript.lazy-cons.no-failure");
|
|
30
|
+
|
|
31
|
+
function requireThunk(value, label) {
|
|
32
|
+
if (typeof value !== "function") {
|
|
33
|
+
throw new TypeError(`${label} must be a function`);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function requireArity(actual, expected, name) {
|
|
39
|
+
if (actual !== expected) {
|
|
40
|
+
const unit = expected === 1 ? "argument" : "arguments";
|
|
41
|
+
throw new TypeError(`${name} requires exactly ${expected} ${unit}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function sequenceIterator(value) {
|
|
46
|
+
const sequence = seq(value);
|
|
47
|
+
return sequence === null
|
|
48
|
+
? [][Symbol.iterator]()
|
|
49
|
+
: sequence[Symbol.iterator]();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function realizeConsTail(state) {
|
|
53
|
+
if (state.failure !== NO_LAZY_CONS_FAILURE) throw state.failure;
|
|
54
|
+
if (!state.tailStarted) {
|
|
55
|
+
state.tailStarted = true;
|
|
56
|
+
try {
|
|
57
|
+
state.tail = state.tailThunk();
|
|
58
|
+
state.tailThunk = null;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
state.failure = error;
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return state.tail;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function lazyConsIterator(initialState) {
|
|
68
|
+
let state = initialState;
|
|
69
|
+
let headPending = true;
|
|
70
|
+
let tailIterator = null;
|
|
71
|
+
let complete = false;
|
|
72
|
+
return {
|
|
73
|
+
next() {
|
|
74
|
+
while (!complete) {
|
|
75
|
+
if (state !== null && headPending) {
|
|
76
|
+
headPending = false;
|
|
77
|
+
return { value: state.value, done: false };
|
|
78
|
+
}
|
|
79
|
+
if (state !== null) {
|
|
80
|
+
const tail = realizeConsTail(state);
|
|
81
|
+
const tailState = lazyConsStates.get(tail);
|
|
82
|
+
if (tailState !== undefined) {
|
|
83
|
+
state = tailState;
|
|
84
|
+
headPending = true;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
state = null;
|
|
88
|
+
tailIterator = sequenceIterator(tail);
|
|
89
|
+
}
|
|
90
|
+
const result = tailIterator.next();
|
|
91
|
+
if (!result.done) return result;
|
|
92
|
+
complete = true;
|
|
93
|
+
}
|
|
94
|
+
return { value: undefined, done: true };
|
|
95
|
+
},
|
|
96
|
+
return(value) {
|
|
97
|
+
complete = true;
|
|
98
|
+
if (tailIterator !== null && typeof tailIterator.return === "function") {
|
|
99
|
+
tailIterator.return();
|
|
100
|
+
}
|
|
101
|
+
return { value, done: true };
|
|
102
|
+
},
|
|
103
|
+
[Symbol.iterator]() {
|
|
104
|
+
return this;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function lazySequence(thunk) {
|
|
110
|
+
if (arguments.length !== 1) {
|
|
111
|
+
throw new TypeError("lazySequence requires exactly one thunk");
|
|
112
|
+
}
|
|
113
|
+
requireThunk(thunk, "lazySequence thunk");
|
|
114
|
+
return memoizedSequenceView(() => sequenceIterator(thunk()));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function lazyCons(value, tailThunk) {
|
|
118
|
+
if (arguments.length !== 2) {
|
|
119
|
+
throw new TypeError("lazyCons requires a value and tail thunk");
|
|
120
|
+
}
|
|
121
|
+
requireThunk(tailThunk, "lazyCons tail thunk");
|
|
122
|
+
const state = {
|
|
123
|
+
value,
|
|
124
|
+
tailThunk,
|
|
125
|
+
tailStarted: false,
|
|
126
|
+
tail: null,
|
|
127
|
+
failure: NO_LAZY_CONS_FAILURE,
|
|
128
|
+
};
|
|
129
|
+
const result = memoizedSequenceView(() => lazyConsIterator(state));
|
|
130
|
+
lazyConsStates.set(result, state);
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function lazyMap(transform, collection) {
|
|
135
|
+
requireArity(arguments.length, 2, "lazyMap");
|
|
136
|
+
return transducerSequence(mapping(transform), collection);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function lazyMapIndexed(transform, collection) {
|
|
140
|
+
requireArity(arguments.length, 2, "lazyMapIndexed");
|
|
141
|
+
return transducerSequence(mappingIndexed(transform), collection);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function lazyKeep(transform, collection) {
|
|
145
|
+
requireArity(arguments.length, 2, "lazyKeep");
|
|
146
|
+
return transducerSequence(keeping(transform), collection);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function lazyKeepIndexed(transform, collection) {
|
|
150
|
+
requireArity(arguments.length, 2, "lazyKeepIndexed");
|
|
151
|
+
return transducerSequence(keepingIndexed(transform), collection);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function lazyFilter(predicate, collection) {
|
|
155
|
+
requireArity(arguments.length, 2, "lazyFilter");
|
|
156
|
+
return transducerSequence(filtering(predicate), collection);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function lazyRemove(predicate, collection) {
|
|
160
|
+
requireArity(arguments.length, 2, "lazyRemove");
|
|
161
|
+
return transducerSequence(removing(predicate), collection);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function lazyTake(limit, collection) {
|
|
165
|
+
requireArity(arguments.length, 2, "lazyTake");
|
|
166
|
+
return transducerSequence(taking(limit), collection);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function lazyDrop(limit, collection) {
|
|
170
|
+
requireArity(arguments.length, 2, "lazyDrop");
|
|
171
|
+
return transducerSequence(dropping(limit), collection);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function lazyTakeWhile(predicate, collection) {
|
|
175
|
+
requireArity(arguments.length, 2, "lazyTakeWhile");
|
|
176
|
+
return transducerSequence(takingWhile(predicate), collection);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function lazyDropWhile(predicate, collection) {
|
|
180
|
+
requireArity(arguments.length, 2, "lazyDropWhile");
|
|
181
|
+
return transducerSequence(droppingWhile(predicate), collection);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function lazyTakeNth(interval, collection) {
|
|
185
|
+
requireArity(arguments.length, 2, "lazyTakeNth");
|
|
186
|
+
return transducerSequence(takingNth(interval), collection);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function lazyInterpose(separator, collection) {
|
|
190
|
+
requireArity(arguments.length, 2, "lazyInterpose");
|
|
191
|
+
return transducerSequence(interposing(separator), collection);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function lazyDedupe(collection) {
|
|
195
|
+
requireArity(arguments.length, 1, "lazyDedupe");
|
|
196
|
+
return transducerSequence(deduping(), collection);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function lazyDistinct(collection) {
|
|
200
|
+
requireArity(arguments.length, 1, "lazyDistinct");
|
|
201
|
+
return transducerSequence(distincting(), collection);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function lazyMapcat(transform, collection) {
|
|
205
|
+
requireArity(arguments.length, 2, "lazyMapcat");
|
|
206
|
+
return transducerSequence(mapcatting(transform), collection);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function lazyPartitionAll(size, collection) {
|
|
210
|
+
requireArity(arguments.length, 2, "lazyPartitionAll");
|
|
211
|
+
return transducerSequence(partitioningAll(size), collection);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function lazyPartitionBy(classifier, collection) {
|
|
215
|
+
requireArity(arguments.length, 2, "lazyPartitionBy");
|
|
216
|
+
return transducerSequence(partitioningBy(classifier), collection);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function isLazySequence(value) {
|
|
220
|
+
return value instanceof MemoizedSequenceView;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function status(value) {
|
|
224
|
+
if (!isLazySequence(value)) {
|
|
225
|
+
throw new TypeError("operation requires a lazy sequence");
|
|
226
|
+
}
|
|
227
|
+
return memoizedSequenceStatus(value);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function isLazySequenceRealized(value) {
|
|
231
|
+
return status(value).started;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function realizedLazySequenceCount(value) {
|
|
235
|
+
return status(value).realizedCount;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function realizeLazySequence(value) {
|
|
239
|
+
status(value);
|
|
240
|
+
for (const _element of value) {
|
|
241
|
+
// Traversal commits each produced value to the shared realization cache.
|
|
242
|
+
}
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const LIST_CONSTRUCTOR_TOKEN = Symbol("eliscript.list.constructor");
|
|
2
|
+
export const LIST_STATE = Symbol("eliscript.list.state");
|
|
3
|
+
|
|
4
|
+
const metrics = {
|
|
5
|
+
nodeAllocations: 0,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export function recordListNodeAllocation() {
|
|
9
|
+
metrics.nodeAllocations += 1;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function resetListMetrics() {
|
|
13
|
+
metrics.nodeAllocations = 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function readListMetrics() {
|
|
17
|
+
return Object.freeze({ ...metrics });
|
|
18
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COLLECTION_CONJ,
|
|
3
|
+
COLLECTION_COUNT,
|
|
4
|
+
COLLECTION_EMPTY,
|
|
5
|
+
COLLECTION_PEEK,
|
|
6
|
+
COLLECTION_POP,
|
|
7
|
+
COLLECTION_REDUCE,
|
|
8
|
+
COLLECTION_SEQ,
|
|
9
|
+
reduceIterable,
|
|
10
|
+
sequenceView,
|
|
11
|
+
} from "./collection-internals.mjs";
|
|
12
|
+
import {
|
|
13
|
+
LIST_CONSTRUCTOR_TOKEN,
|
|
14
|
+
LIST_STATE,
|
|
15
|
+
recordListNodeAllocation,
|
|
16
|
+
} from "./list-internals.mjs";
|
|
17
|
+
import {
|
|
18
|
+
METADATA_READ,
|
|
19
|
+
METADATA_WITH,
|
|
20
|
+
} from "./metadata-internals.mjs";
|
|
21
|
+
import {
|
|
22
|
+
VALUE_EQUAL,
|
|
23
|
+
VALUE_HASH,
|
|
24
|
+
orderedCollectionHash,
|
|
25
|
+
} from "./value-internals.mjs";
|
|
26
|
+
|
|
27
|
+
const MAX_COUNT = 0x7fff_ffff;
|
|
28
|
+
const MISSING = Symbol("eliscript.list.missing");
|
|
29
|
+
const LIST_HASH_TAG = 0x2f4a_6d19;
|
|
30
|
+
|
|
31
|
+
function makeList(count, value, rest, metadata = null) {
|
|
32
|
+
recordListNodeAllocation();
|
|
33
|
+
return new PersistentList(
|
|
34
|
+
LIST_CONSTRUCTOR_TOKEN,
|
|
35
|
+
count,
|
|
36
|
+
false,
|
|
37
|
+
value,
|
|
38
|
+
rest,
|
|
39
|
+
metadata,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function emptyListWithMetadata(metadata) {
|
|
44
|
+
return metadata === null
|
|
45
|
+
? EMPTY_LIST
|
|
46
|
+
: new PersistentList(
|
|
47
|
+
LIST_CONSTRUCTOR_TOKEN,
|
|
48
|
+
0,
|
|
49
|
+
true,
|
|
50
|
+
undefined,
|
|
51
|
+
null,
|
|
52
|
+
metadata,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class PersistentList {
|
|
57
|
+
constructor(token, count, empty, value, rest, metadata = null) {
|
|
58
|
+
if (token !== LIST_CONSTRUCTOR_TOKEN) {
|
|
59
|
+
throw new TypeError(
|
|
60
|
+
"PersistentList values must be created with persistentList or PersistentList.from",
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
this[LIST_STATE] = Object.freeze({
|
|
64
|
+
count,
|
|
65
|
+
empty,
|
|
66
|
+
value,
|
|
67
|
+
rest,
|
|
68
|
+
metadata,
|
|
69
|
+
});
|
|
70
|
+
Object.freeze(this);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static empty() {
|
|
74
|
+
return EMPTY_LIST;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static from(iterable) {
|
|
78
|
+
if (iterable instanceof PersistentList) {
|
|
79
|
+
return iterable;
|
|
80
|
+
}
|
|
81
|
+
const values = Array.from(iterable);
|
|
82
|
+
if (values.length > MAX_COUNT) {
|
|
83
|
+
throw new RangeError(`persistent list cannot exceed ${MAX_COUNT} values`);
|
|
84
|
+
}
|
|
85
|
+
let result = EMPTY_LIST;
|
|
86
|
+
for (let index = values.length - 1; index >= 0; index -= 1) {
|
|
87
|
+
result = makeList(result.count + 1, values[index], result);
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get count() {
|
|
93
|
+
return this[LIST_STATE].count;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get size() {
|
|
97
|
+
return this[LIST_STATE].count;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get isEmpty() {
|
|
101
|
+
return this[LIST_STATE].empty;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
first(notFound = null) {
|
|
105
|
+
const state = this[LIST_STATE];
|
|
106
|
+
return state.empty ? notFound : state.value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
rest() {
|
|
110
|
+
const state = this[LIST_STATE];
|
|
111
|
+
return state.empty ? this : state.rest;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
peek(notFound = null) {
|
|
115
|
+
return this.first(notFound);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
pop() {
|
|
119
|
+
if (this[LIST_STATE].empty) {
|
|
120
|
+
throw new RangeError("cannot pop an empty persistent list");
|
|
121
|
+
}
|
|
122
|
+
return this[LIST_STATE].rest;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
conj(value) {
|
|
126
|
+
if (this.count === MAX_COUNT) {
|
|
127
|
+
throw new RangeError(`persistent list cannot exceed ${MAX_COUNT} values`);
|
|
128
|
+
}
|
|
129
|
+
return makeList(this.count + 1, value, this, this[LIST_STATE].metadata);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
cons(value) {
|
|
133
|
+
return this.conj(value);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
nth(index, notFound = MISSING) {
|
|
137
|
+
if (!Number.isInteger(index) || index < 0 || index >= this.count) {
|
|
138
|
+
if (notFound !== MISSING) {
|
|
139
|
+
return notFound;
|
|
140
|
+
}
|
|
141
|
+
throw new RangeError(
|
|
142
|
+
`nth index ${String(index)} is outside [0, ${this.count})`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
let node = this;
|
|
146
|
+
let remaining = index;
|
|
147
|
+
while (remaining > 0) {
|
|
148
|
+
node = node[LIST_STATE].rest;
|
|
149
|
+
remaining -= 1;
|
|
150
|
+
}
|
|
151
|
+
return node[LIST_STATE].value;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
reduce(reducer, ...initial) {
|
|
155
|
+
return reduceIterable(this, reducer, ...initial);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
toArray() {
|
|
159
|
+
return [...this];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
[COLLECTION_COUNT]() {
|
|
163
|
+
return this.count;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
[COLLECTION_EMPTY]() {
|
|
167
|
+
return emptyListWithMetadata(this[LIST_STATE].metadata);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
[COLLECTION_CONJ](value) {
|
|
171
|
+
return this.conj(value);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
[COLLECTION_PEEK]() {
|
|
175
|
+
return this.peek();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
[COLLECTION_POP]() {
|
|
179
|
+
return this.pop();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
[COLLECTION_SEQ]() {
|
|
183
|
+
return this.isEmpty
|
|
184
|
+
? null
|
|
185
|
+
: sequenceView(() => this[Symbol.iterator](), this.count);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
[COLLECTION_REDUCE](reducer, ...initial) {
|
|
189
|
+
return reduceIterable(this, reducer, ...initial);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
[VALUE_EQUAL](other, equal) {
|
|
193
|
+
if (!(other instanceof PersistentList) || other.count !== this.count) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
const right = other[Symbol.iterator]();
|
|
197
|
+
for (const value of this) {
|
|
198
|
+
if (!equal(value, right.next().value)) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
[VALUE_HASH](hash) {
|
|
206
|
+
return orderedCollectionHash(this, hash, LIST_HASH_TAG);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
[METADATA_READ]() {
|
|
210
|
+
return this[LIST_STATE].metadata;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
[METADATA_WITH](metadata) {
|
|
214
|
+
const state = this[LIST_STATE];
|
|
215
|
+
if (metadata === state.metadata) {
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
return state.empty
|
|
219
|
+
? emptyListWithMetadata(metadata)
|
|
220
|
+
: makeList(state.count, state.value, state.rest, metadata);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
*[Symbol.iterator]() {
|
|
224
|
+
let node = this;
|
|
225
|
+
while (!node[LIST_STATE].empty) {
|
|
226
|
+
yield node[LIST_STATE].value;
|
|
227
|
+
node = node[LIST_STATE].rest;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
get [Symbol.toStringTag]() {
|
|
232
|
+
return "EliscriptPersistentList";
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export const EMPTY_LIST = new PersistentList(
|
|
237
|
+
LIST_CONSTRUCTOR_TOKEN,
|
|
238
|
+
0,
|
|
239
|
+
true,
|
|
240
|
+
undefined,
|
|
241
|
+
null,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
export function persistentList(...values) {
|
|
245
|
+
return PersistentList.from(values);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function isPersistentList(value) {
|
|
249
|
+
return value instanceof PersistentList;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function requireList(value, operation) {
|
|
253
|
+
if (!(value instanceof PersistentList)) {
|
|
254
|
+
throw new TypeError(`${operation} expects a persistent List or nil`);
|
|
255
|
+
}
|
|
256
|
+
return value;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function first(value, notFound = null) {
|
|
260
|
+
return value == null
|
|
261
|
+
? notFound
|
|
262
|
+
: requireList(value, "first").first(notFound);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function rest(value) {
|
|
266
|
+
return value == null
|
|
267
|
+
? EMPTY_LIST
|
|
268
|
+
: requireList(value, "rest").rest();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function cons(value, list) {
|
|
272
|
+
return (list == null ? EMPTY_LIST : requireList(list, "cons")).cons(value);
|
|
273
|
+
}
|