eliscript 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
(module bootstrap.compiler.formatter
|
|
2
|
+
(import "./reader.mjs" read-string)
|
|
3
|
+
|
|
4
|
+
(defconst formatter-format "eliscript-formatter")
|
|
5
|
+
(defconst formatter-version 1)
|
|
6
|
+
(defconst formatter-width 88)
|
|
7
|
+
(defconst formatter-indent 2)
|
|
8
|
+
(defconst formatter-fail
|
|
9
|
+
(js* "(message, filename) => { const error = new Error(message); error.eliscriptDiagnostic = { format: 'eliscript-diagnostic', version: 1, code: 'ELI-F0001', severity: 'error', phase: 'formatter', message, ...(filename ? { location: { file: filename } } : {}) }; throw error; }"))
|
|
10
|
+
(defconst declaration-heads
|
|
11
|
+
(js-array
|
|
12
|
+
"module" "defasync" "defconst" "defmacro" "defmethod" "defmulti"
|
|
13
|
+
"defn" "defportable" "defprotocol" "defun" "defvar"
|
|
14
|
+
"extend-category" "extend-default" "extend-type" "export-default"))
|
|
15
|
+
(defconst operand-heads
|
|
16
|
+
(js-array
|
|
17
|
+
"->" "->>" "as->" "case" "catch" "cond" "condp" "cond->" "cond->>"
|
|
18
|
+
"if" "if-let" "if-some"
|
|
19
|
+
"import" "import-portable" "let" "let*" "letfn" "loop" "unless" "when"
|
|
20
|
+
"some->" "some->>" "when-let" "when-some" "while"))
|
|
21
|
+
|
|
22
|
+
(defun formatter-error (message filename)
|
|
23
|
+
(formatter-fail message filename))
|
|
24
|
+
|
|
25
|
+
(defun make-scan-state (source filename)
|
|
26
|
+
(js-object :source source :filename filename :index 0))
|
|
27
|
+
|
|
28
|
+
(defun peek-code (state)
|
|
29
|
+
(js-call (get state :source) :charCodeAt (get state :index)))
|
|
30
|
+
|
|
31
|
+
(defun peek-code-ahead (state count)
|
|
32
|
+
(js-call
|
|
33
|
+
(get state :source)
|
|
34
|
+
:charCodeAt
|
|
35
|
+
(+ (get state :index) count)))
|
|
36
|
+
|
|
37
|
+
(defun at-end-p (state)
|
|
38
|
+
(>= (get state :index) (get (get state :source) :length)))
|
|
39
|
+
|
|
40
|
+
(defun advance (state)
|
|
41
|
+
(unless (at-end-p state)
|
|
42
|
+
(put state :index (1+ (get state :index)))))
|
|
43
|
+
|
|
44
|
+
(defun whitespace-code-p (code)
|
|
45
|
+
(or (= code 32) (= code 9) (= code 10) (= code 13) (= code 12)))
|
|
46
|
+
|
|
47
|
+
(defun token-delimiter-code-p (code)
|
|
48
|
+
(or (js-call globalThis.Number :isNaN code)
|
|
49
|
+
(whitespace-code-p code)
|
|
50
|
+
(= code 40) (= code 41)
|
|
51
|
+
(= code 91) (= code 93)
|
|
52
|
+
(= code 123) (= code 125)
|
|
53
|
+
(= code 34) (= code 39)
|
|
54
|
+
(= code 44) (= code 59) (= code 96)))
|
|
55
|
+
|
|
56
|
+
(defun token (kind text)
|
|
57
|
+
(js-object :kind kind :text text))
|
|
58
|
+
|
|
59
|
+
(defun collection-token (text closing)
|
|
60
|
+
(js-object :kind "open" :text text :closing closing))
|
|
61
|
+
|
|
62
|
+
(defun read-string-token (state)
|
|
63
|
+
(let ((start (get state :index))
|
|
64
|
+
(escaped false)
|
|
65
|
+
(done false))
|
|
66
|
+
(advance state)
|
|
67
|
+
(while (and (not done) (not (at-end-p state)))
|
|
68
|
+
(let ((code (peek-code state)))
|
|
69
|
+
(advance state)
|
|
70
|
+
(cond
|
|
71
|
+
(escaped (setq escaped false))
|
|
72
|
+
((= code 92) (setq escaped t))
|
|
73
|
+
((= code 34) (setq done t)))))
|
|
74
|
+
(token
|
|
75
|
+
"string"
|
|
76
|
+
(js-call
|
|
77
|
+
(get state :source)
|
|
78
|
+
:slice
|
|
79
|
+
start
|
|
80
|
+
(get state :index)))))
|
|
81
|
+
|
|
82
|
+
(defun read-comment-token (state)
|
|
83
|
+
(let ((start (get state :index)))
|
|
84
|
+
(while (and (not (at-end-p state))
|
|
85
|
+
(not (= (peek-code state) 10))
|
|
86
|
+
(not (= (peek-code state) 13)))
|
|
87
|
+
(advance state))
|
|
88
|
+
(token
|
|
89
|
+
"comment"
|
|
90
|
+
(js-call
|
|
91
|
+
(js-call
|
|
92
|
+
(get state :source)
|
|
93
|
+
:slice
|
|
94
|
+
start
|
|
95
|
+
(get state :index))
|
|
96
|
+
:replace
|
|
97
|
+
(js* "/[ \\t]+$/u")
|
|
98
|
+
""))))
|
|
99
|
+
|
|
100
|
+
(defun read-atom-token (state)
|
|
101
|
+
(let ((start (get state :index)))
|
|
102
|
+
(while (and (not (at-end-p state))
|
|
103
|
+
(not (token-delimiter-code-p (peek-code state))))
|
|
104
|
+
(advance state))
|
|
105
|
+
(token
|
|
106
|
+
"atom"
|
|
107
|
+
(js-call
|
|
108
|
+
(get state :source)
|
|
109
|
+
:slice
|
|
110
|
+
start
|
|
111
|
+
(get state :index)))))
|
|
112
|
+
|
|
113
|
+
(defun tokenize (source filename)
|
|
114
|
+
(let ((state (make-scan-state source filename))
|
|
115
|
+
(tokens (js-array)))
|
|
116
|
+
(while (not (at-end-p state))
|
|
117
|
+
(let ((code (peek-code state))
|
|
118
|
+
next)
|
|
119
|
+
(cond
|
|
120
|
+
((whitespace-code-p code)
|
|
121
|
+
(advance state))
|
|
122
|
+
((= code 59)
|
|
123
|
+
(js-call tokens :push (read-comment-token state)))
|
|
124
|
+
((= code 34)
|
|
125
|
+
(js-call tokens :push (read-string-token state)))
|
|
126
|
+
((and (= code 35) (= (peek-code-ahead state 1) 123))
|
|
127
|
+
(advance state)
|
|
128
|
+
(advance state)
|
|
129
|
+
(js-call tokens :push (collection-token "#{" "}")))
|
|
130
|
+
((and (= code 35) (= (peek-code-ahead state 1) 39))
|
|
131
|
+
(advance state)
|
|
132
|
+
(advance state)
|
|
133
|
+
(js-call tokens :push (token "prefix" "#'")))
|
|
134
|
+
((= code 40)
|
|
135
|
+
(advance state)
|
|
136
|
+
(js-call tokens :push (collection-token "(" ")")))
|
|
137
|
+
((= code 91)
|
|
138
|
+
(advance state)
|
|
139
|
+
(js-call tokens :push (collection-token "[" "]")))
|
|
140
|
+
((= code 123)
|
|
141
|
+
(advance state)
|
|
142
|
+
(js-call tokens :push (collection-token "{" "}")))
|
|
143
|
+
((or (= code 41) (= code 93) (= code 125))
|
|
144
|
+
(setq next (js-call (get state :source) :charAt (get state :index)))
|
|
145
|
+
(advance state)
|
|
146
|
+
(js-call tokens :push (token "close" next)))
|
|
147
|
+
((or (= code 39) (= code 96))
|
|
148
|
+
(setq next (js-call (get state :source) :charAt (get state :index)))
|
|
149
|
+
(advance state)
|
|
150
|
+
(js-call tokens :push (token "prefix" next)))
|
|
151
|
+
((= code 44)
|
|
152
|
+
(advance state)
|
|
153
|
+
(if (= (peek-code state) 64)
|
|
154
|
+
(progn
|
|
155
|
+
(advance state)
|
|
156
|
+
(js-call tokens :push (token "prefix" ",@")))
|
|
157
|
+
(js-call tokens :push (token "prefix" ","))))
|
|
158
|
+
(t
|
|
159
|
+
(let ((atom (read-atom-token state)))
|
|
160
|
+
(when (= (get (get atom :text) :length) 0)
|
|
161
|
+
(formatter-error "formatter tokenizer made no progress" filename))
|
|
162
|
+
(js-call tokens :push atom))))))
|
|
163
|
+
tokens))
|
|
164
|
+
|
|
165
|
+
(defun parse-node (tokens cursor filename)
|
|
166
|
+
(when (>= (get cursor :index) (get tokens :length))
|
|
167
|
+
(formatter-error "unexpected end of formatter token stream" filename))
|
|
168
|
+
(let* ((current (js-nth (get cursor :index) tokens))
|
|
169
|
+
(kind (get current :kind)))
|
|
170
|
+
(put cursor :index (1+ (get cursor :index)))
|
|
171
|
+
(cond
|
|
172
|
+
((or (= kind "atom") (= kind "string") (= kind "comment"))
|
|
173
|
+
current)
|
|
174
|
+
((= kind "prefix")
|
|
175
|
+
(let ((children (js-array)))
|
|
176
|
+
(while (and (< (get cursor :index) (get tokens :length))
|
|
177
|
+
(= (get (js-nth (get cursor :index) tokens) :kind)
|
|
178
|
+
"comment"))
|
|
179
|
+
(js-call children :push (js-nth (get cursor :index) tokens))
|
|
180
|
+
(put cursor :index (1+ (get cursor :index))))
|
|
181
|
+
(when (>= (get cursor :index) (get tokens :length))
|
|
182
|
+
(formatter-error "reader prefix has no target" filename))
|
|
183
|
+
(js-call children :push (parse-node tokens cursor filename))
|
|
184
|
+
(js-object :kind "prefix" :text (get current :text)
|
|
185
|
+
:children children)))
|
|
186
|
+
((= kind "open")
|
|
187
|
+
(let ((children (js-array))
|
|
188
|
+
(done false))
|
|
189
|
+
(while (not done)
|
|
190
|
+
(when (>= (get cursor :index) (get tokens :length))
|
|
191
|
+
(formatter-error "collection has no closing delimiter" filename))
|
|
192
|
+
(let ((next (js-nth (get cursor :index) tokens)))
|
|
193
|
+
(if (= (get next :kind) "close")
|
|
194
|
+
(progn
|
|
195
|
+
(unless (= (get next :text) (get current :closing))
|
|
196
|
+
(formatter-error "mismatched collection delimiter" filename))
|
|
197
|
+
(put cursor :index (1+ (get cursor :index)))
|
|
198
|
+
(setq done t))
|
|
199
|
+
(js-call children :push (parse-node tokens cursor filename)))))
|
|
200
|
+
(js-object :kind "collection" :text (get current :text)
|
|
201
|
+
:closing (get current :closing) :children children)))
|
|
202
|
+
(t
|
|
203
|
+
(formatter-error "unexpected closing delimiter" filename)))))
|
|
204
|
+
|
|
205
|
+
(defun concrete-tree (source filename)
|
|
206
|
+
(let* ((tokens (tokenize source filename))
|
|
207
|
+
(cursor (js-object :index 0))
|
|
208
|
+
(children (js-array)))
|
|
209
|
+
(while (< (get cursor :index) (get tokens :length))
|
|
210
|
+
(js-call children :push (parse-node tokens cursor filename)))
|
|
211
|
+
(js-object :kind "root" :children children)))
|
|
212
|
+
|
|
213
|
+
(defun flat-node (node)
|
|
214
|
+
(let ((kind (get node :kind)))
|
|
215
|
+
(cond
|
|
216
|
+
((or (= kind "atom") (= kind "string"))
|
|
217
|
+
(get node :text))
|
|
218
|
+
((= kind "comment") nil)
|
|
219
|
+
((= kind "prefix")
|
|
220
|
+
(let ((children (get node :children)))
|
|
221
|
+
(if (not (= (get children :length) 1))
|
|
222
|
+
nil
|
|
223
|
+
(let ((child (flat-node (js-nth 0 children))))
|
|
224
|
+
(and child (str (get node :text) child))))))
|
|
225
|
+
((= kind "collection")
|
|
226
|
+
(let ((pieces (js-array))
|
|
227
|
+
(children (get node :children))
|
|
228
|
+
(index 0)
|
|
229
|
+
(possible t))
|
|
230
|
+
(while (and possible (< index (get children :length)))
|
|
231
|
+
(let ((piece (flat-node (js-nth index children))))
|
|
232
|
+
(if piece
|
|
233
|
+
(js-call pieces :push piece)
|
|
234
|
+
(setq possible false)))
|
|
235
|
+
(setq index (1+ index)))
|
|
236
|
+
(and possible
|
|
237
|
+
(str
|
|
238
|
+
(get node :text)
|
|
239
|
+
(js-call pieces :join " ")
|
|
240
|
+
(get node :closing)))))
|
|
241
|
+
(t nil))))
|
|
242
|
+
|
|
243
|
+
(defun spaces (count)
|
|
244
|
+
(js-call " " :repeat count))
|
|
245
|
+
|
|
246
|
+
(defun list-leading-count (node)
|
|
247
|
+
(if (not (= (get node :text) "("))
|
|
248
|
+
0
|
|
249
|
+
(let ((children (get node :children)))
|
|
250
|
+
(if (= (get children :length) 0)
|
|
251
|
+
0
|
|
252
|
+
(let ((head (flat-node (js-nth 0 children))))
|
|
253
|
+
(cond
|
|
254
|
+
((and head (js-call declaration-heads :includes head)) 2)
|
|
255
|
+
((and head (js-call operand-heads :includes head)) 2)
|
|
256
|
+
(t 1)))))))
|
|
257
|
+
|
|
258
|
+
(defun append-inline-child (output child indent)
|
|
259
|
+
(let* ((piece (flat-node child))
|
|
260
|
+
(separator (if (= (get output :length) 1) "" " "))
|
|
261
|
+
(candidate (and piece (str output separator piece))))
|
|
262
|
+
(and candidate
|
|
263
|
+
(<= (+ indent (get candidate :length)) formatter-width)
|
|
264
|
+
candidate)))
|
|
265
|
+
|
|
266
|
+
(defun render-prefix (node indent)
|
|
267
|
+
(let ((children (get node :children)))
|
|
268
|
+
(if (= (get children :length) 1)
|
|
269
|
+
(str
|
|
270
|
+
(get node :text)
|
|
271
|
+
(render-node
|
|
272
|
+
(js-nth 0 children)
|
|
273
|
+
(+ indent (get (get node :text) :length))))
|
|
274
|
+
(let ((output (get node :text))
|
|
275
|
+
(index 0))
|
|
276
|
+
(while (< index (get children :length))
|
|
277
|
+
(setq output
|
|
278
|
+
(str output "\n" (spaces (+ indent formatter-indent))
|
|
279
|
+
(render-node
|
|
280
|
+
(js-nth index children)
|
|
281
|
+
(+ indent formatter-indent))))
|
|
282
|
+
(setq index (1+ index)))
|
|
283
|
+
output))))
|
|
284
|
+
|
|
285
|
+
(defun render-collection (node indent)
|
|
286
|
+
(let* ((flat (flat-node node))
|
|
287
|
+
(children (get node :children)))
|
|
288
|
+
(if (and flat (<= (+ indent (get flat :length)) formatter-width))
|
|
289
|
+
flat
|
|
290
|
+
(let ((output (get node :text))
|
|
291
|
+
(index 0)
|
|
292
|
+
(inline-left (list-leading-count node))
|
|
293
|
+
(last-comment false))
|
|
294
|
+
(while (and (> inline-left 0) (< index (get children :length)))
|
|
295
|
+
(let ((candidate
|
|
296
|
+
(append-inline-child
|
|
297
|
+
output (js-nth index children) indent)))
|
|
298
|
+
(if candidate
|
|
299
|
+
(progn
|
|
300
|
+
(setq output candidate)
|
|
301
|
+
(setq index (1+ index))
|
|
302
|
+
(setq inline-left (1- inline-left)))
|
|
303
|
+
(setq inline-left 0))))
|
|
304
|
+
(while (< index (get children :length))
|
|
305
|
+
(let* ((child (js-nth index children))
|
|
306
|
+
(comment (= (get child :kind) "comment")))
|
|
307
|
+
(setq output
|
|
308
|
+
(str output "\n" (spaces (+ indent formatter-indent))
|
|
309
|
+
(render-node child (+ indent formatter-indent))))
|
|
310
|
+
(setq last-comment comment))
|
|
311
|
+
(setq index (1+ index)))
|
|
312
|
+
(when last-comment
|
|
313
|
+
(setq output (str output "\n" (spaces indent))))
|
|
314
|
+
(str output (get node :closing))))))
|
|
315
|
+
|
|
316
|
+
(defun render-node (node indent)
|
|
317
|
+
(let ((kind (get node :kind)))
|
|
318
|
+
(cond
|
|
319
|
+
((or (= kind "atom") (= kind "string") (= kind "comment"))
|
|
320
|
+
(get node :text))
|
|
321
|
+
((= kind "prefix") (render-prefix node indent))
|
|
322
|
+
((= kind "collection") (render-collection node indent))
|
|
323
|
+
(t (formatter-error "unsupported concrete syntax node" nil)))))
|
|
324
|
+
|
|
325
|
+
(defun render-root (root)
|
|
326
|
+
(let ((children (get root :children))
|
|
327
|
+
(output "")
|
|
328
|
+
(index 0)
|
|
329
|
+
(seen-form false)
|
|
330
|
+
(previous-comment false))
|
|
331
|
+
(while (< index (get children :length))
|
|
332
|
+
(let* ((child (js-nth index children))
|
|
333
|
+
(comment (= (get child :kind) "comment")))
|
|
334
|
+
(when (> (get output :length) 0)
|
|
335
|
+
(setq output
|
|
336
|
+
(str output
|
|
337
|
+
(if (and seen-form (not previous-comment) (not comment))
|
|
338
|
+
"\n\n"
|
|
339
|
+
"\n"))))
|
|
340
|
+
(setq output (str output (render-node child 0)))
|
|
341
|
+
(unless comment (setq seen-form t))
|
|
342
|
+
(setq previous-comment comment))
|
|
343
|
+
(setq index (1+ index)))
|
|
344
|
+
(if (= (get output :length) 0) "" (str output "\n"))))
|
|
345
|
+
|
|
346
|
+
(defun format-source (source filename)
|
|
347
|
+
(unless (= (js* "typeof source") "string")
|
|
348
|
+
(formatter-error "formatter source must be a string" filename))
|
|
349
|
+
(read-string source filename)
|
|
350
|
+
(let ((formatted (render-root (concrete-tree source filename))))
|
|
351
|
+
(read-string formatted filename)
|
|
352
|
+
formatted))
|
|
353
|
+
|
|
354
|
+
(export
|
|
355
|
+
formatter-format
|
|
356
|
+
formatter-version
|
|
357
|
+
formatter-width
|
|
358
|
+
formatter-indent
|
|
359
|
+
format-source))
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
(module bootstrap.compiler.ir
|
|
2
|
+
(defconst ir-format "eliscript-ir")
|
|
3
|
+
(defconst ir-version 1)
|
|
4
|
+
(defconst ir-max-depth 512)
|
|
5
|
+
|
|
6
|
+
(defconst node-kinds
|
|
7
|
+
(js-call globalThis.Object :freeze
|
|
8
|
+
(js-array "module-declaration" "import-declaration" "import-default"
|
|
9
|
+
"import-namespace" "import-named" "variable-declaration"
|
|
10
|
+
"function-declaration" "export-declaration" "export-default"
|
|
11
|
+
"expression-statement" "parameter-binding" "array-binding-pattern"
|
|
12
|
+
"map-binding-pattern" "map-binding-entry"
|
|
13
|
+
"binding-name" "binding-hole" "reference" "literal"
|
|
14
|
+
"array-literal" "persistent-list-literal" "persistent-vector-literal"
|
|
15
|
+
"persistent-map-literal" "persistent-set-literal" "persistent-queue-literal"
|
|
16
|
+
"quoted-literal" "function-expression"
|
|
17
|
+
"await-expression" "throw-expression" "try-expression"
|
|
18
|
+
"catch-clause" "catch-binding" "finally-clause" "conditional"
|
|
19
|
+
"conditional-sugar" "conditional-chain" "conditional-clause"
|
|
20
|
+
"sequence" "lexical-bindings" "lexical-binding" "assignment"
|
|
21
|
+
"assignment-pair" "loop" "binding-loop" "recur"
|
|
22
|
+
"short-circuit" "intrinsic"
|
|
23
|
+
"object-literal" "object-property" "property-read" "property-write"
|
|
24
|
+
"method-call" "constructor-call" "raw-javascript" "print"
|
|
25
|
+
"string-concat" "invoke" "apply-call" "call")))
|
|
26
|
+
|
|
27
|
+
(defconst fail
|
|
28
|
+
(js* "(message) => { throw new Error(message); }"))
|
|
29
|
+
|
|
30
|
+
(defconst schema-fail
|
|
31
|
+
(js* "(message) => { const error = new Error(message); error.eliscriptDiagnostic = { format: 'eliscript-diagnostic', version: 1, code: 'ELI-I0001', severity: 'error', phase: 'ir-schema', message }; throw error; }"))
|
|
32
|
+
|
|
33
|
+
(defconst array-p (js* "Array.isArray"))
|
|
34
|
+
(defconst dense-json-array-p
|
|
35
|
+
(js* "(value) => { if (!Array.isArray(value) || Object.getPrototypeOf(value) !== Array.prototype || Object.getOwnPropertySymbols(value).length !== 0) return false; const names = Object.getOwnPropertyNames(value); if (names.length !== value.length + 1 || names[names.length - 1] !== 'length') return false; for (let index = 0; index < value.length; index += 1) { const name = String(index); if (names[index] !== name) return false; const descriptor = Object.getOwnPropertyDescriptor(value, name); if (!descriptor || !descriptor.enumerable || !Object.hasOwn(descriptor, 'value')) return false; } return true; }"))
|
|
36
|
+
(defconst plain-record-keys
|
|
37
|
+
(js* "(value) => { if (value === null || typeof value !== 'object' || Array.isArray(value)) return null; const prototype = Object.getPrototypeOf(value); if (prototype !== Object.prototype && prototype !== null) return null; if (Object.getOwnPropertySymbols(value).length !== 0) return null; const names = Object.getOwnPropertyNames(value); for (const name of names) { const descriptor = Object.getOwnPropertyDescriptor(value, name); if (!descriptor || !descriptor.enumerable || !Object.hasOwn(descriptor, 'value')) return null; } return names.sort(); }"))
|
|
38
|
+
(defconst has-own (js* "Object.hasOwn"))
|
|
39
|
+
(defconst finite-number-p (js* "Number.isFinite"))
|
|
40
|
+
(defconst non-negative-integer-p
|
|
41
|
+
(js* "(value) => Number.isSafeInteger(value) && value >= 0"))
|
|
42
|
+
(defconst positive-integer-p
|
|
43
|
+
(js* "(value) => Number.isSafeInteger(value) && value >= 1"))
|
|
44
|
+
(defconst parse-json
|
|
45
|
+
(js* "(source, reject) => { try { return JSON.parse(source); } catch (error) { return reject(`invalid canonical IR JSON: ${error.message}`); } }"))
|
|
46
|
+
|
|
47
|
+
(defconst node-kind-set
|
|
48
|
+
(new globalThis.Set node-kinds))
|
|
49
|
+
|
|
50
|
+
(defun require-closed-record (value expected label)
|
|
51
|
+
(let ((keys (funcall plain-record-keys value)))
|
|
52
|
+
(when (nil? keys)
|
|
53
|
+
(schema-fail (str label " must be a plain data object")))
|
|
54
|
+
(unless (= (get keys :length) (get expected :length))
|
|
55
|
+
(schema-fail (str label " has unknown or missing fields")))
|
|
56
|
+
(js-call expected :forEach
|
|
57
|
+
(lambda (key)
|
|
58
|
+
(unless (js-call keys :includes key)
|
|
59
|
+
(schema-fail (str label " has unknown or missing fields")))))
|
|
60
|
+
keys))
|
|
61
|
+
|
|
62
|
+
(defun canonical-json-array (value path active depth)
|
|
63
|
+
(unless (funcall dense-json-array-p value)
|
|
64
|
+
(schema-fail (str path " must be a dense plain array")))
|
|
65
|
+
(when (js-call active :has value)
|
|
66
|
+
(schema-fail (str path " contains a cyclic JSON value")))
|
|
67
|
+
(js-call active :add value)
|
|
68
|
+
(let ((parts (js-array))
|
|
69
|
+
(index 0))
|
|
70
|
+
(while (< index (get value :length))
|
|
71
|
+
(js-call parts :push
|
|
72
|
+
(canonical-json-value
|
|
73
|
+
(js-nth index value)
|
|
74
|
+
(str path "[" index "]")
|
|
75
|
+
active
|
|
76
|
+
(1+ depth)))
|
|
77
|
+
(setq index (1+ index)))
|
|
78
|
+
(js-call active :delete value)
|
|
79
|
+
(str "[" (js-call parts :join ",") "]")))
|
|
80
|
+
|
|
81
|
+
(defun canonical-json-record (value path active depth)
|
|
82
|
+
(let ((keys (funcall plain-record-keys value)))
|
|
83
|
+
(when (nil? keys)
|
|
84
|
+
(schema-fail (str path " must be a plain data object")))
|
|
85
|
+
(when (js-call active :has value)
|
|
86
|
+
(schema-fail (str path " contains a cyclic JSON value")))
|
|
87
|
+
(js-call active :add value)
|
|
88
|
+
(let ((parts (js-array)))
|
|
89
|
+
(js-call keys :forEach
|
|
90
|
+
(lambda (key)
|
|
91
|
+
(js-call parts :push
|
|
92
|
+
(str
|
|
93
|
+
(JSON/stringify key)
|
|
94
|
+
":"
|
|
95
|
+
(canonical-json-value
|
|
96
|
+
(get value key)
|
|
97
|
+
(str path "." key)
|
|
98
|
+
active
|
|
99
|
+
(1+ depth))))))
|
|
100
|
+
(js-call active :delete value)
|
|
101
|
+
(str "{" (js-call parts :join ",") "}"))))
|
|
102
|
+
|
|
103
|
+
(defun canonical-json-value (value path active depth)
|
|
104
|
+
(when (> depth ir-max-depth)
|
|
105
|
+
(schema-fail (str path " exceeds IR nesting depth " ir-max-depth)))
|
|
106
|
+
(let ((type (js* "typeof value")))
|
|
107
|
+
(cond
|
|
108
|
+
((nil? value) "null")
|
|
109
|
+
((= type "boolean") (if value "true" "false"))
|
|
110
|
+
((= type "number")
|
|
111
|
+
(unless (funcall finite-number-p value)
|
|
112
|
+
(schema-fail (str path " must contain only finite numbers")))
|
|
113
|
+
(if (js* "Object.is(value, -0)") "-0" (JSON/stringify value)))
|
|
114
|
+
((= type "string") (JSON/stringify value))
|
|
115
|
+
((funcall array-p value)
|
|
116
|
+
(canonical-json-array value path active depth))
|
|
117
|
+
((= type "object")
|
|
118
|
+
(canonical-json-record value path active depth))
|
|
119
|
+
(t
|
|
120
|
+
(schema-fail (str path " contains unsupported JSON type " type))))))
|
|
121
|
+
|
|
122
|
+
(defun canonical-json (value)
|
|
123
|
+
(canonical-json-value value "$" (new globalThis.Set) 0))
|
|
124
|
+
|
|
125
|
+
(defun validate-span (span)
|
|
126
|
+
(require-closed-record
|
|
127
|
+
span
|
|
128
|
+
(js-array "column" "end" "endColumn" "endLine" "filename" "line" "start")
|
|
129
|
+
"IR span")
|
|
130
|
+
(unless (= (js* "typeof span.filename") "string")
|
|
131
|
+
(schema-fail "IR span filename must be a string"))
|
|
132
|
+
(js-call (js-array "start" "end") :forEach
|
|
133
|
+
(lambda (key)
|
|
134
|
+
(unless (funcall non-negative-integer-p (get span key))
|
|
135
|
+
(schema-fail (str "IR span " key " must be a non-negative integer")))))
|
|
136
|
+
(js-call (js-array "line" "column" "endLine" "endColumn") :forEach
|
|
137
|
+
(lambda (key)
|
|
138
|
+
(unless (funcall positive-integer-p (get span key))
|
|
139
|
+
(schema-fail (str "IR span " key " must be a positive integer")))))
|
|
140
|
+
(when (> (get span :start) (get span :end))
|
|
141
|
+
(schema-fail "IR span start must not exceed end"))
|
|
142
|
+
span)
|
|
143
|
+
|
|
144
|
+
(defun validate-node (node active depth)
|
|
145
|
+
(when (> depth ir-max-depth)
|
|
146
|
+
(schema-fail (str "IR node exceeds nesting depth " ir-max-depth)))
|
|
147
|
+
(when (js-call active :has node)
|
|
148
|
+
(schema-fail "IR node children must not contain a cycle"))
|
|
149
|
+
(let ((keys (funcall plain-record-keys node)))
|
|
150
|
+
(when (nil? keys)
|
|
151
|
+
(schema-fail "IR node must be a plain data object"))
|
|
152
|
+
(let ((expected
|
|
153
|
+
(if (js-call keys :includes "properties")
|
|
154
|
+
(js-array "children" "kind" "properties" "span" "value")
|
|
155
|
+
(js-array "children" "kind" "span" "value"))))
|
|
156
|
+
(require-closed-record node expected "IR node")))
|
|
157
|
+
(unless (and (= (js* "typeof node.kind") "string")
|
|
158
|
+
(node-kind-p (get node :kind)))
|
|
159
|
+
(schema-fail (str "unknown Eliscript IR node kind: " (get node :kind))))
|
|
160
|
+
(validate-span (get node :span))
|
|
161
|
+
(unless (funcall dense-json-array-p (get node :children))
|
|
162
|
+
(schema-fail "IR node children must be a dense plain array"))
|
|
163
|
+
(when (funcall has-own node "properties")
|
|
164
|
+
(when (nil? (funcall plain-record-keys (get node :properties)))
|
|
165
|
+
(schema-fail "IR node properties must be a plain data object")))
|
|
166
|
+
(js-call active :add node)
|
|
167
|
+
(js-call (get node :children) :forEach
|
|
168
|
+
(lambda (child) (validate-node child active (1+ depth))))
|
|
169
|
+
(js-call active :delete node)
|
|
170
|
+
node)
|
|
171
|
+
|
|
172
|
+
(defun validate-program (program)
|
|
173
|
+
(require-closed-record
|
|
174
|
+
program (js-array "body" "filename") "IR program")
|
|
175
|
+
(unless (= (js* "typeof program.filename") "string")
|
|
176
|
+
(schema-fail "IR program filename must be a string"))
|
|
177
|
+
(unless (funcall dense-json-array-p (get program :body))
|
|
178
|
+
(schema-fail "IR program body must be a dense plain array"))
|
|
179
|
+
(let ((active (new globalThis.Set)))
|
|
180
|
+
(js-call (get program :body) :forEach
|
|
181
|
+
(lambda (node) (validate-node node active 0))))
|
|
182
|
+
program)
|
|
183
|
+
|
|
184
|
+
(defun validate-document (document)
|
|
185
|
+
(require-closed-record
|
|
186
|
+
document (js-array "format" "program" "version") "IR document")
|
|
187
|
+
(unless (= (get document :format) ir-format)
|
|
188
|
+
(schema-fail (str "unsupported IR format: " (get document :format))))
|
|
189
|
+
(unless (= (get document :version) ir-version)
|
|
190
|
+
(schema-fail (str "unsupported IR version: " (get document :version))))
|
|
191
|
+
(validate-program (get document :program))
|
|
192
|
+
document)
|
|
193
|
+
|
|
194
|
+
(defun serialize-program (program)
|
|
195
|
+
(let ((document
|
|
196
|
+
(js-object
|
|
197
|
+
:format ir-format
|
|
198
|
+
:version ir-version
|
|
199
|
+
:program program)))
|
|
200
|
+
(validate-document document)
|
|
201
|
+
(canonical-json document)))
|
|
202
|
+
|
|
203
|
+
(defun deserialize-program (source)
|
|
204
|
+
(unless (= (js* "typeof source") "string")
|
|
205
|
+
(schema-fail "canonical IR source must be a string"))
|
|
206
|
+
(let* ((document (funcall parse-json source schema-fail))
|
|
207
|
+
(_validated (validate-document document))
|
|
208
|
+
(canonical (canonical-json document)))
|
|
209
|
+
(unless (= source canonical)
|
|
210
|
+
(schema-fail "IR source is valid JSON but not canonical IR JSON"))
|
|
211
|
+
(get document :program)))
|
|
212
|
+
|
|
213
|
+
(defun node-kind-p (kind)
|
|
214
|
+
(js-call node-kind-set :has kind))
|
|
215
|
+
|
|
216
|
+
(defun reference-node-kind-p (kind)
|
|
217
|
+
(js-call node-kinds :includes kind))
|
|
218
|
+
|
|
219
|
+
(defun make-program (filename body)
|
|
220
|
+
(unless (js* "Array.isArray(body)")
|
|
221
|
+
(fail "IR program body must be an array"))
|
|
222
|
+
(unless (js-call body :every node-p)
|
|
223
|
+
(fail "IR program body must contain only IR nodes"))
|
|
224
|
+
(js-object :filename filename :body body))
|
|
225
|
+
|
|
226
|
+
(defun node-p (value)
|
|
227
|
+
(and value
|
|
228
|
+
(= (js* "typeof value") "object")
|
|
229
|
+
(node-kind-p (get value :kind))))
|
|
230
|
+
|
|
231
|
+
(defun reference-node-p (value)
|
|
232
|
+
(and value
|
|
233
|
+
(= (js* "typeof value") "object")
|
|
234
|
+
(reference-node-kind-p (get value :kind))))
|
|
235
|
+
|
|
236
|
+
(defun make-node (kind span value children properties)
|
|
237
|
+
(unless (node-kind-p kind)
|
|
238
|
+
(fail (str "unknown Eliscript IR node kind: " kind)))
|
|
239
|
+
(unless (js* "Array.isArray(children)")
|
|
240
|
+
(fail "IR node children must be an array"))
|
|
241
|
+
(unless (js-call children :every node-p)
|
|
242
|
+
(fail "IR node children must all be IR nodes"))
|
|
243
|
+
(let ((node
|
|
244
|
+
(js-object
|
|
245
|
+
:kind kind
|
|
246
|
+
:span span
|
|
247
|
+
:value value
|
|
248
|
+
:children children)))
|
|
249
|
+
(when properties
|
|
250
|
+
(put node :properties properties))
|
|
251
|
+
node))
|
|
252
|
+
|
|
253
|
+
(defun program-filename (program)
|
|
254
|
+
(get program :filename))
|
|
255
|
+
|
|
256
|
+
(defun program-body (program)
|
|
257
|
+
(get program :body))
|
|
258
|
+
|
|
259
|
+
(defun ir-kind (node)
|
|
260
|
+
(get node :kind))
|
|
261
|
+
|
|
262
|
+
(defun ir-span (node)
|
|
263
|
+
(get node :span))
|
|
264
|
+
|
|
265
|
+
(defun ir-value (node)
|
|
266
|
+
(get node :value))
|
|
267
|
+
|
|
268
|
+
(defun ir-children (node)
|
|
269
|
+
(get node :children))
|
|
270
|
+
|
|
271
|
+
(defun ir-properties (node)
|
|
272
|
+
(get node :properties))
|
|
273
|
+
|
|
274
|
+
(defun ir-property (node name)
|
|
275
|
+
(let ((properties (ir-properties node)))
|
|
276
|
+
(and properties (get properties name))))
|
|
277
|
+
|
|
278
|
+
(export
|
|
279
|
+
ir-format
|
|
280
|
+
ir-version
|
|
281
|
+
node-kinds
|
|
282
|
+
node-kind-p
|
|
283
|
+
reference-node-kind-p
|
|
284
|
+
node-p
|
|
285
|
+
reference-node-p
|
|
286
|
+
make-program
|
|
287
|
+
make-node
|
|
288
|
+
program-filename
|
|
289
|
+
program-body
|
|
290
|
+
ir-kind
|
|
291
|
+
ir-span
|
|
292
|
+
ir-value
|
|
293
|
+
ir-children
|
|
294
|
+
ir-properties
|
|
295
|
+
ir-property
|
|
296
|
+
serialize-program
|
|
297
|
+
deserialize-program))
|