aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/r/case.jvm.lux
blob: 2a635030c3c1576744162059dc853e9b062e461e (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data [number]
             [text]
             text/format
             (coll [list "list/" Functor<List> Fold<List>]
                   [set #+ Set]))
       [macro #+ "meta/" Monad<Meta>]
       (macro [code]))
  (luxc [lang]
        (lang [".L" variable #+ Register Variable]
              ["ls" synthesis #+ Synthesis Path]
              (host [r #+ Expression Statement SVar @@])))
  [//]
  (// [".T" runtime]
      [".T" primitive]
      [".T" reference]))

(def: #export (translate-let translate register valueS bodyS)
  (-> (-> Synthesis (Meta Expression)) Register Synthesis Synthesis
      (Meta Expression))
  (do macro.Monad<Meta>
    [valueO (translate valueS)
     bodyO (translate bodyS)
     #let [$register (referenceT.variable register)]]
    (wrap (r.block ($_ r.then!
                       (r.set! $register valueO)
                       (r.do! bodyO))))))

(def: #export (translate-record-get translate valueS pathP)
  (-> (-> Synthesis (Meta Expression)) Synthesis (List [Nat Bool])
      (Meta Expression))
  (do macro.Monad<Meta>
    [valueO (translate valueS)]
    (wrap (list/fold (function (_ [idx tail?] source)
                       (let [method (if tail?
                                      runtimeT.product//right
                                      runtimeT.product//left)]
                         (method source (r.int (:! Int idx)))))
                     valueO
                     pathP))))

(def: #export (translate-if testO thenO elseO)
  (-> Expression Expression Expression Expression)
  (r.if testO thenO elseO))

(def: $savepoint (r.var "lux_pm_cursor_savepoint"))
(def: $cursor (r.var "lux_pm_cursor"))

(def: top r.length)
(def: next (|>> r.length (r.+ (r.int 1))))
(def: (push! value var)
  (-> Expression SVar Statement)
  (r.set-nth! (next (@@ var)) value var))
(def: (pop! var)
  (-> SVar Statement)
  (r.set-nth! (top (@@ var)) r.null var))

(def: (push-cursor! value)
  (-> Expression Statement)
  (push! value $cursor))

(def: save-cursor!
  Statement
  (push! (r.slice (r.float 1.0) (r.length (@@ $cursor)) (@@ $cursor))
         $savepoint))

(def: restore-cursor!
  Statement
  (r.set! $cursor (r.nth (top (@@ $savepoint)) (@@ $savepoint))))

(def: cursor-top
  Expression
  (top (@@ $cursor)))

(def: pop-cursor!
  Statement
  (pop! $cursor))

(def: pm-error (r.string "PM-ERROR"))

(def: fail-pm! (r.stop! pm-error))

(def: $temp (r.var "lux_pm_temp"))

(exception: #export (Unrecognized-Path {message Text})
  message)

(def: $alt_error (r.var "alt_error"))

(def: (pm-catch handler!)
  (-> Statement Expression)
  (r.function (list $alt_error)
    (r.if! (|> (@@ $alt_error) (r.= pm-error))
           handler!
           (r.stop! (@@ $alt_error)))))

(def: (translate-pattern-matching' translate pathP)
  (-> (-> Synthesis (Meta Expression)) Path (Meta Statement))
  (case pathP
    (^code ("lux case exec" (~ bodyS)))
    (do macro.Monad<Meta>
      [bodyO (translate bodyS)]
      (wrap (r.do! bodyO)))

    (^code ("lux case pop"))
    (meta/wrap pop-cursor!)

    (^code ("lux case bind" (~ [_ (#.Nat register)])))
    (meta/wrap (r.set! (referenceT.variable register) cursor-top))

    (^template [<tag> <format>]
      [_ (<tag> value)]
      (meta/wrap (r.when! (r.not (r.= (|> value <format>) cursor-top))
                          fail-pm!)))
    ([#.Nat  (<| runtimeT.int (:! Int))]
     [#.Int  runtimeT.int]
     [#.Deg  (<| runtimeT.int (:! Int))]
     [#.Bool r.bool]
     [#.Frac r.float]
     [#.Text r.string])

    (^template [<pm> <getter>]
      (^code (<pm> (~ [_ (#.Nat idx)])))
      (meta/wrap (push-cursor! (<getter> cursor-top (r.int (:! Int idx))))))
    (["lux case tuple left" runtimeT.product//left]
     ["lux case tuple right" runtimeT.product//right])

    (^template [<pm> <flag>]
      (^code (<pm> (~ [_ (#.Nat idx)])))
      (meta/wrap ($_ r.then!
                     (r.set! $temp (runtimeT.sum//get cursor-top (r.int (:! Int idx)) <flag>))
                     (r.if! (r.not (r.= r.null (@@ $temp)))
                            (push-cursor! (@@ $temp))
                            fail-pm!))))
    (["lux case variant left" r.null]
     ["lux case variant right" (r.string "")])

    (^code ("lux case seq" (~ leftP) (~ rightP)))
    (do macro.Monad<Meta>
      [leftO (translate-pattern-matching' translate leftP)
       rightO (translate-pattern-matching' translate rightP)]
      (wrap ($_ r.then!
                leftO
                rightO)))

    (^code ("lux case alt" (~ leftP) (~ rightP)))
    (do macro.Monad<Meta>
      [leftO (translate-pattern-matching' translate leftP)
       rightO (translate-pattern-matching' translate rightP)]
      (wrap (r.do! (r.try ($_ r.then!
                              save-cursor!
                              leftO)
                          #.None
                          (#.Some (pm-catch ($_ r.then!
                                                restore-cursor!
                                                rightO)))
                          #.None))))

    _
    (lang.throw Unrecognized-Path (%code pathP))
    ))

(def: (translate-pattern-matching translate pathP)
  (-> (-> Synthesis (Meta Expression)) Path (Meta Statement))
  (do macro.Monad<Meta>
    [pattern-matching! (translate-pattern-matching' translate pathP)]
    (wrap (r.do! (r.try pattern-matching!
                        #.None
                        (#.Some (pm-catch (r.stop! (r.string "Invalid expression for pattern-matching."))))
                        #.None)))))

(def: (initialize-pattern-matching! stack-init)
  (-> Expression Statement)
  ($_ r.then!
      (r.set! $cursor (r.list (list stack-init)))
      (r.set! $savepoint (r.list (list)))))

(def: #export (translate-case translate valueS pathP)
  (-> (-> Synthesis (Meta Expression)) Synthesis Path (Meta Expression))
  (do macro.Monad<Meta>
    [valueO (translate valueS)
     pattern-matching! (translate-pattern-matching translate pathP)]
    (wrap (r.block ($_ r.then!
                       (initialize-pattern-matching! valueO)
                       pattern-matching!)))))