aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/extension/analysis/lua.lux
blob: b5d02fc226e492dc4b2624b0afacb222956803c9 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except Analysis)
   ["[0]" ffi]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]]
   [data
    [collection
     ["[0]" array]
     ["[0]" dictionary]
     ["[0]" list]]]
   ["[0]" meta (.only)
    [compiler
     ["@" target (.only)
      ["_" lua]]]
    ["[0]" code
     ["<[1]>" \\parser (.only Parser)]]
    ["[0]" type (.only)
     ["[0]" check]]]]]
 [//
  ["/" lux (.only custom)]
  [///
   ["[0]" extension]
   [//
    ["[0]" phase]
    ["[0]" analysis (.only Analysis Operation Phase Handler Bundle)
     ["[1]/[0]" type]]]]])

(def Nil
  (for @.lua ffi.Nil
       Any))

(def Object
  (for @.lua (type_literal (ffi.Object Any))
       Any))

(def Function
  (for @.lua ffi.Function
       Any))

(def array::new
  (-> Text Handler)
  (custom
   [<code>.any
    (function (_ extension phase archive lengthC)
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [lengthA (analysis/type.expecting Nat
                                              (phase archive lengthC))
             _ (analysis/type.inference (type_literal (array.Array' :read: :write:)))
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list lengthA)}]))))]))

(def array::length
  (-> Text Handler)
  (custom
   [<code>.any
    (function (_ extension phase archive arrayC)
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [arrayA (analysis/type.expecting (type_literal (array.Array' :read: :write:))
                                             (phase archive arrayC))
             _ (analysis/type.inference Nat)
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list arrayA)}]))))]))

(def array::read
  (-> Text Handler)
  (custom
   [(<>.and <code>.any <code>.any)
    (function (_ extension phase archive [indexC arrayC])
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [indexA (analysis/type.expecting Nat
                                             (phase archive indexC))
             arrayA (analysis/type.expecting (type_literal (array.Array' :read: :write:))
                                             (phase archive arrayC))
             _ (analysis/type.inference :read:)
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list indexA arrayA)}]))))]))

(def array::write
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any <code>.any <code>.any)
    (function (_ extension phase archive [indexC valueC arrayC])
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [indexA (analysis/type.expecting Nat
                                             (phase archive indexC))
             valueA (analysis/type.expecting :write:
                                             (phase archive valueC))
             arrayA (analysis/type.expecting (type_literal (array.Array' :read: :write:))
                                             (phase archive arrayC))
             _ (analysis/type.inference (type_literal (array.Array' :read: :write:)))
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list indexA valueA arrayA)}]))))]))

(def array::delete
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any <code>.any)
    (function (_ extension phase archive [indexC arrayC])
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [indexA (analysis/type.expecting Nat
                                             (phase archive indexC))
             arrayA (analysis/type.expecting (type_literal (array.Array' :read: :write:))
                                             (phase archive arrayC))
             _ (analysis/type.inference (type_literal (array.Array' :read: :write:)))
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list indexA arrayA)}]))))]))

(def with_array_extensions
  (-> Bundle Bundle)
  (|>> (/.install "lua_array_new#" array::new)
       (/.install "lua_array_length#" array::length)
       (/.install "lua_array_read#" array::read)
       (/.install "lua_array_write#" array::write)
       (/.install "lua_array_delete#" array::delete)
       ))

(def object::get
  (-> Text Handler)
  (custom
   [(all <>.and <code>.text <code>.any)
    (function (_ extension phase archive [fieldC objectC])
      (do phase.monad
        [objectA (analysis/type.expecting ..Object
                                          (phase archive objectC))
         _ (analysis/type.inference .Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.text @ fieldC)
                                          objectA)}])))]))

(def object::do
  (-> Text Handler)
  (custom
   [(all <>.and <code>.text <code>.any (<code>.tuple (<>.some <code>.any)))
    (function (_ extension phase archive [methodC objectC inputsC])
      (do [! phase.monad]
        [objectA (analysis/type.expecting ..Object
                                          (phase archive objectC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC)
         _ (analysis/type.inference .Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list.partial (analysis.text @ methodC)
                                                  objectA
                                                  inputsA)}])))]))

(def with_object_extensions
  (-> Bundle Bundle)
  (|>> (/.install "lua_object_get#" object::get)
       (/.install "lua_object_do#" object::do)
       (/.install "lua_object_nil#" (/.nullary ..Nil))
       (/.install "lua_object_nil?#" (/.unary Any Bit))
       ))

(with_template [<name> <fromT> <toT>]
  [(def <name>
     (-> Text Handler)
     (custom
      [<code>.any
       (function (_ extension phase archive inputC)
         (do [! phase.monad]
           [inputA (analysis/type.expecting (type_literal <fromT>)
                                            (phase archive inputC))
            _ (analysis/type.inference (type_literal <toT>))
            @ meta.location]
           (in [@ {analysis.#Extension (/.translation extension)
                                       (list inputA)}])))]))]

  [utf8::encode Text (array.Array (I64 Any))]
  [utf8::decode (array.Array (I64 Any)) Text]
  )

(def with_utf8_extensions
  (-> Bundle Bundle)
  (|>> (/.install "lua_utf8_encoded#" utf8::encode)
       (/.install "lua_utf8_decoded#" utf8::decode)
       ))

(def lua::constant
  (-> Text Handler)
  (custom
   [<code>.text
    (function (_ extension phase archive name)
      (do phase.monad
        [_ (analysis/type.inference Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.text @ name))}])))]))

(def lua::apply
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any (<code>.tuple (<>.some <code>.any)))
    (function (_ extension phase archive [abstractionC inputsC])
      (do [! phase.monad]
        [abstractionA (analysis/type.expecting ..Function
                                               (phase archive abstractionC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC)
         _ (analysis/type.inference Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list.partial abstractionA inputsA)}])))]))

(def lua::power
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any <code>.any)
    (function (_ extension phase archive [powerC baseC])
      (do [! phase.monad]
        [powerA (analysis/type.expecting Frac
                                         (phase archive powerC))
         baseA (analysis/type.expecting Frac
                                        (phase archive baseC))
         _ (analysis/type.inference Frac)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list powerA baseA)}])))]))

(def lua::import
  (-> Text Handler)
  (custom
   [<code>.text
    (function (_ extension phase archive name)
      (do phase.monad
        [_ (analysis/type.inference ..Object)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.text @ name))}])))]))

(def lua::function
  (-> Text Handler)
  (custom
   [(all <>.and <code>.nat <code>.any)
    (function (_ extension phase archive [arity abstractionC])
      (do phase.monad
        [.let [inputT (type.tuple (list.repeated arity Any))]
         abstractionA (analysis/type.expecting (-> inputT Any)
                                               (phase archive abstractionC))
         _ (analysis/type.inference ..Function)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.nat @ arity)
                                          abstractionA)}])))]))

(def .public bundle
  Bundle
  (|> extension.empty
      with_array_extensions
      with_object_extensions
      with_utf8_extensions

      (/.install "lua_constant#" lua::constant)
      (/.install "lua_apply#" lua::apply)
      (/.install "lua_power#" lua::power)
      (/.install "lua_import#" lua::import)
      (/.install "lua_function#" lua::function)
      (/.install "lua_script_universe#" (/.nullary .Bit))
      ))