aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/target/lua.lux
blob: 063c0fc4db2845f6b7f98d4e3aff8a8e8b15273b (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
(.require
 [library
  [lux (.except Location Code Label int if function or and not let local comment the when)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    ["[0]" enum]]
   [control
    ["[0]" pipe]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [math
    [number
     ["n" nat]
     ["i" int]
     ["f" frac]]]
   [meta
    ["@" target]
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["[0]" template]]
    [type
     ["[0]" primitive (.except def)]]]]])

... Added the carriage return for better Windows compatibility.
(def \n+
  Text
  (format text.carriage_return text.new_line))

(def nested
  (-> Text Text)
  (.let [nested_new_line (format text.new_line text.tab)]
    (|>> (format \n+)
         (text.replaced text.new_line nested_new_line))))

(def input_separator ", ")

(primitive.def .public (Code brand)
  Text

  (def .public equivalence
    (All (_ brand) (Equivalence (Code brand)))
    (implementation
     (def (= reference subject)
       (at text.equivalence = (representation reference) (representation subject)))))

  (def .public hash
    (All (_ brand) (Hash (Code brand)))
    (implementation
     (def equivalence ..equivalence)
     (def hash (|>> representation (at text.hash hash)))))

  (def .public manual
    (-> Text Code)
    (|>> abstraction))

  (def .public code
    (-> (Code Any) Text)
    (|>> representation))

  (with_template [<type> <super>+]
    [(with_expansions [<brand> (template.symbol [<type> "'"])]
       (primitive.def (<brand> brand) Any)
       (`` (type .public <type> (|> Any <brand> (,, (template.spliced <super>+))))))]
    
    [Expression [Code]]
    [Computation [Expression' Code]]
    [Location [Computation' Expression' Code]]
    [Statement [Code]]
    )

  (with_template [<type> <super>+]
    [(with_expansions [<brand> (template.symbol [<type> "'"])]
       (primitive.def <brand> Any)
       (`` (type .public <type> (|> <brand> (,, (template.spliced <super>+))))))]

    [Literal [Computation' Expression' Code]]
    [Var [Location' Computation' Expression' Code]]
    [Access [Location' Computation' Expression' Code]]
    [Label [Code]]
    )

  (def .public nil
    Literal
    (abstraction "nil"))

  (def .public boolean
    (-> Bit Literal)
    (|>> (pipe.when
           #0 "false"
           #1 "true")
         abstraction))

  (def .public int
    (-> Int Literal)
    ... Integers must be turned into hexadecimal to avoid quirks in how Lua parses integers.
    ... In particular, the number -9223372036854775808 will be incorrectly parsed as a float by Lua.
    (.let [to_hex (at n.hex encoded)]
      (|>> .nat
           to_hex
           (format "0x")
           abstraction)))

  (def .public float
    (-> Frac Literal)
    (|>> (pipe.cond [(f.= f.positive_infinity)]
                    [(pipe.new "(1.0/0.0)" [])]
                    
                    [(f.= f.negative_infinity)]
                    [(pipe.new "(-1.0/0.0)" [])]
                    
                    [(f.= f.not_a_number)]
                    [(pipe.new "(0.0/0.0)" [])]

                    ... else
                    [%.frac (text.replaced "+" "")])
         abstraction))

  (def safe
    (-> Text Text)
    (`` (|>> (,, (with_template [<find> <replace>]
                   [(text.replaced <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 .public string
    (-> Text Literal)
    (|>> ..safe (text.enclosed' text.double_quote) abstraction))

  (def .public multi
    (-> (List Expression) Expression)
    (|>> (list#each ..code)
         (text.interposed ..input_separator)
         abstraction))

  (def .public array
    (-> (List Expression) Literal)
    (|>> (list#each ..code)
         (text.interposed ..input_separator)
         (text.enclosed ["{" "}"])
         abstraction))

  (def .public table
    (-> (List [Text Expression]) Literal)
    (|>> (list#each (.function (_ [key value])
                      (format key " = " (representation value))))
         (text.interposed ..input_separator)
         (text.enclosed ["({" "})"])
         abstraction))

  (def .public (item idx array)
    (-> Expression Expression Access)
    (abstraction (format "(" (representation array) ")[" (representation idx) "]")))

  (def .public (the field table)
    (-> Text Expression Access)
    (abstraction (format (representation table) "." field)))

  (def .public length
    (-> Expression Computation)
    (|>> representation
         (text.enclosed ["#(" ")"])
         abstraction))

  (def .public (apply args func)
    (-> (List Expression) Expression Computation)
    (|> args
        (list#each ..code)
        (text.interposed ..input_separator)
        (text.enclosed ["(" ")"])
        (format (representation func))
        abstraction))

  (def .public (do method args table)
    (-> Text (List Expression) Expression Computation)
    (|> args
        (list#each ..code)
        (text.interposed ..input_separator)
        (text.enclosed ["(" ")"])
        (format (representation table) ":" method)
        abstraction))

  (with_template [<op> <name>]
    [(def .public (<name> parameter subject)
       (-> Expression Expression Expression)
       (abstraction (format "("
                            (representation subject)
                            " " <op> " "
                            (representation parameter)
                            ")")))]

    ["==" =]
    ["<"  <]
    ["<=" <=]
    [">"  >]
    [">=" >=]
    ["+"  +]
    ["-"  -]
    ["*"  *]
    ["^"  ^]
    ["/"  /]
    ["//" //]
    ["%"  %]
    [".." concat]

    ["or"  or]
    ["and" and]
    ["|"   bit_or]
    ["&"   bit_and]
    ["~"   bit_xor]

    ["<<" bit_shl]
    [">>" bit_shr]
    )

  (with_template [<name> <unary>]
    [(def .public (<name> subject)
       (-> Expression Expression)
       (abstraction (format "(" <unary> " " (representation subject) ")")))]

    [not "not"]
    [opposite "-"]
    )

  (with_template [<name> <type>]
    [(def .public <name>
       (-> Text <type>)
       (|>> abstraction))]

    [var Var]
    [label Label]
    )

  (def .public statement
    (-> Expression Statement)
    (|>> representation abstraction))

  (def .public (then pre! post!)
    (-> Statement Statement Statement)
    (abstraction
     (format (representation pre!)
             \n+
             (representation post!))))

  (def locations
    (-> (List Location) Text)
    (|>> (list#each ..code)
         (text.interposed ..input_separator)))

  (def .public (local vars)
    (-> (List Var) Statement)
    (abstraction (format "local " (..locations vars))))

  (def .public (set vars value)
    (-> (List Location) Expression Statement)
    (abstraction (format (..locations vars) " = " (representation value))))

  (def .public (let vars value)
    (-> (List Var) Expression Statement)
    (abstraction (format "local " (..locations vars) " = " (representation value))))

  (def .public (local/1 var value)
    (-> Var Expression Statement)
    (abstraction (format "local " (representation var) " = " (representation value))))

  (def .public (if test then! else!)
    (-> Expression Statement Statement Statement)
    (abstraction (format "if " (representation test)
                         \n+ "then" (..nested (representation then!))
                         \n+ "else" (..nested (representation else!))
                         \n+ "end")))

  (def .public (when test then!)
    (-> Expression Statement Statement)
    (abstraction (format "if " (representation test)
                         \n+ "then" (..nested (representation then!))
                         \n+ "end")))

  (def .public (while test body!)
    (-> Expression Statement Statement)
    (abstraction
     (format "while " (representation test) " do"
             (..nested (representation body!))
             \n+ "end")))

  (def .public (repeat until body!)
    (-> Expression Statement Statement)
    (abstraction
     (format "repeat"
             (..nested (representation body!))
             \n+ "until " (representation until))))

  (def .public (for_in vars source body!)
    (-> (List Var) Expression Statement Statement)
    (abstraction
     (format "for " (|> vars
                        (list#each ..code)
                        (text.interposed ..input_separator))
             " in " (representation source) " do"
             (..nested (representation body!))
             \n+ "end")))

  (def .public (for_step var from to step body!)
    (-> Var Expression Expression Expression Statement
        Statement)
    (abstraction
     (format "for " (representation var)
             " = " (representation from)
             ..input_separator (representation to)
             ..input_separator (representation step) " do"
             (..nested (representation body!))
             \n+ "end")))

  (def .public (return value)
    (-> Expression Statement)
    (abstraction (format "return " (representation value))))

  (def .public (closure args body!)
    (-> (List Var) Statement Expression)
    (|> (format "function " (|> args
                                ..locations
                                (text.enclosed ["(" ")"]))
                (..nested (representation body!))
                \n+ "end")
        (text.enclosed ["(" ")"])
        abstraction))

  (with_template [<name> <code> <binding>]
    [(def .public (<name> name args body!)
       (-> <binding> (List Var) Statement Statement)
       (abstraction
        (format <code> " " (representation name)
                (|> args
                    ..locations
                    (text.enclosed ["(" ")"]))
                (..nested (representation body!))
                \n+ "end")))]

    [function "function" Location]
    [local_function "local function" Var]
    )

  (def .public break
    Statement
    (abstraction "break"))

  (def .public (set_label label)
    (-> Label Statement)
    (abstraction (format "::" (representation label) "::")))

  (def .public (go_to label)
    (-> Label Statement)
    (abstraction (format "goto " (representation label))))

  ... https://www.lua.org/pil/1.3.html
  (def .public (comment commentary on)
    (All (_ kind) (-> Text (Code kind) (Code kind)))
    (abstraction (format "-- "  commentary \n+ (representation on))))
  )

(def arity_inputs
  (syntax (_ [arity <code>.nat])
    (in (.when arity
          0 (.list)
          _ (|> (-- arity)
                (enum.range n.enum 0)
                (list#each (|>> %.nat code.local)))))))

(def arity_types
  (syntax (_ [arity <code>.nat])
    (in (list.repeated arity (` ..Expression)))))

(with_template [<arity> <function>+]
  [(with_expansions [<inputs> (arity_inputs <arity>)
                     <types> (arity_types <arity>)
                     <definitions> (template.spliced <function>+)]
     (with_template [<function>]
       [(`` (def .public ((,, (template.symbol [<function> "/" <arity>])) <inputs>)
              (-> <types> Computation)
              (..apply (.list <inputs>) (..var <function>))))]

       <definitions>))]

  [1
   [["error"]
    ["pcall"]
    ["print"]
    ["require"]
    ["type"]
    ["ipairs"]]]
  [2
   [["error"]]]
  )