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,133 @@
1
+ (module eliscript.core.order
2
+ (import "../../runtime/core/collection.mjs" reduce)
3
+ (import "../../runtime/core/order.mjs" :as Runtime)
4
+ (import "../../runtime/core/transducer.mjs" into)
5
+ (import "../../runtime/core/truth.mjs" isTruthy)
6
+ (import "../../runtime/core/vector.mjs" EMPTY_VECTOR)
7
+ (defconst IComparable Runtime/IComparable)
8
+ (defun require-function
9
+ (value label)
10
+ (if (= (value-type value) "function")
11
+ value
12
+ (throw (new globalThis/TypeError (str label " must be a function")))))
13
+ (defun finite-number?
14
+ (value)
15
+ (and (= (value-type value) "number") (js-call globalThis/Number :isFinite value)))
16
+ (defun normalize-comparison-result
17
+ (value label)
18
+ (if (finite-number? value)
19
+ (if (< value 0) -1 (if (> value 0) 1 0))
20
+ (throw (new globalThis/TypeError (str label " must return a finite number")))))
21
+ (defun compare-values (left right) (Runtime/compareValues left right))
22
+ (defun comparator
23
+ (comparison)
24
+ (require-function comparison "comparator comparison")
25
+ (lambda
26
+ (left right)
27
+ (let ((result (funcall comparison left right)))
28
+ (if (= (value-type result) "number")
29
+ (normalize-comparison-result result "comparator comparison")
30
+ (if (isTruthy result) -1 (if (isTruthy (funcall comparison right left)) 1 0))))))
31
+ (defun reverse-comparator
32
+ (&rest arguments)
33
+ (let ((length (js-length arguments)))
34
+ (if (> length 1)
35
+ (throw
36
+ (new globalThis/TypeError "reverseComparator expects zero or one comparator"))
37
+ (let
38
+ (
39
+ (normalized
40
+ (comparator (if (= length 0) compare-values (js-nth 0 arguments)))))
41
+ (lambda (left right) (funcall normalized right left))))))
42
+ (defun collect-decorated
43
+ (collection key-function)
44
+ (reduce
45
+ collection
46
+ (lambda
47
+ (result value)
48
+ (js-call
49
+ result
50
+ :push
51
+ (js-object
52
+ :index
53
+ (js-length result)
54
+ :key
55
+ (funcall key-function value)
56
+ :value
57
+ value))
58
+ result)
59
+ (js-array)))
60
+ (defun sort-decorated
61
+ (values comparison)
62
+ (js-call
63
+ values
64
+ :sort
65
+ (lambda
66
+ (left right)
67
+ (let ((result (funcall comparison (get left :key) (get right :key))))
68
+ (if (= result 0) (- (get left :index) (get right :index)) result))))
69
+ (into EMPTY_VECTOR (js-call values :map (lambda (entry) (get entry :value)))))
70
+ (defun sort
71
+ (&rest arguments)
72
+ (let ((length (js-length arguments)))
73
+ (if (and (/= length 1) (/= length 2))
74
+ (throw
75
+ (new
76
+ globalThis/TypeError
77
+ "sort expects a collection or comparator and collection"))
78
+ (let
79
+ ((comparison (if (= length 1) compare-values (js-nth 0 arguments)))
80
+ (collection (js-nth (1- length) arguments)))
81
+ (sort-decorated
82
+ (collect-decorated collection (lambda (value) value))
83
+ (comparator comparison))))))
84
+ (defun sort-by
85
+ (key-function &rest arguments)
86
+ (require-function key-function "sortBy key function")
87
+ (let ((length (js-length arguments)))
88
+ (if (and (/= length 1) (/= length 2))
89
+ (throw
90
+ (new
91
+ globalThis/TypeError
92
+ "sortBy expects a key function, optional comparator, and collection"))
93
+ (let
94
+ ((comparison (if (= length 1) compare-values (js-nth 0 arguments)))
95
+ (collection (js-nth (1- length) arguments)))
96
+ (sort-decorated
97
+ (collect-decorated collection key-function)
98
+ (comparator comparison))))))
99
+ (defun extreme-key
100
+ (label direction key-function values)
101
+ (require-function key-function (str label " key function"))
102
+ (if (= (js-length values) 0)
103
+ (throw
104
+ (new
105
+ globalThis/TypeError
106
+ (str label " expects a key function and at least one value")))
107
+ (let
108
+ ((selected (js-nth 0 values))
109
+ (selected-key (funcall key-function (js-nth 0 values)))
110
+ (index 1))
111
+ (while (< index (js-length values))
112
+ (let*
113
+ ((candidate (js-nth index values))
114
+ (candidate-key (funcall key-function candidate)))
115
+ (when (>= (* (compare-values candidate-key selected-key) direction) 0)
116
+ (setq selected candidate selected-key candidate-key)))
117
+ (setq index (1+ index)))
118
+ selected)))
119
+ (defun min-key
120
+ (key-function &rest values)
121
+ (extreme-key "minKey" -1 key-function values))
122
+ (defun max-key
123
+ (key-function &rest values)
124
+ (extreme-key "maxKey" 1 key-function values))
125
+ (export
126
+ IComparable
127
+ comparator
128
+ compare-values
129
+ max-key
130
+ min-key
131
+ reverse-comparator
132
+ sort
133
+ sort-by))
@@ -0,0 +1,387 @@
1
+ (module eliscript.core.protocol
2
+ (import "../../runtime/core/protocol-error.mjs" ProtocolDispatchError)
3
+
4
+ (defconst protocol-states (js* "new WeakMap()"))
5
+ (defconst protocol-missing (js* "Symbol(\"eliscript.protocol.missing\")"))
6
+ (defconst protocol-host-categories
7
+ (js-array "null" "undefined" "boolean" "number" "bigint" "string"
8
+ "symbol" "function" "object"))
9
+ (defconst protocol-definition-format "eliscript-protocol-definition")
10
+ (defconst protocol-definition-version 1)
11
+ (defconst protocol-max-operations 1024)
12
+
13
+ ;; These capabilities isolate host reflection from dispatch policy. They are
14
+ ;; deliberately private and contain no protocol-specific decisions.
15
+ (defconst protocol-symbol (js* "(description) => Symbol(description)"))
16
+ (defconst protocol-array? (js* "Array.isArray"))
17
+ (defconst protocol-map (js* "() => new Map()"))
18
+ (defconst protocol-null-object (js* "() => Object.create(null)"))
19
+ (defconst protocol-own-descriptors
20
+ (js* "Object.getOwnPropertyDescriptors"))
21
+ (defconst protocol-own-keys (js* "Reflect.ownKeys"))
22
+ (defconst protocol-has-own
23
+ (js* "(value, key) => Object.prototype.hasOwnProperty.call(value, key)"))
24
+ (defconst protocol-property-present
25
+ (js* "(value, key) => { if (value === null || value === undefined) return false; return Reflect.has(Object(value), key); }"))
26
+ (defconst protocol-property-value
27
+ (js* "(value, key) => Object(value)[key]"))
28
+ (defconst protocol-host-type (js* "(value) => typeof value"))
29
+ (defconst protocol-exact-prototype
30
+ (js* "(value, missing) => ((typeof value === \"object\" && value !== null) || typeof value === \"function\") ? Object.getPrototypeOf(value) : missing"))
31
+ (defconst protocol-object-prototype (js* "Object.prototype"))
32
+ (defconst protocol-get-prototype (js* "Object.getPrototypeOf"))
33
+ (defconst protocol-freeze (js* "Object.freeze"))
34
+ (defconst protocol-define-enumerable
35
+ (js* "(target, key, value) => { Object.defineProperty(target, key, { value, enumerable: true }); return target; }"))
36
+ (defconst protocol-define-metadata
37
+ (js* "(target, protocol, operation, slot) => { Object.defineProperties(target, { protocol: { value: protocol }, operation: { value: operation }, slot: { value: slot } }); return target; }"))
38
+
39
+ (defun protocol-fail (message)
40
+ (throw (new globalThis/TypeError message)))
41
+
42
+ (defun protocol-state (protocol)
43
+ (let ((state (js-call protocol-states :get protocol)))
44
+ (if (undefined? state)
45
+ (protocol-fail "expected an Eliscript protocol")
46
+ state)))
47
+
48
+ (defun normalize-name (value label)
49
+ (if (or (not (= (value-type value) "string"))
50
+ (= (js-length value) 0))
51
+ (protocol-fail (str label " must be a non-empty string"))
52
+ value))
53
+
54
+ (defun normalize-protocol-operations (operations)
55
+ (when (not (funcall protocol-array? operations))
56
+ (protocol-fail "protocol operations must be a non-empty dense array"))
57
+ (let ((length (js-length operations)))
58
+ (when (= length 0)
59
+ (protocol-fail "protocol operations must be a non-empty dense array"))
60
+ (when (> length protocol-max-operations)
61
+ (protocol-fail
62
+ (str "protocol operations exceed " protocol-max-operations)))
63
+ (let* ((descriptors (funcall protocol-own-descriptors operations))
64
+ (keys (funcall protocol-own-keys descriptors))
65
+ (normalized (js-array))
66
+ (index 0))
67
+ ;; A dense Array has one own descriptor per item plus "length". This
68
+ ;; also rejects Symbol keys and named side-channel properties.
69
+ (when (/= (js-length keys) (1+ length))
70
+ (protocol-fail
71
+ "protocol operations must be a dense array with no extra properties"))
72
+ (while (< index length)
73
+ (let ((descriptor (get descriptors (str index) undefined)))
74
+ (when (or (undefined? descriptor)
75
+ (not (funcall protocol-has-own descriptor "value"))
76
+ (not (get descriptor :enumerable false)))
77
+ (protocol-fail
78
+ "protocol operations must contain enumerable data values"))
79
+ (js-call normalized :push
80
+ (normalize-name (get descriptor :value)
81
+ "protocol operation")))
82
+ (setq index (1+ index)))
83
+ normalized)))
84
+
85
+ (defun protocol-definition-field (descriptors name)
86
+ (let ((descriptor (get descriptors name undefined)))
87
+ (when (or (undefined? descriptor)
88
+ (not (funcall protocol-has-own descriptor "value"))
89
+ (not (get descriptor :enumerable false)))
90
+ (protocol-fail
91
+ (str "protocol definition field " name
92
+ " must be an enumerable data property")))
93
+ (get descriptor :value)))
94
+
95
+ (defun normalize-protocol-definition (definition)
96
+ (when (or (nil? definition)
97
+ (not (= (value-type definition) "object"))
98
+ (funcall protocol-array? definition))
99
+ (protocol-fail "protocol definition must be a plain object"))
100
+ (let ((prototype (funcall protocol-get-prototype definition)))
101
+ (when (and (/= prototype protocol-object-prototype)
102
+ (not (nil? prototype)))
103
+ (protocol-fail "protocol definition must be a plain object")))
104
+ (let* ((descriptors (funcall protocol-own-descriptors definition))
105
+ (keys (funcall protocol-own-keys descriptors)))
106
+ (when (/= (js-length keys) 4)
107
+ (protocol-fail
108
+ "protocol definition must contain exactly format, version, name, and operations"))
109
+ (let ((format (protocol-definition-field descriptors "format"))
110
+ (version (protocol-definition-field descriptors "version"))
111
+ (name (protocol-definition-field descriptors "name"))
112
+ (operations (protocol-definition-field descriptors "operations")))
113
+ (when (/= format protocol-definition-format)
114
+ (protocol-fail (str "unsupported protocol definition format " format)))
115
+ (when (/= version protocol-definition-version)
116
+ (protocol-fail (str "unsupported protocol definition version " version)))
117
+ (js-object :name (normalize-name name "protocol name")
118
+ :operations (normalize-protocol-operations operations)))))
119
+
120
+ (defun operation-record (protocol operation)
121
+ (let* ((state (protocol-state protocol))
122
+ (record (js-call (get state :operations) :get operation)))
123
+ (if (undefined? record)
124
+ (protocol-fail
125
+ (str "protocol " (get protocol :name) " has no operation " operation))
126
+ record)))
127
+
128
+ (defun protocol-host-category (value)
129
+ (if (nil? value) "null" (funcall protocol-host-type value)))
130
+
131
+ (defun known-host-category? (category)
132
+ (let ((index 0)
133
+ (found false))
134
+ (while (and (< index (js-length protocol-host-categories)) (not found))
135
+ (when (= category (js-nth index protocol-host-categories))
136
+ (setq found t))
137
+ (setq index (1+ index)))
138
+ found))
139
+
140
+ (defun normalize-implementations (protocol implementations)
141
+ (when (or (nil? implementations)
142
+ (not (= (value-type implementations) "object")))
143
+ (protocol-fail
144
+ (str "protocol " (get protocol :name)
145
+ " implementations must be an object")))
146
+ (let* ((descriptors (funcall protocol-own-descriptors implementations))
147
+ (keys (funcall protocol-own-keys descriptors))
148
+ (normalized (funcall protocol-null-object))
149
+ (index 0))
150
+ (when (= (js-length keys) 0)
151
+ (protocol-fail
152
+ (str "protocol " (get protocol :name)
153
+ " extension must implement at least one operation")))
154
+ (while (< index (js-length keys))
155
+ (let* ((key (js-nth index keys))
156
+ (descriptor (get descriptors key)))
157
+ (when (not (= (value-type key) "string"))
158
+ (protocol-fail
159
+ (str "protocol " (get protocol :name)
160
+ " implementation names must be strings")))
161
+ (operation-record protocol key)
162
+ (when (or (not (funcall protocol-has-own descriptor "value"))
163
+ (not (= (value-type (get descriptor :value)) "function")))
164
+ (protocol-fail
165
+ (str "protocol " (get protocol :name) "/" key
166
+ " implementation must be a function")))
167
+ (funcall protocol-define-enumerable
168
+ normalized key (get descriptor :value)))
169
+ (setq index (1+ index)))
170
+ (funcall protocol-freeze normalized)))
171
+
172
+ (defun merge-implementations (current incoming)
173
+ (let ((merged (funcall protocol-null-object))
174
+ (copy nil))
175
+ (setq copy
176
+ (lambda (source)
177
+ (unless (nullish? source)
178
+ (let ((keys (object-keys source))
179
+ (index 0))
180
+ (while (< index (js-length keys))
181
+ (let ((key (js-nth index keys)))
182
+ (funcall protocol-define-enumerable
183
+ merged key (get source key)))
184
+ (setq index (1+ index)))))))
185
+ (funcall copy current)
186
+ (funcall copy incoming)
187
+ (funcall protocol-freeze merged)))
188
+
189
+ (defun direct-method (record receiver)
190
+ (if (or (nil? receiver) (undefined? receiver))
191
+ protocol-missing
192
+ (let ((slot (get record :slot)))
193
+ (if (not (funcall protocol-property-present receiver slot))
194
+ protocol-missing
195
+ (let ((method (funcall protocol-property-value receiver slot)))
196
+ (if (= (value-type method) "function") method nil))))))
197
+
198
+ (defun external-method (state record receiver)
199
+ (let* ((prototype
200
+ (funcall protocol-exact-prototype receiver protocol-missing))
201
+ (exact-implementations
202
+ (if (= prototype protocol-missing)
203
+ undefined
204
+ (js-call (get state :types) :get prototype)))
205
+ (exact
206
+ (if (undefined? exact-implementations)
207
+ undefined
208
+ (get exact-implementations (get record :name) undefined))))
209
+ (if (not (undefined? exact))
210
+ exact
211
+ (let* ((category-implementations
212
+ (js-call (get state :categories) :get
213
+ (protocol-host-category receiver)))
214
+ (category
215
+ (if (undefined? category-implementations)
216
+ undefined
217
+ (get category-implementations
218
+ (get record :name) undefined))))
219
+ (if (not (undefined? category))
220
+ category
221
+ (get (get state :defaults)
222
+ (get record :name) protocol-missing))))))
223
+
224
+ (defun dispatch (protocol record receiver arguments)
225
+ (let ((direct (direct-method record receiver)))
226
+ (when (nil? direct)
227
+ (throw
228
+ (new ProtocolDispatchError
229
+ protocol (get record :name) receiver "invalid-direct-slot")))
230
+ (if (/= direct protocol-missing)
231
+ (js-call direct :apply receiver arguments)
232
+ (let ((external
233
+ (external-method (protocol-state protocol) record receiver)))
234
+ (if (= external protocol-missing)
235
+ (throw
236
+ (new ProtocolDispatchError protocol (get record :name) receiver))
237
+ (apply external (js-cons receiver arguments)))))))
238
+
239
+ (defun define-protocol (name operations)
240
+ (let ((protocol-name (normalize-name name "protocol name"))
241
+ (protocol-operations (normalize-protocol-operations operations)))
242
+ (let ((records (funcall protocol-map))
243
+ (operation-table (funcall protocol-null-object))
244
+ (protocol nil)
245
+ (index 0))
246
+ (while (< index (js-length protocol-operations))
247
+ (let ((operation
248
+ (js-nth index protocol-operations)))
249
+ (when (js-call records :has operation)
250
+ (protocol-fail
251
+ (str "protocol " protocol-name
252
+ " declares duplicate operation " operation)))
253
+ (let* ((slot
254
+ (funcall protocol-symbol
255
+ (str "eliscript.protocol."
256
+ protocol-name "/" operation)))
257
+ (method
258
+ (lambda (&rest invocation)
259
+ (when (= (js-length invocation) 0)
260
+ (protocol-fail
261
+ (str "protocol " protocol-name "/" operation
262
+ " requires a dispatch value")))
263
+ (dispatch protocol
264
+ (js-call records :get operation)
265
+ (aref invocation 0)
266
+ (js-call invocation :slice 1))))
267
+ (record
268
+ (js-object :name operation :slot slot :method method)))
269
+ (js-call records :set operation record)
270
+ (funcall protocol-define-enumerable
271
+ operation-table operation method)))
272
+ (setq index (1+ index)))
273
+
274
+ (setq protocol
275
+ (funcall protocol-freeze
276
+ (js-object :name protocol-name
277
+ :operations
278
+ (funcall protocol-freeze operation-table))))
279
+ (setq index 0)
280
+ (while (< index (js-length protocol-operations))
281
+ (let* ((operation (js-nth index protocol-operations))
282
+ (record (js-call records :get operation))
283
+ (method (get record :method)))
284
+ (funcall protocol-define-metadata
285
+ method protocol operation (get record :slot))
286
+ (funcall protocol-freeze method)
287
+ (funcall protocol-freeze record))
288
+ (setq index (1+ index)))
289
+
290
+ (js-call protocol-states :set protocol
291
+ (js-object :operations records
292
+ :types (funcall protocol-map)
293
+ :categories (funcall protocol-map)
294
+ :defaults
295
+ (funcall protocol-freeze
296
+ (funcall protocol-null-object))))
297
+ protocol)))
298
+
299
+ (defun protocol-definition (protocol)
300
+ (protocol-state protocol)
301
+ (let ((operations
302
+ (js-call (object-keys (get protocol :operations)) :slice)))
303
+ (funcall protocol-freeze operations)
304
+ (funcall protocol-freeze
305
+ (js-object :format protocol-definition-format
306
+ :version protocol-definition-version
307
+ :name (get protocol :name)
308
+ :operations operations))))
309
+
310
+ (defun define-protocol-from-definition (definition)
311
+ (let ((normalized (normalize-protocol-definition definition)))
312
+ (define-protocol (get normalized :name)
313
+ (get normalized :operations))))
314
+
315
+ (defun protocol-method (protocol operation)
316
+ (get (operation-record protocol operation) :method))
317
+
318
+ (defun protocol-slot (protocol operation)
319
+ (get (operation-record protocol operation) :slot))
320
+
321
+ (defun extend-protocol-type (protocol constructor implementations)
322
+ (let ((state (protocol-state protocol)))
323
+ (when (not (= (value-type constructor) "function"))
324
+ (protocol-fail
325
+ (str "protocol " (get protocol :name)
326
+ " exact type must be a constructor with a prototype")))
327
+ (let ((prototype (get constructor :prototype nil)))
328
+ (when (or (nil? prototype)
329
+ (not (= (value-type prototype) "object")))
330
+ (protocol-fail
331
+ (str "protocol " (get protocol :name)
332
+ " exact type must be a constructor with a prototype")))
333
+ (let* ((normalized
334
+ (normalize-implementations protocol implementations))
335
+ (current (js-call (get state :types) :get prototype)))
336
+ (js-call (get state :types) :set prototype
337
+ (merge-implementations current normalized)))
338
+ protocol)))
339
+
340
+ (defun extend-protocol-category (protocol category implementations)
341
+ (let ((state (protocol-state protocol)))
342
+ (when (not (known-host-category? category))
343
+ (protocol-fail (str "unknown protocol host category " category)))
344
+ (let* ((normalized
345
+ (normalize-implementations protocol implementations))
346
+ (current (js-call (get state :categories) :get category)))
347
+ (js-call (get state :categories) :set category
348
+ (merge-implementations current normalized)))
349
+ protocol))
350
+
351
+ (defun extend-protocol-default (protocol implementations)
352
+ (let* ((state (protocol-state protocol))
353
+ (normalized (normalize-implementations protocol implementations)))
354
+ (put state :defaults
355
+ (merge-implementations (get state :defaults) normalized))
356
+ protocol))
357
+
358
+ (defun implements-protocol-operation? (protocol operation value)
359
+ (let* ((record (operation-record protocol operation))
360
+ (direct (direct-method record value)))
361
+ (and (not (nil? direct))
362
+ (or (/= direct protocol-missing)
363
+ (/= (external-method (protocol-state protocol) record value)
364
+ protocol-missing)))))
365
+
366
+ (defun implements-protocol? (protocol value)
367
+ (let* ((state (protocol-state protocol))
368
+ (operations (object-keys (get protocol :operations)))
369
+ (index 0)
370
+ (implemented t))
371
+ (while (and (< index (js-length operations)) implemented)
372
+ (let* ((record
373
+ (js-call (get state :operations) :get (js-nth index operations)))
374
+ (direct (direct-method record value)))
375
+ (when (or (nil? direct)
376
+ (and (= direct protocol-missing)
377
+ (= (external-method state record value)
378
+ protocol-missing)))
379
+ (setq implemented false)))
380
+ (setq index (1+ index)))
381
+ implemented))
382
+
383
+ (export define-protocol define-protocol-from-definition
384
+ extend-protocol-category extend-protocol-default
385
+ extend-protocol-type implements-protocol-operation?
386
+ implements-protocol? protocol-definition protocol-host-category
387
+ protocol-method protocol-slot))
@@ -0,0 +1,16 @@
1
+ (module eliscript.core.record
2
+ (import "../../runtime/core/record.mjs" :as Runtime)
3
+ (defun define-record-type (name fields)
4
+ (Runtime/defineRecordType name fields))
5
+ (defun record? (value) (Runtime/isRecord value))
6
+ (defun record-type? (value) (Runtime/isRecordType value))
7
+ (defun record-type (value) (Runtime/recordType value))
8
+ (defun record-type-name (value) (Runtime/recordTypeName value))
9
+ (defun record-field-names (value) (Runtime/recordFieldNames value))
10
+ (export
11
+ define-record-type
12
+ record?
13
+ record-type?
14
+ record-type
15
+ record-type-name
16
+ record-field-names))
@@ -0,0 +1,17 @@
1
+ (module eliscript.core.regex
2
+ (import "../../runtime/core/regex.mjs" :as Runtime)
3
+ (defun regex (&rest arguments) (apply Runtime/compileRegex arguments))
4
+ (defun regex? (value) (Runtime/isRegexPattern value))
5
+ (defun regex-source (pattern) (Runtime/regexSource pattern))
6
+ (defun regex-flags (pattern) (Runtime/regexFlags pattern))
7
+ (defun re-matches (pattern text) (Runtime/regexMatches pattern text))
8
+ (defun re-find
9
+ (pattern text &rest start)
10
+ (apply Runtime/regexFind (js-cons pattern (js-cons text start))))
11
+ (defun re-seq
12
+ (pattern text &rest start)
13
+ (apply Runtime/regexSequence (js-cons pattern (js-cons text start))))
14
+ (defun re-replace
15
+ (pattern replacement text)
16
+ (Runtime/regexReplace pattern replacement text))
17
+ (export re-find re-matches re-replace re-seq regex regex-flags regex-source regex?))