aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/extension/analysis/js.lux
blob: d04e04ec96c90bbee747cc2321ca4df05e82bb04 (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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    ["<>" parser
     ["<c>" code (#+ Parser)]]
    pipe]
   [data
    [collection
     ["." array (#+ Array)]
     ["." dictionary]]]
   [type
    ["." check]]
   [target
    ["_" js]]]
  ["." // #_
   ["/" lux (#+ custom)]
   ["/#" //
    ["#." bundle]
    ["/#" // ("#@." monad)
     [analysis
      [".A" type]]
     ["/#" // #_
      ["#." analysis (#+ Analysis Operation Phase Handler Bundle)]]]]])

(def: array::new
  Handler
  (custom
   [<c>.any
    (function (_ extension phase lengthC)
      (do ////.monad
        [lengthA (typeA.with-type Nat
                   (phase lengthC))
         [var-id varT] (typeA.with-env check.var)
         _ (typeA.infer (type (Array varT)))]
        (wrap (#/////analysis.Extension extension (list lengthA)))))]))

(def: array::length
  Handler
  (custom
   [<c>.any
    (function (_ extension phase arrayC)
      (do ////.monad
        [[var-id varT] (typeA.with-env check.var)
         arrayA (typeA.with-type (type (Array varT))
                  (phase arrayC))
         _ (typeA.infer Nat)]
        (wrap (#/////analysis.Extension extension (list arrayA)))))]))

(def: array::read
  Handler
  (custom
   [(<>.and <c>.any <c>.any)
    (function (_ extension phase [indexC arrayC])
      (do ////.monad
        [indexA (typeA.with-type Nat
                  (phase indexC))
         [var-id varT] (typeA.with-env check.var)
         arrayA (typeA.with-type (type (Array varT))
                  (phase arrayC))
         _ (typeA.infer varT)]
        (wrap (#/////analysis.Extension extension (list indexA arrayA)))))]))

(def: array::write
  Handler
  (custom
   [($_ <>.and <c>.any <c>.any <c>.any)
    (function (_ extension phase [indexC valueC arrayC])
      (do ////.monad
        [indexA (typeA.with-type Nat
                  (phase indexC))
         [var-id varT] (typeA.with-env check.var)
         valueA (typeA.with-type varT
                  (phase valueC))
         arrayA (typeA.with-type (type (Array varT))
                  (phase arrayC))
         _ (typeA.infer (type (Array varT)))]
        (wrap (#/////analysis.Extension extension (list indexA valueA arrayA)))))]))

(def: array::delete
  Handler
  (custom
   [($_ <>.and <c>.any <c>.any)
    (function (_ extension phase [indexC arrayC])
      (do ////.monad
        [indexA (typeA.with-type Nat
                  (phase indexC))
         [var-id varT] (typeA.with-env check.var)
         arrayA (typeA.with-type (type (Array varT))
                  (phase arrayC))
         _ (typeA.infer (type (Array varT)))]
        (wrap (#/////analysis.Extension extension (list indexA arrayA)))))]))

(def: bundle::array
  Bundle
  (<| (///bundle.prefix "array")
      (|> ///bundle.empty
          (///bundle.install "new" array::new)
          (///bundle.install "length" array::length)
          (///bundle.install "read" array::read)
          (///bundle.install "write" array::write)
          (///bundle.install "delete" array::delete)
          )))

(def: object::new
  Handler
  (custom
   [($_ <>.and <c>.any (<c>.tuple (<>.some <c>.any)))
    (function (_ extension phase [constructorC inputsC])
      (do ////.monad
        [constructorA (typeA.with-type Any
                        (phase constructorC))
         inputsA (monad.map @ (|>> phase (typeA.with-type Any)) inputsC)
         _ (typeA.infer .Any)]
        (wrap (#/////analysis.Extension extension (list& constructorA inputsA)))))]))

(def: object::get
  Handler
  (custom
   [($_ <>.and <c>.text <c>.any)
    (function (_ extension phase [fieldC objectC])
      (do ////.monad
        [objectA (typeA.with-type Any
                   (phase objectC))
         _ (typeA.infer .Any)]
        (wrap (#/////analysis.Extension extension (list (/////analysis.text fieldC)
                                                        objectA)))))]))

(def: object::do
  Handler
  (custom
   [($_ <>.and <c>.text <c>.any (<c>.tuple (<>.some <c>.any)))
    (function (_ extension phase [methodC objectC inputsC])
      (do ////.monad
        [objectA (typeA.with-type Any
                   (phase objectC))
         inputsA (monad.map @ (|>> phase (typeA.with-type Any)) inputsC)
         _ (typeA.infer .Any)]
        (wrap (#/////analysis.Extension extension (list& (/////analysis.text methodC)
                                                         objectA
                                                         inputsA)))))]))

(def: bundle::object
  Bundle
  (<| (///bundle.prefix "object")
      (|> ///bundle.empty
          (///bundle.install "new" object::new)
          (///bundle.install "get" object::get)
          (///bundle.install "do" object::do)
          (///bundle.install "null" (/.nullary Any))
          (///bundle.install "null?" (/.unary Any Bit))
          (///bundle.install "undefined" (/.nullary Any))
          (///bundle.install "undefined?" (/.unary Any Bit))
          )))

(def: js::constant
  Handler
  (custom
   [<c>.text
    (function (_ extension phase name)
      (do ////.monad
        [_ (typeA.infer Any)]
        (wrap (#/////analysis.Extension extension (list (/////analysis.text name))))))]))

(def: js::apply
  Handler
  (custom
   [($_ <>.and <c>.any (<>.some <c>.any))
    (function (_ extension phase [abstractionC inputsC])
      (do ////.monad
        [abstractionA (typeA.with-type Any
                        (phase abstractionC))
         inputsA (monad.map @ (|>> phase (typeA.with-type Any)) inputsC)
         _ (typeA.infer Any)]
        (wrap (#/////analysis.Extension extension (list& abstractionA inputsA)))))]))

(def: #export bundle
  Bundle
  (<| (///bundle.prefix "js")
      (|> ///bundle.empty
          (///bundle.install "constant" js::constant)
          (///bundle.install "apply" js::apply)
          (dictionary.merge bundle::array)
          (dictionary.merge bundle::object)
          )))