aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/target/ruby.lux
blob: 3e2e6af72688902e111589fe4fdd6ffd16e58a81 (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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
... 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 Location Code static int if function or and not comment local global symbol 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]
     ["f" frac]]]
   [meta
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["[0]" template]]
    [type
     ["[0]" nominal (.except def)]]]]])

(def input_separator ", ")
(def statement_suffix ";")

... 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))))

(nominal.def .public (Code brand)
  Text

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

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

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

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

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

    [Literal [Computation' Expression' Code]]
    [Access [Location' Computation' Expression' Code]]
    [CVar [Var' Location' Computation' Expression' Code]]
    [GVar [Var' Location' Computation' Expression' Code]]
    [IVar [Var' Location' Computation' Expression' Code]]
    [SVar [Var' Location' Computation' Expression' Code]]
    [LVar* [LVar' Var' Location' Computation' Expression' Code]]
    [LVar** [LVar' Var' Location' Computation' Expression' Code]]
    )

  (with_template [<var> <prefix> <constructor>]
    [(def .public <constructor>
       (-> Text <var>)
       (|>> (format <prefix>) abstraction))]

    [LVar "l_" local]
    [CVar "C_" constant]
    [GVar "$"  global]
    [IVar "@"  instance]
    [SVar "@@" static]
    )

  (with_template [<var> <prefix> <modifier> <unpacker>]
    [(with_template [<name> <input> <output>]
       [(def .public <name>
          (-> <input> <output>)
          (|>> representation (format <prefix>) abstraction))]

       [<modifier> LVar <var>]
       [<unpacker> Expression Computation]
       )]

    [LVar*  "*"  variadic    splat]
    [LVar** "**" variadic_kv double_splat]
    )

  (with_template [<ruby_name> <lux_name>]
    [(def .public <lux_name>
       GVar
       (..global <ruby_name>))]

    ["!" latest_error_message]
    ["@" latest_error_location]
    ["_" last_string_read]
    ["." last_line_number_read]
    ["&" last_string_matched]
    ["~" last_regexp_match]
    ["=" case_insensitivity_flag]
    ["/" input_record_separator]
    ["\" output_record_separator]
    ["0" script_name]
    ["$" process_id]
    ["?" exit_status]

    ["stdout" stdout]
    ["stdin" stdin]
    )

  (with_template [<ruby_name> <lux_name>]
    [(def .public <lux_name>
       CVar
       (..manual <ruby_name>))]

    ["ARGV" command_line_arguments]
    )

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

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

  (def .public bool
    (-> Bit Literal)
    (|>> (pipe.when
           #0 "false"
           #1 "true")
         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)]
                   ))
             )))

  (with_template [<format> <name> <type> <prep>]
    [(def .public <name>
       (-> <type> Literal)
       (|>> <prep> <format> abstraction))]

    [%.int  int    Int  (<|)]
    [%.text string Text ..safe]
    [(<|)   symbol Text (format ":")]
    )

  (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])
         abstraction))

  (def .public (array_range from to array)
    (-> Expression Expression Expression Computation)
    (|> (format (representation from) ".." (representation to))
        (text.enclosed ["[" "]"])
        (format (representation array))
        abstraction))

  (def .public array
    (-> (List Expression) Computation)
    (|>> (list#each (|>> representation (text.suffix ..input_separator)))
         text.together
         (text.enclosed ["[" "]"])
         abstraction))

  (def .public hash
    (-> (List [Expression Expression]) Computation)
    (|>> (list#each (.function (_ [k v])
                      (format (representation k) " => " (representation v) ..input_separator)))
         text.together
         (text.enclosed ["{" "}"])
         abstraction))

  (def (control_structure content)
    (-> Text Text)
    (format content
            \n+ "end" ..statement_suffix))

  (type .public Block
    (Record
     [#parameters (List Var)
      #body Statement]))

  (def (block it)
    (-> Block Text)
    (|> (format (|> (.the #parameters it)
                    (list#each (|>> representation))
                    (text.interposed ..input_separator)
                    (text.enclosed' "|"))
                (..nested (representation (.the #body it))))
        (text.enclosed ["{" "}"])))

  (def .public (apply arguments block func)
    (-> (List Expression) (Maybe Block) Expression Computation)
    (let [arguments (|> arguments
                        (list#each (|>> representation))
                        (text.interposed ..input_separator)
                        (text.enclosed ["(" ")"]))
          block (.when block
                  {.#None}
                  ""
                  
                  {.#Some [inputs block]}
                  (|> block
                      representation
                      nested
                      control_structure
                      (format " do "
                              (|> inputs
                                  (list#each (|>> representation))
                                  (text.interposed ..input_separator)
                                  (text.enclosed' "|")))))]
      (abstraction (format (representation func) arguments block))))

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

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

  (def .public (? test then else)
    (-> Expression Expression Expression Computation)
    (|> (format (representation test) " ? "
                (representation then) " : "
                (representation else))
        (text.enclosed ["(" ")"])
        abstraction))

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

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

  (def .public (set vars value)
    (-> (List Location) Expression Statement)
    (abstraction
     (format (|> vars
                 (list#each (|>> representation))
                 (text.interposed ..input_separator))
             " = " (representation value) ..statement_suffix)))

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

  (with_template [<name> <control_structure>]
    [(def .public (<name> test then!)
       (-> Expression Statement Statement)
       (<| abstraction
           ..control_structure
           (format <control_structure> " " (representation test)
                   (..nested (representation then!)))))]

    [when "if"]
    [while "while"]
    )

  (def .public (for_in var array iteration!)
    (-> LVar Expression Statement Statement)
    (<| abstraction
        ..control_structure
        (format "for " (representation var)
                " in " (representation array)
                " do "
                (..nested (representation iteration!)))))

  (type .public Rescue
    (Record
     [#classes (List Text)
      #exception LVar
      #rescue Statement]))

  (def .public (begin body! rescues)
    (-> Statement (List Rescue) Statement)
    (<| abstraction
        ..control_structure
        (format "begin" (..nested (representation body!))
                (|> rescues
                    (list#each (.function (_ [classes exception rescue])
                                 (format \n+ "rescue " (text.interposed ..input_separator classes)
                                         " => " (representation exception)
                                         (..nested (representation rescue)))))
                    (text.interposed \n+)))))

  (def .public (catch expectation block)
    (-> Expression Block Expression)
    (<| abstraction
        (format "catch(" (representation expectation) ") "
                (..block block))))

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

  (def .public (raise message)
    (-> Expression Expression)
    (abstraction (format "raise " (representation message))))

  (with_template [<name> <keyword>]
    [(def .public <name>
       Statement
       (|> <keyword>
           (text.suffix ..statement_suffix)
           abstraction))]

    [next "next"]
    [redo "redo"]
    [break "break"]
    )

  (def .public initialize
    LVar
    (..manual "initialize"))

  (def .public (function name args body!)
    (-> LVar (List LVar) Statement Statement)
    (<| abstraction
        ..control_structure
        (format "def " (representation name)
                (|> args
                    (list#each (|>> representation))
                    (text.interposed ..input_separator)
                    (text.enclosed ["(" ")"]))
                (..nested (representation body!)))))

  (def .public (lambda name block)
    (-> (Maybe LVar) Block Literal)
    (let [proc (format "lambda " (..block block))]
      (|> (.when name
            {.#None}
            proc
            
            {.#Some name}
            (format (representation name) " = " proc))
          (text.enclosed ["(" ")"])
          abstraction)))

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

    ["==" =]
    [ "<" <]
    ["<=" <=]
    [ ">" >]
    [">=" >=]

    [ "+" +]
    [ "-" -]
    [ "*" *]
    [ "/" /]
    [ "%" %]
    ["**" pow]

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

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

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

    ["!" not]
    ["~" bit_not]
    ["-" opposite]
    )

  (def .public (comment commentary on)
    (All (_ brand) (-> Text (Code brand) (Code brand)))
    (abstraction (format "# "  (..safe commentary) \n+
                         (representation on))))

  (with_template [<name>]
    [(`` (def .public ((,, (template.symbol [<name> "/*"])) attributes)
           (-> (List Text) Statement)
           (..statement
            (..apply (list#each ..string attributes) {.#None} (..manual <name>)))))]

    ["attr_reader"]
    ["attr_writer"]
    ["attr_accessor"])
  )

(def .public (do method arguments block object)
  (-> Text (List Expression) (Maybe Block) Expression Computation)
  (|> object (..the method) (..apply arguments block)))

(def .public new
  (-> (List Expression) (Maybe Block) Expression Computation)
  (..do "new"))

(def .public (class definition)
  (-> Block Computation)
  (|> (..manual "Class")
      (..new (list) {.#Some definition})))

(def .public (module definition)
  (-> Block Computation)
  (|> (..manual "Module")
      (..new (list) {.#Some definition})))

(def .public (apply_lambda args lambda)
  (-> (List Expression) Expression Computation)
  (|> lambda
      (..do "call" args {.#None})))

(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 [<apply> (template.symbol ["apply/" <arity>])
                     <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>) {.#None} (..manual <function>))))]

       <definitions>))]

  [0
   [["gets"]
    ["caller"]]]

  [1
   [["print"]
    ["include"]
    ["extend"]
    ["require"]
    ["defined?"]]]

  [2
   [["print"]
    ["alias_method"]]]
  )

(def .public (throw/1 error)
  (-> Expression Statement)
  (..statement (..apply (list error) {.#None} (..manual "throw"))))

(def .public (throw/2 tag value)
  (-> Expression Expression Statement)
  (..statement (..apply (list tag value) {.#None} (..manual "throw"))))

(def .public (class_variable_set var value object)
  (-> SVar Expression Expression Computation)
  (..do "class_variable_set" (list (..string (..code var)) value) {.#None} object))

(def .public (class_variable_get var object)
  (-> SVar Expression Computation)
  (..do "class_variable_get" (list (..string (..code var))) {.#None} object))