aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/js.lux
blob: 6de21c89b8bffc7bf2f71bdcd8198ad2dba41fc1 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(.module:
  [library
   [lux "*"
    ["[0]" ffi]
    [abstract
     ["[0]" monad {"+" [do]}]]
    [control
     ["<>" parser
      ["<c>" code {"+" [Parser]}]]]
    [data
     [collection
      ["[0]" array {"+" [Array]}]
      ["[0]" dictionary]
      ["[0]" list]]]
    ["[0]" type
     ["[0]" check]]
    ["@" target
     ["_" js]]]]
  [//
   ["/" lux {"+" [custom]}]
   [//
    ["[0]" bundle]
    [//
     ["[0]" analysis "_"
      ["[1]/[0]" type]]
     [//
      ["[0]" analysis {"+" [Analysis Operation Phase Handler Bundle]}]
      [///
       ["[0]" phase]]]]]])

(def: array::new
  Handler
  (custom
   [<c>.any
    (function (_ extension phase archive lengthC)
      (do phase.monad
        [lengthA (analysis/type.with_type Nat
                   (phase archive lengthC))
         [var_id varT] (analysis/type.with_env check.var)
         _ (analysis/type.infer (type (Array varT)))]
        (in {analysis.#Extension extension (list lengthA)})))]))

(def: array::length
  Handler
  (custom
   [<c>.any
    (function (_ extension phase archive arrayC)
      (do phase.monad
        [[var_id varT] (analysis/type.with_env check.var)
         arrayA (analysis/type.with_type (type (Array varT))
                  (phase archive arrayC))
         _ (analysis/type.infer Nat)]
        (in {analysis.#Extension extension (list arrayA)})))]))

(def: array::read
  Handler
  (custom
   [(<>.and <c>.any <c>.any)
    (function (_ extension phase archive [indexC arrayC])
      (do phase.monad
        [indexA (analysis/type.with_type Nat
                  (phase archive indexC))
         [var_id varT] (analysis/type.with_env check.var)
         arrayA (analysis/type.with_type (type (Array varT))
                  (phase archive arrayC))
         _ (analysis/type.infer varT)]
        (in {analysis.#Extension extension (list indexA arrayA)})))]))

(def: array::write
  Handler
  (custom
   [($_ <>.and <c>.any <c>.any <c>.any)
    (function (_ extension phase archive [indexC valueC arrayC])
      (do phase.monad
        [indexA (analysis/type.with_type Nat
                  (phase archive indexC))
         [var_id varT] (analysis/type.with_env check.var)
         valueA (analysis/type.with_type varT
                  (phase archive valueC))
         arrayA (analysis/type.with_type (type (Array varT))
                  (phase archive arrayC))
         _ (analysis/type.infer (type (Array varT)))]
        (in {analysis.#Extension extension (list indexA valueA arrayA)})))]))

(def: array::delete
  Handler
  (custom
   [($_ <>.and <c>.any <c>.any)
    (function (_ extension phase archive [indexC arrayC])
      (do phase.monad
        [indexA (analysis/type.with_type Nat
                  (phase archive indexC))
         [var_id varT] (analysis/type.with_env check.var)
         arrayA (analysis/type.with_type (type (Array varT))
                  (phase archive arrayC))
         _ (analysis/type.infer (type (Array varT)))]
        (in {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 archive [constructorC inputsC])
      (do [! phase.monad]
        [constructorA (analysis/type.with_type Any
                        (phase archive constructorC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.with_type Any)) inputsC)
         _ (analysis/type.infer .Any)]
        (in {analysis.#Extension extension (list& constructorA inputsA)})))]))

(def: object::get
  Handler
  (custom
   [($_ <>.and <c>.text <c>.any)
    (function (_ extension phase archive [fieldC objectC])
      (do phase.monad
        [objectA (analysis/type.with_type Any
                   (phase archive objectC))
         _ (analysis/type.infer .Any)]
        (in {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 archive [methodC objectC inputsC])
      (do [! phase.monad]
        [objectA (analysis/type.with_type Any
                   (phase archive objectC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.with_type Any)) inputsC)
         _ (analysis/type.infer .Any)]
        (in {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 archive name)
      (do phase.monad
        [_ (analysis/type.infer Any)]
        (in {analysis.#Extension extension (list (analysis.text name))})))]))

(def: js::apply
  Handler
  (custom
   [($_ <>.and <c>.any (<>.some <c>.any))
    (function (_ extension phase archive [abstractionC inputsC])
      (do [! phase.monad]
        [abstractionA (analysis/type.with_type Any
                        (phase archive abstractionC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.with_type Any)) inputsC)
         _ (analysis/type.infer Any)]
        (in {analysis.#Extension extension (list& abstractionA inputsA)})))]))

(def: js::type_of
  Handler
  (custom
   [<c>.any
    (function (_ extension phase archive objectC)
      (do phase.monad
        [objectA (analysis/type.with_type Any
                   (phase archive objectC))
         _ (analysis/type.infer .Text)]
        (in {analysis.#Extension extension (list objectA)})))]))

(def: js::function
  Handler
  (custom
   [($_ <>.and <c>.nat <c>.any)
    (function (_ extension phase archive [arity abstractionC])
      (do phase.monad
        [.let [inputT (type.tuple (list.repeated arity Any))]
         abstractionA (analysis/type.with_type (-> inputT Any)
                        (phase archive abstractionC))
         _ (analysis/type.infer (for [@.js ffi.Function]
                                     Any))]
        (in {analysis.#Extension extension (list (analysis.nat arity)
                                                 abstractionA)})))]))

(def: .public bundle
  Bundle
  (<| (bundle.prefix "js")
      (|> bundle.empty
          (dictionary.merged bundle::array)
          (dictionary.merged bundle::object)

          (bundle.install "constant" js::constant)
          (bundle.install "apply" js::apply)
          (bundle.install "type-of" js::type_of)
          (bundle.install "function" js::function)
          )))