aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/generator/case.jvm.lux
blob: 1f351325e4cc464fe140356d804da8452dfdfff6 (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
(;module:
  lux
  (lux (control [monad #+ do])
       [macro "lux/" Monad<Lux>])
  (luxc (lang ["ls" synthesis])
        (generator (host ["$" jvm]
                         (jvm ["$t" type]
                              ["$i" inst]))
                   [expr]))
  [../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 ../runtime;runtime-name
                        "pm_peek"
                        ($t;method (list ../runtime;$Stack)
                                   (#;Some $Object)
                                   (list))
                        false)))

(def: popI
  $;Inst
  (|>. ($i;INVOKESTATIC ../runtime;runtime-name
                        "pm_pop"
                        ($t;method (list ../runtime;$Stack)
                                   (#;Some ../runtime;$Stack)
                                   (list))
                        false)))

(def: pushI
  $;Inst
  (|>. ($i;INVOKESTATIC ../runtime;runtime-name
                        "pm_push"
                        ($t;method (list ../runtime;$Stack $Object)
                                   (#;Some ../runtime;$Stack)
                                   (list))
                        false)))

(def: (generate-pattern' stack-depth @else @end path)
  (-> Nat $;Label $;Label ls;Path (Lux $;Inst))
  (case path
    (#ls;ExecP bodyS)
    (do macro;Monad<Lux>
      [bodyI (expr;generate bodyS)]
      (wrap (|>. (pop-altI stack-depth)
                 bodyI
                 ($i;GOTO @end))))

    #ls;UnitP
    (lux/wrap popI)

    (#ls;BindP register)
    (lux/wrap (|>. peekI
                   ($i;ASTORE register)
                   popI))

    (#ls;BoolP value)
    (lux/wrap (let [jumpI (if value $i;IFEQ $i;IFNE)]
                (|>. peekI
                     ($i;unwrap #$;Boolean)
                     (jumpI @else))))

    (^template [<tag> <prep>]
      (<tag> value)
      (lux/wrap (|>. peekI
                     ($i;unwrap #$;Long)
                     ($i;long (|> value <prep>))
                     $i;LCMP
                     ($i;IFNE @else))))
    ([#ls;NatP (:! Int)]
     [#ls;IntP (: Int)]
     [#ls;DegP (:! Int)])

    (#ls;FracP value)
    (lux/wrap (|>. peekI
                   ($i;unwrap #$;Double)
                   ($i;double value)
                   $i;DCMPL
                   ($i;IFNE @else)))
    
    (#ls;TextP value)
    (lux/wrap (|>. peekI
                   ($i;string value)
                   ($i;INVOKEVIRTUAL "java.lang.Object"
                                     "equals"
                                     ($t;method (list $Object)
                                                (#;Some $t;boolean)
                                                (list))
                                     false)
                   ($i;IFEQ @else)))

    (#ls;TupleP idx subP)
    (do macro;Monad<Lux>
      [subI (generate-pattern' stack-depth @else @end subP)
       #let [[idx tail?] (case idx
                           (#;Left idx)
                           [idx false]

                           (#;Right idx)
                           [idx true])]]
      (wrap (case idx
              +0
              (|>. peekI
                   ($i;CHECKCAST ($t;descriptor ../runtime;$Tuple))
                   ($i;int 0)
                   $i;AALOAD
                   pushI
                   subI)
              
              _
              (|>. peekI
                   ($i;CHECKCAST ($t;descriptor ../runtime;$Tuple))
                   ($i;int (nat-to-int idx))
                   ($i;INVOKESTATIC ../runtime;runtime-name
                                    (if tail? "pm_right" "pm_left")
                                    ($t;method (list ../runtime;$Tuple $t;int)
                                               (#;Some $Object)
                                               (list))
                                    false)
                   pushI
                   subI))))

    (#ls;VariantP idx subP)
    (do macro;Monad<Lux>
      [subI (generate-pattern' stack-depth @else @end subP)
       #let [[idx last?] (case idx
                           (#;Left idx)
                           [idx false]

                           (#;Right idx)
                           [idx true])
             flagI (if last?
                     ($i;string "")
                     $i;NULL)]]
      (wrap (<| $i;with-label (function [@success])
                $i;with-label (function [@fail])
                (|>. peekI
                     ($i;CHECKCAST ($t;descriptor ../runtime;$Variant))
                     ($i;int (nat-to-int idx))
                     flagI
                     ($i;INVOKESTATIC ../runtime;runtime-name "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
                     subI))))

    (#ls;SeqP leftP rightP)
    (do macro;Monad<Lux>
      [leftI (generate-pattern' stack-depth @else @end leftP)
       rightI (generate-pattern' stack-depth @else @end rightP)]
      (wrap (|>. leftI
                 rightI)))

    (#ls;AltP leftP rightP)
    (do macro;Monad<Lux>
      [@alt-else $i;make-label
       leftI (generate-pattern' (n.inc stack-depth) @alt-else @end leftP)
       rightI (generate-pattern' stack-depth @else @end rightP)]
      (wrap (|>. $i;DUP
                 leftI
                 ($i;label @alt-else)
                 $i;POP
                 rightI)))
    ))

(def: (generate-pattern path @end)
  (-> ls;Path $;Label (Lux $;Inst))
  (do macro;Monad<Lux>
    [@else $i;make-label
     pathI (generate-pattern' +1 @else @end path)]
    (wrap (|>. pathI
               ($i;label @else)
               $i;POP
               ($i;INVOKESTATIC ../runtime;runtime-name
                                "pm_fail"
                                ($t;method (list) #;None (list))
                                false)
               $i;NULL
               ($i;GOTO @end)))))

(def: #export (generate valueS path)
  (-> ls;Synthesis ls;Path (Lux $;Inst))
  (do macro;Monad<Lux>
    [@end $i;make-label
     valueI (expr;generate valueS)
     pathI (generate-pattern path @end)]
    (wrap (|>. valueI
               $i;NULL
               $i;SWAP
               pushI
               pathI
               ($i;label @end)))))