aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/ruby.lux
blob: fb1eaed26050a2b0d1517da6f8eeec4518108936 (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
(.module:
  [library
   [lux (#- Location Code static int if cond function or and not comment local global)
    ["@" target]
    [abstract
     [equivalence (#+ Equivalence)]
     [hash (#+ Hash)]
     ["." enum]]
    [control
     [pipe (#+ case> cond> new>)]
     [parser
      ["<.>" code]]]
    [data
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." functor mix)]]]
    [macro
     [syntax (#+ syntax:)]
     ["." template]
     ["." code]]
    [math
     [number
      ["n" nat]
      ["f" frac]]]
    [type
     abstract]]])

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

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

(abstract: .public (Code brand)
  {}
  
  Text

  (implementation: .public code_equivalence
    (All (_ brand) (Equivalence (Code brand)))

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

  (implementation: .public code_hash
    (All (_ brand) (Hash (Code brand)))

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

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

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

  (template [<type> <super>+]
    [(with_expansions [<brand> (template.identifier [<type> "'"])]
       (abstract: (<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]]
    )

  (template [<type> <super>+]
    [(with_expansions [<brand> (template.identifier [<type> "'"])]
       (abstract: .public <brand> {} Any)
       (`` (type: .public <type> (|> <brand> (~~ (template.spliced <super>+))))))]

    [Literal [Computation' Expression' Code]]
    [Access [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]]
    )

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

    [GVar "$"  global]
    [IVar "@"  instance]
    [SVar "@@" static]
    )

  (def: .public local
    (-> Text LVar)
    (|>> :abstraction))

  (template [<var> <prefix> <modifier> <unpacker>]
    [(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]
    )

  (template [<ruby_name> <lux_name>]
    [(def: .public <lux_name>
       (..global <ruby_name>))]

    ["@" latest_error]
    ["_" 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]
    )

  (template [<ruby_name> <lux_name>]
    [(def: .public <lux_name>
       (..local <ruby_name>))]

    ["ARGV" command_line_arguments]
    )

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

  (def: .public bool
    (-> Bit Literal)
    (|>> (case> #0 "false"
                #1 "true")
         :abstraction))

  (def: safe
    (-> Text Text)
    (`` (|>> (~~ (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)]
                   ))
             )))

  (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)
    (|>> (cond> [(f.= f.positive_infinity)]
                [(new> "(+1.0/0.0)" [])]
                
                [(f.= f.negative_infinity)]
                [(new> "(-1.0/0.0)" [])]
                
                [(f.= f.not_a_number)]
                [(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) Literal)
    (|>> (list\each (|>> :representation))
         (text.interposed ..input_separator)
         (text.enclosed ["[" "]"])
         :abstraction))

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

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

  (def: .public (apply_lambda/* args lambda)
    (-> (List Expression) Expression Computation)
    (|> args
        (list\each (|>> :representation))
        (text.interposed ..input_separator)
        (text.enclosed ["[" "]"])
        (format (:representation lambda))
        :abstraction))

  (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!)
             text.new_line
             (: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: (block content)
    (-> Text Text)
    (format content
            text.new_line "end" ..statement_suffix))

  (def: .public (if test then! else!)
    (-> Expression Statement Statement Statement)
    (<| :abstraction
        ..block
        (format "if " (:representation test)
                (..nested (:representation then!))
                text.new_line "else"
                (..nested (:representation else!)))))

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

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

  (def: .public (for_in var array iteration!)
    (-> LVar Expression Statement Statement)
    (<| :abstraction
        ..block
        (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
        ..block
        (format "begin" (..nested (:representation body!))
                (|> rescues
                    (list\each (.function (_ [classes exception rescue])
                                 (format text.new_line "rescue " (text.interposed ..input_separator classes)
                                         " => " (:representation exception)
                                         (..nested (:representation rescue)))))
                    (text.interposed text.new_line)))))

  (def: .public (catch expectation body!)
    (-> Expression Statement Statement)
    (<| :abstraction
        ..block
        (format "catch(" (:representation expectation) ") do"
                (..nested (:representation body!)))))

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

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

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

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

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

  (def: .public (lambda name args body!)
    (-> (Maybe LVar) (List Var) Statement Literal)
    (let [proc (|> (format (|> args
                               (list\each (|>> :representation))
                               (text.interposed ..input_separator)
                               (text.enclosed' "|"))
                           (..nested (:representation body!)))
                   (text.enclosed ["{" "}"])
                   (format "lambda "))]
      (|> (case name
            #.None
            proc
            
            (#.Some name)
            (format (:representation name) " = " proc))
          (text.enclosed ["(" ")"])
          :abstraction)))

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

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

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

  (def: .public (comment commentary on)
    (All (_ brand) (-> Text (Code brand) (Code brand)))
    (:abstraction (format "# "  (..safe commentary) text.new_line
                          (:representation on))))
  )

(def: .public (do method args object)
  (-> Text (List Expression) Expression Computation)
  (|> object (..the method) (..apply/* args)))

(def: .public (cond clauses else!)
  (-> (List [Expression Statement]) Statement Statement)
  (list\mix (.function (_ [test then!] next!)
              (..if test then! next!))
            else!
            (list.reversed clauses)))

(syntax: (arity_inputs [arity <code>.nat])
  (in (case arity
        0 (.list)
        _ (|> (-- arity)
              (enum.range n.enum 0)
              (list\each (|>> %.nat code.local_identifier))))))

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

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

     (template [<function>]
       [(`` (def: .public (~~ (template.identifier [<function> "/" <arity>]))
              (<apply> (..local <function>))))]

       <definitions>))]

  [1
   [["print"]
    ["require"]]]

  [2
   [["print"]]]

  [3
   [["print"]]]
  )

(def: .public throw/1
  (-> Expression Statement)
  (|>> (..apply/1 (..local "throw"))
       ..statement))