-
Notifications
You must be signed in to change notification settings - Fork 31
/
lang.phn
507 lines (389 loc) · 11 KB
/
lang.phn
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
(define "SYSTEM" (dirname __FILE__))
(define "LIB_PATH" (. SYSTEM "/lib/"))
(define "LIB_PHAREN_PATH" (. SYSTEM "/lib/pharen/"))
(set-include-path (. (get-include-path) PATH_SEPARATOR LIB_PATH PATH_SEPARATOR LIB_PHAREN_PATH))
(require "sequence.php")
(defmacro comment (&exprs)
'NULL)
(defmacro when (c &body)
'(if ~c
(do
~@body)
NULL))
(defmacro when-set (val)
'(when (isset ~val) ~val))
(defmacro error (&msgs)
'(error-log (. ~@msgs)))
(defmacro not (expr)
'(! ~expr))
(defmacro not-set (expr)
'(not (isset ~expr)))
(defmacro pharen-or (e1 e2)
'(if ~e1 ~e1 ~e2))
(defmacro pharen-and (e1 e2)
'(cond
((not ~e1) ~e1)
((not ~e2) ~e2)
(TRUE ~e2)))
(defmacro load (name)
(def php-file (. name ".php"))
(local file (. name ".phn"))
(local old-node-ns (:: Node ns))
(local old-ns (:: RootNode ns))
(local raw-ns (:: RootNode raw-ns))
(local old-ns_string (:: RootNode ns-string))
(local lib-path (stream-resolve-include-path file))
(when lib-path
(compile-file lib-path)
(= (:: Node ns) old-node-ns)
(= (:: RootNode ns) old-ns)
(= (:: RootNode ns-string) old-ns-string)
(= (:: RootNode raw-ns) raw-ns)
'(include ~php-file)))
(defmacro runtime (expr)
'(let [time-start (microtime TRUE)
result ~expr
time-duration (- (microtime TRUE) time-start)
time-to-display (if (> time-duration 1)
(. time-duration " seconds")
(. (* time-duration 1000) " milliseconds"))]
(print (. "Elapsed time: " time-to-display "\n"))
NULL))
(defmacro thunk (&exprs)
'(lambda () ~@exprs))
(defmacro inst? (var cls)
'(instanceof ~var ~-cls))
(defmacro print* (&s)
'(print (. ~@s)))
(fn first (xs)
(-> (seq xs) (first)))
(poly-ann first (^Seq xs))
(fun first (^FastSeq xs)
(-> xs first))
(fn rest (xs)
(-> (seq xs) (rest)))
(poly-ann rest (^Seq xs))
(fun rest (^FastSeq xs)
(-> xs rest))
(fn pharen-list (a &xs)
(cons a xs))
(fn butlast (xs)
(take (dec (count xs)) xs))
(fn last (xs)
(:xs (dec (count xs))))
(fn eq (val1 val2)
(cond
((inst? val1 IPharenComparable) (-> val1 (eq val2)))
((inst? val2 IPharenComparable) (-> val2 (eq val1)))
(TRUE (=== val1 val2))))
(defmacro if-let (binding then else)
(def cond (:binding 1))
'(if ~cond
(let ~binding
~then)
~else))
(defmacro when-let (binding then)
(def cond (:binding 1))
'(when ~cond
(let ~binding
~then)))
(defmacro if-not (cond then else)
'(if (not ~cond)
~then
~else))
(defmacro when-not (cond &then)
'(when (not ~cond)
~@then))
(defmacro dotimes (binding &exprs)
(def varname (:binding 0))
(def n (:binding 1))
'((lambda (~varname --dotimes-result)
(if (>= ~varname ~n)
--dotimes-result
(do
(local --dotimes-result (do ~@exprs))
(recur (inc ~varname) --dotimes-result)))) 0 NULL))
(fn prn (s &other)
(let
[str (. s (seq-join other))]
(print* str "\n")
NULL))
(fn false? (x)
(=== FALSE x))
(fn true? (x)
(=== TRUE x))
(fn null? (x)
(=== NULL x))
(fn zero? (n)
(=== n 0))
(fn pos? (n)
(> n 0))
(fn neg? (n)
(< n 0))
(fn odd? (n)
(=== (% n 2) 1))
(fn even? (n)
(=== (% n 2) 0))
(fn str (s1 s2)
(. s1 s2))
(fn identity (x)
x)
(fn inc (x)
(+ 1 x))
(fn dec (x)
(- x 1))
(defmacro set! (var val)
'(local ~var ~val))
(defmacro alter (var f &args)
'(local ~var (~$f ~var ~@args)))
(fn comp (&fs)
(let [rfs (reverse fs)]
(lambda (&args)
(let [init (call-user-func-array (first rfs) (arr args))]
(reduce #apply init (rest rfs))))))
(fn zero-or-empty? (n xs)
(or (zero? n) (empty? xs)))
(fn empty? (xs)
(or (inst? (seq xs) .PharenEmptyList) (not (seq xs))))
(poly-ann empty? (^Seq xs))
(fun empty? (^FastSeq xs)
(=== (-> xs length) 0))
(fn seq? (x)
(inst? x IPharenSeq))
(fun seq? (^Seq x)
TRUE)
(fn sequential? (x)
(or (inst? x IPharenSeq) (is-array x)))
(fn assoc? (x)
(inst? x PharenHashMap))
(fn seq (x)
(if (inst? x IPharenSeq)
(-> x (seq))
(:: PharenList (seqify x))))
(fun seq (^Seq x)
x)
(fun seq (^FastSeq x)
x)
(fn hashify (x)
(if (inst? x PharenHashMap)
x
(new PharenHashMap x))) ; TODO: Do actual conversion into a hashmap-suitable data structure
(fn hash-from-pairs (pairs)
(reduce (lambda (pair hm)
(assoc (:pair 0) (:pair 1) hm))
{}
pairs))
(fn force (x)
(-> x (force)))
(fn realized? (x)
(-> x (realized)))
(defmacro lazy-obj (cls &exprs)
'(new ~cls (lambda () ~@exprs)))
(defmacro delay (expr)
'(lazy-obj .PharenDelay ~expr))
(defmacro lazy-seq (list-expr)
'(lazy-obj .PharenLazyList ~list-expr))
(fn arr (x)
(cond
((is-array x) x)
((inst? x "PharenHashMap") (-> x (arr)))
((inst? x "IPharenSeq") (-> x (arr)))))
(defmacro second (xs)
'(first (rest ~xs)))
(fn first-pair (xs)
(array-slice xs 0 1))
(fn cons (x xs)
(if (seq? xs)
(-> xs (cons x))
(-> (seq xs) (cons x))))
(poly-ann cons (x ^Seq xs))
(fn assoc (key val hm)
(-> (hashify hm) (assoc key val)))
(fn get (key hm)
(if (isset (:hm key))
(:hm key)
NULL))
(fn take (n xs)
(if (zero-or-empty? n xs)
[]
(cons (first xs) (take (- n 1) (rest xs)))))
(fn drop (n xs)
(if (zero-or-empty? n xs)
xs
(drop (- n 1) (rest xs))))
(fn reverse (xs [acc []])
(if (empty? xs)
acc
(reverse (rest xs) (cons (first xs) acc))))
(fn interpose (sep xs [acc []])
(if (=== (count xs) 1)
[(first xs)]
(cons (first xs) (cons sep (interpose sep (rest xs))))))
(fn partition (n xs)
(if (empty? xs)
xs
(cons (take n xs) (partition n (drop n xs)))))
(fn interleave (xs ys)
(if (or (empty? xs) (empty? ys))
[]
(cons (first xs) (cons (first ys) (interleave (rest xs) (rest ys))))))
(fn zip-with (f xs ys)
(if (or (empty? xs) (empty? ys))
[]
(cons ($f (first xs) (first ys))
(zip-with f (rest xs) (rest ys)))))
(fn pharen-sort (xs)
(let [arr (arr xs)]
(sort arr)
arr))
(fn seq-join (xs [glue ""])
(implode glue (arr xs)))
(fn seq-rand (xs)
(:xs (rand 0 (dec (count xs)))))
(fn infinity ([n 0])
(lazy-seq (cons n (infinity (+ n 1)))))
(fn repeat (x)
(lazy-seq (cons x (repeat x))))
(fn repeatedly (f)
(lazy-seq (cons ($f) (repeatedly f))))
(fn iterate (f init)
(lazy-seq (cons init (iterate f ($f init)))))
(fn cycle (xs)
(lazy-seq (concat xs (cycle xs))))
(fn cycle-with (f xs)
(lazy-seq
(let [new-xs (map f xs)]
(concat xs (cycle-with f new-xs)))))
(fn vals (m)
(array-values (arr m)))
(fn append (x xs)
(concat xs [x]))
(fn apply (f &args)
(let [name (if (is-array f) (:f 0) f)
args-array (arr (if (sequential? (last args))
(concat (butlast args) (last args))
args))]
(when (is-array f)
(array-push args-array (:f 1)))
(call-user-func-array name args-array)))
(fn flip (f)
(lambda (x y)
($f y x)))
(fn juxt (&fs)
(lambda (&args)
(map (lambda (f) (apply f args))
fs)))
(fn concat (xs1 xs2)
(if (empty? xs1)
xs2
(cons (first xs1) (concat (rest xs1) xs2))))
(fn into (to from)
(reduce #cons to from))
(fn reduce (f acc xs)
(if (empty? xs)
acc
(reduce f ($f (first xs) acc) (rest xs))))
(poly-ann reduce (f acc ^Seq xs))
(poly-ann reduce (f acc ^FastSeq xs))
(fn reduce-fns (fns acc xs)
(if (empty? xs)
acc
(reduce-fns (rest fns) ((first fns) (first xs) acc) (rest xs))))
(fn reduce-to-str (new-val-func xs)
(reduce (lambda (val acc)
(. acc ($new-val-func val)))
"" xs))
(fn reduce-pairs (f acc xs)
(if (empty? xs)
acc
(reduce-pairs f ($f (each xs) acc) (rest xs))))
(fn map (f xs)
(if (empty? xs)
xs
(cons ($f (first xs)) (map f (rest xs)))))
(fn filter (f coll)
(if (empty? coll)
[]
(let [x (first coll), xs (rest coll)]
(if (not ($f x))
(filter f xs)
(cons x (filter f xs))))))
(fn until (f xs)
(cond
((empty? xs) FALSE)
((local result ($f (first xs))) result)
(TRUE (until f (rest xs)))))
(fn map-indexed (f xs [idx 0])
(if (empty? xs)
[]
(cons ($f (first xs) idx) (map-indexed f (rest xs) (inc idx)))))
(fn map-pairs (f pairs)
(reduce-pairs (lambda (pair acc) (append ($f (:pair 0) (:pair 1)) acc))
[]
pairs))
(fn repling ()
(and (defined "PHARENREPLMODE") (constant "PHARENREPLMODE")))
(defmacro attr* (access-modifier name val)
'(access ~access-modifier (local ~name ~val)))
(defmacro attr (name val)
'(attr* public ~name ~val))
(defmacro private-attr (name val)
'(attr* private ~name ~val))
(defmacro static-attr (name val)
'(attr* static ~name ~val))
(defmacro method* (access-modifier name args &code)
'(access ~access-modifier
(fn ~name ~args
~@code)))
(defmacro method (name args &code)
'(method* public ~name ~args
~@code))
(defmacro private-method (name args &code)
'(method* private ~name ~args
~@code))
(defmacro static-method (name args &code)
'(method* static ~name ~args
~@code))
(defmacro this (&exprs)
'(-> this ~@exprs))
(defmacro self (&expr)
'(:: self ~@exprs))
(defmacro signature (name args)
'(signature* public ~name ~args))
(defmacro private-signature (name args)
'(signature* private ~name ~args))
(class MultiManager
(access static (local multis NULL))
(access static (fn matching-multi-exists (multi-name serialized-args)
(isset (:: self (:multis multi-name serialized-args)))))
(access static (fn get-matching-multi (multi-name serialized-args)
(:: self (:multis multi-name serialized-args))))
(access static (fn set-multi (multi-name pattern f)
(= (:: self (:multis multi-name pattern)) f))))
(= (:: MultiManager multis) {})
(fn multi-serialize-args (vals)
(reduce-to-str (lambda (val)
(cond
((is-string val) "str")
((is-int val) "int")
((is-float val) "float")
((is-bool val) "bool")
((is-array val) (if (isset (:val "_multitype")) (:val "_multitype") "5"))
((is-object val) (get-class val))))
vals))
(fn multi-serialize-pattern (pattern)
(implode (arr pattern)))
(fn get-multi (name args)
(let [serialized-args (multi-serialize-args args)]
(if (:: MultiManager (matching-multi-exists name serialized-args))
(:: MultiManager (get-matching-multi name serialized-args))
"No matching pattern")))
(defmacro defmulti (nm args)
(def name (. nm ""))
'(do
(= (:: MultiManager (:multis ~name)) {})
(fn ~nm ~args
((get-multi ~name (func-get-args)) ~@args))))
(defmacro defmethod (nm pattern args &body)
(def name (. nm ""))
'(:: MultiManager (set-multi ~name (multi-serialize-pattern ~pattern) (lambda (a) ~@body))))