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,321 @@
1
+ (module bootstrap.compiler.reader
2
+ (import "./syntax.mjs"
3
+ make-span
4
+ literal-node
5
+ undefined-node
6
+ symbol-node
7
+ keyword-node
8
+ collection-node)
9
+ (defconst fail
10
+ (js*
11
+ "(diagnostic) => { const p = diagnostic.location.start; const error = new Error(`${diagnostic.location.file}:${p.line}:${p.column}: ${diagnostic.message}`); error.eliscriptDiagnostic = diagnostic; throw error; }"))
12
+ (defconst number-pattern
13
+ (js* "/^[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?$/"))
14
+ (defconst native-whitespace-code-p
15
+ (js*
16
+ "(code) => code === 32 || code === 9 || code === 10 || code === 13 || code === 12"))
17
+ (defconst native-delimiter-code-p
18
+ (js*
19
+ "(code) => code === undefined || code === 32 || code === 9 || code === 10 || code === 13 || code === 12 || code === 40 || code === 41 || code === 91 || code === 93 || code === 123 || code === 125 || code === 34 || code === 39 || code === 44 || code === 59 || code === 96"))
20
+ (defun make-state
21
+ (source filename)
22
+ (js-object :source source :filename filename :index 0 :offset 0 :line 1 :column 1))
23
+ (defun mark
24
+ (state)
25
+ (js-object
26
+ :index
27
+ (get state :index)
28
+ :offset
29
+ (get state :offset)
30
+ :line
31
+ (get state :line)
32
+ :column
33
+ (get state :column)))
34
+ (defun span-from
35
+ (state start)
36
+ (make-span
37
+ (get state :filename)
38
+ (get start :offset)
39
+ (get state :offset)
40
+ (get start :line)
41
+ (get start :column)
42
+ (get state :line)
43
+ (get state :column)))
44
+ (defun fail-at
45
+ (state position message &optional width)
46
+ (let ((span-width (or width 1)))
47
+ (fail
48
+ (js-object
49
+ :format
50
+ "eliscript-diagnostic"
51
+ :version
52
+ 1
53
+ :code
54
+ "ELI-R0001"
55
+ :severity
56
+ "error"
57
+ :phase
58
+ "reader"
59
+ :message
60
+ message
61
+ :location
62
+ (js-object
63
+ :file
64
+ (or (get state :filename) "<input>")
65
+ :start
66
+ (js-object
67
+ :offset
68
+ (get position :offset)
69
+ :line
70
+ (get position :line)
71
+ :column
72
+ (get position :column))
73
+ :end
74
+ (js-object
75
+ :offset
76
+ (+ (get position :offset) span-width)
77
+ :line
78
+ (get position :line)
79
+ :column
80
+ (+ (get position :column) span-width)))))))
81
+ (defun peek-code
82
+ (state)
83
+ (js-call (get state :source) :codePointAt (get state :index)))
84
+ (defun peek-code-ahead
85
+ (state code-units)
86
+ (js-call (get state :source) :codePointAt (+ (get state :index) code-units)))
87
+ (defun advance
88
+ (state)
89
+ (let ((code (peek-code state)))
90
+ (unless (eq code undefined)
91
+ (put state :index (+ (get state :index) (if (> code 65535) 2 1)))
92
+ (put state :offset (1+ (get state :offset)))
93
+ (if (= code 10)
94
+ (progn (put state :line (1+ (get state :line))) (put state :column 1))
95
+ (put state :column (1+ (get state :column)))))
96
+ code))
97
+ (defun reference-whitespace-code-p
98
+ (code)
99
+ (or (= code 32) (= code 9) (= code 10) (= code 13) (= code 12)))
100
+ (defun whitespace-code-p (code) (funcall native-whitespace-code-p code))
101
+ (defun reference-delimiter-code-p
102
+ (code)
103
+ (or
104
+ (eq code undefined)
105
+ (reference-whitespace-code-p code)
106
+ (= code 40)
107
+ (= code 41)
108
+ (= code 91)
109
+ (= code 93)
110
+ (= code 123)
111
+ (= code 125)
112
+ (= code 34)
113
+ (= code 39)
114
+ (= code 44)
115
+ (= code 59)
116
+ (= code 96)))
117
+ (defun delimiter-code-p (code) (funcall native-delimiter-code-p code))
118
+ (defun closing-code-p (code) (or (= code 41) (= code 93) (= code 125)))
119
+ (defun code-character (code) (js-call (js* "String") :fromCodePoint code))
120
+ (defun invalid-closing
121
+ (state position code)
122
+ (fail-at
123
+ state
124
+ position
125
+ (str "Invalid read syntax: \"" (code-character code) "\"")
126
+ 1))
127
+ (defun skip-space-and-comments
128
+ (state)
129
+ (let ((done false))
130
+ (while (not done)
131
+ (let ((code (peek-code state)))
132
+ (cond ((eq code undefined) (setq done t))
133
+ ((whitespace-code-p code) (advance state))
134
+ ((= code 59)
135
+ (while
136
+ (and
137
+ (not (eq (peek-code state) undefined))
138
+ (not (= (peek-code state) 10)))
139
+ (advance state)))
140
+ (t (setq done t)))))))
141
+ (defun read-token
142
+ (state start)
143
+ (while (not (delimiter-code-p (peek-code state))) (advance state))
144
+ (js-call (get state :source) :slice (get start :index) (get state :index)))
145
+ (defun queue-opening-index
146
+ (state)
147
+ (let*
148
+ ((source (get state :source)) (start (get state :index)) (tag-end (+ start 6)))
149
+ (if (not (= (js-call source :slice start tag-end) "#queue"))
150
+ -1
151
+ (let ((cursor tag-end) (done false))
152
+ (while (and (< cursor (get source :length)) (not done))
153
+ (let ((code (js-call source :codePointAt cursor)))
154
+ (cond
155
+ ((reference-whitespace-code-p code)
156
+ (setq cursor (+ cursor (if (> code 65535) 2 1))))
157
+ ((= code 59)
158
+ (while
159
+ (and
160
+ (< cursor (get source :length))
161
+ (not (= (js-call source :codePointAt cursor) 10)))
162
+ (setq cursor (1+ cursor))))
163
+ (t (setq done t)))))
164
+ (if
165
+ (and
166
+ (< cursor (get source :length))
167
+ (= (js-call source :codePointAt cursor) 91))
168
+ cursor
169
+ -1)))))
170
+ (defun number-token-p (token) (js-call number-pattern :test token))
171
+ (defun valid-keyword-name-p
172
+ (name)
173
+ (let ((separator (js-call name :indexOf "/")))
174
+ (and
175
+ (> (get name :length) 0)
176
+ (or
177
+ (= separator -1)
178
+ (and
179
+ (> separator 0)
180
+ (< separator (1- (get name :length)))
181
+ (= (js-call name :indexOf "/" (1+ separator)) -1))))))
182
+ (defun read-atom
183
+ (state start)
184
+ (let* ((token (read-token state start)) (span (span-from state start)))
185
+ (cond ((= token "nil") (literal-node nil span))
186
+ ((= token "t") (literal-node t span))
187
+ ((= token "false") (literal-node false span))
188
+ ((= token "undefined") (undefined-node span))
189
+ ((js-call token :startsWith ":")
190
+ (let ((name (js-call token :slice 1)))
191
+ (unless (valid-keyword-name-p name)
192
+ (fail-at
193
+ state
194
+ start
195
+ (str "invalid keyword literal: " token)
196
+ (- (get state :offset) (get start :offset))))
197
+ (keyword-node name span)))
198
+ ((number-token-p token) (literal-node (funcall (js* "Number") token) span))
199
+ (t (symbol-node token span)))))
200
+ (defun read-string-literal
201
+ (state start)
202
+ (advance state)
203
+ (let ((done false))
204
+ (while (not done)
205
+ (let ((code (peek-code state)))
206
+ (cond ((eq code undefined) (fail-at state start "unexpected end of input"))
207
+ ((= code 92)
208
+ (advance state)
209
+ (when (eq (peek-code state) undefined)
210
+ (fail-at state start "unexpected end of input"))
211
+ (advance state))
212
+ ((= code 34) (advance state) (setq done t))
213
+ (t (advance state)))))
214
+ (let*
215
+ (
216
+ (raw
217
+ (js-call (get state :source) :slice (get start :index) (get state :index)))
218
+ (value (js-call (js* "JSON") :parse raw)))
219
+ (literal-node value (span-from state start)))))
220
+ (defun read-prefix
221
+ (state start name width)
222
+ (let ((remaining width))
223
+ (while (> remaining 0) (advance state) (setq remaining (1- remaining))))
224
+ (let ((prefix-span (span-from state start)))
225
+ (skip-space-and-comments state)
226
+ (when (eq (peek-code state) undefined)
227
+ (fail-at state start "unexpected end of input"))
228
+ (let ((child (read-node state)))
229
+ (collection-node
230
+ "list"
231
+ (js-array (symbol-node name prefix-span) child)
232
+ (span-from state start)))))
233
+ (defun read-collection
234
+ (state start closing kind &optional tagged-opening-span)
235
+ (advance state)
236
+ (let ((opening-span (span-from state start)) (items (js-array)) (done false))
237
+ (while (not done)
238
+ (skip-space-and-comments state)
239
+ (let ((code (peek-code state)))
240
+ (cond ((eq code undefined) (fail-at state start "unexpected end of input"))
241
+ ((= code closing) (advance state) (setq done t))
242
+ ((closing-code-p code) (invalid-closing state start code))
243
+ (t (js-call items :push (read-node state))))))
244
+ (let ((span (span-from state start)))
245
+ (cond
246
+ ((= kind "map")
247
+ (when (= (% (get items :length) 2) 1)
248
+ (fail-at
249
+ state
250
+ start
251
+ "map literal must contain an even number of forms"
252
+ (- (get state :offset) (get start :offset))))
253
+ (let ((form-items (js-array (symbol-node "hash-map" opening-span))))
254
+ (js-call items :forEach (lambda (item) (js-call form-items :push item)))
255
+ (collection-node "list" form-items span)))
256
+ ((= kind "set")
257
+ (let ((form-items (js-array (symbol-node "hash-set" opening-span))))
258
+ (js-call items :forEach (lambda (item) (js-call form-items :push item)))
259
+ (collection-node "list" form-items span)))
260
+ ((= kind "queue")
261
+ (let
262
+ (
263
+ (form-items
264
+ (js-array (symbol-node "queue" (or tagged-opening-span opening-span)))))
265
+ (js-call items :forEach (lambda (item) (js-call form-items :push item)))
266
+ (collection-node "list" form-items span)))
267
+ ((and (= kind "list") (= (get items :length) 0)) (literal-node nil span))
268
+ (t (collection-node kind items span))))))
269
+ (defun read-queue
270
+ (state start opening)
271
+ (let ((tag-end (+ (get start :index) 6)))
272
+ (while (< (get state :index) tag-end) (advance state))
273
+ (let ((tag-span (span-from state start)))
274
+ (while (< (get state :index) opening) (advance state))
275
+ (read-collection state start 93 "queue" tag-span))))
276
+ (defun read-dispatch
277
+ (state start)
278
+ (let ((next (peek-code-ahead state 1)) (queue-opening (queue-opening-index state)))
279
+ (cond ((= next 39) (read-prefix state start "function" 2))
280
+ ((= next 123) (advance state) (read-collection state start 125 "set"))
281
+ ((>= queue-opening 0) (read-queue state start queue-opening))
282
+ (t
283
+ (fail-at
284
+ state
285
+ start
286
+ (str
287
+ "Invalid read syntax: \"#"
288
+ (if (eq next undefined) "" (code-character next))
289
+ "\""))))))
290
+ (defun read-node
291
+ (state)
292
+ (skip-space-and-comments state)
293
+ (let ((start (mark state)) (code (peek-code state)))
294
+ (cond ((eq code undefined) (fail-at state start "unexpected end of input"))
295
+ ((= code 40) (read-collection state start 41 "list"))
296
+ ((= code 91) (read-collection state start 93 "vector"))
297
+ ((= code 123) (read-collection state start 125 "map"))
298
+ ((closing-code-p code) (invalid-closing state start code))
299
+ ((= code 34) (read-string-literal state start))
300
+ ((= code 39) (read-prefix state start "quote" 1))
301
+ ((= code 96) (read-prefix state start "`" 1))
302
+ ((= code 44)
303
+ (if (= (peek-code-ahead state 1) 64)
304
+ (read-prefix state start ",@" 2)
305
+ (read-prefix state start "," 1)))
306
+ ((= code 35) (read-dispatch state start))
307
+ (t (read-atom state start)))))
308
+ (defun read-string
309
+ (source filename)
310
+ (let ((state (make-state source filename)) (forms (js-array)))
311
+ (skip-space-and-comments state)
312
+ (while (not (eq (peek-code state) undefined))
313
+ (js-call forms :push (read-node state))
314
+ (skip-space-and-comments state))
315
+ forms))
316
+ (export
317
+ read-string
318
+ whitespace-code-p
319
+ reference-whitespace-code-p
320
+ delimiter-code-p
321
+ reference-delimiter-code-p))
@@ -0,0 +1,165 @@
1
+ (module bootstrap.compiler.source-map
2
+ (defconst base64
3
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
4
+ (defconst fail
5
+ (js* "(message) => { throw new Error(message); }"))
6
+ (defconst native-source-locations
7
+ (js* "(source, marks) => { const wanted = new Set(); const locations = new Map(); for (const mark of marks) { const span = (mark ?? [])[2] ?? null; const start = span.start; if (start !== undefined) wanted.add(start); } const starts = [...wanted].filter((start) => Number.isSafeInteger(start) && start >= 0).sort((left, right) => left - right); let index = 0; let offset = 0; let line = 0; let column = 0; for (const start of starts) { while (offset < start && index < source.length) { const code = source.codePointAt(index); const width = code > 65535 ? 2 : 1; if (code === 10) { line += 1; column = 0; } else { column += width; } index += width; offset += 1; } if (offset === start && (index < source.length || source.length === 0)) locations.set(offset, [line, column]); } return locations; }"))
8
+ (defconst native-collect-generated-marks
9
+ (js* "(generated, marks) => { const result = []; let index = 0; let line = 0; let column = 0; for (const mark of marks) { const target = mark.offset; while (index < target && index < generated.length) { if (generated.charCodeAt(index) === 10) { line += 1; column = 0; } else { column += 1; } index += 1; } if (index === target && index < generated.length) result.push([line, column, mark.span]); } return result; }"))
10
+
11
+ (defun signed-vlq (value)
12
+ (if (< value 0)
13
+ (1+ (* (- value) 2))
14
+ (* value 2)))
15
+
16
+ (defun encode-vlq (value)
17
+ (let ((remaining (signed-vlq value))
18
+ (characters (js-array)))
19
+ (while (>= remaining 32)
20
+ (js-call characters :push
21
+ (js-call base64 :charAt (+ (% remaining 32) 32)))
22
+ (setq remaining (Math/floor (/ remaining 32))))
23
+ (js-call characters :push (js-call base64 :charAt remaining))
24
+ (js-call characters :join "")))
25
+
26
+ (defun reference-collect-generated-marks (generated marks)
27
+ (let ((index 0)
28
+ (mark-index 0)
29
+ (line 0)
30
+ (column 0)
31
+ (result (js-array)))
32
+ (while (< index (get generated :length))
33
+ (while (and (< mark-index (get marks :length))
34
+ (= (get (js-nth mark-index marks) :offset) index))
35
+ (js-call result :push
36
+ (js-array line column (get (js-nth mark-index marks) :span)))
37
+ (setq mark-index (1+ mark-index)))
38
+ (if (= (js-call generated :charCodeAt index) 10)
39
+ (progn
40
+ (setq line (1+ line))
41
+ (setq column 0))
42
+ (setq column (1+ column)))
43
+ (setq index (1+ index)))
44
+ result))
45
+
46
+ (defun collect-generated-marks (generated marks)
47
+ (funcall native-collect-generated-marks generated marks))
48
+
49
+ (defun reference-source-locations (source marks)
50
+ (let ((wanted (new (js* "Map")))
51
+ (locations (new (js* "Map"))))
52
+ (js-call marks :forEach
53
+ (lambda (mark)
54
+ (let ((start (get (js-nth 2 mark) :start)))
55
+ (unless (eq start undefined)
56
+ (js-call wanted :set start t)))))
57
+ (let ((index 0)
58
+ (offset 0)
59
+ (line 0)
60
+ (column 0))
61
+ (while (<= index (get source :length))
62
+ (when (js-call wanted :has offset)
63
+ (js-call locations :set offset (js-array line column)))
64
+ (when (< index (get source :length))
65
+ (let* ((code (js-call source :codePointAt index))
66
+ (width (if (> code 65535) 2 1)))
67
+ (if (= code 10)
68
+ (progn
69
+ (setq line (1+ line))
70
+ (setq column 0))
71
+ (setq column (+ column width)))
72
+ (setq index (+ index width))
73
+ (setq offset (1+ offset))))
74
+ (when (= index (get source :length))
75
+ (setq index (1+ index)))))
76
+ locations))
77
+
78
+ (defun source-locations (source marks)
79
+ (funcall native-source-locations source marks))
80
+
81
+ (defun original-location (span locations)
82
+ (let ((start (get span :start)))
83
+ (if (and (not (eq start undefined))
84
+ (js-call locations :has start))
85
+ (js-call locations :get start)
86
+ (js-array (1- (get span :line)) (1- (get span :column))))))
87
+
88
+ (defun group-marks (marks locations)
89
+ (let ((current-line 0)
90
+ (current-segments (js-array))
91
+ (lines (js-array)))
92
+ (js-call marks :forEach
93
+ (lambda (mark)
94
+ (let ((generated-line (js-nth 0 mark)))
95
+ (while (< current-line generated-line)
96
+ (js-call lines :push current-segments)
97
+ (setq current-segments (js-array))
98
+ (setq current-line (1+ current-line)))
99
+ (let ((original (original-location (js-nth 2 mark) locations)))
100
+ (js-call current-segments :push
101
+ (js-array (js-nth 1 mark) 0 (js-nth 0 original) (js-nth 1 original)))))))
102
+ (js-call lines :push current-segments)
103
+ lines))
104
+
105
+ (defun encode-segment (segment state)
106
+ (let ((generated-column (js-nth 0 segment))
107
+ (source (js-nth 1 segment))
108
+ (original-line (js-nth 2 segment))
109
+ (original-column (js-nth 3 segment)))
110
+ (let ((encoded
111
+ (str
112
+ (encode-vlq
113
+ (- generated-column (get state :generatedColumn)))
114
+ (encode-vlq (- source (get state :source)))
115
+ (encode-vlq
116
+ (- original-line (get state :originalLine)))
117
+ (encode-vlq
118
+ (- original-column (get state :originalColumn))))))
119
+ (put state :generatedColumn generated-column)
120
+ (put state :source source)
121
+ (put state :originalLine original-line)
122
+ (put state :originalColumn original-column)
123
+ encoded)))
124
+
125
+ (defun encode-mappings (lines)
126
+ (let ((state
127
+ (js-object
128
+ :generatedColumn 0
129
+ :source 0
130
+ :originalLine 0
131
+ :originalColumn 0))
132
+ (encoded-lines (js-array)))
133
+ (js-call lines :forEach
134
+ (lambda (segments)
135
+ (put state :generatedColumn 0)
136
+ (let ((encoded (js-array)))
137
+ (js-call segments :forEach
138
+ (lambda (segment)
139
+ (js-call encoded :push (encode-segment segment state))))
140
+ (js-call encoded-lines :push (js-call encoded :join ",")))))
141
+ (js-call encoded-lines :join ";")))
142
+
143
+ (defun create-source-map
144
+ (generated marks source source-name generated-name)
145
+ (unless (js* "Array.isArray(marks)")
146
+ (fail "source map marks must be an array"))
147
+ (let* ((generated-marks (collect-generated-marks generated marks))
148
+ (locations (source-locations source generated-marks))
149
+ (mappings (encode-mappings (group-marks generated-marks locations)))
150
+ (document (js-object :version 3)))
151
+ (when generated-name
152
+ (put document :file generated-name))
153
+ (put document :sources (js-array source-name))
154
+ (put document :sourcesContent (js-array source))
155
+ (put document :names (js-array))
156
+ (put document :mappings mappings)
157
+ (str (JSON/stringify document) "\n")))
158
+
159
+ (export
160
+ encode-vlq
161
+ collect-generated-marks
162
+ reference-collect-generated-marks
163
+ source-locations
164
+ reference-source-locations
165
+ create-source-map))
@@ -0,0 +1,92 @@
1
+ (module bootstrap.compiler.symbol
2
+ (defconst reserved-words
3
+ (js-array "await" "break" "case" "catch" "class" "const" "continue"
4
+ "debugger" "default" "delete" "do" "else" "enum" "export"
5
+ "extends" "false" "finally" "for" "function" "if" "implements"
6
+ "import" "in" "instanceof" "interface" "let" "new" "null"
7
+ "package" "private" "protected" "public" "return" "static"
8
+ "super" "switch" "this" "throw" "true" "try" "typeof" "var"
9
+ "void" "while" "with" "yield"))
10
+
11
+ (defconst reserved-values (js-array "nil" "t" "false" "undefined"))
12
+ (defconst strict-binding-names (js-array "arguments" "eval"))
13
+ (defconst internal-prefix "__eliscript_")
14
+ (defconst fail
15
+ (js* "(message) => { throw new Error(message); }"))
16
+
17
+ (defun ascii-identifier-code-p (code)
18
+ (or (and (>= code 65) (<= code 90))
19
+ (and (>= code 97) (<= code 122))
20
+ (and (>= code 48) (<= code 57))
21
+ (= code 95)
22
+ (= code 36)))
23
+
24
+ (defun unicode-escape (code)
25
+ (let* ((hex (js-call code :toString 16))
26
+ (upper (js-call hex :toUpperCase))
27
+ (padded (js-call upper :padStart 4 "0")))
28
+ (str "_U" padded "_")))
29
+
30
+ (defun munge-character (code)
31
+ (cond
32
+ ((ascii-identifier-code-p code)
33
+ (js-call (js* "String") :fromCodePoint code))
34
+ ((= code 45) "_")
35
+ ((= code 63) "_QMARK_")
36
+ ((= code 33) "_BANG_")
37
+ ((= code 42) "_STAR_")
38
+ ((= code 43) "_PLUS_")
39
+ ((= code 61) "_EQ_")
40
+ ((= code 60) "_LT_")
41
+ ((= code 62) "_GT_")
42
+ (t (unicode-escape code))))
43
+
44
+ (defun munge-segment (name)
45
+ (let ((index 0)
46
+ (pieces (js-array)))
47
+ (while (< index (get name :length))
48
+ (let ((code (js-call name :codePointAt index)))
49
+ (js-call pieces :push (munge-character code))
50
+ (setq index (+ index (if (> code 65535) 2 1)))))
51
+ (let ((result (js-call pieces :join "")))
52
+ (when (= result "")
53
+ (fail "empty identifier"))
54
+ (when (and (>= (js-call result :charCodeAt 0) 48)
55
+ (<= (js-call result :charCodeAt 0) 57))
56
+ (setq result (str "_" result)))
57
+ (when (js-call reserved-words :includes result)
58
+ (setq result (str result "$")))
59
+ result)))
60
+
61
+ (defun binding-name (name)
62
+ (when (or (js-call reserved-values :includes name)
63
+ (js-call name :startsWith "&"))
64
+ (fail (str "reserved value cannot be a binding: " name)))
65
+ (when (or (js-call name :includes ".")
66
+ (js-call name :includes "/"))
67
+ (fail (str "qualified name cannot be a binding: " name)))
68
+ (let ((output-name (munge-segment name)))
69
+ (when (js-call strict-binding-names :includes output-name)
70
+ (setq output-name (str output-name "$")))
71
+ (when (js-call output-name :startsWith internal-prefix)
72
+ (fail (str "binding name uses reserved compiler prefix: " name)))
73
+ output-name))
74
+
75
+ (defun reference-name (name)
76
+ (let* ((normalized (js-call name :replaceAll "/" "."))
77
+ (qualified (or (js-call name :includes "/")
78
+ (js-call name :includes ".")))
79
+ (segments (js-call normalized :split "."))
80
+ (present
81
+ (js-call segments :filter
82
+ (lambda (segment) (> (get segment :length) 0))))
83
+ (munged (js-call present :map munge-segment))
84
+ (reference (js-call munged :join ".")))
85
+ (when (and (not qualified)
86
+ (js-call strict-binding-names :includes reference))
87
+ (setq reference (str reference "$")))
88
+ (when (js-call reference :startsWith internal-prefix)
89
+ (fail (str "reference uses reserved compiler prefix: " name)))
90
+ reference))
91
+
92
+ (export munge-segment binding-name reference-name))
@@ -0,0 +1,54 @@
1
+ (module bootstrap.compiler.syntax
2
+ (defun make-span
3
+ (filename start end line column end-line end-column)
4
+ (js-object
5
+ :filename filename
6
+ :start start
7
+ :end end
8
+ :line line
9
+ :column column
10
+ :endLine end-line
11
+ :endColumn end-column))
12
+
13
+ (defun literal-node (value span)
14
+ (js-object :kind "literal" :value value :span span))
15
+
16
+ (defun undefined-node (span)
17
+ (js-object :kind "undefined" :span span))
18
+
19
+ (defun symbol-node (name span)
20
+ (js-object :kind "symbol" :name name :span span))
21
+
22
+ (defun keyword-node (name span)
23
+ (js-object :kind "keyword" :name name :span span))
24
+
25
+ (defun collection-node (kind items span)
26
+ (js-object :kind kind :items items :span span))
27
+
28
+ (defun node-kind (node)
29
+ (get node :kind))
30
+
31
+ (defun node-span (node)
32
+ (get node :span))
33
+
34
+ (defun node-value (node)
35
+ (get node :value))
36
+
37
+ (defun node-name (node)
38
+ (get node :name undefined))
39
+
40
+ (defun node-items (node)
41
+ (get node :items (js-array)))
42
+
43
+ (export
44
+ make-span
45
+ literal-node
46
+ undefined-node
47
+ symbol-node
48
+ keyword-node
49
+ collection-node
50
+ node-kind
51
+ node-span
52
+ node-value
53
+ node-name
54
+ node-items))