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.
Files changed (195) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +135 -0
  3. package/bin/eliscript +16 -0
  4. package/bin/eliscript-bootstrap +15 -0
  5. package/bin/eliscript-build +15 -0
  6. package/bin/eliscript-check +15 -0
  7. package/bin/eliscript-eval +15 -0
  8. package/bin/eliscript-format +15 -0
  9. package/bin/eliscript-org +10 -0
  10. package/bin/eliscript-portable +17 -0
  11. package/bin/eliscript-seed +10 -0
  12. package/bin/eliscript-seed-build +10 -0
  13. package/bin/eliscript-watch +15 -0
  14. package/bootstrap/README.md +100 -0
  15. package/bootstrap/compiler/analyzer.eli +1426 -0
  16. package/bootstrap/compiler/compiler.eli +472 -0
  17. package/bootstrap/compiler/emitter.eli +1745 -0
  18. package/bootstrap/compiler/expander.eli +2522 -0
  19. package/bootstrap/compiler/formatter.eli +359 -0
  20. package/bootstrap/compiler/ir.eli +297 -0
  21. package/bootstrap/compiler/lower.eli +661 -0
  22. package/bootstrap/compiler/project.eli +1120 -0
  23. package/bootstrap/compiler/reader.eli +321 -0
  24. package/bootstrap/compiler/source-map.eli +165 -0
  25. package/bootstrap/compiler/symbol.eli +92 -0
  26. package/bootstrap/compiler/syntax.eli +54 -0
  27. package/bootstrap/compiler/transient-analysis.eli +825 -0
  28. package/bootstrap/host/build.mjs +28 -0
  29. package/bootstrap/host/bun.mjs +282 -0
  30. package/bootstrap/host/check-cli.mjs +163 -0
  31. package/bootstrap/host/eliscript-cli.mjs +36 -0
  32. package/bootstrap/host/evaluation-cli.mjs +401 -0
  33. package/bootstrap/host/evaluation.mjs +560 -0
  34. package/bootstrap/host/format-cli.mjs +139 -0
  35. package/bootstrap/host/project-cli.mjs +295 -0
  36. package/bootstrap/host/project.mjs +1080 -0
  37. package/bootstrap/host/watch-cli.mjs +129 -0
  38. package/bootstrap/host/watch.mjs +267 -0
  39. package/browser/host.mjs +357 -0
  40. package/browser/worker.mjs +60 -0
  41. package/compiler/README.md +118 -0
  42. package/compiler/eliscript-analyzer.el +716 -0
  43. package/compiler/eliscript-cli.el +108 -0
  44. package/compiler/eliscript-diagnostic.el +158 -0
  45. package/compiler/eliscript-emitter.el +1351 -0
  46. package/compiler/eliscript-evaluation.el +259 -0
  47. package/compiler/eliscript-expander.el +1630 -0
  48. package/compiler/eliscript-form.el +79 -0
  49. package/compiler/eliscript-ir-emitter.el +1460 -0
  50. package/compiler/eliscript-ir.el +356 -0
  51. package/compiler/eliscript-lower.el +463 -0
  52. package/compiler/eliscript-macro-eval.el +811 -0
  53. package/compiler/eliscript-parameters.el +324 -0
  54. package/compiler/eliscript-portable.el +492 -0
  55. package/compiler/eliscript-project-cli.el +156 -0
  56. package/compiler/eliscript-project.el +1676 -0
  57. package/compiler/eliscript-reader.el +421 -0
  58. package/compiler/eliscript-source-map.el +192 -0
  59. package/compiler/eliscript-symbol.el +101 -0
  60. package/compiler/eliscript-transient-analysis.el +817 -0
  61. package/compiler/eliscript.el +239 -0
  62. package/dist/bootstrap/analyzer.mjs +968 -0
  63. package/dist/bootstrap/analyzer.mjs.map +1 -0
  64. package/dist/bootstrap/compiler.mjs +228 -0
  65. package/dist/bootstrap/compiler.mjs.map +1 -0
  66. package/dist/bootstrap/emitter.mjs +1173 -0
  67. package/dist/bootstrap/emitter.mjs.map +1 -0
  68. package/dist/bootstrap/expander.mjs +1469 -0
  69. package/dist/bootstrap/expander.mjs.map +1 -0
  70. package/dist/bootstrap/formatter.mjs +304 -0
  71. package/dist/bootstrap/formatter.mjs.map +1 -0
  72. package/dist/bootstrap/ir.mjs +193 -0
  73. package/dist/bootstrap/ir.mjs.map +1 -0
  74. package/dist/bootstrap/lower.mjs +344 -0
  75. package/dist/bootstrap/lower.mjs.map +1 -0
  76. package/dist/bootstrap/project.mjs +585 -0
  77. package/dist/bootstrap/project.mjs.map +1 -0
  78. package/dist/bootstrap/reader.mjs +259 -0
  79. package/dist/bootstrap/reader.mjs.map +1 -0
  80. package/dist/bootstrap/source-map.mjs +154 -0
  81. package/dist/bootstrap/source-map.mjs.map +1 -0
  82. package/dist/bootstrap/symbol.mjs +74 -0
  83. package/dist/bootstrap/symbol.mjs.map +1 -0
  84. package/dist/bootstrap/syntax.mjs +38 -0
  85. package/dist/bootstrap/syntax.mjs.map +1 -0
  86. package/dist/bootstrap/transient-analysis.mjs +660 -0
  87. package/dist/bootstrap/transient-analysis.mjs.map +1 -0
  88. package/dist/browser/compiler.js +6491 -0
  89. package/editor/README.md +83 -0
  90. package/editor/eliscript-mode.el +878 -0
  91. package/editor/eliscript-repl.el +501 -0
  92. package/package.json +163 -0
  93. package/platform/README.md +48 -0
  94. package/platform/browser.mjs +204 -0
  95. package/platform/worker.mjs +166 -0
  96. package/runtime/README.md +296 -0
  97. package/runtime/core/collection-internals.mjs +512 -0
  98. package/runtime/core/collection.mjs +645 -0
  99. package/runtime/core/data-text.mjs +532 -0
  100. package/runtime/core/data.mjs +268 -0
  101. package/runtime/core/identifier.mjs +314 -0
  102. package/runtime/core/lazy-sequence.mjs +244 -0
  103. package/runtime/core/list-internals.mjs +18 -0
  104. package/runtime/core/list.mjs +273 -0
  105. package/runtime/core/map-internals.mjs +793 -0
  106. package/runtime/core/map.mjs +394 -0
  107. package/runtime/core/metadata-internals.mjs +14 -0
  108. package/runtime/core/metadata.mjs +44 -0
  109. package/runtime/core/object-impl.mjs +65 -0
  110. package/runtime/core/object-impl.mjs.map +1 -0
  111. package/runtime/core/object.mjs +13 -0
  112. package/runtime/core/order.mjs +234 -0
  113. package/runtime/core/persistent-kind.mjs +43 -0
  114. package/runtime/core/protocol-error.mjs +36 -0
  115. package/runtime/core/protocol-impl.mjs +326 -0
  116. package/runtime/core/protocol-impl.mjs.map +1 -0
  117. package/runtime/core/protocol.mjs +14 -0
  118. package/runtime/core/queue-internals.mjs +27 -0
  119. package/runtime/core/queue.mjs +238 -0
  120. package/runtime/core/record.mjs +346 -0
  121. package/runtime/core/regex.mjs +184 -0
  122. package/runtime/core/sequence.mjs +632 -0
  123. package/runtime/core/set-algebra.mjs +287 -0
  124. package/runtime/core/set-internals.mjs +33 -0
  125. package/runtime/core/set.mjs +378 -0
  126. package/runtime/core/sorted-internals.mjs +215 -0
  127. package/runtime/core/sorted.mjs +598 -0
  128. package/runtime/core/text-impl.mjs +95 -0
  129. package/runtime/core/text-impl.mjs.map +1 -0
  130. package/runtime/core/text.mjs +15 -0
  131. package/runtime/core/transducer.mjs +668 -0
  132. package/runtime/core/transient-internals.mjs +38 -0
  133. package/runtime/core/transient.mjs +53 -0
  134. package/runtime/core/truth.mjs +3 -0
  135. package/runtime/core/type.mjs +262 -0
  136. package/runtime/core/value-internals.mjs +188 -0
  137. package/runtime/core/value.mjs +93 -0
  138. package/runtime/core/vector-internals.mjs +102 -0
  139. package/runtime/core/vector.mjs +951 -0
  140. package/runtime/literals.mjs +34 -0
  141. package/runtime/source-mapping.mjs +220 -0
  142. package/runtime/testing/list.mjs +52 -0
  143. package/runtime/testing/map.mjs +145 -0
  144. package/runtime/testing/queue.mjs +49 -0
  145. package/runtime/testing/set.mjs +61 -0
  146. package/runtime/testing/sorted.mjs +105 -0
  147. package/runtime/testing/value.mjs +17 -0
  148. package/runtime/testing/vector.mjs +102 -0
  149. package/runtime/worker-value-codec.mjs +565 -0
  150. package/runtime/worker-value-stream.mjs +786 -0
  151. package/runtime/worker.mjs +975 -0
  152. package/stdlib/README.md +774 -0
  153. package/stdlib/bit.eli +36 -0
  154. package/stdlib/core/collection.eli +134 -0
  155. package/stdlib/core/data-text.eli +16 -0
  156. package/stdlib/core/data.eli +306 -0
  157. package/stdlib/core/identifier.eli +32 -0
  158. package/stdlib/core/lazy-sequence.eli +63 -0
  159. package/stdlib/core/metadata.eli +20 -0
  160. package/stdlib/core/object.eli +89 -0
  161. package/stdlib/core/order.eli +133 -0
  162. package/stdlib/core/protocol.eli +387 -0
  163. package/stdlib/core/record.eli +16 -0
  164. package/stdlib/core/regex.eli +17 -0
  165. package/stdlib/core/seq.eli +701 -0
  166. package/stdlib/core/set.eli +312 -0
  167. package/stdlib/core/sorted.eli +53 -0
  168. package/stdlib/core/text.eli +103 -0
  169. package/stdlib/core/transducer.eli +59 -0
  170. package/stdlib/core/transient.eli +24 -0
  171. package/stdlib/core/type.eli +19 -0
  172. package/stdlib/core/walk.eli +195 -0
  173. package/stdlib/core/zipper.eli +508 -0
  174. package/stdlib/data-text.eli +1092 -0
  175. package/stdlib/data.eli +42 -0
  176. package/stdlib/deferred.eli +70 -0
  177. package/stdlib/function.eli +120 -0
  178. package/stdlib/hierarchy.eli +245 -0
  179. package/stdlib/identifier.eli +147 -0
  180. package/stdlib/interop/js.eli +616 -0
  181. package/stdlib/json.eli +870 -0
  182. package/stdlib/metadata.eli +56 -0
  183. package/stdlib/multimethod.eli +464 -0
  184. package/stdlib/numeric.eli +193 -0
  185. package/stdlib/object.eli +96 -0
  186. package/stdlib/persistent-list.eli +145 -0
  187. package/stdlib/persistent-map.eli +724 -0
  188. package/stdlib/persistent-queue.eli +32 -0
  189. package/stdlib/persistent-set.eli +236 -0
  190. package/stdlib/persistent-vector.eli +342 -0
  191. package/stdlib/result.eli +141 -0
  192. package/stdlib/sequence.eli +106 -0
  193. package/stdlib/state/atom.eli +265 -0
  194. package/stdlib/text.eli +88 -0
  195. package/stdlib/value.eli +325 -0
@@ -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?))