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
|
## Copyright (c) Eduardo Julian. All rights reserved.
## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
## If a copy of the MPL was not distributed with this file,
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;import lux
(lux (control (monoid #as m)
(functor #as F)
(monad #as M #refer (#only do)))
(data (list #as l #refer #all #open ("" List/Functor))
(text #as text)
(number (int #open ("i" Int/Eq))))
(meta lux
ast
syntax)))
## [Utils]
## Parsers
(def finally^
(Parser AST)
(form^ (do Parser/Monad
[_ (symbol?^ ["" "finally"])
expr id^]
(wrap expr))))
(def catch^
(Parser (, Text Ident AST))
(form^ (do Parser/Monad
[_ (symbol?^ ["" "catch"])
ex-class local-symbol^
ex symbol^
expr id^]
(wrap [ex-class ex expr]))))
(def method-decl^
(Parser (, (List Text) Text (List Text) Text))
(form^ (do Parser/Monad
[modifiers (*^ local-tag^)
name local-symbol^
inputs (tuple^ (*^ local-symbol^))
output local-symbol^]
(wrap [modifiers name inputs output]))))
(def field-decl^
(Parser (, (List Text) Text Text))
(form^ (do Parser/Monad
[modifiers (*^ local-tag^)
name local-symbol^
class local-symbol^]
(wrap [modifiers name class]))))
(def arg-decl^
(Parser (, Text Text))
(form^ (do Parser/Monad
[arg-name local-symbol^
arg-class local-symbol^]
(wrap [arg-name arg-class]))))
(def method-def^
(Parser (, (List Text) Text (List (, Text Text)) Text AST))
(form^ (do Parser/Monad
[modifiers (*^ local-tag^)
name local-symbol^
inputs (tuple^ (*^ arg-decl^))
output local-symbol^
body id^]
(wrap [modifiers name inputs output body]))))
(def method-call^
(Parser (, Text (List Text) (List AST)))
(form^ (do Parser/Monad
[method local-symbol^
arity-classes (tuple^ (*^ local-symbol^))
arity-args (tuple^ (*^ id^))
_ (: (Parser (,))
(if (i= (size arity-classes)
(size arity-args))
(wrap [])
(lambda [_] #;None)))]
(wrap [method arity-classes arity-args])
)))
## [Syntax]
(defsyntax #export (throw ex)
(emit (@list (` (;_jvm_throw (~ ex))))))
(defsyntax #export (try body [catches (*^ catch^)] [finally (?^ finally^)])
(emit (@list (` (;_jvm_try (~ body)
(~@ (:: List/Monoid (m;++ (map (: (-> (, Text Ident AST) AST)
(lambda [catch]
(let [[class ex body] catch]
(` (;_jvm_catch (~ (text$ class)) (~ (symbol$ ex)) (~ body))))))
catches)
(case finally
#;None
(@list)
(#;Some finally)
(: (List AST) (@list (` (;_jvm_finally (~ finally))))))))))))))
(defsyntax #export (definterface [name local-symbol^] [supers (tuple^ (*^ local-symbol^))] [members (*^ method-decl^)])
(let [members' (map (: (-> (, (List Text) Text (List Text) Text) AST)
(lambda [member]
(let [[modifiers name inputs output] member]
(` ((~ (text$ name)) [(~@ (map text$ inputs))] (~ (text$ output)) [(~@ (map text$ modifiers))])))))
members)]
(emit (@list (` (;_jvm_interface (~ (text$ name)) [(~@ (map text$ supers))]
(~@ members')))))))
(defsyntax #export (defclass [name local-symbol^] [super local-symbol^] [interfaces (tuple^ (*^ local-symbol^))]
[fields (*^ field-decl^)]
[methods (*^ method-def^)])
(do Lux/Monad
[current-module get-module-name
#let [fields' (map (: (-> (, (List Text) Text Text) AST)
(lambda [field]
(let [[modifiers name class] field]
(` ((~ (text$ name))
(~ (text$ class))
[(~@ (map text$ modifiers))])))))
fields)
methods' (map (: (-> (, (List Text) Text (List (, Text Text)) Text AST) AST)
(lambda [methods]
(let [[modifiers name inputs output body] methods]
(` ((~ (text$ name))
[(~@ (map (: (-> (, Text Text) AST)
(lambda [in]
(let [[left right] in]
(form$ (@list (symbol$ ["" left])
(text$ right))))))
inputs))]
(~ (text$ output))
[(~@ (map text$ modifiers))]
(~ body))))))
methods)]]
(emit (@list (` (;_jvm_class (~ (text$ name)) (~ (text$ super))
[(~@ (map text$ interfaces))]
[(~@ fields')]
[(~@ methods')]))))))
(defsyntax #export (new [class local-symbol^] [arg-classes (tuple^ (*^ local-symbol^))] [args (tuple^ (*^ id^))])
(emit (@list (` (;_jvm_new (~ (text$ class))
[(~@ (map text$ arg-classes))]
[(~@ args)])))))
(defsyntax #export (instance? [class local-symbol^] obj)
(emit (@list (` (;_jvm_instanceof (~ (text$ class)) (~ obj))))))
(defsyntax #export (locking lock body)
(do Lux/Monad
[g!lock (gensym "")
g!body (gensym "")
g!_ (gensym "")]
(emit (@list (` (let [(~ g!lock) (~ lock)
(~ g!_) (;_jvm_monitorenter (~ g!lock))
(~ g!body) (~ body)
(~ g!_) (;_jvm_monitorexit (~ g!lock))]
(~ g!body)))))
))
(defsyntax #export (null? obj)
(emit (@list (` (;_jvm_null? (~ obj))))))
(defsyntax #export (program [args symbol^] body)
(emit (@list (` (;_jvm_program (~ (symbol$ args))
(~ body))))))
(defsyntax #export (.? [field local-symbol^] obj)
(case obj
[_ (#;SymbolS obj-name)]
(do Lux/Monad
[obj-type (find-var-type obj-name)]
(case obj-type
(#;DataT class)
(emit (@list (` (;_jvm_getfield (~ (text$ class)) (~ (text$ field))))))
_
(fail "Can only get field from object.")))
_
(do Lux/Monad
[g!obj (gensym "")]
(emit (@list (` (let [(~ g!obj) (~ obj)]
(;;.? (~ (text$ field)) (~ g!obj)))))))))
(defsyntax #export (.= [field local-symbol^] value obj)
(case obj
[_ (#;SymbolS obj-name)]
(do Lux/Monad
[obj-type (find-var-type obj-name)]
(case obj-type
(#;DataT class)
(emit (@list (` (;_jvm_putfield (~ (text$ class)) (~ (text$ field)) (~ value)))))
_
(fail "Can only set field of object.")))
_
(do Lux/Monad
[g!obj (gensym "")]
(emit (@list (` (let [(~ g!obj) (~ obj)]
(;;.= (~ (text$ field)) (~ value) (~ g!obj)))))))))
(defsyntax #export (.! [call method-call^] obj)
(let [[m-name ?m-classes m-args] call]
(case obj
[_ (#;SymbolS obj-name)]
(do Lux/Monad
[obj-type (find-var-type obj-name)]
(case obj-type
(#;DataT class)
(emit (@list (` (;_jvm_invokevirtual (~ (text$ class)) (~ (text$ m-name)) [(~@ (map text$ ?m-classes))]
(~ obj) [(~@ m-args)]))))
_
(fail "Can only call method on object.")))
_
(do Lux/Monad
[g!obj (gensym "")]
(emit (@list (` (let [(~ g!obj) (~ obj)]
(;;.! ((~ (symbol$ ["" m-name]))
[(~@ (map (lambda [c] (symbol$ ["" c])) ?m-classes))]
[(~@ m-args)])
(~ g!obj))))))))))
(defsyntax #export (..? [field local-symbol^] [class local-symbol^])
(emit (@list (` (;_jvm_getstatic (~ (text$ class)) (~ (text$ field)))))))
(defsyntax #export (..= [field local-symbol^] value [class local-symbol^])
(emit (@list (` (;_jvm_putstatic (~ (text$ class)) (~ (text$ field)) (~ value))))))
(defsyntax #export (..! [call method-call^] [class local-symbol^])
(let [[m-name m-classes m-args] call]
(emit (@list (` (;_jvm_invokestatic (~ (text$ class)) (~ (text$ m-name))
[(~@ (map text$ m-classes))]
[(~@ m-args)]))))))
(defsyntax #export (->maybe expr)
(do Lux/Monad
[g!val (gensym "")]
(emit (@list (` (let [(~ g!val) (~ expr)]
(if (null? (~ g!val))
#;None
(#;Some (~ g!val)))))))))
(defsyntax #export (try$ expr)
(emit (@list (` (try (#;Right (~ expr))
(~ (' (catch java.lang.Exception e
(#;Left (_jvm_invokevirtual "java.lang.Throwable" "getMessage" [] e []))))))))))
|