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,195 @@
|
|
|
1
|
+
(module eliscript.core.walk
|
|
2
|
+
(import "../../runtime/core/collection.mjs" :as Collection)
|
|
3
|
+
(import "../../runtime/core/list.mjs" persistentList)
|
|
4
|
+
(import "../../runtime/core/metadata.mjs" meta withMeta)
|
|
5
|
+
(import "../../runtime/core/record.mjs" isRecord)
|
|
6
|
+
(import "../../runtime/core/value.mjs" equalValues)
|
|
7
|
+
(defconst walk-list-kind "list")
|
|
8
|
+
(defconst walk-vector-kind "vector")
|
|
9
|
+
(defconst walk-queue-kind "queue")
|
|
10
|
+
(defconst walk-record-kind "record")
|
|
11
|
+
(defconst walk-map-kind "map")
|
|
12
|
+
(defconst walk-set-kind "set")
|
|
13
|
+
(defun require-function
|
|
14
|
+
(value label)
|
|
15
|
+
(if (= (value-type value) "function")
|
|
16
|
+
value
|
|
17
|
+
(throw (new globalThis/TypeError (str label " must be a function")))))
|
|
18
|
+
(defun walk-collection-kind
|
|
19
|
+
(value)
|
|
20
|
+
(cond ((Collection/isList value) walk-list-kind)
|
|
21
|
+
((Collection/isVector value) walk-vector-kind)
|
|
22
|
+
((Collection/isQueue value) walk-queue-kind)
|
|
23
|
+
((isRecord value) walk-record-kind)
|
|
24
|
+
((Collection/isMap value) walk-map-kind)
|
|
25
|
+
((Collection/isSet value) walk-set-kind)
|
|
26
|
+
(t nil)))
|
|
27
|
+
(defun walk-children
|
|
28
|
+
(kind value)
|
|
29
|
+
(let ((children (js-array)))
|
|
30
|
+
(cond
|
|
31
|
+
(
|
|
32
|
+
(or
|
|
33
|
+
(= kind walk-list-kind)
|
|
34
|
+
(= kind walk-vector-kind)
|
|
35
|
+
(= kind walk-queue-kind)
|
|
36
|
+
(= kind walk-set-kind))
|
|
37
|
+
(Collection/reduce
|
|
38
|
+
value
|
|
39
|
+
(lambda (result child) (js-call result :push child) result)
|
|
40
|
+
children))
|
|
41
|
+
((or (= kind walk-record-kind) (= kind walk-map-kind))
|
|
42
|
+
(Collection/reduceKV
|
|
43
|
+
value
|
|
44
|
+
(lambda (result key child) (js-call result :push key child) result)
|
|
45
|
+
children)))
|
|
46
|
+
children))
|
|
47
|
+
(defun walk-children-identical?
|
|
48
|
+
(children transformed)
|
|
49
|
+
(let ((index 0) (identical t))
|
|
50
|
+
(while (and identical (< index (js-length children)))
|
|
51
|
+
(unless (eq (aref children index) (aref transformed index))
|
|
52
|
+
(setq identical false))
|
|
53
|
+
(setq index (1+ index)))
|
|
54
|
+
identical))
|
|
55
|
+
(defun walk-rebuild-linear
|
|
56
|
+
(value transformed)
|
|
57
|
+
(let ((result (Collection/empty value)) (index 0))
|
|
58
|
+
(while (< index (js-length transformed))
|
|
59
|
+
(setq result (Collection/conj result (aref transformed index)) index (1+ index)))
|
|
60
|
+
result))
|
|
61
|
+
(defun walk-rebuild-map
|
|
62
|
+
(value transformed)
|
|
63
|
+
(let ((result (Collection/empty value)) (index 0))
|
|
64
|
+
(while (< index (js-length transformed))
|
|
65
|
+
(setq
|
|
66
|
+
result
|
|
67
|
+
(Collection/assoc
|
|
68
|
+
result
|
|
69
|
+
(aref transformed index)
|
|
70
|
+
(aref transformed (1+ index)))
|
|
71
|
+
index
|
|
72
|
+
(+ index 2)))
|
|
73
|
+
result))
|
|
74
|
+
(defun walk-record-keys-identical?
|
|
75
|
+
(children transformed)
|
|
76
|
+
(let ((index 0) (identical t))
|
|
77
|
+
(while (and identical (< index (js-length children)))
|
|
78
|
+
(unless (equalValues (aref children index) (aref transformed index))
|
|
79
|
+
(setq identical false))
|
|
80
|
+
(setq index (+ index 2)))
|
|
81
|
+
identical))
|
|
82
|
+
(defun walk-rebuild-record
|
|
83
|
+
(value children transformed)
|
|
84
|
+
(if (not (walk-record-keys-identical? children transformed))
|
|
85
|
+
(walk-rebuild-map value transformed)
|
|
86
|
+
(let ((result value) (index 0))
|
|
87
|
+
(while (< index (js-length transformed))
|
|
88
|
+
(setq
|
|
89
|
+
result
|
|
90
|
+
(Collection/assoc
|
|
91
|
+
result
|
|
92
|
+
(aref transformed index)
|
|
93
|
+
(aref transformed (1+ index)))
|
|
94
|
+
index
|
|
95
|
+
(+ index 2)))
|
|
96
|
+
result)))
|
|
97
|
+
(defun walk-rebuild
|
|
98
|
+
(kind value children transformed)
|
|
99
|
+
(if (or (nil? kind) (walk-children-identical? children transformed))
|
|
100
|
+
value
|
|
101
|
+
(cond
|
|
102
|
+
((= kind walk-list-kind)
|
|
103
|
+
(withMeta (apply persistentList transformed) (meta value)))
|
|
104
|
+
((or (= kind walk-vector-kind) (= kind walk-queue-kind) (= kind walk-set-kind))
|
|
105
|
+
(walk-rebuild-linear value transformed))
|
|
106
|
+
((= kind walk-record-kind) (walk-rebuild-record value children transformed))
|
|
107
|
+
(t (walk-rebuild-map value transformed)))))
|
|
108
|
+
(defun walk-value
|
|
109
|
+
(inner outer form)
|
|
110
|
+
(let*
|
|
111
|
+
((kind (walk-collection-kind form))
|
|
112
|
+
(children (walk-children kind form))
|
|
113
|
+
(transformed (js-array))
|
|
114
|
+
(index 0))
|
|
115
|
+
(while (< index (js-length children))
|
|
116
|
+
(js-call transformed :push (funcall inner (aref children index)))
|
|
117
|
+
(setq index (1+ index)))
|
|
118
|
+
(funcall outer (walk-rebuild kind form children transformed))))
|
|
119
|
+
(defun walk
|
|
120
|
+
(inner outer form)
|
|
121
|
+
(walk-value
|
|
122
|
+
(require-function inner "walk inner")
|
|
123
|
+
(require-function outer "walk outer")
|
|
124
|
+
form))
|
|
125
|
+
(defun walk-frame
|
|
126
|
+
(value)
|
|
127
|
+
(let ((kind (walk-collection-kind value)))
|
|
128
|
+
(js-object
|
|
129
|
+
:value
|
|
130
|
+
value
|
|
131
|
+
:kind
|
|
132
|
+
kind
|
|
133
|
+
:children
|
|
134
|
+
(walk-children kind value)
|
|
135
|
+
:transformed
|
|
136
|
+
(js-array)
|
|
137
|
+
:index
|
|
138
|
+
0)))
|
|
139
|
+
(defun walk-tree
|
|
140
|
+
(transform form preorder)
|
|
141
|
+
(let*
|
|
142
|
+
((initial (if preorder (funcall transform form) form))
|
|
143
|
+
(stack (js-array (walk-frame initial)))
|
|
144
|
+
(result initial))
|
|
145
|
+
(while (> (js-length stack) 0)
|
|
146
|
+
(let*
|
|
147
|
+
((frame (aref stack (1- (js-length stack))))
|
|
148
|
+
(children (get frame :children))
|
|
149
|
+
(index (get frame :index)))
|
|
150
|
+
(if (< index (js-length children))
|
|
151
|
+
(progn
|
|
152
|
+
(put frame :index (1+ index))
|
|
153
|
+
(let ((child (aref children index)))
|
|
154
|
+
(js-call
|
|
155
|
+
stack
|
|
156
|
+
:push
|
|
157
|
+
(walk-frame (if preorder (funcall transform child) child)))))
|
|
158
|
+
(let
|
|
159
|
+
(
|
|
160
|
+
(completed
|
|
161
|
+
(walk-rebuild
|
|
162
|
+
(get frame :kind)
|
|
163
|
+
(get frame :value)
|
|
164
|
+
children
|
|
165
|
+
(get frame :transformed))))
|
|
166
|
+
(setq result (if preorder completed (funcall transform completed)))
|
|
167
|
+
(js-call stack :pop)
|
|
168
|
+
(when (> (js-length stack) 0)
|
|
169
|
+
(js-call
|
|
170
|
+
(get (aref stack (1- (js-length stack))) :transformed)
|
|
171
|
+
:push
|
|
172
|
+
result))))))
|
|
173
|
+
result))
|
|
174
|
+
(defun prewalk
|
|
175
|
+
(transform form)
|
|
176
|
+
(walk-tree (require-function transform "prewalk transform") form t))
|
|
177
|
+
(defun postwalk
|
|
178
|
+
(transform form)
|
|
179
|
+
(walk-tree (require-function transform "postwalk transform") form false))
|
|
180
|
+
(defun walk-replacer
|
|
181
|
+
(replacements label)
|
|
182
|
+
(when (not (Collection/isMap replacements))
|
|
183
|
+
(throw (new globalThis/TypeError (str label " replacements must be a map"))))
|
|
184
|
+
(lambda
|
|
185
|
+
(value)
|
|
186
|
+
(if (Collection/contains replacements value)
|
|
187
|
+
(Collection/get replacements value)
|
|
188
|
+
value)))
|
|
189
|
+
(defun prewalk-replace
|
|
190
|
+
(replacements form)
|
|
191
|
+
(prewalk (walk-replacer replacements "prewalk-replace") form))
|
|
192
|
+
(defun postwalk-replace
|
|
193
|
+
(replacements form)
|
|
194
|
+
(postwalk (walk-replacer replacements "postwalk-replace") form))
|
|
195
|
+
(export postwalk postwalk-replace prewalk prewalk-replace walk))
|
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
(module eliscript.core.zipper
|
|
2
|
+
(import "../../runtime/core/collection.mjs" :as Collection)
|
|
3
|
+
(import "../../runtime/core/list.mjs" EMPTY_LIST first persistentList rest)
|
|
4
|
+
(import "../../runtime/core/map.mjs" persistentHashMap)
|
|
5
|
+
(import "../../runtime/core/metadata.mjs" meta withMeta)
|
|
6
|
+
(import "../../runtime/core/vector.mjs" EMPTY_VECTOR persistentVector)
|
|
7
|
+
(defconst zipper-branch-key "eliscript.zipper/branch")
|
|
8
|
+
(defconst zipper-children-key "eliscript.zipper/children")
|
|
9
|
+
(defconst zipper-make-node-key "eliscript.zipper/make-node")
|
|
10
|
+
(defconst zipper-end-path "eliscript.zipper/end")
|
|
11
|
+
(defconst zipper-missing (js-object))
|
|
12
|
+
(defconst zipper-lefts-index 0)
|
|
13
|
+
(defconst zipper-rights-index 1)
|
|
14
|
+
(defconst zipper-parent-path-index 2)
|
|
15
|
+
(defconst zipper-parent-nodes-index 3)
|
|
16
|
+
(defconst zipper-changed-index 4)
|
|
17
|
+
(defun zipper-require-function
|
|
18
|
+
(value label)
|
|
19
|
+
(if (= (value-type value) "function")
|
|
20
|
+
value
|
|
21
|
+
(throw (new globalThis/TypeError (str label " must be a function")))))
|
|
22
|
+
(defun zipper-slot
|
|
23
|
+
(values target-index)
|
|
24
|
+
(let ((index 0) (box zipper-missing))
|
|
25
|
+
(Collection/reduce
|
|
26
|
+
values
|
|
27
|
+
(lambda
|
|
28
|
+
(state value)
|
|
29
|
+
(if (= index target-index)
|
|
30
|
+
(progn (setq box (js-object :value value)) (Collection/reduced state))
|
|
31
|
+
(progn (setq index (1+ index)) state)))
|
|
32
|
+
nil)
|
|
33
|
+
(if (eq box zipper-missing)
|
|
34
|
+
(throw (new globalThis/TypeError "invalid zipper internal slot"))
|
|
35
|
+
(get box :value))))
|
|
36
|
+
(defun zipper-path-value (location) (zipper-slot location 1))
|
|
37
|
+
(defun zipper-config-value (location key) (Collection/get (meta location) key))
|
|
38
|
+
(defun zipper-location
|
|
39
|
+
(node-value path-value configuration)
|
|
40
|
+
(withMeta (persistentVector node-value path-value) configuration))
|
|
41
|
+
(defun zipper-path
|
|
42
|
+
(left-values right-values parent-path parent-nodes changed)
|
|
43
|
+
(persistentVector left-values right-values parent-path parent-nodes changed))
|
|
44
|
+
(defun zipper-materialize
|
|
45
|
+
(values)
|
|
46
|
+
(if (nullish? values)
|
|
47
|
+
EMPTY_VECTOR
|
|
48
|
+
(Collection/reduce
|
|
49
|
+
values
|
|
50
|
+
(lambda (result value) (Collection/conj result value))
|
|
51
|
+
EMPTY_VECTOR)))
|
|
52
|
+
(defun zipper-right-list
|
|
53
|
+
(values start-index)
|
|
54
|
+
(let ((result EMPTY_LIST) (index (1- (Collection/count values))))
|
|
55
|
+
(while (>= index start-index)
|
|
56
|
+
(setq result (Collection/conj result (zipper-slot values index)))
|
|
57
|
+
(setq index (1- index)))
|
|
58
|
+
result))
|
|
59
|
+
(defun zipper-children-vector
|
|
60
|
+
(location)
|
|
61
|
+
(zipper-materialize
|
|
62
|
+
(funcall (zipper-config-value location zipper-children-key) (node location))))
|
|
63
|
+
(defun zipper-rebuild-children
|
|
64
|
+
(left-values node-value right-values)
|
|
65
|
+
(let
|
|
66
|
+
(
|
|
67
|
+
(result
|
|
68
|
+
(Collection/reduce
|
|
69
|
+
left-values
|
|
70
|
+
(lambda (values value) (Collection/conj values value))
|
|
71
|
+
EMPTY_VECTOR)))
|
|
72
|
+
(setq result (Collection/conj result node-value))
|
|
73
|
+
(Collection/reduce
|
|
74
|
+
right-values
|
|
75
|
+
(lambda (values value) (Collection/conj values value))
|
|
76
|
+
result)))
|
|
77
|
+
(defun zipper-mark-changed
|
|
78
|
+
(path-value)
|
|
79
|
+
(if (nil? path-value) nil (Collection/assoc path-value zipper-changed-index t)))
|
|
80
|
+
(defun zipper-active?
|
|
81
|
+
(location)
|
|
82
|
+
(not (= (zipper-path-value location) zipper-end-path)))
|
|
83
|
+
(defun zipper-require-location
|
|
84
|
+
(location label)
|
|
85
|
+
(if (zipper? location)
|
|
86
|
+
location
|
|
87
|
+
(throw (new globalThis/TypeError (str label " requires a zipper location")))))
|
|
88
|
+
(defun zipper-require-active-location
|
|
89
|
+
(location label)
|
|
90
|
+
(zipper-require-location location label)
|
|
91
|
+
(if (zipper-active? location)
|
|
92
|
+
location
|
|
93
|
+
(throw (new globalThis/TypeError (str label " cannot modify the end location")))))
|
|
94
|
+
(defun zipper-vector-node
|
|
95
|
+
(original children-values)
|
|
96
|
+
(withMeta children-values (meta original)))
|
|
97
|
+
(defun zipper-list-node
|
|
98
|
+
(original children-values)
|
|
99
|
+
(let ((values (js-array)))
|
|
100
|
+
(Collection/reduce
|
|
101
|
+
children-values
|
|
102
|
+
(lambda (result value) (js-call result :push value) result)
|
|
103
|
+
values)
|
|
104
|
+
(withMeta (apply persistentList values) (meta original))))
|
|
105
|
+
(defun zipper
|
|
106
|
+
(branch-predicate child-reader node-builder root-node)
|
|
107
|
+
(zipper-require-function branch-predicate "zipper branch predicate")
|
|
108
|
+
(zipper-require-function child-reader "zipper child reader")
|
|
109
|
+
(zipper-require-function node-builder "zipper node builder")
|
|
110
|
+
(let
|
|
111
|
+
(
|
|
112
|
+
(configuration
|
|
113
|
+
(persistentHashMap
|
|
114
|
+
(js-array zipper-branch-key branch-predicate)
|
|
115
|
+
(js-array zipper-children-key child-reader)
|
|
116
|
+
(js-array zipper-make-node-key node-builder))))
|
|
117
|
+
(zipper-location root-node nil configuration)))
|
|
118
|
+
(defun vector-zipper
|
|
119
|
+
(root-node)
|
|
120
|
+
(zipper Collection/isVector (lambda (value) value) zipper-vector-node root-node))
|
|
121
|
+
(defun list-zipper
|
|
122
|
+
(root-node)
|
|
123
|
+
(zipper Collection/isList (lambda (value) value) zipper-list-node root-node))
|
|
124
|
+
(defun zipper?
|
|
125
|
+
(value)
|
|
126
|
+
(if (and (Collection/isVector value) (= (Collection/count value) 2))
|
|
127
|
+
(let ((configuration (meta value)))
|
|
128
|
+
(and
|
|
129
|
+
(Collection/isMap configuration)
|
|
130
|
+
(= (value-type (Collection/get configuration zipper-branch-key)) "function")
|
|
131
|
+
(= (value-type (Collection/get configuration zipper-children-key)) "function")
|
|
132
|
+
(=
|
|
133
|
+
(value-type (Collection/get configuration zipper-make-node-key))
|
|
134
|
+
"function")))
|
|
135
|
+
false))
|
|
136
|
+
(defun node
|
|
137
|
+
(location)
|
|
138
|
+
(zipper-require-location location "node")
|
|
139
|
+
(zipper-slot location 0))
|
|
140
|
+
(defun end?
|
|
141
|
+
(location)
|
|
142
|
+
(zipper-require-location location "end?")
|
|
143
|
+
(= (zipper-path-value location) zipper-end-path))
|
|
144
|
+
(defun branch?
|
|
145
|
+
(location)
|
|
146
|
+
(zipper-require-location location "branch?")
|
|
147
|
+
(if (end? location)
|
|
148
|
+
false
|
|
149
|
+
(if (funcall (zipper-config-value location zipper-branch-key) (node location))
|
|
150
|
+
t
|
|
151
|
+
false)))
|
|
152
|
+
(defun children
|
|
153
|
+
(location)
|
|
154
|
+
(zipper-require-active-location location "children")
|
|
155
|
+
(if (branch? location)
|
|
156
|
+
(zipper-children-vector location)
|
|
157
|
+
(throw (new globalThis/TypeError "children requires a branch location"))))
|
|
158
|
+
(defun make-node
|
|
159
|
+
(location node-value children-values)
|
|
160
|
+
(zipper-require-active-location location "make-node")
|
|
161
|
+
(funcall
|
|
162
|
+
(zipper-config-value location zipper-make-node-key)
|
|
163
|
+
node-value
|
|
164
|
+
(zipper-materialize children-values)))
|
|
165
|
+
(defun path
|
|
166
|
+
(location)
|
|
167
|
+
(zipper-require-location location "path")
|
|
168
|
+
(let ((path-value (zipper-path-value location)))
|
|
169
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
170
|
+
nil
|
|
171
|
+
(zipper-slot path-value zipper-parent-nodes-index))))
|
|
172
|
+
(defun lefts
|
|
173
|
+
(location)
|
|
174
|
+
(zipper-require-location location "lefts")
|
|
175
|
+
(let ((path-value (zipper-path-value location)))
|
|
176
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
177
|
+
nil
|
|
178
|
+
(zipper-slot path-value zipper-lefts-index))))
|
|
179
|
+
(defun rights
|
|
180
|
+
(location)
|
|
181
|
+
(zipper-require-location location "rights")
|
|
182
|
+
(let ((path-value (zipper-path-value location)))
|
|
183
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
184
|
+
nil
|
|
185
|
+
(zipper-slot path-value zipper-rights-index))))
|
|
186
|
+
(defun down
|
|
187
|
+
(location)
|
|
188
|
+
(zipper-require-location location "down")
|
|
189
|
+
(if (or (end? location) (not (branch? location)))
|
|
190
|
+
nil
|
|
191
|
+
(let*
|
|
192
|
+
((child-values (zipper-children-vector location))
|
|
193
|
+
(child-count (Collection/count child-values)))
|
|
194
|
+
(if (= child-count 0)
|
|
195
|
+
nil
|
|
196
|
+
(let*
|
|
197
|
+
((path-value (zipper-path-value location))
|
|
198
|
+
(parent-nodes
|
|
199
|
+
(if (nil? path-value)
|
|
200
|
+
EMPTY_VECTOR
|
|
201
|
+
(zipper-slot path-value zipper-parent-nodes-index)))
|
|
202
|
+
(next-parent-nodes (Collection/conj parent-nodes (node location))))
|
|
203
|
+
(zipper-location
|
|
204
|
+
(zipper-slot child-values 0)
|
|
205
|
+
(zipper-path
|
|
206
|
+
EMPTY_VECTOR
|
|
207
|
+
(zipper-right-list child-values 1)
|
|
208
|
+
path-value
|
|
209
|
+
next-parent-nodes
|
|
210
|
+
false)
|
|
211
|
+
(meta location)))))))
|
|
212
|
+
(defun up
|
|
213
|
+
(location)
|
|
214
|
+
(zipper-require-location location "up")
|
|
215
|
+
(let ((path-value (zipper-path-value location)))
|
|
216
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
217
|
+
nil
|
|
218
|
+
(let*
|
|
219
|
+
((parent-path (zipper-slot path-value zipper-parent-path-index))
|
|
220
|
+
(parent-nodes (zipper-slot path-value zipper-parent-nodes-index))
|
|
221
|
+
(parent-node
|
|
222
|
+
(zipper-slot parent-nodes (1- (Collection/count parent-nodes)))))
|
|
223
|
+
(if (zipper-slot path-value zipper-changed-index)
|
|
224
|
+
(zipper-location
|
|
225
|
+
(make-node
|
|
226
|
+
location
|
|
227
|
+
parent-node
|
|
228
|
+
(zipper-rebuild-children
|
|
229
|
+
(zipper-slot path-value zipper-lefts-index)
|
|
230
|
+
(node location)
|
|
231
|
+
(zipper-slot path-value zipper-rights-index)))
|
|
232
|
+
(zipper-mark-changed parent-path)
|
|
233
|
+
(meta location))
|
|
234
|
+
(zipper-location parent-node parent-path (meta location)))))))
|
|
235
|
+
(defun right
|
|
236
|
+
(location)
|
|
237
|
+
(zipper-require-location location "right")
|
|
238
|
+
(let ((path-value (zipper-path-value location)))
|
|
239
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
240
|
+
nil
|
|
241
|
+
(let ((right-values (zipper-slot path-value zipper-rights-index)))
|
|
242
|
+
(if (= (Collection/count right-values) 0)
|
|
243
|
+
nil
|
|
244
|
+
(zipper-location
|
|
245
|
+
(first right-values)
|
|
246
|
+
(Collection/assoc
|
|
247
|
+
(Collection/assoc
|
|
248
|
+
path-value
|
|
249
|
+
zipper-lefts-index
|
|
250
|
+
(Collection/conj
|
|
251
|
+
(zipper-slot path-value zipper-lefts-index)
|
|
252
|
+
(node location)))
|
|
253
|
+
zipper-rights-index
|
|
254
|
+
(rest right-values))
|
|
255
|
+
(meta location)))))))
|
|
256
|
+
(defun left
|
|
257
|
+
(location)
|
|
258
|
+
(zipper-require-location location "left")
|
|
259
|
+
(let ((path-value (zipper-path-value location)))
|
|
260
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
261
|
+
nil
|
|
262
|
+
(let*
|
|
263
|
+
((left-values (zipper-slot path-value zipper-lefts-index))
|
|
264
|
+
(left-count (Collection/count left-values)))
|
|
265
|
+
(if (= left-count 0)
|
|
266
|
+
nil
|
|
267
|
+
(zipper-location
|
|
268
|
+
(zipper-slot left-values (1- left-count))
|
|
269
|
+
(Collection/assoc
|
|
270
|
+
(Collection/assoc
|
|
271
|
+
path-value
|
|
272
|
+
zipper-lefts-index
|
|
273
|
+
(Collection/pop left-values))
|
|
274
|
+
zipper-rights-index
|
|
275
|
+
(Collection/conj
|
|
276
|
+
(zipper-slot path-value zipper-rights-index)
|
|
277
|
+
(node location)))
|
|
278
|
+
(meta location)))))))
|
|
279
|
+
(defun rightmost
|
|
280
|
+
(location)
|
|
281
|
+
(zipper-require-location location "rightmost")
|
|
282
|
+
(let ((path-value (zipper-path-value location)))
|
|
283
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
284
|
+
location
|
|
285
|
+
(let*
|
|
286
|
+
(
|
|
287
|
+
(right-values
|
|
288
|
+
(zipper-materialize (zipper-slot path-value zipper-rights-index)))
|
|
289
|
+
(right-count (Collection/count right-values)))
|
|
290
|
+
(if (= right-count 0)
|
|
291
|
+
location
|
|
292
|
+
(let
|
|
293
|
+
(
|
|
294
|
+
(next-lefts
|
|
295
|
+
(Collection/conj
|
|
296
|
+
(zipper-slot path-value zipper-lefts-index)
|
|
297
|
+
(node location)))
|
|
298
|
+
(index 0))
|
|
299
|
+
(while (< index (1- right-count))
|
|
300
|
+
(setq
|
|
301
|
+
next-lefts
|
|
302
|
+
(Collection/conj next-lefts (zipper-slot right-values index)))
|
|
303
|
+
(setq index (1+ index)))
|
|
304
|
+
(zipper-location
|
|
305
|
+
(zipper-slot right-values (1- right-count))
|
|
306
|
+
(Collection/assoc
|
|
307
|
+
(Collection/assoc path-value zipper-lefts-index next-lefts)
|
|
308
|
+
zipper-rights-index
|
|
309
|
+
EMPTY_LIST)
|
|
310
|
+
(meta location))))))))
|
|
311
|
+
(defun leftmost
|
|
312
|
+
(location)
|
|
313
|
+
(zipper-require-location location "leftmost")
|
|
314
|
+
(let ((path-value (zipper-path-value location)))
|
|
315
|
+
(if (or (nil? path-value) (= path-value zipper-end-path))
|
|
316
|
+
location
|
|
317
|
+
(let*
|
|
318
|
+
((left-values (zipper-slot path-value zipper-lefts-index))
|
|
319
|
+
(left-count (Collection/count left-values)))
|
|
320
|
+
(if (= left-count 0)
|
|
321
|
+
location
|
|
322
|
+
(let
|
|
323
|
+
(
|
|
324
|
+
(next-rights
|
|
325
|
+
(Collection/conj
|
|
326
|
+
(zipper-slot path-value zipper-rights-index)
|
|
327
|
+
(node location)))
|
|
328
|
+
(index (1- left-count)))
|
|
329
|
+
(while (> index 0)
|
|
330
|
+
(setq
|
|
331
|
+
next-rights
|
|
332
|
+
(Collection/conj next-rights (zipper-slot left-values index)))
|
|
333
|
+
(setq index (1- index)))
|
|
334
|
+
(zipper-location
|
|
335
|
+
(zipper-slot left-values 0)
|
|
336
|
+
(Collection/assoc
|
|
337
|
+
(Collection/assoc path-value zipper-lefts-index EMPTY_VECTOR)
|
|
338
|
+
zipper-rights-index
|
|
339
|
+
next-rights)
|
|
340
|
+
(meta location))))))))
|
|
341
|
+
(defun root
|
|
342
|
+
(location)
|
|
343
|
+
(zipper-require-location location "root")
|
|
344
|
+
(if (end? location)
|
|
345
|
+
(node location)
|
|
346
|
+
(let ((current location) (parent (up location)))
|
|
347
|
+
(while (not (nil? parent)) (setq current parent parent (up current)))
|
|
348
|
+
(node current))))
|
|
349
|
+
(defun insert-left
|
|
350
|
+
(location item)
|
|
351
|
+
(zipper-require-active-location location "insert-left")
|
|
352
|
+
(let ((path-value (zipper-path-value location)))
|
|
353
|
+
(if (nil? path-value)
|
|
354
|
+
(throw (new globalThis/TypeError "insert-left cannot insert at the root"))
|
|
355
|
+
(zipper-location
|
|
356
|
+
(node location)
|
|
357
|
+
(Collection/assoc
|
|
358
|
+
(Collection/assoc path-value zipper-changed-index t)
|
|
359
|
+
zipper-lefts-index
|
|
360
|
+
(Collection/conj (zipper-slot path-value zipper-lefts-index) item))
|
|
361
|
+
(meta location)))))
|
|
362
|
+
(defun insert-right
|
|
363
|
+
(location item)
|
|
364
|
+
(zipper-require-active-location location "insert-right")
|
|
365
|
+
(let ((path-value (zipper-path-value location)))
|
|
366
|
+
(if (nil? path-value)
|
|
367
|
+
(throw (new globalThis/TypeError "insert-right cannot insert at the root"))
|
|
368
|
+
(zipper-location
|
|
369
|
+
(node location)
|
|
370
|
+
(Collection/assoc
|
|
371
|
+
(Collection/assoc path-value zipper-changed-index t)
|
|
372
|
+
zipper-rights-index
|
|
373
|
+
(Collection/conj (zipper-slot path-value zipper-rights-index) item))
|
|
374
|
+
(meta location)))))
|
|
375
|
+
(defun replace
|
|
376
|
+
(location replacement)
|
|
377
|
+
(zipper-require-active-location location "replace")
|
|
378
|
+
(zipper-location
|
|
379
|
+
replacement
|
|
380
|
+
(zipper-mark-changed (zipper-path-value location))
|
|
381
|
+
(meta location)))
|
|
382
|
+
(defun edit
|
|
383
|
+
(location transform &rest arguments)
|
|
384
|
+
(zipper-require-active-location location "edit")
|
|
385
|
+
(zipper-require-function transform "edit transform")
|
|
386
|
+
(js-call arguments :unshift (node location))
|
|
387
|
+
(replace location (apply transform arguments)))
|
|
388
|
+
(defun insert-child
|
|
389
|
+
(location item)
|
|
390
|
+
(zipper-require-active-location location "insert-child")
|
|
391
|
+
(let
|
|
392
|
+
(
|
|
393
|
+
(next-children
|
|
394
|
+
(Collection/reduce
|
|
395
|
+
(children location)
|
|
396
|
+
(lambda (result value) (Collection/conj result value))
|
|
397
|
+
(persistentVector item))))
|
|
398
|
+
(replace location (make-node location (node location) next-children))))
|
|
399
|
+
(defun append-child
|
|
400
|
+
(location item)
|
|
401
|
+
(zipper-require-active-location location "append-child")
|
|
402
|
+
(replace
|
|
403
|
+
location
|
|
404
|
+
(make-node location (node location) (Collection/conj (children location) item))))
|
|
405
|
+
(defun zipper-deepest-right
|
|
406
|
+
(location)
|
|
407
|
+
(let ((current location) (searching t))
|
|
408
|
+
(while searching
|
|
409
|
+
(let ((child (if (branch? current) (down current) nil)))
|
|
410
|
+
(if (nil? child) (setq searching false) (setq current (rightmost child)))))
|
|
411
|
+
current))
|
|
412
|
+
(defun remove
|
|
413
|
+
(location)
|
|
414
|
+
(zipper-require-active-location location "remove")
|
|
415
|
+
(let ((path-value (zipper-path-value location)))
|
|
416
|
+
(if (nil? path-value)
|
|
417
|
+
(throw (new globalThis/TypeError "remove cannot remove the root"))
|
|
418
|
+
(let*
|
|
419
|
+
((left-values (zipper-slot path-value zipper-lefts-index))
|
|
420
|
+
(left-count (Collection/count left-values)))
|
|
421
|
+
(if (> left-count 0)
|
|
422
|
+
(zipper-deepest-right
|
|
423
|
+
(zipper-location
|
|
424
|
+
(zipper-slot left-values (1- left-count))
|
|
425
|
+
(Collection/assoc
|
|
426
|
+
(Collection/assoc
|
|
427
|
+
path-value
|
|
428
|
+
zipper-lefts-index
|
|
429
|
+
(Collection/pop left-values))
|
|
430
|
+
zipper-changed-index
|
|
431
|
+
t)
|
|
432
|
+
(meta location)))
|
|
433
|
+
(let*
|
|
434
|
+
((parent-path (zipper-slot path-value zipper-parent-path-index))
|
|
435
|
+
(parent-nodes (zipper-slot path-value zipper-parent-nodes-index))
|
|
436
|
+
(parent-node
|
|
437
|
+
(zipper-slot parent-nodes (1- (Collection/count parent-nodes)))))
|
|
438
|
+
(zipper-location
|
|
439
|
+
(make-node
|
|
440
|
+
location
|
|
441
|
+
parent-node
|
|
442
|
+
(zipper-slot path-value zipper-rights-index))
|
|
443
|
+
(zipper-mark-changed parent-path)
|
|
444
|
+
(meta location))))))))
|
|
445
|
+
(defun next
|
|
446
|
+
(location)
|
|
447
|
+
(zipper-require-location location "next")
|
|
448
|
+
(if (end? location)
|
|
449
|
+
location
|
|
450
|
+
(let ((child (if (branch? location) (down location) nil)))
|
|
451
|
+
(if (not (nil? child))
|
|
452
|
+
child
|
|
453
|
+
(let ((sibling (right location)))
|
|
454
|
+
(if (not (nil? sibling))
|
|
455
|
+
sibling
|
|
456
|
+
(let ((current location) (result nil) (searching t))
|
|
457
|
+
(while searching
|
|
458
|
+
(let ((parent (up current)))
|
|
459
|
+
(if (nil? parent)
|
|
460
|
+
(progn
|
|
461
|
+
(setq
|
|
462
|
+
result
|
|
463
|
+
(zipper-location
|
|
464
|
+
(node current)
|
|
465
|
+
zipper-end-path
|
|
466
|
+
(meta location)))
|
|
467
|
+
(setq searching false))
|
|
468
|
+
(let ((parent-sibling (right parent)))
|
|
469
|
+
(if (nil? parent-sibling)
|
|
470
|
+
(setq current parent)
|
|
471
|
+
(setq result parent-sibling searching false))))))
|
|
472
|
+
result)))))))
|
|
473
|
+
(defun prev
|
|
474
|
+
(location)
|
|
475
|
+
(zipper-require-location location "prev")
|
|
476
|
+
(if (end? location)
|
|
477
|
+
(zipper-deepest-right (zipper-location (node location) nil (meta location)))
|
|
478
|
+
(let ((sibling (left location)))
|
|
479
|
+
(if (nil? sibling) (up location) (zipper-deepest-right sibling)))))
|
|
480
|
+
(export
|
|
481
|
+
append-child
|
|
482
|
+
branch?
|
|
483
|
+
children
|
|
484
|
+
down
|
|
485
|
+
edit
|
|
486
|
+
end?
|
|
487
|
+
insert-child
|
|
488
|
+
insert-left
|
|
489
|
+
insert-right
|
|
490
|
+
left
|
|
491
|
+
leftmost
|
|
492
|
+
lefts
|
|
493
|
+
list-zipper
|
|
494
|
+
make-node
|
|
495
|
+
next
|
|
496
|
+
node
|
|
497
|
+
path
|
|
498
|
+
prev
|
|
499
|
+
remove
|
|
500
|
+
replace
|
|
501
|
+
right
|
|
502
|
+
rightmost
|
|
503
|
+
rights
|
|
504
|
+
root
|
|
505
|
+
up
|
|
506
|
+
vector-zipper
|
|
507
|
+
zipper
|
|
508
|
+
zipper?))
|