aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/scheme/case.jvm.lux
blob: 49733e6f3bf575d6ad7eec398654dafefd90ae47 (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
(.module:
  [lux (#- case let if)
   [control
    [monad (#+ do)]
    ["ex" exception (#+ exception:)]]
   [data
    ["." number]
    ["." text
     format]
    [collection
     ["." list ("#;." functor fold)]]]
   [host
    ["_" scheme (#+ Expression Computation Var)]]]
  ["." // #_
   ["#." runtime (#+ Operation Phase)]
   ["#." primitive]
   ["#/" // #_
    ["#." reference]
    ["#/" // ("#;." monad)
     ["#/" // #_
      [reference (#+ Register)]
      ["#." synthesis (#+ Synthesis Path)]]]]])

(def: #export register
  (///reference.local _.var))

(def: #export (let generate [valueS register bodyS])
  (-> Phase [Synthesis Register Synthesis]
      (Operation Computation))
  (do ////.monad
    [valueO (generate valueS)
     bodyO (generate bodyS)]
    (wrap (_.let (list [(..register register) valueO])
            bodyO))))

(def: #export (record-get generate valueS pathP)
  (-> Phase Synthesis (List [Nat Bit])
      (Operation Expression))
  (do ////.monad
    [valueO (generate valueS)]
    (wrap (list;fold (function (_ [idx tail?] source)
                       (.let [method (.if tail?
                                       //runtime.product//right
                                       //runtime.product//left)]
                         (method source (_.int (.int idx)))))
                     valueO
                     pathP))))

(def: #export (if generate [testS thenS elseS])
  (-> Phase [Synthesis Synthesis Synthesis]
      (Operation Computation))
  (do ////.monad
    [testO (generate testS)
     thenO (generate thenS)
     elseO (generate elseS)]
    (wrap (_.if testO thenO elseO))))

(def: @savepoint (_.var "lux_pm_cursor_savepoint"))
(def: @cursor (_.var "lux_pm_cursor"))
(def: @temp (_.var "lux_pm_temp"))
(def: @alt-error (_.var "alt_error"))

(def: (push! value var)
  (-> Expression Var Computation)
  (_.set! var (_.cons/2 value var)))

(def: (push-cursor! value)
  (-> Expression Computation)
  (push! value @cursor))

(def: (pop! var)
  (-> Var Computation)
  (_.set! var var))

(def: save-cursor!
  Computation
  (push! @cursor @savepoint))

(def: restore-cursor!
  Computation
  (_.set! @cursor (_.car/1 @savepoint)))

(def: cursor-top
  Computation
  (_.car/1 @cursor))

(def: pop-cursor!
  Computation
  (pop! @cursor))

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

(def: fail-pm! (_.raise/1 pm-error))

(exception: #export unrecognized-path)

(def: (pm-catch handler)
  (-> Expression Computation)
  (_.lambda [(list @alt-error) #.None]
       (_.if (|> @alt-error (_.eqv?/2 pm-error))
         handler
         (_.raise/1 @alt-error))))

(def: (pattern-matching' generate pathP)
  (-> Phase Path (Operation Expression))
  (.case pathP
    (^ (/////synthesis.path/then bodyS))
    (generate bodyS)

    #/////synthesis.Pop
    (////;wrap pop-cursor!)

    (#/////synthesis.Bind register)
    (////;wrap (_.define (..register register) [(list) #.None]
                         cursor-top))

    (^template [<tag> <format> <=>]
      (^ (<tag> value))
      (////;wrap (_.when (|> value <format> (<=> cursor-top) _.not/1)
                         fail-pm!)))
    ([/////synthesis.path/bit  //primitive.bit           _.eqv?/2]
     [/////synthesis.path/i64  (<| //primitive.i64 .int) _.=/2]
     [/////synthesis.path/f64  //primitive.f64           _.=/2]
     [/////synthesis.path/text //primitive.text          _.eqv?/2])

    (^template [<pm> <flag> <prep>]
      (^ (<pm> idx))
      (////;wrap (_.let (list [@temp (|> idx <prep> .int _.int (//runtime.sum//get cursor-top <flag>))])
                   (_.if (_.null?/1 @temp)
                     fail-pm!
                     (push-cursor! @temp)))))
    ([/////synthesis.side/left  _.nil         (<|)]
     [/////synthesis.side/right (_.string "") inc])

    (^template [<pm> <getter> <prep>]
      (^ (<pm> idx))
      (////;wrap (|> idx <prep> .int _.int (<getter> cursor-top) push-cursor!)))
    ([/////synthesis.member/left  //runtime.product//left  (<|)]
     [/////synthesis.member/right //runtime.product//right inc])

    (^template [<tag> <computation>]
      (^ (<tag> leftP rightP))
      (do ////.monad
        [leftO (pattern-matching' generate leftP)
         rightO (pattern-matching' generate rightP)]
        (wrap <computation>)))
    ([/////synthesis.path/seq (_.begin (list leftO
                                             rightO))]
     [/////synthesis.path/alt (_.with-exception-handler
                                (pm-catch (_.begin (list restore-cursor!
                                                         rightO)))
                                (_.lambda [(list) #.None]
                                     (_.begin (list save-cursor!
                                                    leftO))))])
    
    _
    (////.throw unrecognized-path [])))

(def: (pattern-matching generate pathP)
  (-> Phase Path (Operation Computation))
  (do ////.monad
    [pattern-matching! (pattern-matching' generate pathP)]
    (wrap (_.with-exception-handler
            (pm-catch (_.raise/1 (_.string "Invalid expression for pattern-matching.")))
            (_.lambda [(list) #.None]
                 pattern-matching!)))))

(def: #export (case generate [valueS pathP])
  (-> Phase [Synthesis Path] (Operation Computation))
  (do ////.monad
    [valueO (generate valueS)]
    (<| (:: @ map (_.let (list [@cursor (_.list/* (list valueO))]
                               [@savepoint (_.list/* (list))])))
        (pattern-matching generate pathP))))