aboutsummaryrefslogtreecommitdiff
path: root/lux-python/source/program.lux
blob: 21dac4f046ae444c9a4d5334bc9f7edfd43b79c8 (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
(.module:
  [lux #*
   [cli (#+ program:)]
   ["." io (#+ IO io)]
   [control
    [monad (#+ do)]
    ["." exception (#+ exception:)]]
   [data
    ["." maybe]
    ["." error (#+ Error)]
    [number
     ["." i64]]
    ["." text ("#@." hash)
     format]
    [collection
     ["." array (#+ Array)]
     ["." list ("#@." functor)]]]
   [macro
    ["." template]]
   [world
    ["." file]]
   ["." host (#+ import: interface: do-to object)
    ["_" python]]
   [tool
    [compiler
     ["." name]
     [phase
      [macro (#+ Expander)]
      ["." generation
       ["." python
        ["." runtime]
        ["." extension]]]]
     [default
      ["." platform (#+ Platform)]]]]]
  [program
   ["/" compositor
    ["/." cli]]])

(import: #long java/lang/String)

(import: #long java/lang/Object
  (toString [] java/lang/String))

(import: #long java/lang/Integer
  (longValue [] java/lang/Long))

(import: #long java/lang/Long
  (intValue [] java/lang/Integer))

(import: #long java/lang/Number
  (intValue [] java/lang/Integer)
  (longValue [] long)
  (doubleValue [] double))

(def: (inspect object)
  (-> java/lang/Object Text)
  (<| (case (host.check java/lang/Boolean object)
        (#.Some value)
        (%b value)
        #.None)
      (case (host.check java/lang/String object)
        (#.Some value)
        (%t value)
        #.None)
      (case (host.check java/lang/Long object)
        (#.Some value)
        (%i (.int value))
        #.None)
      (case (host.check java/lang/Number object)
        (#.Some value)
        (%f (java/lang/Number::doubleValue value))
        #.None)
      (case (host.check (Array java/lang/Object) object)
        (#.Some value)
        (let [value (:coerce (Array java/lang/Object) value)]
          (case (array.read 0 value)
            (^multi (#.Some tag)
                    [(host.check java/lang/Integer tag)
                     (#.Some tag)]
                    [[(array.read 1 value)
                      (array.read 2 value)]
                     [last?
                      (#.Some choice)]])
            (let [last? (case last?
                          (#.Some _) #1
                          #.None #0)]
              (|> (format (%n (.nat (java/lang/Integer::longValue tag)))
                          " " (%b last?)
                          " " (inspect choice))
                  (text.enclose ["(" ")"])))

            _
            (|> value
                array.to-list
                (list@map inspect)
                (text.join-with " ")
                (text.enclose ["[" "]"]))))
        #.None)
      (java/lang/Object::toString object)))

(import: #long org/python/core/PyType
  (getName [] java/lang/String))

(import: #long org/python/core/PyString
  (new [java/lang/String]))

(import: #long org/python/core/PyObject
  (asLong [] long)
  (asDouble [] double)
  (asString [] java/lang/String)
  (__nonzero__ [] boolean)
  (__getitem__ [int] #try org/python/core/PyObject)
  (__getitem__ #as __getitem__dict [org/python/core/PyObject] #try org/python/core/PyObject)
  (__len__ [] int)
  (getType [] org/python/core/PyType))

(import: #long org/python/util/PythonInterpreter
  (new [])
  (exec [String] #try void)
  (eval [String] #try PyObject))

(type: Translator
  (-> org/python/core/PyObject (Error Any)))

(def: (tuple lux-object host-object)
  (-> Translator Translator)
  (let [size (|> host-object org/python/core/PyObject::__len__ .nat)]
    (loop [idx 0
           output (:coerce (Array Any) (array.new size))]
      (if (n/< size idx)
        (case (org/python/core/PyObject::__getitem__ (.int idx) host-object)
          (#error.Failure error)
          (#error.Failure error)
          
          (#error.Success value)
          (case (lux-object value)
            (#error.Failure error)
            (#error.Failure error)

            (#error.Success lux-value)
            (recur (inc idx) (array.write idx lux-value output))))
        (#error.Success output)))))

(def: python-type
  (-> org/python/core/PyObject Text)
  (|>> org/python/core/PyObject::getType org/python/core/PyType::getName (:coerce Text)))

(exception: (unknown-kind-of-object {object java/lang/Object})
  (exception.report
   ["Object" (java/lang/Object::toString object)]))

(def: tag-field (org/python/core/PyString::new runtime.variant-tag-field))
(def: flag-field (org/python/core/PyString::new runtime.variant-flag-field))
(def: value-field (org/python/core/PyString::new runtime.variant-value-field))

(def: (variant lux-object host-object)
  (-> Translator Translator)
  (case [(org/python/core/PyObject::__getitem__dict tag-field host-object)
         (org/python/core/PyObject::__getitem__dict flag-field host-object)
         (org/python/core/PyObject::__getitem__dict value-field host-object)]
    (^or [(#error.Failure error) _ _] [_ (#error.Failure error) _] [_ _ (#error.Failure error)])
    (#error.Failure error)
    
    (^multi [(#error.Success tag) (#error.Success flag) (#error.Success value)]
            [(lux-object tag)
             (#error.Success tag)]
            [(lux-object value)
             (#error.Success value)])
    (#error.Success [(java/lang/Long::intValue (:coerce java/lang/Long tag))
                     (: Any
                        (case (python-type (:coerce org/python/core/PyObject flag))
                          "NoneType"
                          (host.null)

                          _
                          ""))
                     value])

    _
    (exception.throw ..unknown-kind-of-object host-object)))

(def: (lux-object host-object)
  Translator
  (case (python-type host-object)
    "str"
    (#error.Success (org/python/core/PyObject::asString host-object))
    
    "bool"
    (#error.Success (org/python/core/PyObject::__nonzero__ host-object))

    "float"
    (#error.Success (org/python/core/PyObject::asDouble host-object))

    (^or "int" "long")
    (#error.Success (org/python/core/PyObject::asLong host-object))

    "tuple"
    (..tuple lux-object host-object)

    "dict"
    (..variant lux-object host-object)

    "NoneType"
    (#error.Success [])
    
    type
    (exception.throw ..unknown-kind-of-object host-object)))

(def: (expander macro inputs lux)
  Expander
  (#error.Failure "YOLO"))

(def: separator "___")

(type: Host
  (generation.Host (_.Expression Any) (_.Statement Any)))

(def: host
  (IO Host)
  (io (let [interpreter (org/python/util/PythonInterpreter::new)
            evaluate! (: (-> Text (_.Expression Any) (Error Any))
                         (function (evaluate! alias input)
                           (do error.monad
                             [output (org/python/util/PythonInterpreter::eval (_.code input) interpreter)]
                             (..lux-object output))))
            execute! (: (-> Text (_.Statement Any) (Error Nothing))
                        (function (execute! alias input)
                          (do error.monad
                            [_ (org/python/util/PythonInterpreter::exec (_.code input) interpreter)]
                            (wrap (:coerce Nothing [])))))]
        (: Host
           (structure
            (def: evaluate! evaluate!)
            (def: execute! execute!)
            (def: (define! [module name] input)
              (let [global (format (text.replace-all .module-separator ..separator module)
                                   ..separator (name.normalize name)
                                   "___" (%n (text@hash name)))
                    @global (_.var global)]
                (do error.monad
                  [#let [definition (_.set (list @global) input)]
                   _ (execute! global definition)
                   value (evaluate! global @global)]
                  (wrap [global value definition])))))))))

(def: platform
  (IO (Platform IO _.SVar (_.Expression Any) (_.Statement Any)))
  (do io.monad
    [host ..host]
    (wrap {#platform.&monad io.monad
           #platform.&file-system file.system
           #platform.host host
           #platform.phase python.generate
           #platform.runtime runtime.generate})))

(def: (program program)
  (-> (_.Expression Any) (_.Statement Any))
  ($_ _.then
      (_.import "sys")
      (_.when (_.= (_.string "main") (_.var "__name__"))
              (_.statement (_.apply/2 program
                                      (runtime.lux//program-args (|> (_.var "sys") (_.the "argv")))
                                      _.none)))))

(program: [{service /cli.service}]
  (/.compiler ..expander
              ..platform
              extension.bundle
              ..program
              service))