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,268 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assoc,
|
|
3
|
+
contains,
|
|
4
|
+
count,
|
|
5
|
+
get,
|
|
6
|
+
IIndexed,
|
|
7
|
+
IKVReduce,
|
|
8
|
+
nth,
|
|
9
|
+
reduce,
|
|
10
|
+
reduceKV,
|
|
11
|
+
} from "./collection.mjs";
|
|
12
|
+
import { implementsProtocolOperation } from "./protocol.mjs";
|
|
13
|
+
import {
|
|
14
|
+
EMPTY_MAP,
|
|
15
|
+
} from "./map.mjs";
|
|
16
|
+
import {
|
|
17
|
+
assocBang,
|
|
18
|
+
conjBang,
|
|
19
|
+
persistentBang,
|
|
20
|
+
transient,
|
|
21
|
+
} from "./transient.mjs";
|
|
22
|
+
import { into } from "./transducer.mjs";
|
|
23
|
+
import { EMPTY_VECTOR } from "./vector.mjs";
|
|
24
|
+
|
|
25
|
+
const MISSING = Symbol("eliscript.data.missing");
|
|
26
|
+
|
|
27
|
+
function requireFunction(value, label) {
|
|
28
|
+
if (typeof value !== "function") {
|
|
29
|
+
throw new TypeError(`${label} must be a function`);
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function pathVector(path) {
|
|
35
|
+
return into(EMPTY_VECTOR, path);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function readEntry(entry) {
|
|
39
|
+
if (count(entry) !== 2) {
|
|
40
|
+
throw new TypeError("data entries must contain exactly one key/value pair");
|
|
41
|
+
}
|
|
42
|
+
return [nth(entry, 0), nth(entry, 1)];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function reduceEntries(collection, reducer, initial) {
|
|
46
|
+
if (implementsProtocolOperation(IKVReduce, "reduceKV", collection) &&
|
|
47
|
+
!implementsProtocolOperation(IIndexed, "nth", collection)) {
|
|
48
|
+
return reduceKV(collection, reducer, initial);
|
|
49
|
+
}
|
|
50
|
+
return reduce(collection, (result, entry) => {
|
|
51
|
+
const [key, value] = readEntry(entry);
|
|
52
|
+
return reducer(result, key, value);
|
|
53
|
+
}, initial);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function collectBuckets(keyFunction, collection, createBucket, updateBucket) {
|
|
57
|
+
requireFunction(keyFunction, "data key function");
|
|
58
|
+
let bucketIndex = EMPTY_MAP;
|
|
59
|
+
const keys = [];
|
|
60
|
+
const buckets = [];
|
|
61
|
+
reduce(collection, (result, value) => {
|
|
62
|
+
const key = keyFunction(value);
|
|
63
|
+
let index = bucketIndex.get(key, MISSING);
|
|
64
|
+
if (index === MISSING) {
|
|
65
|
+
index = keys.length;
|
|
66
|
+
bucketIndex = bucketIndex.assoc(key, index);
|
|
67
|
+
keys.push(key);
|
|
68
|
+
buckets.push(createBucket(value));
|
|
69
|
+
} else {
|
|
70
|
+
updateBucket(buckets, index, value);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}, null);
|
|
74
|
+
return { keys, buckets };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function persistentMapFromBuckets(keys, buckets, finishBucket) {
|
|
78
|
+
const result = transient(EMPTY_MAP);
|
|
79
|
+
for (let index = 0; index < keys.length; index += 1) {
|
|
80
|
+
assocBang(result, keys[index], finishBucket(buckets[index]));
|
|
81
|
+
}
|
|
82
|
+
return persistentBang(result);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function indexBy(keyFunction, collection) {
|
|
86
|
+
requireFunction(keyFunction, "indexBy key function");
|
|
87
|
+
const result = transient(EMPTY_MAP);
|
|
88
|
+
reduce(collection, (builder, value) => {
|
|
89
|
+
assocBang(builder, keyFunction(value), value);
|
|
90
|
+
return builder;
|
|
91
|
+
}, result);
|
|
92
|
+
return persistentBang(result);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function groupBy(keyFunction, collection) {
|
|
96
|
+
const { keys, buckets } = collectBuckets(
|
|
97
|
+
keyFunction,
|
|
98
|
+
collection,
|
|
99
|
+
(value) => [value],
|
|
100
|
+
(values, index, value) => values[index].push(value),
|
|
101
|
+
);
|
|
102
|
+
return persistentMapFromBuckets(
|
|
103
|
+
keys,
|
|
104
|
+
buckets,
|
|
105
|
+
(values) => into(EMPTY_VECTOR, values),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function countBy(keyFunction, collection) {
|
|
110
|
+
const { keys, buckets } = collectBuckets(
|
|
111
|
+
keyFunction,
|
|
112
|
+
collection,
|
|
113
|
+
() => 1,
|
|
114
|
+
(counts, index) => {
|
|
115
|
+
counts[index] += 1;
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
return persistentMapFromBuckets(keys, buckets, (count) => count);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function frequencies(collection) {
|
|
122
|
+
return countBy((value) => value, collection);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function getIn(collection, path, notFound = null) {
|
|
126
|
+
const keys = pathVector(path);
|
|
127
|
+
let current = collection;
|
|
128
|
+
for (let index = 0; index < count(keys); index += 1) {
|
|
129
|
+
if (current == null) return notFound;
|
|
130
|
+
current = get(current, nth(keys, index), MISSING);
|
|
131
|
+
if (current === MISSING) return notFound;
|
|
132
|
+
}
|
|
133
|
+
return current;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function assocIn(collection, path, value) {
|
|
137
|
+
const keys = pathVector(path);
|
|
138
|
+
const size = count(keys);
|
|
139
|
+
if (size === 0) return value;
|
|
140
|
+
|
|
141
|
+
const parents = [];
|
|
142
|
+
let current = collection;
|
|
143
|
+
for (let index = 0; index < size - 1; index += 1) {
|
|
144
|
+
parents.push(current);
|
|
145
|
+
if (current == null) {
|
|
146
|
+
current = EMPTY_MAP;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const child = get(current, nth(keys, index), MISSING);
|
|
150
|
+
current = child === MISSING || child == null ? EMPTY_MAP : child;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let result = assoc(
|
|
154
|
+
current == null ? EMPTY_MAP : current,
|
|
155
|
+
nth(keys, size - 1),
|
|
156
|
+
value,
|
|
157
|
+
);
|
|
158
|
+
for (let index = size - 2; index >= 0; index -= 1) {
|
|
159
|
+
const parent = parents[index];
|
|
160
|
+
result = assoc(
|
|
161
|
+
parent == null ? EMPTY_MAP : parent,
|
|
162
|
+
nth(keys, index),
|
|
163
|
+
result,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function update(collection, key, transform, ...arguments_) {
|
|
170
|
+
requireFunction(transform, "update transform");
|
|
171
|
+
const target = collection == null ? EMPTY_MAP : collection;
|
|
172
|
+
return assoc(target, key, transform(get(target, key), ...arguments_));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function updateIn(collection, path, transform, ...arguments_) {
|
|
176
|
+
requireFunction(transform, "updateIn transform");
|
|
177
|
+
return assocIn(
|
|
178
|
+
collection,
|
|
179
|
+
path,
|
|
180
|
+
transform(getIn(collection, path), ...arguments_),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function keys(collection) {
|
|
185
|
+
const result = transient(EMPTY_VECTOR);
|
|
186
|
+
reduceKV(collection, (builder, key) => {
|
|
187
|
+
conjBang(builder, key);
|
|
188
|
+
return builder;
|
|
189
|
+
}, result);
|
|
190
|
+
return persistentBang(result);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function vals(collection) {
|
|
194
|
+
const result = transient(EMPTY_VECTOR);
|
|
195
|
+
reduceKV(collection, (builder, _key, value) => {
|
|
196
|
+
conjBang(builder, value);
|
|
197
|
+
return builder;
|
|
198
|
+
}, result);
|
|
199
|
+
return persistentBang(result);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function updateKeys(collection, transform) {
|
|
203
|
+
requireFunction(transform, "updateKeys transform");
|
|
204
|
+
const result = transient(EMPTY_MAP);
|
|
205
|
+
reduceKV(collection, (builder, key, value) => {
|
|
206
|
+
assocBang(builder, transform(key), value);
|
|
207
|
+
return builder;
|
|
208
|
+
}, result);
|
|
209
|
+
return persistentBang(result);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function updateVals(collection, transform) {
|
|
213
|
+
requireFunction(transform, "updateVals transform");
|
|
214
|
+
const result = transient(EMPTY_MAP);
|
|
215
|
+
reduceKV(collection, (builder, key, value) => {
|
|
216
|
+
assocBang(builder, key, transform(value));
|
|
217
|
+
return builder;
|
|
218
|
+
}, result);
|
|
219
|
+
return persistentBang(result);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function selectKeys(collection, keys) {
|
|
223
|
+
const result = transient(EMPTY_MAP);
|
|
224
|
+
reduce(keys, (builder, key) => {
|
|
225
|
+
if (collection != null && contains(collection, key)) {
|
|
226
|
+
assocBang(builder, key, get(collection, key));
|
|
227
|
+
}
|
|
228
|
+
return builder;
|
|
229
|
+
}, result);
|
|
230
|
+
return persistentBang(result);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function merge(...collections) {
|
|
234
|
+
const result = transient(EMPTY_MAP);
|
|
235
|
+
for (const collection of collections) {
|
|
236
|
+
reduceEntries(collection, (builder, key, value) => {
|
|
237
|
+
assocBang(builder, key, value);
|
|
238
|
+
return builder;
|
|
239
|
+
}, result);
|
|
240
|
+
}
|
|
241
|
+
return persistentBang(result);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function mergeWith(combine, ...collections) {
|
|
245
|
+
requireFunction(combine, "mergeWith combine function");
|
|
246
|
+
let result = EMPTY_MAP;
|
|
247
|
+
for (const collection of collections) {
|
|
248
|
+
result = reduceEntries(collection, (current, key, value) => {
|
|
249
|
+
return assoc(
|
|
250
|
+
current,
|
|
251
|
+
key,
|
|
252
|
+
contains(current, key) ? combine(get(current, key), value) : value,
|
|
253
|
+
);
|
|
254
|
+
}, result);
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function zipmap(keys, values) {
|
|
260
|
+
const keyVector = pathVector(keys);
|
|
261
|
+
const valueVector = pathVector(values);
|
|
262
|
+
const size = Math.min(count(keyVector), count(valueVector));
|
|
263
|
+
const result = transient(EMPTY_MAP);
|
|
264
|
+
for (let index = 0; index < size; index += 1) {
|
|
265
|
+
assocBang(result, nth(keyVector, index), nth(valueVector, index));
|
|
266
|
+
}
|
|
267
|
+
return persistentBang(result);
|
|
268
|
+
}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VALUE_EQUAL,
|
|
3
|
+
VALUE_HASH,
|
|
4
|
+
finishHash,
|
|
5
|
+
hashString,
|
|
6
|
+
mixHash,
|
|
7
|
+
} from "./value-internals.mjs";
|
|
8
|
+
import {
|
|
9
|
+
METADATA_READ,
|
|
10
|
+
METADATA_WITH,
|
|
11
|
+
} from "./metadata-internals.mjs";
|
|
12
|
+
|
|
13
|
+
const VALUE_TYPE = Symbol.for("eliscript.value.type");
|
|
14
|
+
const KEYWORD_TYPE = "keyword";
|
|
15
|
+
const SYMBOL_TYPE = "symbol";
|
|
16
|
+
|
|
17
|
+
const KEYWORD_HASH_TAG = 0x2f6d_31a7;
|
|
18
|
+
const SYMBOL_HASH_TAG = 0x71c8_4e93;
|
|
19
|
+
const UNQUALIFIED_HASH = 0x4a91_b67d;
|
|
20
|
+
|
|
21
|
+
const KEYWORD_TOKEN = Object.freeze({});
|
|
22
|
+
const SYMBOL_TOKEN = Object.freeze({});
|
|
23
|
+
const keywordState = new WeakMap();
|
|
24
|
+
const symbolState = new WeakMap();
|
|
25
|
+
const keywordInterns = new Map();
|
|
26
|
+
|
|
27
|
+
function logicalType(value) {
|
|
28
|
+
if ((typeof value !== "object" || value === null) &&
|
|
29
|
+
typeof value !== "function") {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, VALUE_TYPE);
|
|
34
|
+
return descriptor !== undefined && "value" in descriptor
|
|
35
|
+
? descriptor.value
|
|
36
|
+
: null;
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function normalizePart(value, label) {
|
|
43
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
44
|
+
throw new TypeError(`${label} must be a non-empty string`);
|
|
45
|
+
}
|
|
46
|
+
if (value.includes("/")) {
|
|
47
|
+
throw new TypeError(`${label} cannot contain /`);
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function splitQualified(value, label) {
|
|
53
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
54
|
+
throw new TypeError(`${label} must be a non-empty string`);
|
|
55
|
+
}
|
|
56
|
+
const separator = value.indexOf("/");
|
|
57
|
+
if (separator === -1) {
|
|
58
|
+
return { namespace: null, name: value };
|
|
59
|
+
}
|
|
60
|
+
if (separator === 0 || separator === value.length - 1 ||
|
|
61
|
+
value.indexOf("/", separator + 1) !== -1) {
|
|
62
|
+
throw new TypeError(`${label} must be name or namespace/name`);
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
namespace: value.slice(0, separator),
|
|
66
|
+
name: value.slice(separator + 1),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalizeArguments(arguments_, type, predicate) {
|
|
71
|
+
if (arguments_.length === 1) {
|
|
72
|
+
const value = arguments_[0];
|
|
73
|
+
if (predicate(value)) {
|
|
74
|
+
return {
|
|
75
|
+
existing: value,
|
|
76
|
+
namespace: identifierNamespace(value),
|
|
77
|
+
name: identifierName(value),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return { existing: null, ...splitQualified(value, `${type} name`) };
|
|
81
|
+
}
|
|
82
|
+
if (arguments_.length === 2) {
|
|
83
|
+
const [namespace, name] = arguments_;
|
|
84
|
+
if (namespace !== null) {
|
|
85
|
+
normalizePart(namespace, `${type} namespace`);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
existing: null,
|
|
89
|
+
namespace,
|
|
90
|
+
name: normalizePart(name, `${type} name`),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
throw new TypeError(`${type} expects one qualified name or namespace and name`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function qualifiedName(state) {
|
|
97
|
+
return state.namespace === null
|
|
98
|
+
? state.name
|
|
99
|
+
: `${state.namespace}/${state.name}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function identifierHash(tag, state) {
|
|
103
|
+
const namespaceHash = state.namespace === null
|
|
104
|
+
? UNQUALIFIED_HASH
|
|
105
|
+
: hashString(state.namespace);
|
|
106
|
+
return finishHash(
|
|
107
|
+
mixHash(mixHash(tag, namespaceHash), hashString(state.name)),
|
|
108
|
+
2,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function defineIdentifier(instance, type, state, table) {
|
|
113
|
+
table.set(instance, state);
|
|
114
|
+
Object.defineProperty(instance, VALUE_TYPE, {
|
|
115
|
+
value: type,
|
|
116
|
+
enumerable: false,
|
|
117
|
+
});
|
|
118
|
+
Object.freeze(instance);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function equalIdentifier(leftType, leftState, right) {
|
|
122
|
+
if (logicalType(right) !== leftType) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
return identifierNamespace(right) === leftState.namespace &&
|
|
127
|
+
identifierName(right) === leftState.name;
|
|
128
|
+
} catch {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export class Keyword {
|
|
134
|
+
constructor(token, namespace, name) {
|
|
135
|
+
if (token !== KEYWORD_TOKEN) {
|
|
136
|
+
throw new TypeError("Keyword values must be created with keyword()");
|
|
137
|
+
}
|
|
138
|
+
defineIdentifier(this, KEYWORD_TYPE, { namespace, name }, keywordState);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get name() {
|
|
142
|
+
return keywordState.get(this).name;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
get namespace() {
|
|
146
|
+
return keywordState.get(this).namespace;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get qualifiedName() {
|
|
150
|
+
return qualifiedName(keywordState.get(this));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
toString() {
|
|
154
|
+
return `:${this.qualifiedName}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
toJSON() {
|
|
158
|
+
throw new TypeError("Keyword values require an explicit serialization codec");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
[VALUE_EQUAL](other) {
|
|
162
|
+
return equalIdentifier(KEYWORD_TYPE, keywordState.get(this), other);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
[VALUE_HASH]() {
|
|
166
|
+
return identifierHash(KEYWORD_HASH_TAG, keywordState.get(this));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get [Symbol.toStringTag]() {
|
|
170
|
+
return "EliscriptKeyword";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export class EliscriptSymbol {
|
|
175
|
+
constructor(token, namespace, name, metadata = null) {
|
|
176
|
+
if (token !== SYMBOL_TOKEN) {
|
|
177
|
+
throw new TypeError(
|
|
178
|
+
"EliscriptSymbol values must be created with eliscriptSymbol()",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
defineIdentifier(
|
|
182
|
+
this,
|
|
183
|
+
SYMBOL_TYPE,
|
|
184
|
+
{ namespace, name, metadata },
|
|
185
|
+
symbolState,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
get name() {
|
|
190
|
+
return symbolState.get(this).name;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
get namespace() {
|
|
194
|
+
return symbolState.get(this).namespace;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
get qualifiedName() {
|
|
198
|
+
return qualifiedName(symbolState.get(this));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
toString() {
|
|
202
|
+
return this.qualifiedName;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
toJSON() {
|
|
206
|
+
throw new TypeError(
|
|
207
|
+
"EliscriptSymbol values require an explicit serialization codec",
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
[VALUE_EQUAL](other) {
|
|
212
|
+
return equalIdentifier(SYMBOL_TYPE, symbolState.get(this), other);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
[VALUE_HASH]() {
|
|
216
|
+
return identifierHash(SYMBOL_HASH_TAG, symbolState.get(this));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
[METADATA_READ]() {
|
|
220
|
+
return symbolState.get(this).metadata;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
[METADATA_WITH](metadata) {
|
|
224
|
+
const state = symbolState.get(this);
|
|
225
|
+
return metadata === state.metadata
|
|
226
|
+
? this
|
|
227
|
+
: new EliscriptSymbol(
|
|
228
|
+
SYMBOL_TOKEN,
|
|
229
|
+
state.namespace,
|
|
230
|
+
state.name,
|
|
231
|
+
metadata,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
get [Symbol.toStringTag]() {
|
|
236
|
+
return "EliscriptSymbol";
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function keyword(...arguments_) {
|
|
241
|
+
const normalized = normalizeArguments(arguments_, KEYWORD_TYPE, isKeyword);
|
|
242
|
+
if (normalized.existing !== null) {
|
|
243
|
+
return normalized.existing;
|
|
244
|
+
}
|
|
245
|
+
const key = normalized.namespace === null
|
|
246
|
+
? normalized.name
|
|
247
|
+
: `${normalized.namespace}/${normalized.name}`;
|
|
248
|
+
const existing = keywordInterns.get(key);
|
|
249
|
+
if (existing !== undefined) {
|
|
250
|
+
return existing;
|
|
251
|
+
}
|
|
252
|
+
const result = new Keyword(
|
|
253
|
+
KEYWORD_TOKEN,
|
|
254
|
+
normalized.namespace,
|
|
255
|
+
normalized.name,
|
|
256
|
+
);
|
|
257
|
+
keywordInterns.set(key, result);
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function eliscriptSymbol(...arguments_) {
|
|
262
|
+
const normalized = normalizeArguments(
|
|
263
|
+
arguments_,
|
|
264
|
+
SYMBOL_TYPE,
|
|
265
|
+
isEliscriptSymbol,
|
|
266
|
+
);
|
|
267
|
+
return normalized.existing ?? new EliscriptSymbol(
|
|
268
|
+
SYMBOL_TOKEN,
|
|
269
|
+
normalized.namespace,
|
|
270
|
+
normalized.name,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function isKeyword(value) {
|
|
275
|
+
return logicalType(value) === KEYWORD_TYPE;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function isEliscriptSymbol(value) {
|
|
279
|
+
return logicalType(value) === SYMBOL_TYPE;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function isIdentifier(value) {
|
|
283
|
+
return isKeyword(value) || isEliscriptSymbol(value);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function identifierName(value) {
|
|
287
|
+
if (!isIdentifier(value)) {
|
|
288
|
+
throw new TypeError("expected an Eliscript keyword or symbol");
|
|
289
|
+
}
|
|
290
|
+
const name = value.name;
|
|
291
|
+
if (typeof name !== "string" || name.length === 0 || name.includes("/")) {
|
|
292
|
+
throw new TypeError("expected an Eliscript keyword or symbol");
|
|
293
|
+
}
|
|
294
|
+
return name;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function identifierNamespace(value) {
|
|
298
|
+
if (!isIdentifier(value)) {
|
|
299
|
+
throw new TypeError("expected an Eliscript keyword or symbol");
|
|
300
|
+
}
|
|
301
|
+
const namespace = value.namespace;
|
|
302
|
+
if (namespace !== null &&
|
|
303
|
+
(typeof namespace !== "string" || namespace.length === 0 ||
|
|
304
|
+
namespace.includes("/"))) {
|
|
305
|
+
throw new TypeError("expected an Eliscript keyword or symbol");
|
|
306
|
+
}
|
|
307
|
+
return namespace;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export function qualifiedIdentifierName(value) {
|
|
311
|
+
const namespace = identifierNamespace(value);
|
|
312
|
+
const name = identifierName(value);
|
|
313
|
+
return namespace === null ? name : `${namespace}/${name}`;
|
|
314
|
+
}
|