aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/js/case.jvm.lux
blob: cbb0e6c7731348773cd2020917057826c16a81ce (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data text/format
             (coll [list "list/" Fold<List>]))
       [macro #+ "meta/" Monad<Meta>])
  (luxc [lang]
        (lang ["ls" synthesis]))
  [//]
  (// [".T" runtime]
      [".T" primitive]
      [".T" reference]))

(def: #export (translate-let translate register valueS bodyS)
  (-> (-> ls.Synthesis (Meta //.Expression)) Nat ls.Synthesis ls.Synthesis
      (Meta //.Expression))
  (do macro.Monad<Meta>
    [valueJS (translate valueS)
     bodyJS (translate bodyS)]
    (wrap (format "(function() {"
                  "var " (referenceT.variable register) " = " valueJS ";"
                  "return " bodyJS ";"
                  "})()"))))

(def: #export (translate-record-get translate valueS path)
  (-> (-> ls.Synthesis (Meta //.Expression)) ls.Synthesis (List [Nat Bool])
      (Meta //.Expression))
  (do macro.Monad<Meta>
    [valueJS (translate valueS)]
    (wrap (list/fold (function [[idx tail?] source]
                       (let [method (if tail? runtimeT.product//right runtimeT.product//left)]
                         (format method "(" source "," (|> idx nat-to-int %i) ")")))
                     (format "(" valueJS ")")
                     path))))

(def: #export (translate-if testJS thenJS elseJS)
  (-> //.Expression //.Expression //.Expression
      //.Expression)
  (format "(" testJS " ? " thenJS " : " elseJS ")"))

(def: savepoint
  //.Expression
  "pm_cursor_savepoint")

(def: cursor
  //.Expression
  "pm_cursor")

(def: (push-cursor value)
  (-> //.Expression //.Expression)
  (format cursor ".push(" value ");"))

(def: save-cursor
  //.Statement
  (format savepoint ".push(" cursor ".slice());"))

(def: restore-cursor
  //.Statement
  (format cursor " = " savepoint ".pop();"))

(def: peek-cursor
  //.Expression
  (format cursor "[" cursor ".length - 1]"))

(def: pop-cursor
  //.Statement
  (format cursor ".pop();"))

(def: pm-error
  //.Expression
  (%t "PM-ERROR"))

(def: fail-pattern-matching
  //.Statement
  (format "throw " pm-error ";"))

(exception: #export Unrecognized-Path)

(def: (translate-pattern-matching' translate path)
  (-> (-> ls.Synthesis (Meta //.Expression)) Code (Meta //.Expression))
  (case path
    (^code ("lux case exec" (~ bodyS)))
    (do macro.Monad<Meta>
      [bodyJS (translate bodyS)]
      (wrap (format "return " bodyJS ";")))

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

    (^code ("lux case bind" (~ [_ (#.Nat register)])))
    (meta/wrap (format "var " (referenceT.variable register) " = " peek-cursor ";"))

    (^template [<tag> <translate>]
      [_ (<tag> value)]
      (do macro.Monad<Meta>
        [valueJS (<translate> value)]
        (wrap (format "if(!" (format runtimeT.int//= "(" peek-cursor "," valueJS ")") ") { " fail-pattern-matching " }"))))
    ([#.Nat primitiveT.translate-nat]
     [#.Int primitiveT.translate-int]
     [#.Deg primitiveT.translate-deg])

    (^template [<tag> <format>]
      [_ (<tag> value)]
      (meta/wrap (format "if(" peek-cursor " !== " (<format> value) ") { " fail-pattern-matching " }")))
    ([#.Bool %b]
     [#.Frac %f]
     [#.Text %t])

    (^template [<pm> <getter>]
      (^code (<pm> (~ [_ (#.Nat idx)])))
      (meta/wrap (push-cursor (format <getter> "(" peek-cursor "," (|> idx nat-to-int %i) ")"))))
    (["lux case tuple left" runtimeT.product//left]
     ["lux case tuple right" runtimeT.product//right])

    (^template [<pm> <flag>]
      (^code (<pm> (~ [_ (#.Nat idx)])))
      (meta/wrap (format "temp = " runtimeT.sum//get "(" peek-cursor "," (|> idx nat-to-int %i) "," <flag> ");"
                         "if(temp !== null) {"
                         (push-cursor "temp")
                         "}"
                         "else {"
                         fail-pattern-matching
                         "}")))
    (["lux case variant left" "null"]
     ["lux case variant right" "\"\""])

    (^code ("lux case seq" (~ leftP) (~ rightP)))
    (do macro.Monad<Meta>
      [leftJS (translate-pattern-matching' translate leftP)
       rightJS (translate-pattern-matching' translate rightP)]
      (wrap (format leftJS rightJS)))

    (^code ("lux case alt" (~ leftP) (~ rightP)))
    (do macro.Monad<Meta>
      [leftJS (translate-pattern-matching' translate leftP)
       rightJS (translate-pattern-matching' translate rightP)]
      (wrap (format "try {"
                    save-cursor
                    leftJS
                    "}"
                    "catch(ex) {"
                    "if(ex === " pm-error ") {"
                    restore-cursor
                    rightJS
                    "}"
                    "else {"
                    "throw ex;"
                    "}"
                    "}")))

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

(def: report-pattern-matching-error
  //.Statement
  (format "if(ex === " pm-error ") {"
          "throw \"Invalid expression for pattern-matching.\";"
          "}"
          "else {"
          "throw ex;"
          "}"))

(def: (translate-pattern-matching translate path)
  (-> (-> ls.Synthesis (Meta //.Expression)) Code (Meta //.Expression))
  (do macro.Monad<Meta>
    [pmJS (translate-pattern-matching' translate path)]
    (wrap (format "try {" pmJS "}"
                  "catch(ex) {"
                  report-pattern-matching-error
                  "}"))))

(def: (initialize-pattern-matching stack-init)
  (-> //.Expression //.Statement)
  (format "var temp;"
          "var " cursor " = [" stack-init "];"
          "var " savepoint " = [];"))

(def: #export (translate-case translate valueS path)
  (-> (-> ls.Synthesis (Meta //.Expression)) ls.Synthesis Code (Meta //.Expression))
  (do macro.Monad<Meta>
    [valueJS (translate valueS)
     pmJS (translate-pattern-matching translate path)]
    (wrap (format "(function() {"
                  "\"use strict\";"
                  (initialize-pattern-matching valueJS)
                  pmJS
                  "})()"))))