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

(abstract: Global' {} Unit)
(abstract: Var' {} Unit)
(abstract: Computation' {} Unit)
(abstract: (Expression' k) {} Unit)
(abstract: Statement' {} Unit)

(abstract: (Code' k)
  {}
  
  Text

  (type: #export Code (Ex [k] (Code' k)))
  (type: #export Expression (Code' (Ex [k] (Expression' k))))
  (type: #export Global (Code' (Expression' Global')))
  (type: #export Var (Code' (Expression' Var')))
  (type: #export Argument
    {#reference? Bool
     #var Var})
  (type: #export Computation (Code' (Expression' Computation')))
  (type: #export Statement (Code' Statement'))
  
  (def: #export code (-> Code Text) (|>> @representation))

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

  (def: block
    (-> Text Text)
    (|>> nest (text.enclose ["{" "\n}"])))

  (def: computation
    (-> Text Computation)
    (|>> (text.enclose ["(" ")"]) @abstraction))

  (def: (statement code)
    (-> Text Statement)
    (@abstraction (format code ";")))

  (def: parameters
    (-> (List Argument) Text)
    (|>> (list/map (.function (_ [reference? var])
                     (if reference?
                       (format "&" (@representation var))
                       (@representation var))))
         (text.join-with ", ")
         (text.enclose ["(" ")"])))

  (do-template [<name> <reference?>]
    [(def: #export <name>
       (-> Var Argument)
       (|>> [<reference?>]))]

    [parameter false]
    [reference true]
    )

  (def: arguments
    (-> (List Expression) Text)
    (|>> (list/map ..code) (text.join-with ", ") (text.enclose ["(" ")"])))

  (def: #export var
    (-> Text Var)
    (|>> (format "$") @abstraction))

  (def: #export global
    (-> Text Global)
    (|>> @abstraction))

  (def: #export null
    Computation
    (@abstraction "NULL"))

  (def: #export bool
    (-> Bool Computation)
    (|>> %b @abstraction))

  (def: #export int
    (-> Int Computation)
    (|>> %i @abstraction))

  (def: #export float
    (-> Frac Computation)
    (|>> (cond> [(f/= number.positive-infinity)]
                [(new> "INF" computation)]
                
                [(f/= number.negative-infinity)]
                [(new> "-INF" computation)]
                
                [(f/= number.not-a-number)]
                [(new> "NAN" computation)]
                
                ## else
                [%f @abstraction])))

  (def: #export string
    (-> Text Computation)
    (|>> %t @abstraction))

  (def: #export (apply args func)
    (-> (List Expression) Expression Computation)
    (@abstraction
     (format (@representation func) (..arguments args))))

  (def: #export (function arguments uses body)
    (-> (List Argument) (List Argument) Statement Computation)
    (let [uses (case uses
                 #.Nil
                 ""

                 _
                 (format "use " (..parameters uses)))]
      (computation
       (format "function " (..parameters arguments)
               " " uses " "
               (block (@representation body))))))

  (do-template [<name> <function>]
    [(def: #export <name>
       Computation
       (..apply (list) (..global <function>)))]

    [func-num-args/0 "func_num_args"]
    [func-get-args/0 "func_get_args"]
    )

  (do-template [<name> <function>]
    [(def: #export (<name> values)
       (-> (List Expression) Computation)
       (..apply values (..global <function>)))]

    [array/* "array"]
    )

  (do-template [<name> <function>]
    [(def: #export (<name> required optionals)
       (-> Expression (List Expression) Computation)
       (..apply (list& required optionals) (..global <function>)))]

    [array-merge/+ "array_merge"]
    )

  (def: #export (array/** kvs)
    (-> (List [Expression Expression]) Computation)
    (computation
     (format "array("
             (|> kvs
                 (list/map (.function (_ [key value])
                             (format (@representation key) " => " (@representation value))))
                 (text.join-with ", "))
             ")")))

  (do-template [<name> <function>]
    [(def: #export (<name> input0)
       (-> Expression Computation)
       (..apply (list input0) (..global <function>)))]

    [is-null/1   "is_null"]
    [empty/1     "empty"]
    [count/1     "count"]
    [array-pop/1 "array_pop"]
    [floatval/1 "floatval"]
    )

  (do-template [<name> <function>]
    [(def: #export (<name> input0 input1)
       (-> Expression Expression Computation)
       (..apply (list input0 input1) (..global <function>)))]

    [call-user-func-array/2 "call_user_func_array"]
    [array-slice/2          "array_slice"]
    [array-push/2           "array_push"]
    )

  (do-template [<name> <function>]
    [(def: #export (<name> input0 input1 input2)
       (-> Expression Expression Expression Computation)
       (..apply (list input0 input1 input2) (..global <function>)))]

    [array-slice/3 "array_slice"])

  (def: #export (new constructor inputs)
    (-> Global (List Expression) Computation)
    (computation
     (format "new " (@representation constructor) (arguments inputs))))

  (def: #export (send method inputs object)
    (-> Text (List Expression) Expression Computation)
    (computation
     (format (@representation object) "->" method (arguments inputs))))

  (def: #export (nth idx array)
    (-> Expression Expression Computation)
    (computation
     (format (@representation array) "[" (@representation idx) "]")))

  (def: #export (? test then else)
    (-> Expression Expression Expression Computation)
    (computation
     (format (@representation test) " ? "
             (@representation then) " : "
             (@representation else))))

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

    [or      "||"]
    [and     "&&"]
    ## [is      "is"]
    [=       "=="]
    [<       "<"]
    [<=      "<="]
    [>       ">"]
    [>=      ">="]
    [+       "+"]
    [-       "-"]
    [*       "*"]
    [/       "/"]
    [%       "%"]
    [**      "**"]
    ## [bit-or  "|"]
    ## [bit-and "&"]
    ## [bit-xor "^"]
    ## [bit-shl "<<"]
    ## [bit-shr ">>"]
    )

  (def: #export not
    (-> Computation Computation)
    (|>> @representation (format "!") @abstraction))

  (do-template [<name> <type> <constructor>]
    [(def: #export (<name> var value)
       (-> Var Expression <type>)
       (<constructor> (format (@representation var) " = " (@representation value))))]

    [set!  Statement   ..statement]
    [set!' Computation ..computation]
    )
  
  (def: #export (set-nth! idx value array)
    (-> Expression Expression Expression Statement)
    (..statement
     (format (@representation array) "[" (@representation idx) "] = " (@representation value))))

  (def: #export global!
    (-> Var Statement)
    (|>> @representation (format "global ") ..statement))

  (def: #export (set-global! name value)
    (-> Text Expression Statement)
    (|> (..var "GLOBALS") (..set-nth! (..string name) value)))

  (def: #export (if! test then! else!)
    (-> Expression Statement Statement Statement)
    (@abstraction
     (format "if (" (@representation test) ")"
             (block (@representation then!))
             " else "
             (block (@representation else!)))))

  (def: #export (when! test then!)
    (-> Expression Statement Statement)
    (@abstraction
     (format "if (" (@representation test) ") "
             (block (@representation then!)))))

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

  ## (def: #export (while! test body!)
  ##   (-> Computation Statement Statement)
  ##   (@abstraction
  ##    (format "while " (expression test) ":"
  ##            (nest body!))))

  ## (def: #export (for-in! variable inputs body!)
  ##   (-> SVariable Computation Statement Statement)
  ##   (@abstraction
  ##    (format "for " (..name variable) " in " (expression inputs) ":"
  ##            (nest body!))))

  (type: #export Except
    {#class Global
     #exception Var
     #handler Statement})

  (def: (catch! except)
    (-> Except Text)
    (let [declaration (format "(" (@representation (get@ #class except))
                              " " (@representation (get@ #exception except)) ")")]
      (format "catch" declaration  " "
              (block (@representation (get@ #handler except))))))
  
  (def: #export (try! body! excepts)
    (-> Statement (List Except) Statement)
    (@abstraction
     (format "try " (block (@representation body!)) "\n"
             (|> excepts (list/map catch!) (text.join-with "\n")))))

  (do-template [<name> <keyword>]
    [(def: #export (<name> message)
       (-> Expression Statement)
       (statement (format <keyword> " " (@representation message))))]

    [throw!  "throw"]
    [return! "return"]
    [echo!   "echo"]
    )

  (def: #export do!
    (-> Expression Statement)
    (|>> @representation statement))

  (def: #export (define! name value)
    (-> Global Expression Statement)
    (do! (..apply (list (|> name @representation ..string)
                        value)
                  (..global "define"))))
  
  (def: #export (function! name args body)
    (-> Global (List Argument) Statement Statement)
    (@abstraction
     (format "function " (@representation name) (..parameters args)
             " " (block (@representation body)))))
  )