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
|
## 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 seq%)))
(data (list #refer #all #open ("" List/Functor List/Fold))
(number/int #refer #all #open ("i:" Int/Ord))
maybe
tuple)
(meta lux
ast
syntax)))
(open List/Monad "list:")
## [Utils]
## Parsers
(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 opt-arg^
(Parser (, Bool Text))
(&^ (tag?^ ["" "?"])
local-symbol^))
## [Syntax]
(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 (program [args symbol^] body)
(emit (@list (` (;_jvm_program (~ (symbol$ args))
(~ body))))))
(defsyntax #export (??? expr)
(do Lux/Monad
[g!val (gensym "")]
(emit (@list (` (let [(~ g!val) (~ expr)]
(if (;_jvm_null? (~ g!val))
#;None
(#;Some (~ g!val)))))))))
(defsyntax #export (try expr)
(emit (@list (` (;_jvm_try (#;Right (~ expr))
(~ (' (_jvm_catch "java.lang.Exception" e
(#;Left (_jvm_invokevirtual "java.lang.Throwable" "getMessage" [] e []))))))))))
(defsyntax #export (Array [dimensions (?^ int^)] type)
(let [dimensions (? 1 dimensions)]
(if (i:> dimensions 0)
(emit (@list (foldL (lambda [inner _] (` (#;DataT "Array" (@list (~ inner)))))
type
(repeat dimensions []))))
(fail "Array must have positive dimension."))))
(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))))))
(def (prepare-args args)
(-> (List (, Bool Text)) (Lux (, (List AST) (List AST) (List AST) (List Text))))
(do Lux/Monad
[vars (seq% Lux/Monad (repeat (size args) (gensym "")))
#let [pairings (map (: (-> (, (, Bool Text) AST) (, AST (List AST)))
(lambda [[[opt? arg-class] var]]
(if opt?
[(` (Maybe (^ (~ (symbol$ ["" arg-class])))))
(@list var (` (: (^ (~ (symbol$ ["" arg-class])))
(case (~ var)
(#;Some (~ var)) (~ var)
#;None ;_jvm_null))))]
[(` (^ (~ (symbol$ ["" arg-class]))))
(@list)])))
(zip2 args vars))
var-types (map first pairings)
var-rebinds (map second pairings)
arg-classes (map second args)]]
(wrap [vars var-types (list:join var-rebinds) arg-classes])))
(defsyntax #export (new$ [class local-symbol^] [args (tuple^ (*^ opt-arg^))])
(do Lux/Monad
[[vars var-types var-rebinds arg-classes] (prepare-args args)]
(case vars
(\ (@list))
(do Lux/Monad
[g!_ (gensym "")]
(wrap (@list (` (: (-> (,) (^ (~ (symbol$ ["" class]))))
(lambda [(~ g!_)]
(;_jvm_new (~ (text$ class)) [] [])))))))
_
(wrap (@list (` (: (-> (, (~@ var-types)) (^ (~ (symbol$ ["" class]))))
(lambda [[(~@ vars)]]
(let [(~@ var-rebinds)]
(;_jvm_new (~ (text$ class)) [(~@ (map text$ arg-classes))] [(~@ vars)])))))))
)))
(do-template [<name> <op>]
[(defsyntax #export (<name> [class local-symbol^] [method local-symbol^] [args (tuple^ (*^ opt-arg^))]
[ex? (tag?^ ["" "!"])] [opt? (tag?^ ["" "?"])] [return local-symbol^])
(do Lux/Monad
[[vars var-types var-rebinds arg-classes] (prepare-args args)
g!self (gensym "self")
g!temp (gensym "temp")
#let [return-type (` (^ (~ (symbol$ ["" return]))))
body (` (<op> (~ (text$ class)) (~ (text$ method)) [(~@ (map text$ arg-classes))] (~ g!self) [(~@ vars)]))
[body return-type] (if opt?
[(` (let [(~ g!temp) (~ body)]
(if (;_jvm_null? (~ g!temp))
#;None
(#;Some (~ g!temp)))))
(` (Maybe (~ return-type)))]
[body return-type])
[body return-type] (if ex?
[(` (try (~ body)))
(` (Either Text (~ return-type)))]
[body return-type])]]
(wrap (@list (` (: (-> (, (~@ var-types)) (^ (~ (symbol$ ["" class]))) (~ return-type))
(lambda [[(~@ vars)] (~ g!self)]
(let [(~@ var-rebinds)]
(~ body)))))))
))]
[invoke-virtual$ ;_jvm_invokevirtual]
[invoke-interface$ ;_jvm_invokeinterface]
)
(defsyntax #export (invoke-static$ [class local-symbol^] [method local-symbol^] [args (tuple^ (*^ opt-arg^))]
[ex? (tag?^ ["" "!"])] [opt? (tag?^ ["" "?"])] [return local-symbol^])
(do Lux/Monad
[[vars var-types var-rebinds arg-classes] (prepare-args args)
g!temp (gensym "temp")
#let [return-type (` (^ (~ (symbol$ ["" return]))))
body (` (;_jvm_invokestatic (~ (text$ class)) (~ (text$ method)) [(~@ (map text$ arg-classes))] [(~@ vars)]))
[body return-type] (if opt?
[(` (let [(~ g!temp) (~ body)]
(if (;_jvm_null? (~ g!temp))
#;None
(#;Some (~ g!temp)))))
(` (Maybe (~ return-type)))]
[body return-type])
[body return-type] (if ex?
[(` (try (~ body)))
(` (Either Text (~ return-type)))]
[body return-type])]]
(wrap (@list (` (: (-> (, (~@ var-types)) (~ return-type))
(lambda [[(~@ vars)]]
(let [(~@ var-rebinds)]
(~ body)))))))
))
|