aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/target/js.lux
blob: c158d9651062403616c262b9f992f56dca509725 (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
(.require
 [library
  [lux (.except Location Code Label or and function if undefined for comment not int try ++ -- the type_of , when)
   [control
    ["[0]" pipe]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [math
    [number
     ["i" int]
     ["f" frac]]]
   [meta
    [macro
     ["[0]" template]]
    [type
     ["[0]" nominal (.except def)]]]]])

(def expression
  (text.enclosed ["(" ")"]))

(def element
  (text.enclosed ["[" "]"]))

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

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

(nominal.def .public (Code brand)
  Text

  (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]]
    [Statement [Code]]
    )

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

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

  (with_template [<name> <literal>]
    [(def .public <name> Literal (abstraction <literal>))]

    [null "null"]
    [undefined "undefined"]
    )

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

  (def .public (number value)
    (-> Frac Literal)
    (abstraction
     (cond (f.not_a_number? value)
           "NaN"

           (f.= f.positive_infinity value)
           "Infinity"
           
           (f.= f.negative_infinity value)
           "-Infinity"

           ... else
           (|> value %.frac ..expression))))

  (def safe
    (-> Text Text)
    (`` (|>> (,, (with_template [<replace> <find>]
                   [(text.replaced <find> <replace>)]

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

  (def .public string
    (-> Text Literal)
    (|>> ..safe
         (text.enclosed [text.double_quote text.double_quote])
         abstraction))

  (def argument_separator ", ")
  (def field_separator ": ")
  (def statement_suffix ";")

  (def .public array
    (-> (List Expression) Computation)
    (|>> (list#each ..code)
         (text.interposed ..argument_separator)
         ..element
         abstraction))

  (def .public var
    (-> Text Var)
    (|>> abstraction))

  (def .public (at index array_or_object)
    (-> Expression Expression Access)
    (abstraction (format (representation array_or_object) (..element (representation index)))))

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

  (def .public (apply function inputs)
    (-> Expression (List Expression) Computation)
    (|> inputs
        (list#each ..code)
        (text.interposed ..argument_separator)
        ..expression
        (format (representation function))
        abstraction))

  (def .public (do method inputs object)
    (-> Text (List Expression) Expression Computation)
    (apply (..the method object) inputs))

  (def .public object
    (-> (List [Text Expression]) Computation)
    (|>> (list#each (.function (_ [key val])
                      (format (representation (..string key)) ..field_separator (representation val))))
         (text.interposed ..argument_separator)
         (text.enclosed ["{" "}"])
         ..expression
         abstraction))

  (def .public (, pre post)
    (-> Expression Expression Computation)
    (|> (format (representation pre) ..argument_separator (representation post))
        ..expression
        abstraction))

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

  (def block
    (-> Statement Text)
    (let [close (format \n+ "}")]
      (|>> representation
           ..nested
           (text.enclosed ["{"
                           close]))))

  (def .public (function_definition name inputs body)
    (-> Var (List Var) Statement Statement)
    (|> body
        ..block
        (format "function " (representation name)
                (|> inputs
                    (list#each ..code)
                    (text.interposed ..argument_separator)
                    ..expression)
                " ")
        abstraction))

  (def .public (function name inputs body)
    (-> Var (List Var) Statement Computation)
    (|> (..function_definition name inputs body)
        representation
        ..expression
        abstraction))

  (def .public (closure inputs body)
    (-> (List Var) Statement Computation)
    (|> body
        ..block
        (format "function"
                (|> inputs
                    (list#each ..code)
                    (text.interposed ..argument_separator)
                    ..expression)
                " ")
        ..expression
        abstraction))

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

    [=  "==="]
    [<  "<"]
    [<= "<="]
    [>  ">"]
    [>= ">="]
    
    [+  "+"]
    [-  "-"]
    [*  "*"]
    [/  "/"]
    [%  "%"]

    [left_shift "<<"]
    [arithmetic_right_shift ">>"]
    [logic_right_shift ">>>"]

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

  (with_template [<prefix> <name>]
    [(def .public <name>
       (-> Expression Computation)
       (|>> representation (text.prefix <prefix>) ..expression abstraction))]

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

  (with_template [<name> <input> <format>]
    [... A 32-bit integer expression.
     (def .public (<name> value)
       (-> <input> Computation)
       (abstraction (..expression (format (<format> value) "|0"))))]

    [to_i32 Expression representation]
    [i32 Int %.int]
    )

  (def .public (int value)
    (-> Int Literal)
    (abstraction (.if (i.< +0 value)
                   (%.int value)
                   (%.nat (.nat value)))))

  (def .public (? test then else)
    (-> Expression Expression Expression Computation)
    (|> (format (representation test)
                " ? " (representation then)
                " : " (representation else))
        ..expression
        abstraction))

  (def .public type_of
    (-> Expression Computation)
    (|>> representation
         (format "typeof ")
         ..expression
         abstraction))

  (def .public (new constructor inputs)
    (-> Expression (List Expression) Computation)
    (|> (format "new " (representation constructor)
                (|> inputs
                    (list#each ..code)
                    (text.interposed ..argument_separator)
                    ..expression))
        ..expression
        abstraction))

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

  (def .public use_strict
    Statement
    (abstraction (format text.double_quote "use strict" text.double_quote ..statement_suffix)))

  (def .public (declare name)
    (-> Var Statement)
    (abstraction (format "var " (representation name) ..statement_suffix)))

  (def .public (define name value)
    (-> Var Expression Statement)
    (abstraction (format "var " (representation name) " = " (representation value) ..statement_suffix)))

  (def .public (set name value)
    (-> Location Expression Expression)
    (abstraction (format (representation name) " = " (representation value))))

  (def .public (throw message)
    (-> Expression Statement)
    (abstraction (format "throw " (representation message) ..statement_suffix)))

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

  (def .public delete
    (-> Location Expression)
    (|>> representation
         (format "delete ")
         ..expression
         abstraction))

  (def .public (if test then! else!)
    (-> Expression Statement Statement Statement)
    (abstraction (format "if(" (representation test) ") "
                         (..block then!)
                         " else "
                         (..block else!))))

  (def .public (when test then!)
    (-> Expression Statement Statement)
    (abstraction (format "if(" (representation test) ") "
                         (..block then!))))

  (def .public (while test body)
    (-> Expression Statement Loop)
    (abstraction (format "while(" (representation test) ") "
                         (..block body))))

  (def .public (do_while test body)
    (-> Expression Statement Loop)
    (abstraction (format "do " (..block body)
                         " while(" (representation test) ")" ..statement_suffix)))

  (def .public (try body [exception catch])
    (-> Statement [Var Statement] Statement)
    (abstraction (format "try "
                         (..block body)
                         " catch(" (representation exception) ") "
                         (..block catch))))

  (def .public (for var init condition update iteration)
    (-> Var Expression Expression Expression Statement Loop)
    (abstraction (format "for(" (representation (..define var init))
                         " " (representation condition)
                         ..statement_suffix " " (representation update)
                         ")"
                         (..block iteration))))

  (def .public label
    (-> Text Label)
    (|>> abstraction))

  (def .public (with_label label loop)
    (-> Label Loop Statement)
    (abstraction (format (representation label) ": " (representation loop))))

  (with_template [<keyword> <0> <1>]
    [(def .public <0>
       Statement
       (abstraction (format <keyword> ..statement_suffix)))

     (def .public (<1> label)
       (-> Label Statement)
       (abstraction (format <keyword> " " (representation label) ..statement_suffix)))]

    ["break"    break    break_at]
    ["continue" continue continue_at]
    )

  (with_template [<name> <js>]
    [(def .public <name>
       (-> Location Expression)
       (|>> representation
            (text.suffix <js>)
            abstraction))]

    [++ "++"]
    [-- "--"]
    )

  (def .public (comment commentary on)
    (All (_ kind) (-> Text (Code kind) (Code kind)))
    (abstraction (format "/* "  commentary " */" " " (representation on))))

  (def .public (switch input cases default)
    (-> Expression (List [(List Literal) Statement]) (Maybe Statement) Statement)
    (abstraction (format "switch (" (representation input) ") "
                         (|> (format (|> cases
                                         (list#each (.function (_ [when then])
                                                      (format (|> when
                                                                  (list#each (|>> representation (text.enclosed ["case " ":"])))
                                                                  (text.interposed \n+))
                                                              (..nested (representation then)))))
                                         (text.interposed \n+))
                                     \n+
                                     (.when default
                                       {.#Some default}
                                       (format "default:"
                                               (..nested (representation default)))
                                       
                                       {.#None}
                                       ""))
                             abstraction
                             ..block))))
  )

(with_template [<apply> <arg>+ <type>+ <function>+]
  [(`` (def .public (<apply> function)
         (-> Expression (,, (template.spliced <type>+)) Computation)
         (.function (_ (,, (template.spliced <arg>+)))
           (..apply function (list (,, (template.spliced <arg>+)))))))

   (`` (with_template [<definition> <function>]
         [(def .public <definition> (<apply> (..var <function>)))]

         (,, (template.spliced <function>+))))]

  [apply_1 [_0] [Expression]
   [[not_a_number? "isNaN"]]]

  [apply_2 [_0 _1] [Expression Expression]
   []]

  [apply_3 [_0 _1 _2] [Expression Expression Expression]
   []]
  )