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

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

(def: #export (let translate [valueS register bodyS])
  (-> Phase [Synthesis Register Synthesis]
      (Operation Computation))
  (do ////.monad
    [valueO (translate valueS)
     bodyO (translate bodyS)]
    (wrap (_.apply/* (<| (_.closure (list))
                         ($_ _.then
                             (_.define (..register register) valueO)
                             (_.return bodyO)))
                     (list)))))

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

(def: #export (if translate [testS thenS elseS])
  (-> Phase [Synthesis Synthesis Synthesis]
      (Operation Computation))
  (do ////.monad
    [testO (translate testS)
     thenO (translate thenS)
     elseO (translate elseS)]
    (wrap (_.? 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-cursor! value)
  (-> Expression Statement)
  (_.statement (|> @cursor (_.do "push" (list value)))))

(def: pop-cursor!
  Statement
  (_.statement (|> @cursor (_.do "pop" (list)))))

(def: peek-cursor
  Expression
  (.let [idx (|> @cursor (_.the "length") (_.- (_.i32 +1)))]
    (|> @cursor (_.at idx))))

(def: save-cursor!
  Statement
  (.let [cursor (|> @cursor (_.do "slice" (list)))]
    (_.statement (|> @savepoint (_.do "push" (list cursor))))))

(def: restore-cursor!
  Statement
  (_.set @cursor (|> @savepoint (_.do "pop" (list)))))

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

(exception: #export unrecognized-path)

(def: (pm-catch on-catch!)
  (-> Statement [Var Statement])
  [@alt-error
   (_.if (_.= ..pm-error @alt-error)
     on-catch!
     (_.throw @alt-error))])

(def: (pattern-matching' translate pathP)
  (-> Phase Path (Operation Statement))
  (.case pathP
    (^ (synthesis.path/then bodyS))
    (do ////.monad
      [body! (translate bodyS)]
      (wrap (_.return body!)))

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

    (#synthesis.Bind register)
    (/////wrap (_.define (..register register) ..peek-cursor))

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

    (^template [<pm> <flag> <prep>]
      (^ (<pm> idx))
      (/////wrap ($_ _.then
                     (_.set @temp (|> idx <prep> .int _.i32 (//runtime.sum//get ..peek-cursor <flag>)))
                     (_.if (_.= _.null @temp)
                       fail-pm!
                       (push-cursor! @temp)))))
    ([synthesis.side/left  _.null        (<|)]
     [synthesis.side/right (_.string "") inc])

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

    (^template [<tag> <computation>]
      (^ (<tag> leftP rightP))
      (do ////.monad
        [left! (pattern-matching' translate leftP)
         right! (pattern-matching' translate rightP)]
        (wrap <computation>)))
    ([synthesis.path/seq (_.then left! right!)]
     [synthesis.path/alt (_.try ($_ _.then
                                    ..save-cursor!
                                    left!)
                                (pm-catch ($_ _.then
                                              ..restore-cursor!
                                              right!)))])

    _
    (////.throw unrecognized-path [])))

(def: (pattern-matching translate pathP)
  (-> Phase Path (Operation Statement))
  (do ////.monad
    [pattern-matching! (pattern-matching' translate pathP)]
    (wrap (_.try pattern-matching!
                 (pm-catch (_.throw (_.string "Invalid expression for pattern-matching.")))))))

(def: #export (case translate [valueS pathP])
  (-> Phase [Synthesis Path] (Operation Computation))
  (do ////.monad
    [stack-init (translate valueS)
     path! (pattern-matching translate pathP)
     #let [closure (<| (_.closure (list))
                       ($_ _.then
                           (_.declare @temp)
                           (_.define @cursor (_.array (list stack-init)))
                           (_.define @savepoint (_.array (list)))
                           path!))]]
    (wrap (_.apply/* closure (list)))))