aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/host/r.lux
blob: 6af15d058b0482cf95de9af7e556a3ca650fb883 (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
(.module:
  [lux #- not or and list if function cond when]
  (lux (control pipe)
       (data [maybe "maybe/" Functor<Maybe>]
             [text]
             text/format
             [number]
             (coll [list "list/" Functor<List> Fold<List>]))
       (type abstract)))

(abstract: #export Single {} Unit)
(abstract: #export Poly {} Unit)

(abstract: #export (Var kind)
  {}

  Text

  (def: name (All [k] (-> (Var k) Text)) (|>> @representation))

  (def: #export var (-> Text (Var Single)) (|>> @abstraction))
  (def: #export var-args (Var Poly) (@abstraction "..."))
  )

(type: #export SVar (Var Single))
(type: #export PVar (Var Poly))

(abstract: #export Expression
  {}
  
  Text

  (def: #export expression (-> Expression Text) (|>> @representation))

  (def: #export code (-> Text Expression) (|>> @abstraction))

  (def: (self-contained code)
    (-> Text Expression)
    (@abstraction
     (format "(" code ")")))

  (def: nest
    (-> Text Text)
    (|>> (format "\n")
         (text.replace-all "\n" "\n  ")))

  (def: (_block expression)
    (-> Text Text)
    (format "{" (nest expression) "\n" "}"))

  (def: #export (block expression)
    (-> Expression Expression)
    (@abstraction
     (format "{" (@representation expression) "}")))

  (def: #export null
    Expression
    (|> "NULL" self-contained))

  (def: #export n/a
    Expression
    (|> "NA" self-contained))

  (def: #export not-available Expression n/a)
  (def: #export not-applicable Expression n/a)
  (def: #export no-answer Expression n/a)

  (def: #export bool
    (-> Bool Expression)
    (|>> (case> true "TRUE"
                false "FALSE")
         self-contained))

  (def: #export (int value)
    (-> Int Expression)
    (self-contained
     (format "as.integer(" (%i value) ")")))

  (def: #export float
    (-> Frac Expression)
    (|>> (cond> [(f/= number.positive-infinity)]
                [(new> "1.0/0.0")]
                
                [(f/= number.negative-infinity)]
                [(new> "-1.0/0.0")]
                
                [(f/= number.not-a-number)]
                [(new> "0.0/0.0")]
                
                ## else
                [%f])
         self-contained))

  (def: #export string
    (-> Text Expression)
    (|>> %t self-contained))

  (def: (composite-literal left-delimiter right-delimiter entry-serializer)
    (All [a] (-> Text Text (-> a Text)
                 (-> (List a) Expression)))
    (.function (_ entries)
      (self-contained
       (format left-delimiter
               (|> entries (list/map entry-serializer) (text.join-with ","))
               right-delimiter))))

  (def: #export named-list
    (-> (List [Text Expression]) Expression)
    (composite-literal "list(" ")" (.function (_ [key value])
                                     (format key "=" (@representation value)))))
  
  (do-template [<name> <function>]
    [(def: #export <name>
       (-> (List Expression) Expression)
       (composite-literal (format <function> "(") ")" expression))]

    [vector "c"]
    [list   "list"]
    )
  
  (def: #export (slice from to list)
    (-> Expression Expression Expression Expression)
    (self-contained
     (format (@representation list)
             "[" (@representation from) ":" (@representation to) "]")))

  (def: #export (slice-from from list)
    (-> Expression Expression Expression)
    (self-contained
     (format (@representation list)
             "[-1"  ":-" (@representation from) "]")))

  (def: #export (apply args func)
    (-> (List Expression) Expression Expression)
    (self-contained
     (format (@representation func) "(" (text.join-with "," (list/map expression args)) ")")))

  (def: #export (apply-kw args kw-args func)
    (-> (List Expression) (List [Text Expression]) Expression Expression)
    (self-contained
     (format (@representation func)
             (format "("
                     (text.join-with "," (list/map expression args)) ","
                     (text.join-with "," (list/map (.function (_ [key val])
                                                     (format key "=" (expression val)))
                                                   kw-args))
                     ")"))))

  (def: #export (nth idx list)
    (-> Expression Expression Expression)
    (self-contained
     (format (@representation list) "[[" (@representation idx) "]]")))

  (def: #export (if test then else)
    (-> Expression Expression Expression Expression)
    (self-contained
     (format "if(" (@representation test) ")"
             " " (.._block (@representation then))
             " else " (.._block (@representation else)))))

  (def: #export (when test then)
    (-> Expression Expression Expression)
    (self-contained
     (format "if(" (@representation test) ") {"
             (.._block (@representation then))
             "\n" "}")))

  (def: #export (cond clauses else)
    (-> (List [Expression Expression]) Expression Expression)
    (list/fold (.function (_ [test then] next)
                 (if test then next))
               else
               (list.reverse clauses)))

  (do-template [<name> <op>]
    [(def: #export (<name> param subject)
       (-> Expression Expression Expression)
       (self-contained
        (format (@representation subject)
                " " <op> " "
                (@representation param))))]

    [=       "=="]
    [<       "<"]
    [<=      "<="]
    [>       ">"]
    [>=      ">="]
    [+       "+"]
    [-       "-"]
    [*       "*"]
    [/       "/"]
    [%%      "%%"]
    [**      "**"]
    [or      "||"]
    [and     "&&"]
    )

  (def: #export @@
    (All [k] (-> (Var k) Expression))
    (|>> ..name self-contained))

  (def: #export global
    (-> Text Expression)
    (|>> var @@))

  (do-template [<name> <func>]
    [(def: #export (<name> param subject)
       (-> Expression Expression Expression)
       (..apply (.list subject param) (..global <func>)))]

    [bit-or   "bitwOr"]
    [bit-and  "bitwAnd"]
    [bit-xor  "bitwXor"]
    [bit-shl  "bitwShiftL"]
    [bit-ushr "bitwShiftR"]
    )

  (def: #export (bit-not subject)
    (-> Expression Expression)
    (..apply (.list subject) (..global "bitwNot")))

  (do-template [<name> <op>]
    [(def: #export <name>
       (-> Expression Expression)
       (|>> @representation (format <op>) self-contained))]

    [not    "!"]
    [negate "-"]
    )
  
  (def: #export (length list)
    (-> Expression Expression)
    (..apply (.list list) (..global "length")))

  (def: #export (range from to)
    (-> Expression Expression Expression)
    (self-contained
     (format (@representation from) ":" (@representation to))))

  (def: #export (function inputs body)
    (-> (List (Ex [k] (Var k))) Expression Expression)
    (let [args (|> inputs (list/map ..name) (text.join-with ", "))]
      (self-contained
       (format "function(" args ") "
               (.._block (@representation body))))))

  (def: #export (try body warning error finally)
    (-> Expression (Maybe Expression) (Maybe Expression) (Maybe Expression) Expression)
    (let [optional (: (-> Text (Maybe Expression) (-> Text Text) Text)
                      (.function (_ parameter value preparation)
                        (|> value
                            (maybe/map (|>> @representation preparation (format ", " parameter " = ")))
                            (maybe.default ""))))]
      (self-contained
       (format "tryCatch("
               (.._block (@representation body))
               (optional "warning" warning id)
               (optional "error" error id)
               (optional "finally" finally .._block)
               ")"))))

  (def: #export (while test body)
    (-> Expression Expression Expression)
    (self-contained
     (format "while (" (@representation test) ") "
             (.._block (@representation body)))))

  (def: #export (for-in var inputs body)
    (-> SVar Expression Expression Expression)
    (self-contained
     (format "for (" (..name var) " in " (..expression inputs) ")"
             (.._block (@representation body)))))

  (do-template [<name> <keyword>]
    [(def: #export (<name> message)
       (-> Expression Expression)
       (..apply (.list message) (..global <keyword>)))]

    [stop  "stop"]
    [print "print"]
    )

  (def: #export (set! var value)
    (-> (Var Single) Expression Expression)
    (self-contained
     (format (..name var) " <- " (@representation value))))

  (def: #export (set-nth! idx value list)
    (-> Expression Expression SVar Expression)
    (self-contained
     (format (..name list) "[[" (@representation idx) "]] <- " (@representation value))))

  (def: #export (then pre post)
    (-> Expression Expression Expression)
    (@abstraction
     (format (@representation pre)
             "\n"
             (@representation post))))
  )