blob: 604fd85516577715d366e077747d8f9d1374786d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
(.module:
[lux (#- Module)
[type (#+ :share)]
[abstract
["." monad (#+ do)]]
[control
["ex" exception (#+ exception:)]]
[data
["." product]
["." error (#+ Error)]
["." text ("#@." hash)
format]
[collection
["." list ("#@." functor)]
["." dictionary]
["." set]]]
["." macro]
[world
["." file]]]
["." //
["#." syntax (#+ Aliases)]
["#." evaluation]
["#/" // (#+ Instancer)
["#." analysis]
["#." synthesis]
["#." statement (#+ Requirements)]
["#." host]
["#." phase
[macro (#+ Expander)]
[".P" analysis
["." module]]
[".P" synthesis]
["." generation]
[".P" statement]
["." extension
[".E" analysis]
[".E" synthesis]
[".E" statement]]]
[meta
[archive
["." signature]
["." key (#+ Key)]
["." descriptor (#+ Module)]
["." document]]]]])
(def: #export info
Info
{#.target (`` (for {(~~ (static ///host.common-lisp)) ///host.common-lisp
(~~ (static ///host.js)) ///host.js
(~~ (static ///host.old)) ///host.jvm
(~~ (static ///host.jvm)) ///host.jvm
(~~ (static ///host.lua)) ///host.lua
(~~ (static ///host.php)) ///host.php
(~~ (static ///host.python)) ///host.python
(~~ (static ///host.r)) ///host.r
(~~ (static ///host.ruby)) ///host.ruby
(~~ (static ///host.scheme)) ///host.scheme}))
#.version //.version
#.mode #.Build})
(def: #export (state expander host generate generation-bundle program)
(All [anchor expression statement]
(-> Expander
(generation.Host expression statement)
(generation.Phase anchor expression statement)
(generation.Bundle anchor expression statement)
(-> expression statement)
(///statement.State+ anchor expression statement)))
(let [synthesis-state [synthesisE.bundle ///synthesis.init]
generation-state [generation-bundle (generation.state host)]
eval (//evaluation.evaluator expander synthesis-state generation-state generate)
analysis-state [(analysisE.bundle eval) (///analysis.state ..info host)]]
[(statementE.bundle expander program)
{#///statement.analysis {#///statement.state analysis-state
#///statement.phase (analysisP.phase expander)}
#///statement.synthesis {#///statement.state synthesis-state
#///statement.phase synthesisP.phase}
#///statement.generation {#///statement.state generation-state
#///statement.phase generate}}]))
(type: Reader
(-> Source (Either [Source Text] [Source Code])))
(def: (reader current-module aliases [cursor offset source-code])
(-> Module Aliases Source (///analysis.Operation Reader))
(function (_ [bundle state])
(#error.Success [[bundle state]
(//syntax.parse current-module aliases ("lux text size" source-code))])))
(def: (read source reader)
(-> Source Reader (///analysis.Operation [Source Code]))
(function (_ [bundle compiler])
(case (reader source)
(#error.Failure [source' error])
(#error.Failure error)
(#error.Success [source' output])
(let [[cursor _] output]
(#error.Success [[bundle (|> compiler
(set@ #.source source')
(set@ #.cursor cursor))]
[source' output]])))))
(type: (Operation a)
(All [anchor expression statement]
(///statement.Operation anchor expression statement a)))
(def: (begin dependencies hash input)
(-> (List Module) Nat ///.Input
(All [anchor expression statement]
(///statement.Operation anchor expression statement
[Source (generation.Buffer statement)])))
(///statement.lift-analysis
(do ///phase.monad
[#let [module (get@ #///.module input)]
_ (module.create hash module)
_ (///analysis.set-current-module module)
_ (monad.map @ module.import dependencies)
#let [source (///analysis.source (get@ #///.module input) (get@ #///.code input))]
_ (///analysis.set-source-code source)]
(wrap [source generation.empty-buffer]))))
(def: (end module)
(-> Module (Operation Any))
(do ///phase.monad
[_ (///statement.lift-analysis
(module.set-compiled module))]
(///statement.lift-generation
(generation.save-buffer! module))))
## TODO: Inline ASAP
(def: (get-current-buffer old-buffer)
(All [statement]
(-> (generation.Buffer statement)
(All [anchor expression]
(///statement.Operation anchor expression statement
(generation.Buffer statement)))))
(///statement.lift-generation
generation.buffer))
## TODO: Inline ASAP
(def: (process-statement expander pre-buffer code)
(All [statement]
(-> Expander (generation.Buffer statement) Code
(All [anchor expression]
(///statement.Operation anchor expression statement
[Requirements (generation.Buffer statement)]))))
(do ///phase.monad
[_ (///statement.lift-generation
(generation.set-buffer pre-buffer))
requirements (let [execute! (statementP.phase expander)]
(execute! code))
post-buffer (..get-current-buffer pre-buffer)]
(wrap [requirements post-buffer])))
(def: (iteration expander reader source pre-buffer)
(All [statement]
(-> Expander Reader Source (generation.Buffer statement)
(All [anchor expression]
(///statement.Operation anchor expression statement
[Source Requirements (generation.Buffer statement)]))))
(do ///phase.monad
[[source code] (///statement.lift-analysis
(..read source reader))
[requirements post-buffer] (process-statement expander pre-buffer code)]
(wrap [source requirements post-buffer])))
(def: (iterate expander module source pre-buffer aliases)
(All [statement]
(-> Expander Module Source (generation.Buffer statement) Aliases
(All [anchor expression]
(///statement.Operation anchor expression statement
(Maybe [Source Requirements (generation.Buffer statement)])))))
(do ///phase.monad
[reader (///statement.lift-analysis
(..reader module aliases source))]
(function (_ state)
(case (///phase.run' state (..iteration expander reader source pre-buffer))
(#error.Success [state source&requirements&buffer])
(#error.Success [state (#.Some source&requirements&buffer)])
(#error.Failure error)
(if (ex.match? //syntax.end-of-file error)
(#error.Success [state #.None])
(ex.with-stack ///.cannot-compile module (#error.Failure error)))))))
(def: (default-dependencies prelude input)
(-> Module ///.Input (List Module))
(if (text@= prelude (get@ #///.module input))
(list)
(list prelude)))
(def: module-aliases
(-> .Module Aliases)
(|>> (get@ #.module-aliases) (dictionary.from-list text.hash)))
(def: #export (compiler expander prelude)
(-> Expander Module
(All [anchor expression statement]
(Instancer (///statement.State+ anchor expression statement) .Module)))
(let [execute! (statementP.phase expander)]
(function (_ key parameters input)
(let [dependencies (default-dependencies prelude input)]
{#///.dependencies dependencies
#///.process (function (_ state archive)
(do error.monad
[#let [hash (text@hash (get@ #///.code input))]
[state [source buffer]] (<| (///phase.run' state)
(..begin dependencies hash input))
#let [module (get@ #///.module input)]]
(loop [iteration (<| (///phase.run' state)
(..iterate expander module source buffer //syntax.no-aliases))]
(do @
[[state ?source&requirements&buffer] iteration]
(case ?source&requirements&buffer
#.None
(do @
[[state analysis-module] (<| (///phase.run' state)
(do ///phase.monad
[_ (..end module)]
(<| (: (Operation .Module))
///statement.lift-analysis
extension.lift
macro.current-module)))
#let [descriptor {#descriptor.hash hash
#descriptor.name module
#descriptor.file (get@ #///.file input)
#descriptor.references (set.from-list text.hash dependencies)
#descriptor.state #.Compiled}]]
(wrap [state
(#.Right [[descriptor (document.write key analysis-module)]
(dictionary.new text.hash)])]))
(#.Some [source requirements buffer])
(wrap [state
(#.Left {#///.dependencies (|> requirements
(get@ #///statement.imports)
(list@map product.left))
#///.process (function (_ state archive)
(recur (<| (///phase.run' state)
(do ///phase.monad
[analysis-module (<| (: (Operation .Module))
///statement.lift-analysis
extension.lift
macro.current-module)
_ (monad.map @ execute! (get@ #///statement.referrals requirements))]
(..iterate expander module source buffer (..module-aliases analysis-module))))))})])
)))))}))))
(def: #export key
(Key .Module)
(key.key {#signature.name (name-of ..compiler)
#signature.version //.version}
(module.new 0)))
|