aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/target/scheme.lux
blob: af3fd8b00b98c4d9f4b312963d67d838d07e1e68 (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
(.require
 [library
  [lux (.except Code int or and if cond let symbol when)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]]
   [control
    ["[0]" pipe]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor monoid)]]]
   [math
    [number
     ["n" nat]
     ["f" frac]]]
   [meta
    ["@" target]
    [macro
     ["[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)]
    (text.replaced text.new_line nested_new_line)))

(primitive.def .public (Code k)
  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)))))

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

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

    [Var Var' [Expression' Code]]
    [Computation Computation' [Expression' Code]]
    )

  (type .public Arguments
    (Record
     [#mandatory (List Var)
      #rest (Maybe Var)]))

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

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

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

  (def (arguments [mandatory rest])
    (-> Arguments (Code Any))
    (.when rest
      {.#Some rest}
      (.when mandatory
        {.#End}
        rest

        _
        (|> (format " . " (representation rest))
            (format (|> mandatory
                        (list#each ..code)
                        (text.interposed " ")))
            (text.enclosed ["(" ")"])
            abstraction))
      
      {.#None}
      (|> mandatory
          (list#each ..code)
          (text.interposed " ")
          (text.enclosed ["(" ")"])
          abstraction)))

  (def .public nil
    Computation
    (abstraction "'()"))

  (def .public bool
    (-> Bit Computation)
    (|>> (pipe.when
           #0 "#f"
           #1 "#t")
         abstraction))

  (def .public int
    (-> Int Computation)
    (|>> %.int abstraction))

  (def .public float
    (-> Frac Computation)
    (|>> (pipe.cond [(f.= f.positive_infinity)]
                    [(pipe.new "+inf.0" [])]
                    
                    [(f.= f.negative_infinity)]
                    [(pipe.new "-inf.0" [])]
                    
                    [f.not_a_number?]
                    [(pipe.new "+nan.0" [])]
                    
                    ... else
                    [%.frac])
         abstraction))

  (def .public positive_infinity Computation (..float f.positive_infinity))
  (def .public negative_infinity Computation (..float f.negative_infinity))
  (def .public not_a_number Computation (..float f.not_a_number))

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

                   ["\" "\\"]
                   ["|" "\|"]
                   [text.alarm "\a"]
                   [text.back_space "\b"]
                   [text.tab "\t"]
                   [text.new_line "\n"]
                   [text.carriage_return "\r"]
                   [text.double_quote (format "\" text.double_quote)]
                   ))
             )))
  
  (def .public string
    (-> Text Computation)
    (|>> ..safe %.text abstraction))

  (def .public symbol
    (-> Text Computation)
    (|>> (format "'") abstraction))

  (def form
    (-> (List (Code Any)) Code)
    (.let [nested_new_line (format \n+ text.tab)]
      (|>> (pipe.when
             {.#End}
             (abstraction "()")
             
             {.#Item head tail}
             (|> tail
                 (list#each (|>> representation ..nested))
                 {.#Item (representation head)}
                 (text.interposed nested_new_line)
                 (text.enclosed ["(" ")"])
                 abstraction)))))
  
  (def .public (apply args func)
    (-> (List Expression) Expression Computation)
    (..form {.#Item func args}))
  
  (with_template [<name> <function>]
    [(def .public (<name> members)
       (-> (List Expression) Computation)
       (..apply members (..var <function>)))]

    [vector/* "vector"]
    [list/*   "list"]
    )

  (def .public apply/0
    (-> Expression Computation)
    (..apply (list)))

  (with_template [<lux_name> <scheme_name>]
    [(def .public <lux_name>
       (apply/0 (..var <scheme_name>)))]

    [newline/0 "newline"]
    )

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

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

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

    [apply/1 [_0] [Expression]
     [[exact/1 "exact"]
      [integer->char/1 "integer->char"]
      [char->integer/1 "char->integer"]
      [number->string/1 "number->string"]
      [string->number/1 "string->number"]
      [floor/1 "floor"]
      [truncate/1 "truncate"]
      [string/1 "string"]
      [string?/1 "string?"]
      [length/1 "length"]
      [values/1 "values"]
      [null?/1 "null?"]
      [car/1 "car"]
      [cdr/1 "cdr"]
      [raise/1 "raise"]
      [error_object_message/1 "error-object-message"]
      [make_vector/1 "make-vector"]
      [vector_length/1 "vector-length"]
      [not/1 "not"]
      [string_hash/1 "string-hash"]
      [reverse/1 "reverse"]
      [display/1 "display"]
      [exit/1 "exit"]
      [string_length/1 "string-length"]
      [load_relative/1 "load-relative"]]]

    [apply/2 [_0 _1] [Expression Expression]
     [[append/2 "append"]
      [cons/2 "cons"]
      [make_vector/2 "make-vector"]
      ... [vector_ref/2 "vector-ref"]
      [list_tail/2 "list-tail"]
      [map/2 "map"]
      [string_ref/2 "string-ref"]
      [string_append/2 "string-append"]
      [make_string/2 "make-string"]]]

    [apply/3 [_0 _1 _2] [Expression Expression Expression]
     [[substring/3 "substring"]
      [vector_set!/3 "vector-set!"]
      [string_contains/3 "string-contains"]]]

    [apply/5 [_0 _1 _2 _3 _4] [Expression Expression Expression Expression Expression]
     [[vector_copy!/5 "vector-copy!"]]]
    )

  ... TODO: define "vector_ref/2" like a normal apply/2 function.
  ... "vector_ref/2" as an 'invoke' is problematic, since it only works
  ... in Kawa.
  ... However, the way Kawa defines "vector-ref" causes trouble,
  ... because it does a runtime type-check which throws an error when
  ... it checks against custom values/objects/classes made for
  ... JVM<->Scheme interop.
  ... There are 2 ways to deal with this:
  ... 0. To fork Kawa, and get rid of the type-check so the normal
  ... "vector-ref" can be used instead.
  ... 1. To carry on, and then, when it's time to compile the compiler
  ... itself into Scheme, switch from 'invoke' to normal 'vector-ref'.
  ... Either way, the 'invoke' needs to go away.
  (def .public (vector_ref/2 vector index)
    (-> Expression Expression Computation)
    (..form (list (..var "invoke") vector (..symbol "getRaw") index)))

  (with_template [<lux_name> <scheme_name>]
    [(def .public (<lux_name> param subject)
       (-> Expression Expression Computation)
       (..apply/2 (..var <scheme_name>) subject param))]

    [=/2   "="]
    [eq?/2 "eq?"]
    [eqv?/2 "eqv?"]
    [</2   "<"]
    [<=/2  "<="]
    [>/2   ">"]
    [>=/2  ">="]
    [string=?/2 "string=?"]
    [string<?/2 "string<?"]
    [+/2   "+"]
    [-/2   "-"]
    [//2   "/"]
    [*/2   "*"]
    [expt/2 "expt"]
    [remainder/2 "remainder"]
    [quotient/2 "quotient"]
    [mod/2 "mod"]
    [arithmetic_shift/2 "arithmetic-shift"]
    [bitwise_and/2 "bitwise-and"]
    [bitwise_ior/2 "bitwise-ior"]
    [bitwise_xor/2 "bitwise-xor"]
    )

  (with_template [<lux_name> <scheme_name>]
    [(def .public <lux_name>
       (-> (List Expression) Computation)
       (|>> (list.partial (..var <scheme_name>)) ..form))]

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

  (with_template [<lux_name> <scheme_name> <var> <pre>]
    [(def .public (<lux_name> bindings body)
       (-> (List [<var> Expression]) Expression Computation)
       (..form (list (..var <scheme_name>)
                     (|> bindings
                         (list#each (function (_ [binding/name binding/value])
                                      (..form (list (|> binding/name <pre>)
                                                    binding/value))))
                         ..form)
                     body)))]

    [let           "let"           Var       (<|)]
    [let*          "let*"          Var       (<|)]
    [letrec        "letrec"        Var       (<|)]
    [let_values    "let-values"    Arguments ..arguments]
    [let*_values   "let*-values"   Arguments ..arguments]
    [letrec_values "letrec-values" Arguments ..arguments]
    )

  (def .public (if test then else)
    (-> Expression Expression Expression Computation)
    (..form (list (..var "if") test then else)))

  (def .public (when test then)
    (-> Expression Expression Computation)
    (..form (list (..var "when") test then)))

  (def .public (lambda arguments body)
    (-> Arguments Expression Computation)
    (..form (list (..var "lambda")
                  (..arguments arguments)
                  body)))

  (def .public (define_function name arguments body)
    (-> Var Arguments Expression Computation)
    (..form (list (..var "define")
                  (|> arguments
                      (revised #mandatory (|>> {.#Item name}))
                      ..arguments)
                  body)))

  (def .public (define_constant name value)
    (-> Var Expression Computation)
    (..form (list (..var "define") name value)))

  (def .public begin
    (-> (List Expression) Computation)
    (|>> {.#Item (..var "begin")} ..form))

  (def .public (set! name value)
    (-> Var Expression Computation)
    (..form (list (..var "set!") name value)))

  (def .public (with_exception_handler handler body)
    (-> Expression Expression Computation)
    (..form (list (..var "with-exception-handler") handler body)))

  (def .public (call_with_current_continuation body)
    (-> Expression Computation)
    (..form (list (..var "call-with-current-continuation") body)))

  (def .public (guard variable clauses else body)
    (-> Var (List [Expression Expression]) (Maybe Expression) Expression Computation)
    (..form (list (..var "guard")
                  (..form (|> (.when else
                                {.#None}
                                (list)
                                
                                {.#Some else}
                                (list (..form (list (..var "else") else))))
                              (list#composite (list#each (function (_ [when then])
                                                           (..form (list when then)))
                                                         clauses))
                              (list.partial variable)))
                  body)))
  )