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

(def: array::new
  Handler
  (custom
   [<code>.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
   [<code>.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 <code>.any <code>.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 <code>.any <code>.any <code>.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 <code>.any <code>.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: None
  (for {@.python
        ffi.None}
       Any))

(def: Object
  (for {@.python (type (ffi.Object Any))}
       Any))

(def: Function
  (for {@.python ffi.Function}
       Any))

(def: Dict
  (for {@.python ffi.Dict}
       Any))

(def: object::get
  Handler
  (custom
   [($_ <>.and <code>.text <code>.any)
    (function (_ extension phase archive [fieldC objectC])
      (do phase.monad
        [objectA (analysis/type.with_type ..Object
                   (phase archive objectC))
         _ (analysis/type.infer .Any)]
        (in (#analysis.Extension extension (list (analysis.text fieldC)
                                                 objectA)))))]))

(def: object::do
  Handler
  (custom
   [($_ <>.and <code>.text <code>.any (<>.some <code>.any))
    (function (_ extension phase archive [methodC objectC inputsC])
      (do {! phase.monad}
        [objectA (analysis/type.with_type ..Object
                   (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 "get" object::get)
          (bundle.install "do" object::do)
          (bundle.install "none" (/.nullary ..None))
          (bundle.install "none?" (/.unary Any Bit))
          )))

(def: python::constant
  Handler
  (custom
   [<code>.text
    (function (_ extension phase archive name)
      (do phase.monad
        [_ (analysis/type.infer Any)]
        (in (#analysis.Extension extension (list (analysis.text name))))))]))

(def: python::import
  Handler
  (custom
   [<code>.text
    (function (_ extension phase archive name)
      (do phase.monad
        [_ (analysis/type.infer ..Object)]
        (in (#analysis.Extension extension (list (analysis.text name))))))]))

(def: python::apply
  Handler
  (custom
   [($_ <>.and <code>.any (<>.some <code>.any))
    (function (_ extension phase archive [abstractionC inputsC])
      (do {! phase.monad}
        [abstractionA (analysis/type.with_type ..Function
                        (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: python::function
  Handler
  (custom
   [($_ <>.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.with_type (-> inputT Any)
                        (phase archive abstractionC))
         _ (analysis/type.infer ..Function)]
        (in (#analysis.Extension extension (list (analysis.nat arity)
                                                 abstractionA)))))]))

(def: python::exec
  Handler
  (custom
   [($_ <>.and <code>.any <code>.any)
    (function (_ extension phase archive [codeC globalsC])
      (do phase.monad
        [codeA (analysis/type.with_type Text
                 (phase archive codeC))
         globalsA (analysis/type.with_type ..Dict
                    (phase archive globalsC))
         _ (analysis/type.infer .Any)]
        (in (#analysis.Extension extension (list codeA globalsA)))))]))

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

          (bundle.install "constant" python::constant)
          (bundle.install "import" python::import)
          (bundle.install "apply" python::apply)
          (bundle.install "function" python::function)
          (bundle.install "exec" python::exec)
          )))