blob: b693f50b8444209f5640b6439247038f6b6b0928 (
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
|
(.module:
lux
(lux (control [monad #+ do]
["ex" exception #+ exception:])
(data text/format)
[macro "macro/" Monad<Meta>])
(luxc ["_" lang]
(lang [".L" host]
(host ["$" jvm]
(jvm ["$t" type]
["$i" inst]))
["ls" synthesis]))
[//runtime])
(def: $Object $.Type ($t.class "java.lang.Object" (list)))
(def: (pop-altI stack-depth)
(-> Nat $.Inst)
(case stack-depth
+0 id
+1 $i.POP
+2 $i.POP2
_ ## (n/> +2)
(|>> $i.POP2
(pop-altI (n/- +2 stack-depth)))))
(def: peekI
$.Inst
(|>> $i.DUP
($i.INVOKESTATIC hostL.runtime-class
"pm_peek"
($t.method (list //runtime.$Stack)
(#.Some $Object)
(list))
false)))
(def: popI
$.Inst
(|>> ($i.INVOKESTATIC hostL.runtime-class
"pm_pop"
($t.method (list //runtime.$Stack)
(#.Some //runtime.$Stack)
(list))
false)))
(def: pushI
$.Inst
(|>> ($i.INVOKESTATIC hostL.runtime-class
"pm_push"
($t.method (list //runtime.$Stack $Object)
(#.Some //runtime.$Stack)
(list))
false)))
(exception: #export Unrecognized-Path)
(def: (translate-path' translate stack-depth @else @end path)
(-> (-> ls.Synthesis (Meta $.Inst))
Nat $.Label $.Label ls.Path (Meta $.Inst))
(case path
(^ [_ (#.Form (list [_ (#.Text "lux case exec")] bodyS))])
(do macro.Monad<Meta>
[bodyI (translate bodyS)]
(wrap (|>> (pop-altI stack-depth)
bodyI
($i.GOTO @end))))
(^ [_ (#.Form (list [_ (#.Text "lux case pop")]))])
(macro/wrap popI)
(^ [_ (#.Form (list [_ (#.Text "lux case bind")] [_ (#.Nat register)]))])
(macro/wrap (|>> peekI
($i.ASTORE register)))
[_ (#.Bool value)]
(macro/wrap (let [jumpI (if value $i.IFEQ $i.IFNE)]
(|>> peekI
($i.unwrap #$.Boolean)
(jumpI @else))))
(^template [<tag> <prep>]
[_ (<tag> value)]
(macro/wrap (|>> peekI
($i.unwrap #$.Long)
($i.long (|> value <prep>))
$i.LCMP
($i.IFNE @else))))
([#.Nat (:! Int)]
[#.Int (: Int)]
[#.Deg (:! Int)])
[_ (#.Frac value)]
(macro/wrap (|>> peekI
($i.unwrap #$.Double)
($i.double value)
$i.DCMPL
($i.IFNE @else)))
[_ (#.Text value)]
(macro/wrap (|>> peekI
($i.string value)
($i.INVOKEVIRTUAL "java.lang.Object"
"equals"
($t.method (list $Object)
(#.Some $t.boolean)
(list))
false)
($i.IFEQ @else)))
(^template [<special> <method>]
(^ [_ (#.Form (list [_ (#.Text <special>)] [_ (#.Nat idx)]))])
(macro/wrap (case idx
+0
(|>> peekI
($i.CHECKCAST ($t.descriptor //runtime.$Tuple))
($i.int 0)
$i.AALOAD
pushI)
_
(|>> peekI
($i.CHECKCAST ($t.descriptor //runtime.$Tuple))
($i.int (nat-to-int idx))
($i.INVOKESTATIC hostL.runtime-class
<method>
($t.method (list //runtime.$Tuple $t.int)
(#.Some $Object)
(list))
false)
pushI))))
(["lux case tuple left" "pm_left"]
["lux case tuple right" "pm_right"])
(^template [<special> <flag>]
(^ [_ (#.Form (list [_ (#.Text <special>)] [_ (#.Nat idx)]))])
(macro/wrap (<| $i.with-label (function [@success])
$i.with-label (function [@fail])
(|>> peekI
($i.CHECKCAST ($t.descriptor //runtime.$Variant))
($i.int (nat-to-int idx))
<flag>
($i.INVOKESTATIC hostL.runtime-class "pm_variant"
($t.method (list //runtime.$Variant //runtime.$Tag //runtime.$Flag)
(#.Some //runtime.$Datum)
(list))
false)
$i.DUP
($i.IFNULL @fail)
($i.GOTO @success)
($i.label @fail)
$i.POP
($i.GOTO @else)
($i.label @success)
pushI))))
(["lux case variant left" $i.NULL]
["lux case variant right" ($i.string "")])
(^ [_ (#.Form (list [_ (#.Text "lux case seq")] leftP rightP))])
(do macro.Monad<Meta>
[leftI (translate-path' translate stack-depth @else @end leftP)
rightI (translate-path' translate stack-depth @else @end rightP)]
(wrap (|>> leftI
rightI)))
(^ [_ (#.Form (list [_ (#.Text "lux case alt")] leftP rightP))])
(do macro.Monad<Meta>
[@alt-else $i.make-label
leftI (translate-path' translate (n/inc stack-depth) @alt-else @end leftP)
rightI (translate-path' translate stack-depth @else @end rightP)]
(wrap (|>> $i.DUP
leftI
($i.label @alt-else)
$i.POP
rightI)))
_
(_.throw Unrecognized-Path (%code path))))
(def: (translate-path translate path @end)
(-> (-> ls.Synthesis (Meta $.Inst))
ls.Path $.Label (Meta $.Inst))
(do macro.Monad<Meta>
[@else $i.make-label
pathI (translate-path' translate +1 @else @end path)]
(wrap (|>> pathI
($i.label @else)
$i.POP
($i.INVOKESTATIC hostL.runtime-class
"pm_fail"
($t.method (list) #.None (list))
false)
$i.NULL
($i.GOTO @end)))))
(def: #export (translate-if testI thenI elseI)
(-> $.Inst $.Inst $.Inst $.Inst)
(<| $i.with-label (function [@else])
$i.with-label (function [@end])
(|>> testI
($i.unwrap #$.Boolean)
($i.IFEQ @else)
thenI
($i.GOTO @end)
($i.label @else)
elseI
($i.label @end))))
(def: #export (translate-case translate valueS path)
(-> (-> ls.Synthesis (Meta $.Inst))
ls.Synthesis ls.Path (Meta $.Inst))
(do macro.Monad<Meta>
[@end $i.make-label
valueI (translate valueS)
pathI (translate-path translate path @end)]
(wrap (|>> valueI
$i.NULL
$i.SWAP
pushI
pathI
($i.label @end)))))
(def: #export (translate-let translate register inputS exprS)
(-> (-> ls.Synthesis (Meta $.Inst))
Nat ls.Synthesis ls.Synthesis (Meta $.Inst))
(do macro.Monad<Meta>
[inputI (translate inputS)
exprI (translate exprS)]
(wrap (|>> inputI
($i.ASTORE register)
exprI))))
|