aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/python.lux
blob: 1ae1cca64775028f22ced93e5a0b850ab9765dfb (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
(.module:
  [lux (#- Location Code not or and list if cond int comment exec)
   ["@" target]
   ["." host]
   [abstract
    [equivalence (#+ Equivalence)]
    [hash (#+ Hash)]
    ["." enum]]
   [control
    [pipe (#+ new> case> cond>)]
    [parser
     ["<.>" code]]]
   [data
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." list ("#\." functor fold)]]]
   [macro
    [syntax (#+ syntax:)]
    ["." template]
    ["." code]]
   [math
    [number
     ["n" nat]
     ["f" frac]]]
   [type
    abstract]])

(def: expression
  (-> Text Text)
  (text.enclose ["(" ")"]))

(for {@.old (as_is (host.import: java/lang/CharSequence)
                   (host.import: java/lang/String
                     ["#::."
                      (replace [java/lang/CharSequence java/lang/CharSequence] java/lang/String)]))}
     (as_is))

(def: nest
  (-> Text Text)
  (.let [nested_new_line (format text.new_line text.tab)]
    (for {@.old (|>> (format text.new_line)
                     (:coerce java/lang/String)
                     (java/lang/String::replace (:coerce java/lang/CharSequence text.new_line)
                                                (:coerce java/lang/CharSequence nested_new_line)))}
         (|>> (format text.new_line)
              (text.replace_all text.new_line nested_new_line)))))

(abstract: #export (Code brand)
  Text

  (structure: #export equivalence
    (All [brand] (Equivalence (Code brand)))

    (def: (= reference subject)
      (\ text.equivalence = (:representation reference) (:representation subject))))

  (structure: #export hash
    (All [brand] (Hash (Code brand)))

    (def: &equivalence ..equivalence)
    (def: hash (|>> :representation (\ text.hash hash))))

  (def: #export manual
    (-> Text Code)
    (|>> :abstraction))

  (def: #export code
    (-> (Code Any) Text)
    (|>> :representation))

  (template [<type> <super>]
    [(with_expansions [<brand> (template.identifier [<type> "'"])]
       (`` (abstract: #export (<brand> brand) Any))
       (`` (type: #export (<type> brand)
             (<super> (<brand> brand)))))]
    
    [Expression Code]
    [Computation Expression]
    [Location Computation]
    [Var Location]
    [Statement Code]
    )

  (template [<type> <super>]
    [(with_expansions [<brand> (template.identifier [<type> "'"])]
       (`` (abstract: #export <brand> Any))
       (`` (type: #export <type> (<super> <brand>))))]

    [Literal Computation]
    [Access Location]
    [Loop Statement]
    [Label Code]
    )

  (template [<var> <brand>]
    [(abstract: #export <brand> Any)

     (type: #export <var> (Var <brand>))]

    [SVar Single]
    [PVar Poly]
    [KVar Keyword]
    )

  (def: #export var
    (-> Text SVar)
    (|>> :abstraction))

  (template [<name> <brand> <prefix>]
    [(def: #export <name>
       (-> SVar (Var <brand>))
       (|>> :representation (format <prefix>) :abstraction))]

    [poly    Poly    "*"]
    [keyword Keyword "**"]
    )

  (def: #export none
    Literal
    (:abstraction "None"))

  (def: #export bool
    (-> Bit Literal)
    (|>> (case> #0 "False"
                #1 "True")
         :abstraction))

  (def: #export int
    (-> Int Literal)
    (|>> %.int :abstraction))

  (def: #export (long value)
    (-> Int Literal)
    (:abstraction (format (%.int value) "L")))

  (def: #export float
    (-> Frac Literal)
    (`` (|>> (cond> (~~ (template [<lux> <python>]
                          [[(f.= <lux>)]
                           [(new> (format "float(" text.double_quote <python> text.double_quote ")") [])]]

                          [f.positive_infinity "inf"]
                          [f.negative_infinity "-inf"]
                          [f.not_a_number "nan"]
                          ))
                    
                    ## else
                    [%.frac])
             :abstraction)))

  (def: sanitize
    (-> Text Text)
    (`` (|>> (~~ (template [<find> <replace>]
                   [(text.replace_all <find> <replace>)]

                   ["\" "\\"]
                   [text.tab "\t"]
                   [text.vertical_tab "\v"]
                   [text.null "\0"]
                   [text.back_space "\b"]
                   [text.form_feed "\f"]
                   [text.new_line "\n"]
                   [text.carriage_return "\r"]
                   [text.double_quote (format "\" text.double_quote)]
                   ))
             )))

  (def: #export string
    (-> Text Literal)
    (|>> ..sanitize
         (text.enclose [text.double_quote text.double_quote])
         :abstraction))

  (def: #export unicode
    (-> Text Literal)
    (|>> ..string
         :representation
         (format "u")
         :abstraction))

  (def: (composite_literal left_delimiter right_delimiter entry_serializer)
    (All [a]
      (-> Text Text (-> a Text)
          (-> (List a) Literal)))
    (function (_ entries)
      (<| :abstraction
          ## ..expression
          (format left_delimiter
                  (|> entries
                      (list\map entry_serializer)
                      (text.join_with ", "))
                  right_delimiter))))

  (template [<name> <pre> <post>]
    [(def: #export <name>
       (-> (List (Expression Any)) Literal)
       (composite_literal <pre> <post> ..code))]

    [tuple "(" ")"]
    [list  "[" "]"]
    )

  (def: #export (slice from to list)
    (-> (Expression Any) (Expression Any) (Expression Any) Access)
    (<| :abstraction
        ## ..expression
        (format (:representation list) "[" (:representation from) ":" (:representation to) "]")))

  (def: #export (slice_from from list)
    (-> (Expression Any) (Expression Any) Access)
    (<| :abstraction
        ## ..expression
        (format (:representation list) "[" (:representation from) ":]")))

  (def: #export dict
    (-> (List [(Expression Any) (Expression Any)]) (Computation Any))
    (composite_literal "{" "}" (.function (_ [k v]) (format (:representation k) " : " (:representation v)))))

  (def: #export (apply/* func args)
    (-> (Expression Any) (List (Expression Any)) (Computation Any))
    (<| :abstraction
        ## ..expression
        (format (:representation func) "(" (text.join_with ", " (list\map ..code args)) ")")))

  (template [<name> <brand> <prefix>]
    [(def: (<name> var)
       (-> (Expression Any) Text)
       (format <prefix> (:representation var)))]

    [splat_poly    Poly    "*"]
    [splat_keyword Keyword "**"]
    )

  (template [<name> <splat>]
    [(def: #export (<name> args extra func)
       (-> (List (Expression Any)) (Expression Any) (Expression Any) (Computation Any))
       (<| :abstraction
           ## ..expression
           (format (:representation func)
                   (format "(" (|> args
                                   (list\map (function (_ arg) (format (:representation arg) ", ")))
                                   (text.join_with ""))
                           (<splat> extra) ")"))))]

    [apply_poly    splat_poly]
    [apply_keyword splat_keyword]
    )

  (def: #export (the name object)
    (-> Text (Expression Any) (Computation Any))
    (:abstraction (format (:representation object) "." name)))

  (def: #export (do method args object)
    (-> Text (List (Expression Any)) (Expression Any) (Computation Any))
    (..apply/* (..the method object) args))

  (template [<name> <apply>]
    [(def: #export (<name> args extra method)
       (-> (List (Expression Any)) (Expression Any) Text
           (-> (Expression Any) (Computation Any)))
       (|>> (..the method) (<apply> args extra)))]

    [do_poly    apply_poly]
    [do_keyword apply_keyword]
    )

  (def: #export (nth idx array)
    (-> (Expression Any) (Expression Any) Location)
    (:abstraction (format (:representation array) "[" (:representation idx) "]")))

  (def: #export (? test then else)
    (-> (Expression Any) (Expression Any) (Expression Any) (Computation Any))
    (<| :abstraction
        ..expression
        (format (:representation then) " if " (:representation test) " else " (:representation else))))

  (template [<name> <op>]
    [(def: #export (<name> param subject)
       (-> (Expression Any) (Expression Any) (Computation Any))
       (<| :abstraction
           ..expression
           (format (:representation subject) " " <op> " " (:representation param))))]

    [is      "is"]
    [=       "=="]
    [<       "<"]
    [<=      "<="]
    [>       ">"]
    [>=      ">="]
    [+       "+"]
    [-       "-"]
    [*       "*"]
    [/       "/"]
    [//      "//"]
    [%       "%"]
    [**      "**"]
    [bit_or  "|"]
    [bit_and "&"]
    [bit_xor "^"]
    [bit_shl "<<"]
    [bit_shr ">>"]

    [or      "or"]
    [and     "and"]
    )

  (template [<name> <unary>]
    [(def: #export (<name> subject)
       (-> (Expression Any) (Computation Any))
       (<| :abstraction
           ## ..expression
           (format <unary> " " (:representation subject))))]

    [not "not"]
    [negate "-"]
    )

  (def: #export (lambda arguments body)
    (-> (List (Var Any)) (Expression Any) (Computation Any))
    (<| :abstraction
        ..expression
        (format "lambda " (|> arguments (list\map ..code) (text.join_with ", ")) ": "
                (:representation body))))

  (def: #export (set vars value)
    (-> (List (Location Any)) (Expression Any) (Statement Any))
    (:abstraction
     (format (|> vars (list\map ..code) (text.join_with ", "))
             " = "
             (:representation value))))

  (def: #export (delete where)
    (-> (Location Any) (Statement Any))
    (:abstraction (format "del " (:representation where))))

  (def: #export (if test then! else!)
    (-> (Expression Any) (Statement Any) (Statement Any) (Statement Any))
    (:abstraction
     (format "if " (:representation test) ":"
             (..nest (:representation then!))
             text.new_line "else:"
             (..nest (:representation else!)))))

  (def: #export (when test then!)
    (-> (Expression Any) (Statement Any) (Statement Any))
    (:abstraction
     (format "if " (:representation test) ":"
             (..nest (:representation then!)))))

  (def: #export (then pre! post!)
    (-> (Statement Any) (Statement Any) (Statement Any))
    (:abstraction
     (format (:representation pre!)
             text.new_line
             (:representation post!))))

  (template [<keyword> <0>]
    [(def: #export <0>
       (Statement Any)
       (:abstraction <keyword>))]

    ["break"    break]
    ["continue" continue]
    )

  (def: #export (while test body! else!)
    (-> (Expression Any) (Statement Any) (Maybe (Statement Any)) Loop)
    (:abstraction
     (format "while " (:representation test) ":"
             (..nest (:representation body!))
             (case else!
               (#.Some else!)
               (format text.new_line "else:"
                       (..nest (:representation else!)))
               
               #.None
               ""))))

  (def: #export (for_in var inputs body!)
    (-> SVar (Expression Any) (Statement Any) Loop)
    (:abstraction
     (format "for " (:representation var) " in " (:representation inputs) ":"
             (..nest (:representation body!)))))

  (def: #export statement
    (-> (Expression Any) (Statement Any))
    (|>> :transmutation))

  (def: #export pass
    (Statement Any)
    (:abstraction "pass"))

  (type: #export Except
    {#classes (List SVar)
     #exception SVar
     #handler (Statement Any)})
  
  (def: #export (try body! excepts)
    (-> (Statement Any) (List Except) (Statement Any))
    (:abstraction
     (format "try:"
             (..nest (:representation body!))
             (|> excepts
                 (list\map (function (_ [classes exception catch!])
                             (format text.new_line "except (" (text.join_with ", " (list\map ..code classes))
                                     ") as " (:representation exception) ":"
                                     (..nest (:representation catch!)))))
                 (text.join_with "")))))

  (template [<name> <keyword> <pre>]
    [(def: #export (<name> value)
       (-> (Expression Any) (Statement Any))
       (:abstraction
        (format <keyword> (<pre> (:representation value)))))]

    [raise  "raise "  |>]
    [return "return " |>]
    [print  "print"   ..expression]
    )

  (def: #export (exec code globals)
    (-> (Expression Any) (Maybe (Expression Any)) (Statement Any))
    (let [extra (case globals
                  (#.Some globals)
                  (.list globals)

                  #.None
                  (.list))]
      (:abstraction
       (format "exec" (:representation (..tuple (list& code extra)))))))

  (def: #export (def name args body)
    (-> SVar (List (Ex [k] (Var k))) (Statement Any) (Statement Any))
    (:abstraction
     (format "def " (:representation name)
             "(" (|> args (list\map ..code) (text.join_with ", ")) "):"
             (..nest (:representation body)))))

  (def: #export (import module_name)
    (-> Text (Statement Any))
    (:abstraction (format "import " module_name)))

  (def: #export (comment commentary on)
    (All [brand] (-> Text (Code brand) (Code brand)))
    (:abstraction (format "# "  (..sanitize commentary) text.new_line
                          (:representation on))))
  )

(def: #export (cond clauses else!)
  (-> (List [(Expression Any) (Statement Any)]) (Statement Any) (Statement Any))
  (list\fold (.function (_ [test then!] next!)
               (..if test then! next!))
             else!
             (list.reverse clauses)))

(syntax: (arity_inputs {arity <code>.nat})
  (wrap (case arity
          0 (.list)
          _ (|> (dec arity)
                (enum.range n.enum 0)
                (list\map (|>> %.nat code.local_identifier))))))

(syntax: (arity_types {arity <code>.nat})
  (wrap (list.repeat arity (` (Expression Any)))))

(template [<arity> <function>+]
  [(with_expansions [<apply> (template.identifier ["apply/" <arity>])
                     <inputs> (arity_inputs <arity>)
                     <types> (arity_types <arity>)
                     <definitions> (template.splice <function>+)]
     (def: #export (<apply> function <inputs>)
       (-> (Expression Any) <types> (Computation Any))
       (..apply/* function (.list <inputs>)))

     (template [<function>]
       [(`` (def: #export (~~ (template.identifier [<function> "/" <arity>]))
              (<apply> (..var <function>))))]

       <definitions>))]

  [1
   [["str"]
    ["ord"]
    ["float"]
    ["int"]
    ["len"]
    ["chr"]
    ["unichr"]
    ["unicode"]
    ["repr"]
    ["__import__"]
    ["Exception"]]]

  [2
   []]

  [3
   []]
  )