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
package/stdlib/data.eli
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
(module eliscript.data
|
|
2
|
+
(import-portable "./object.eli" assoc)
|
|
3
|
+
(import-portable "./object.eli" has?)
|
|
4
|
+
|
|
5
|
+
(defportable index-by (key-function values)
|
|
6
|
+
(let ((result (js-object))
|
|
7
|
+
(index 0))
|
|
8
|
+
(while (< index (js-length values))
|
|
9
|
+
(let* ((value (js-nth index values))
|
|
10
|
+
(key (funcall key-function value)))
|
|
11
|
+
(setq result (assoc result key value)))
|
|
12
|
+
(setq index (1+ index)))
|
|
13
|
+
result))
|
|
14
|
+
|
|
15
|
+
(defportable group-by (key-function values)
|
|
16
|
+
(let ((result (js-object))
|
|
17
|
+
(index 0))
|
|
18
|
+
(while (< index (js-length values))
|
|
19
|
+
(let* ((value (js-nth index values))
|
|
20
|
+
(key (funcall key-function value))
|
|
21
|
+
(group (if (has? result key) (get result key) (js-array)))
|
|
22
|
+
(next-group (js-array value))
|
|
23
|
+
(group-index (1- (js-length group))))
|
|
24
|
+
(while (>= group-index 0)
|
|
25
|
+
(setq next-group (js-cons (js-nth group-index group) next-group)
|
|
26
|
+
group-index (1- group-index)))
|
|
27
|
+
(setq result (assoc result key next-group)))
|
|
28
|
+
(setq index (1+ index)))
|
|
29
|
+
result))
|
|
30
|
+
|
|
31
|
+
(defportable count-by (key-function values)
|
|
32
|
+
(let ((result (js-object))
|
|
33
|
+
(index 0))
|
|
34
|
+
(while (< index (js-length values))
|
|
35
|
+
(let* ((value (js-nth index values))
|
|
36
|
+
(key (funcall key-function value))
|
|
37
|
+
(count (if (has? result key) (get result key) 0)))
|
|
38
|
+
(setq result (assoc result key (1+ count))))
|
|
39
|
+
(setq index (1+ index)))
|
|
40
|
+
result))
|
|
41
|
+
|
|
42
|
+
(export index-by group-by count-by))
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
(module eliscript.deferred
|
|
2
|
+
(import-portable "./persistent-map.eli"
|
|
3
|
+
persistent-map-assoc
|
|
4
|
+
persistent-map-get
|
|
5
|
+
persistent-map-has?)
|
|
6
|
+
(import-portable "./persistent-vector.eli" persistent-vector-from-array)
|
|
7
|
+
(import-portable "./value.eli" empty-value-map)
|
|
8
|
+
(defconst deferred-kind "eliscript/delay")
|
|
9
|
+
(defconst deferred-pending "pending")
|
|
10
|
+
(defconst deferred-running "running")
|
|
11
|
+
(defconst deferred-realized "realized")
|
|
12
|
+
(defconst deferred-registry (js* "new WeakSet()"))
|
|
13
|
+
(defun deferred-fail
|
|
14
|
+
(code message)
|
|
15
|
+
(throw (js-object :kind "eliscript/deferred-error" :code code :message message)))
|
|
16
|
+
(defun delay
|
|
17
|
+
(producer)
|
|
18
|
+
(when (not (= (value-type producer) "function"))
|
|
19
|
+
(deferred-fail
|
|
20
|
+
"ELI-DEFERRED-INVALID-PRODUCER"
|
|
21
|
+
"delay producer must be a function"))
|
|
22
|
+
(let ((state deferred-pending) (value nil) (reference nil))
|
|
23
|
+
(setq
|
|
24
|
+
reference
|
|
25
|
+
(js-call
|
|
26
|
+
globalThis.Object
|
|
27
|
+
:freeze
|
|
28
|
+
(js-object
|
|
29
|
+
:kind
|
|
30
|
+
deferred-kind
|
|
31
|
+
:realized
|
|
32
|
+
(lambda () (= state deferred-realized))
|
|
33
|
+
:force
|
|
34
|
+
(lambda
|
|
35
|
+
()
|
|
36
|
+
(cond ((= state deferred-realized) value)
|
|
37
|
+
((= state deferred-running)
|
|
38
|
+
(deferred-fail
|
|
39
|
+
"ELI-DEFERRED-REENTRANT-FORCE"
|
|
40
|
+
"delay cannot force itself while it is running"))
|
|
41
|
+
(t
|
|
42
|
+
(setq state deferred-running)
|
|
43
|
+
(try
|
|
44
|
+
(let ((computed (funcall producer)))
|
|
45
|
+
(setq value computed state deferred-realized)
|
|
46
|
+
computed)
|
|
47
|
+
(catch error (setq state deferred-pending) (throw error)))))))))
|
|
48
|
+
(js-call deferred-registry :add reference)
|
|
49
|
+
reference))
|
|
50
|
+
(defun delay?
|
|
51
|
+
(value)
|
|
52
|
+
(if (= (value-type value) "object")
|
|
53
|
+
(try (js-call deferred-registry :has value) (catch _error false))
|
|
54
|
+
false))
|
|
55
|
+
(defun realized? (value) (and (delay? value) (funcall (get value "realized" nil))))
|
|
56
|
+
(defun force (value) (if (delay? value) (funcall (get value "force" nil)) value))
|
|
57
|
+
(defun memoize
|
|
58
|
+
(function)
|
|
59
|
+
(when (not (= (value-type function) "function"))
|
|
60
|
+
(deferred-fail "ELI-DEFERRED-INVALID-FUNCTION" "memoize value must be a function"))
|
|
61
|
+
(let ((cache (empty-value-map)))
|
|
62
|
+
(lambda
|
|
63
|
+
(&rest arguments)
|
|
64
|
+
(let ((key (persistent-vector-from-array arguments)))
|
|
65
|
+
(if (persistent-map-has? cache key)
|
|
66
|
+
(persistent-map-get cache key nil)
|
|
67
|
+
(let ((result (apply function arguments)))
|
|
68
|
+
(setq cache (persistent-map-assoc cache key result))
|
|
69
|
+
result))))))
|
|
70
|
+
(export delay delay? force memoize realized?))
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
(module eliscript.function
|
|
2
|
+
(import-portable "./persistent-vector.eli"
|
|
3
|
+
empty-persistent-vector
|
|
4
|
+
persistent-vector-conj)
|
|
5
|
+
(defportable reverse-values
|
|
6
|
+
(values)
|
|
7
|
+
(let ((result (js-array)) (index 0))
|
|
8
|
+
(while (< index (js-length values))
|
|
9
|
+
(setq result (js-cons (aref values index) result) index (1+ index)))
|
|
10
|
+
result))
|
|
11
|
+
(defportable append-values
|
|
12
|
+
(left right)
|
|
13
|
+
(let ((result (js-array)) (index 0))
|
|
14
|
+
(while (< index (js-length left))
|
|
15
|
+
(setq result (js-cons (aref left index) result) index (1+ index)))
|
|
16
|
+
(setq index 0)
|
|
17
|
+
(while (< index (js-length right))
|
|
18
|
+
(setq result (js-cons (aref right index) result) index (1+ index)))
|
|
19
|
+
(reverse-values result)))
|
|
20
|
+
(defportable identity (value) value)
|
|
21
|
+
(defportable constantly (value) (lambda (&rest arguments) value))
|
|
22
|
+
(defportable complement
|
|
23
|
+
(predicate)
|
|
24
|
+
(lambda (&rest arguments) (not (apply predicate arguments))))
|
|
25
|
+
(defportable comp
|
|
26
|
+
(&rest functions)
|
|
27
|
+
(let ((length (js-length functions)))
|
|
28
|
+
(cond ((= length 0) identity)
|
|
29
|
+
((= length 1) (aref functions 0))
|
|
30
|
+
(t
|
|
31
|
+
(lambda
|
|
32
|
+
(&rest arguments)
|
|
33
|
+
(let ((index (1- length)) result)
|
|
34
|
+
(setq result (apply (aref functions index) arguments) index (1- index))
|
|
35
|
+
(while (>= index 0)
|
|
36
|
+
(setq result (funcall (aref functions index) result) index (1- index)))
|
|
37
|
+
result))))))
|
|
38
|
+
(defportable partial
|
|
39
|
+
(function &rest bound-arguments)
|
|
40
|
+
(if (= (js-length bound-arguments) 0)
|
|
41
|
+
function
|
|
42
|
+
(lambda
|
|
43
|
+
(&rest arguments)
|
|
44
|
+
(apply function (append-values bound-arguments arguments)))))
|
|
45
|
+
(defportable juxt
|
|
46
|
+
(&rest functions)
|
|
47
|
+
(lambda
|
|
48
|
+
(&rest arguments)
|
|
49
|
+
(let ((result (empty-persistent-vector)) (index 0))
|
|
50
|
+
(while (< index (js-length functions))
|
|
51
|
+
(setq
|
|
52
|
+
result
|
|
53
|
+
(persistent-vector-conj result (apply (aref functions index) arguments))
|
|
54
|
+
index
|
|
55
|
+
(1+ index)))
|
|
56
|
+
result)))
|
|
57
|
+
(defportable fnil
|
|
58
|
+
(function &rest defaults)
|
|
59
|
+
(lambda
|
|
60
|
+
(&rest arguments)
|
|
61
|
+
(let ((normalized (js-array)) (index 0))
|
|
62
|
+
(while (< index (js-length arguments))
|
|
63
|
+
(let ((value (aref arguments index)))
|
|
64
|
+
(setq
|
|
65
|
+
normalized
|
|
66
|
+
(js-cons
|
|
67
|
+
(if (and (< index (js-length defaults)) (nil? value))
|
|
68
|
+
(aref defaults index)
|
|
69
|
+
value)
|
|
70
|
+
normalized)
|
|
71
|
+
index
|
|
72
|
+
(1+ index))))
|
|
73
|
+
(apply function (reverse-values normalized)))))
|
|
74
|
+
(defportable every-pred
|
|
75
|
+
(&rest predicates)
|
|
76
|
+
(lambda
|
|
77
|
+
(&rest arguments)
|
|
78
|
+
(let ((predicate-index 0) (matched t))
|
|
79
|
+
(while (and matched (< predicate-index (js-length predicates)))
|
|
80
|
+
(let ((argument-index 0) (predicate (aref predicates predicate-index)))
|
|
81
|
+
(while (and matched (< argument-index (js-length arguments)))
|
|
82
|
+
(unless (funcall predicate (aref arguments argument-index))
|
|
83
|
+
(setq matched false))
|
|
84
|
+
(setq argument-index (1+ argument-index))))
|
|
85
|
+
(setq predicate-index (1+ predicate-index)))
|
|
86
|
+
matched)))
|
|
87
|
+
(defportable some-fn
|
|
88
|
+
(&rest predicates)
|
|
89
|
+
(lambda
|
|
90
|
+
(&rest arguments)
|
|
91
|
+
(let ((predicate-index 0) (argument-index 0) (found false) (result nil))
|
|
92
|
+
(while (and (not found) (< predicate-index (js-length predicates)))
|
|
93
|
+
(setq argument-index 0)
|
|
94
|
+
(while (and (not found) (< argument-index (js-length arguments)))
|
|
95
|
+
(let
|
|
96
|
+
(
|
|
97
|
+
(candidate
|
|
98
|
+
(funcall
|
|
99
|
+
(aref predicates predicate-index)
|
|
100
|
+
(aref arguments argument-index))))
|
|
101
|
+
(when candidate (setq found t result candidate)))
|
|
102
|
+
(setq argument-index (1+ argument-index)))
|
|
103
|
+
(setq predicate-index (1+ predicate-index)))
|
|
104
|
+
result)))
|
|
105
|
+
(defportable trampoline
|
|
106
|
+
(function &rest arguments)
|
|
107
|
+
(let ((result (apply function arguments)))
|
|
108
|
+
(while (= (value-type result) "function") (setq result (funcall result)))
|
|
109
|
+
result))
|
|
110
|
+
(export
|
|
111
|
+
comp
|
|
112
|
+
complement
|
|
113
|
+
constantly
|
|
114
|
+
every-pred
|
|
115
|
+
fnil
|
|
116
|
+
identity
|
|
117
|
+
juxt
|
|
118
|
+
partial
|
|
119
|
+
some-fn
|
|
120
|
+
trampoline))
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
(module eliscript.hierarchy
|
|
2
|
+
(import-portable "./persistent-map.eli"
|
|
3
|
+
persistent-map-assoc
|
|
4
|
+
persistent-map-dissoc
|
|
5
|
+
persistent-map-get
|
|
6
|
+
persistent-map-reduce)
|
|
7
|
+
(import-portable "./persistent-set.eli"
|
|
8
|
+
persistent-set-conj
|
|
9
|
+
persistent-set-count
|
|
10
|
+
persistent-set-disj
|
|
11
|
+
persistent-set-has?
|
|
12
|
+
persistent-set-reduce
|
|
13
|
+
persistent-set-union)
|
|
14
|
+
(import-portable "./persistent-vector.eli"
|
|
15
|
+
persistent-vector?
|
|
16
|
+
persistent-vector-count
|
|
17
|
+
persistent-vector-nth)
|
|
18
|
+
(import-portable "./value.eli" empty-value-map empty-value-set value-equal?)
|
|
19
|
+
(defconst hierarchy-registry (js* "new WeakMap()"))
|
|
20
|
+
(defconst hierarchy-missing (js-object))
|
|
21
|
+
(defun hierarchy-fail
|
|
22
|
+
(code message child parent)
|
|
23
|
+
(throw
|
|
24
|
+
(js-object
|
|
25
|
+
:kind
|
|
26
|
+
"eliscript/hierarchy-error"
|
|
27
|
+
:code
|
|
28
|
+
code
|
|
29
|
+
:message
|
|
30
|
+
message
|
|
31
|
+
:child
|
|
32
|
+
child
|
|
33
|
+
:parent
|
|
34
|
+
parent)))
|
|
35
|
+
(defun hierarchy?
|
|
36
|
+
(candidate)
|
|
37
|
+
(let ((type (value-type candidate)))
|
|
38
|
+
(if (or (= type "object") (= type "function"))
|
|
39
|
+
(try (js-call hierarchy-registry :has candidate) (catch _error false))
|
|
40
|
+
false)))
|
|
41
|
+
(defun hierarchy-state
|
|
42
|
+
(hierarchy)
|
|
43
|
+
(if (hierarchy? hierarchy)
|
|
44
|
+
(js-call hierarchy-registry :get hierarchy)
|
|
45
|
+
(hierarchy-fail "ELI-HIERARCHY-INVALID" "operation requires a hierarchy" nil nil)))
|
|
46
|
+
(defun hierarchy-close-ancestors
|
|
47
|
+
(direct-parents)
|
|
48
|
+
(let ((result direct-parents) (changed t))
|
|
49
|
+
(while changed
|
|
50
|
+
(setq changed false)
|
|
51
|
+
(setq
|
|
52
|
+
result
|
|
53
|
+
(persistent-map-reduce
|
|
54
|
+
(lambda
|
|
55
|
+
(next child known-ancestors)
|
|
56
|
+
(let
|
|
57
|
+
(
|
|
58
|
+
(expanded
|
|
59
|
+
(persistent-set-reduce
|
|
60
|
+
(lambda
|
|
61
|
+
(values ancestor)
|
|
62
|
+
(let
|
|
63
|
+
(
|
|
64
|
+
(ancestor-values
|
|
65
|
+
(persistent-map-get result ancestor hierarchy-missing)))
|
|
66
|
+
(if (eq ancestor-values hierarchy-missing)
|
|
67
|
+
values
|
|
68
|
+
(persistent-set-union values ancestor-values))))
|
|
69
|
+
known-ancestors
|
|
70
|
+
known-ancestors)))
|
|
71
|
+
(when
|
|
72
|
+
(>
|
|
73
|
+
(persistent-set-count expanded)
|
|
74
|
+
(persistent-set-count known-ancestors))
|
|
75
|
+
(setq changed t))
|
|
76
|
+
(persistent-map-assoc next child expanded)))
|
|
77
|
+
result
|
|
78
|
+
result)))
|
|
79
|
+
result))
|
|
80
|
+
(defun hierarchy-invert-ancestors
|
|
81
|
+
(ancestor-table)
|
|
82
|
+
(persistent-map-reduce
|
|
83
|
+
(lambda
|
|
84
|
+
(result child child-ancestors)
|
|
85
|
+
(persistent-set-reduce
|
|
86
|
+
(lambda
|
|
87
|
+
(next ancestor)
|
|
88
|
+
(let ((known (persistent-map-get next ancestor hierarchy-missing)))
|
|
89
|
+
(persistent-map-assoc
|
|
90
|
+
next
|
|
91
|
+
ancestor
|
|
92
|
+
(persistent-set-conj
|
|
93
|
+
(if (eq known hierarchy-missing) (empty-value-set) known)
|
|
94
|
+
child))))
|
|
95
|
+
result
|
|
96
|
+
child-ancestors))
|
|
97
|
+
(empty-value-map)
|
|
98
|
+
ancestor-table))
|
|
99
|
+
(defun hierarchy-register
|
|
100
|
+
(direct-parents ancestor-table descendant-table)
|
|
101
|
+
(let ((hierarchy (js-object)))
|
|
102
|
+
(js-call
|
|
103
|
+
hierarchy-registry
|
|
104
|
+
:set
|
|
105
|
+
hierarchy
|
|
106
|
+
(js-object
|
|
107
|
+
:parents
|
|
108
|
+
direct-parents
|
|
109
|
+
:ancestors
|
|
110
|
+
ancestor-table
|
|
111
|
+
:descendants
|
|
112
|
+
descendant-table))
|
|
113
|
+
hierarchy))
|
|
114
|
+
(defun hierarchy-rebuild
|
|
115
|
+
(direct-parents)
|
|
116
|
+
(let ((ancestor-table (hierarchy-close-ancestors direct-parents)))
|
|
117
|
+
(hierarchy-register
|
|
118
|
+
direct-parents
|
|
119
|
+
ancestor-table
|
|
120
|
+
(hierarchy-invert-ancestors ancestor-table))))
|
|
121
|
+
(defun hierarchy-update-transitive
|
|
122
|
+
(table source source-relations target target-relations)
|
|
123
|
+
(let*
|
|
124
|
+
((source-values (persistent-map-get source-relations source nil))
|
|
125
|
+
(target-values (persistent-map-get target-relations target nil))
|
|
126
|
+
(affected
|
|
127
|
+
(persistent-set-conj
|
|
128
|
+
(if source-values source-values (empty-value-set))
|
|
129
|
+
source))
|
|
130
|
+
(additions
|
|
131
|
+
(persistent-set-conj
|
|
132
|
+
(if target-values target-values (empty-value-set))
|
|
133
|
+
target)))
|
|
134
|
+
(persistent-set-reduce
|
|
135
|
+
(lambda
|
|
136
|
+
(result value)
|
|
137
|
+
(let ((known (persistent-map-get result value nil)))
|
|
138
|
+
(persistent-map-assoc
|
|
139
|
+
result
|
|
140
|
+
value
|
|
141
|
+
(persistent-set-union (if known known (empty-value-set)) additions))))
|
|
142
|
+
table
|
|
143
|
+
affected)))
|
|
144
|
+
(defun empty-hierarchy () (hierarchy-rebuild (empty-value-map)))
|
|
145
|
+
(defun hierarchy-relation
|
|
146
|
+
(hierarchy relation value)
|
|
147
|
+
(persistent-map-get (get (hierarchy-state hierarchy) relation nil) value nil))
|
|
148
|
+
(defun parents (hierarchy value) (hierarchy-relation hierarchy "parents" value))
|
|
149
|
+
(defun ancestors (hierarchy value) (hierarchy-relation hierarchy "ancestors" value))
|
|
150
|
+
(defun descendants
|
|
151
|
+
(hierarchy value)
|
|
152
|
+
(hierarchy-relation hierarchy "descendants" value))
|
|
153
|
+
(defun is-a?
|
|
154
|
+
(hierarchy child parent)
|
|
155
|
+
(hierarchy-state hierarchy)
|
|
156
|
+
(if (value-equal? child parent)
|
|
157
|
+
t
|
|
158
|
+
(let ((known-ancestors (ancestors hierarchy child)))
|
|
159
|
+
(if (and known-ancestors (persistent-set-has? known-ancestors parent))
|
|
160
|
+
t
|
|
161
|
+
(if
|
|
162
|
+
(and
|
|
163
|
+
(persistent-vector? child)
|
|
164
|
+
(persistent-vector? parent)
|
|
165
|
+
(= (persistent-vector-count child) (persistent-vector-count parent)))
|
|
166
|
+
(let ((matches t) (index 0))
|
|
167
|
+
(while (and matches (< index (persistent-vector-count child)))
|
|
168
|
+
(setq
|
|
169
|
+
matches
|
|
170
|
+
(is-a?
|
|
171
|
+
hierarchy
|
|
172
|
+
(persistent-vector-nth child index nil)
|
|
173
|
+
(persistent-vector-nth parent index nil))
|
|
174
|
+
index
|
|
175
|
+
(1+ index)))
|
|
176
|
+
matches)
|
|
177
|
+
false)))))
|
|
178
|
+
(defun derive
|
|
179
|
+
(hierarchy child parent)
|
|
180
|
+
(let*
|
|
181
|
+
((state (hierarchy-state hierarchy))
|
|
182
|
+
(direct-parents (get state "parents" nil))
|
|
183
|
+
(known (persistent-map-get direct-parents child hierarchy-missing)))
|
|
184
|
+
(when (value-equal? child parent)
|
|
185
|
+
(hierarchy-fail
|
|
186
|
+
"ELI-HIERARCHY-SELF-DERIVATION"
|
|
187
|
+
"a hierarchy value cannot derive from itself"
|
|
188
|
+
child
|
|
189
|
+
parent))
|
|
190
|
+
(if (and (not (eq known hierarchy-missing)) (persistent-set-has? known parent))
|
|
191
|
+
hierarchy
|
|
192
|
+
(progn
|
|
193
|
+
(when (is-a? hierarchy parent child)
|
|
194
|
+
(hierarchy-fail
|
|
195
|
+
"ELI-HIERARCHY-CYCLE"
|
|
196
|
+
"hierarchy derivation would create a cycle"
|
|
197
|
+
child
|
|
198
|
+
parent))
|
|
199
|
+
(let*
|
|
200
|
+
((ancestor-table (get state "ancestors" nil))
|
|
201
|
+
(descendant-table (get state "descendants" nil))
|
|
202
|
+
(next-parents
|
|
203
|
+
(persistent-map-assoc
|
|
204
|
+
direct-parents
|
|
205
|
+
child
|
|
206
|
+
(persistent-set-conj
|
|
207
|
+
(if (eq known hierarchy-missing) (empty-value-set) known)
|
|
208
|
+
parent)))
|
|
209
|
+
(next-ancestors
|
|
210
|
+
(hierarchy-update-transitive
|
|
211
|
+
ancestor-table
|
|
212
|
+
child
|
|
213
|
+
descendant-table
|
|
214
|
+
parent
|
|
215
|
+
ancestor-table))
|
|
216
|
+
(next-descendants
|
|
217
|
+
(hierarchy-update-transitive
|
|
218
|
+
descendant-table
|
|
219
|
+
parent
|
|
220
|
+
ancestor-table
|
|
221
|
+
child
|
|
222
|
+
descendant-table)))
|
|
223
|
+
(hierarchy-register next-parents next-ancestors next-descendants))))))
|
|
224
|
+
(defun underive
|
|
225
|
+
(hierarchy child parent)
|
|
226
|
+
(let*
|
|
227
|
+
((state (hierarchy-state hierarchy))
|
|
228
|
+
(direct-parents (get state "parents" nil))
|
|
229
|
+
(known (persistent-map-get direct-parents child hierarchy-missing)))
|
|
230
|
+
(if (or (eq known hierarchy-missing) (not (persistent-set-has? known parent)))
|
|
231
|
+
hierarchy
|
|
232
|
+
(let ((remaining (persistent-set-disj known parent)))
|
|
233
|
+
(hierarchy-rebuild
|
|
234
|
+
(if (= (persistent-set-count remaining) 0)
|
|
235
|
+
(persistent-map-dissoc direct-parents child)
|
|
236
|
+
(persistent-map-assoc direct-parents child remaining)))))))
|
|
237
|
+
(export
|
|
238
|
+
ancestors
|
|
239
|
+
derive
|
|
240
|
+
descendants
|
|
241
|
+
empty-hierarchy
|
|
242
|
+
hierarchy?
|
|
243
|
+
is-a?
|
|
244
|
+
parents
|
|
245
|
+
underive))
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
(module eliscript.identifier
|
|
2
|
+
(defconst portable-keyword-kind "eliscript/keyword")
|
|
3
|
+
(defconst portable-symbol-kind "eliscript/symbol")
|
|
4
|
+
|
|
5
|
+
(defportable identifier-valid-part? (value)
|
|
6
|
+
(if (or (not (= (value-type value) "string"))
|
|
7
|
+
(= (js-length value) 0))
|
|
8
|
+
false
|
|
9
|
+
(let ((valid t)
|
|
10
|
+
(index 0))
|
|
11
|
+
(while (and valid (< index (js-length value)))
|
|
12
|
+
(when (= (js-nth index value) "/")
|
|
13
|
+
(setq valid false))
|
|
14
|
+
(setq index (1+ index)))
|
|
15
|
+
valid)))
|
|
16
|
+
|
|
17
|
+
(defportable identifier-split-qualified (value)
|
|
18
|
+
(if (or (not (= (value-type value) "string"))
|
|
19
|
+
(= (js-length value) 0))
|
|
20
|
+
nil
|
|
21
|
+
(let ((separator -1)
|
|
22
|
+
(valid t)
|
|
23
|
+
(index 0))
|
|
24
|
+
(while (and valid (< index (js-length value)))
|
|
25
|
+
(when (= (js-nth index value) "/")
|
|
26
|
+
(if (= separator -1)
|
|
27
|
+
(setq separator index)
|
|
28
|
+
(setq valid false)))
|
|
29
|
+
(setq index (1+ index)))
|
|
30
|
+
(cond
|
|
31
|
+
((not valid) nil)
|
|
32
|
+
((= separator -1) (js-array nil value))
|
|
33
|
+
((or (= separator 0) (= separator (1- (js-length value)))) nil)
|
|
34
|
+
(t
|
|
35
|
+
(js-array (text-slice 0 separator value)
|
|
36
|
+
(text-slice (1+ separator) (js-length value) value)))))))
|
|
37
|
+
|
|
38
|
+
(defportable text-slice (start end text)
|
|
39
|
+
(let ((result "")
|
|
40
|
+
(index start))
|
|
41
|
+
(while (< index end)
|
|
42
|
+
(setq result (str result (js-nth index text))
|
|
43
|
+
index (1+ index)))
|
|
44
|
+
result))
|
|
45
|
+
|
|
46
|
+
(defportable portable-keyword? (value)
|
|
47
|
+
(and (not (nullish? value))
|
|
48
|
+
(object-has? value "kind")
|
|
49
|
+
(= (get value "kind" nil) portable-keyword-kind)
|
|
50
|
+
(object-has? value "namespace")
|
|
51
|
+
(object-has? value "name")
|
|
52
|
+
(or (nil? (get value "namespace" nil))
|
|
53
|
+
(identifier-valid-part? (get value "namespace" nil)))
|
|
54
|
+
(identifier-valid-part? (get value "name" nil))))
|
|
55
|
+
|
|
56
|
+
(defportable portable-symbol? (value)
|
|
57
|
+
(and (not (nullish? value))
|
|
58
|
+
(object-has? value "kind")
|
|
59
|
+
(= (get value "kind" nil) portable-symbol-kind)
|
|
60
|
+
(object-has? value "namespace")
|
|
61
|
+
(object-has? value "name")
|
|
62
|
+
(or (nil? (get value "namespace" nil))
|
|
63
|
+
(identifier-valid-part? (get value "namespace" nil)))
|
|
64
|
+
(identifier-valid-part? (get value "name" nil))))
|
|
65
|
+
|
|
66
|
+
(defportable keyword? (value)
|
|
67
|
+
(= (value-type value) "keyword"))
|
|
68
|
+
|
|
69
|
+
(defportable symbol? (value)
|
|
70
|
+
(= (value-type value) "symbol"))
|
|
71
|
+
|
|
72
|
+
(defportable identifier? (value)
|
|
73
|
+
(or (keyword? value) (symbol? value)))
|
|
74
|
+
|
|
75
|
+
(defportable identifier-name (value)
|
|
76
|
+
(if (identifier? value) (get value "name" nil) nil))
|
|
77
|
+
|
|
78
|
+
(defportable identifier-namespace (value)
|
|
79
|
+
(if (identifier? value) (get value "namespace" nil) nil))
|
|
80
|
+
|
|
81
|
+
(defportable qualified-name (value)
|
|
82
|
+
(if (not (identifier? value))
|
|
83
|
+
nil
|
|
84
|
+
(let ((namespace (identifier-namespace value))
|
|
85
|
+
(name (identifier-name value)))
|
|
86
|
+
(if (nil? namespace) name (str namespace "/" name)))))
|
|
87
|
+
|
|
88
|
+
(defportable normalize-identifier-arguments (arguments type)
|
|
89
|
+
(cond
|
|
90
|
+
((= (js-length arguments) 1)
|
|
91
|
+
(let ((value (js-nth 0 arguments)))
|
|
92
|
+
(if (= (value-type value) type)
|
|
93
|
+
(let ((namespace (get value "namespace" nil))
|
|
94
|
+
(name (get value "name" nil)))
|
|
95
|
+
(if (and (or (nil? namespace)
|
|
96
|
+
(identifier-valid-part? namespace))
|
|
97
|
+
(identifier-valid-part? name))
|
|
98
|
+
(js-object :existing value
|
|
99
|
+
:namespace namespace
|
|
100
|
+
:name name)
|
|
101
|
+
nil))
|
|
102
|
+
(let ((parts (identifier-split-qualified value)))
|
|
103
|
+
(if (nil? parts)
|
|
104
|
+
nil
|
|
105
|
+
(js-object :existing nil
|
|
106
|
+
:namespace (js-nth 0 parts)
|
|
107
|
+
:name (js-nth 1 parts)))))))
|
|
108
|
+
((= (js-length arguments) 2)
|
|
109
|
+
(let ((namespace (js-nth 0 arguments))
|
|
110
|
+
(name (js-nth 1 arguments)))
|
|
111
|
+
(if (and (or (nil? namespace)
|
|
112
|
+
(identifier-valid-part? namespace))
|
|
113
|
+
(identifier-valid-part? name))
|
|
114
|
+
(js-object :existing nil :namespace namespace :name name)
|
|
115
|
+
nil)))
|
|
116
|
+
(t nil)))
|
|
117
|
+
|
|
118
|
+
(defportable make-portable-identifier (kind normalized)
|
|
119
|
+
(if (nil? normalized)
|
|
120
|
+
nil
|
|
121
|
+
(let ((existing (get normalized "existing" nil)))
|
|
122
|
+
(if (not (nil? existing))
|
|
123
|
+
existing
|
|
124
|
+
(js-object :kind kind
|
|
125
|
+
:namespace (get normalized "namespace" nil)
|
|
126
|
+
:name (get normalized "name" nil))))))
|
|
127
|
+
|
|
128
|
+
(defportable keyword (&rest arguments)
|
|
129
|
+
(make-portable-identifier
|
|
130
|
+
portable-keyword-kind
|
|
131
|
+
(normalize-identifier-arguments arguments "keyword")))
|
|
132
|
+
|
|
133
|
+
(defportable symbol (&rest arguments)
|
|
134
|
+
(make-portable-identifier
|
|
135
|
+
portable-symbol-kind
|
|
136
|
+
(normalize-identifier-arguments arguments "symbol")))
|
|
137
|
+
|
|
138
|
+
(export identifier-name
|
|
139
|
+
identifier-namespace
|
|
140
|
+
identifier?
|
|
141
|
+
keyword
|
|
142
|
+
keyword?
|
|
143
|
+
portable-keyword?
|
|
144
|
+
portable-symbol?
|
|
145
|
+
qualified-name
|
|
146
|
+
symbol
|
|
147
|
+
symbol?))
|