blob: b1e65c952eceb2191b40452b3c19a77a7a431fa1 (
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
(.module:
lux
(lux (control [monad #+ do]
["ex" exception #+ exception:])
(concurrency ["P" promise]
["T" task])
(data [product]
["e" error]
[text "text/" Hash<Text>]
text/format
(coll [list "list/" Functor<List>]
[dict]))
[macro]
(lang [syntax #+ Aliases]
(type ["tc" check]))
[host]
[io #+ IO Process io]
(world [blob #+ Blob]
[file #+ File]))
(luxc ["&" lang]
["&." io]
[cache]
[cache/description]
[cache/io]
(lang [".L" module]
[".L" host]
[".L" macro]
[".L" extension]
(extension [".E" analysis]
[".E" synthesis]
[".E" translation]
[".E" statement])
(host ["$" jvm])
(analysis [".A" expression]
[".A" common])
(synthesis [".S" expression])
(translation [".T" runtime]
[".T" statement]
[".T" common #+ Artifacts]
[".T" expression]
[".T" eval]
[".T" imports])
["&." eval])
))
(def: analyse
(&.Analyser)
(expressionA.analyser &eval.eval))
(exception: #export Macro-Expansion-Failed)
(exception: #export Unrecognized-Statement)
(exception: #export Invalid-Macro)
(def: (process-annotations annsC)
(-> Code (Meta [$.Inst Code]))
(do macro.Monad<Meta>
[[_ annsA] (&.with-scope
(&.with-type Code
(analyse annsC)))
annsI (expressionT.translate (expressionS.synthesize annsA))
annsV (evalT.eval annsI)]
(wrap [annsI (:! Code annsV)])))
(def: (switch-compiler new-compiler)
(-> Compiler (Meta Aliases))
(function [old-compiler]
((do macro.Monad<Meta>
[this macro.current-module]
(wrap (|> this (get@ #.module-aliases) (dict.from-list text.Hash<Text>) (: Aliases))))
new-compiler)))
(def: #export (translate translate-module aliases code)
(-> (-> Text Compiler (Process Compiler)) Aliases Code (Meta Aliases))
(case code
(^code ("lux module" (~ annsC)))
(do macro.Monad<Meta>
[[annsI annsV] (process-annotations annsC)
process (importsT.translate-imports translate-module annsV)]
(case (io.run process)
(#e.Success compiler')
(switch-compiler compiler')
(#e.Error error)
(macro.fail error)))
(^code ((~ [_ (#.Text statement)]) (~+ argsC+)))
(do macro.Monad<Meta>
[statement (extensionL.find-statement statement)
_ (statement argsC+)]
(wrap aliases))
(^code ((~ macroC) (~+ argsC+)))
(do macro.Monad<Meta>
[[_ macroA] (&.with-scope
(&.with-type Macro
(analyse macroC)))
[_macroT _macroM _macroV] (case macroA
[_ (#.Symbol macro-name)]
(&.with-error-tracking
(macro.find-def macro-name))
_
(&.throw Invalid-Macro (%code code)))
expansion (: (Meta (List Code))
(function [compiler]
(case (macroL.expand (:! Macro _macroV) argsC+ compiler)
(#e.Error error)
((&.throw Macro-Expansion-Failed error) compiler)
output
output)))
expansion-aliases (monad.map @ (translate translate-module aliases) expansion)]
(wrap (if (dict.empty? aliases)
(loop [expansion-aliases expansion-aliases]
(case expansion-aliases
#.Nil
aliases
(#.Cons head tail)
(if (dict.empty? head)
(recur tail)
head)))
aliases)))
_
(&.throw Unrecognized-Statement (%code code))))
(def: (forgive-eof action)
(-> (Meta Unit) (Meta Unit))
(function [compiler]
(case (action compiler)
(#e.Error error)
(if (ex.match? syntax.End-Of-File error)
(#e.Success [compiler []])
(#e.Error error))
output
output)))
(def: #export prelude Text "lux")
(def: (with-active-compilation [module-name file-name source-code] action)
(All [a] (-> [Text Text Text] (Meta a) (Meta a)))
(do macro.Monad<Meta>
[#let [init-cursor [file-name +1 +0]]
output (&.with-source-code [init-cursor +0 source-code]
action)
_ (moduleL.flag-compiled! module-name)]
(wrap output)))
(def: (read current-module aliases)
(-> Text Aliases (Meta Code))
(function [compiler]
(case (syntax.read current-module aliases (get@ #.source compiler))
(#e.Error error)
(#e.Error error)
(#e.Success [source' output])
(#e.Success [(set@ #.source source' compiler)
output]))))
(for {"JVM" (as-is (host.import java/lang/String
(getBytes [String] #try (Array byte)))
(def: text-to-blob
(-> Text Blob)
(|>> (:! String)
(String::getBytes ["UTF-8"])
e.assume)))})
(def: (write-module target-dir file-name module-name module artifacts)
(-> File Text Text Module Artifacts (Process Unit))
(do io.Monad<Process>
[_ (monad.map @ (product.uncurry (&io.write target-dir))
(dict.entries artifacts))]
(&io.write target-dir
(format module-name "/" cache.descriptor-name)
(text-to-blob (%code (cache/description.write file-name module))))))
(def: no-aliases Aliases (dict.new text.Hash<Text>))
(def: #export (translate-module source-dirs target-dir module-name compiler)
(-> (List File) File Text Compiler (Process Compiler))
(do io.Monad<Process>
[[file-name file-content] (&io.read source-dirs module-name)
#let [module-hash (text/hash file-content)
translate-module (translate-module source-dirs target-dir)]]
(case (macro.run' compiler
(do macro.Monad<Meta>
[[module artifacts _] (moduleL.with-module module-hash module-name
(commonT.with-artifacts
(with-active-compilation [module-name
file-name
file-content]
(forgive-eof
(loop [aliases no-aliases]
(do @
[code (read module-name aliases)
#let [[cursor _] code]
aliases' (&.with-cursor cursor
(translate translate-module aliases code))]
(forgive-eof (recur aliases'))))))))]
(wrap [module artifacts])))
(#e.Success [compiler [module artifacts]])
(do @
[_ (&io.prepare-module target-dir module-name)
_ (write-module target-dir file-name module-name module artifacts)]
(wrap compiler))
(#e.Error error)
(io.fail error))))
(def: init-cursor Cursor ["" +1 +0])
(def: #export init-type-context
Type-Context
{#.ex-counter +0
#.var-counter +0
#.var-bindings (list)})
(def: #export init-info
Info
{#.target (for {"JVM" "JVM"
"JS" "JS"})
#.version &.version
#.mode #.Build})
(def: #export (init-compiler host)
(-> commonT.Host Compiler)
{#.info init-info
#.source [init-cursor +0 ""]
#.cursor .dummy-cursor
#.current-module #.None
#.modules (list)
#.scopes (list)
#.type-context init-type-context
#.expected #.None
#.seed +0
#.scope-type-vars (list)
#.extensions (:! Void extensionL.fresh)
#.host (:! Void host)})
(def: (initialize sources target)
(-> (List File) File (Process Compiler))
(do io.Monad<Process>
[compiler (: (Process Compiler)
(case (runtimeT.translate (init-compiler (io.run hostL.init-host)))
(#e.Error error)
(io.fail error)
(#e.Success [compiler [runtime-bc function-bc]])
(do @
[_ (&io.prepare-target target)
_ (&io.write target (format hostL.runtime-class ".class") runtime-bc)
_ (&io.write target (format hostL.function-class ".class") function-bc)
_ (cache/io.pre-load sources target (commonT.load-definition compiler))]
(wrap (set@ #.extensions
(:! Void
{#extensionL.analysis analysisE.defaults
#extensionL.synthesis synthesisE.defaults
#extensionL.translation translationE.defaults
#extensionL.statement statementE.defaults})
compiler)))))]
(translate-module sources target prelude compiler)))
(def: #export (translate-program sources target program)
(-> (List File) File Text (Process Unit))
(do io.Monad<Process>
[compiler (initialize sources target)
_ (translate-module sources target program compiler)
## _ (cache/io.clean target ...)
#let [_ (log! "Compilation complete!")]]
(wrap [])))
|