This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
048control.wart
139 lines (108 loc) · 2.47 KB
/
048control.wart
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
mac (do1 ... body)
`(ret $ret ,car.body
,@cdr.body)
mac (between before after|and ... body)
`(do1
(do ,before ,@body)
,after)
mac (before cleanup ... body)
`(do1
(do ,@body)
,cleanup)
mac (when cond ... body)
`(if ,cond
(do ,@body))
mac (unless cond ... body)
`(if (not ,cond)
(do ,@body))
mac (iflet var expr ... branches)
if no.branches
expr
`(let $tmp ,expr
(if $tmp
(let ,var $tmp
,car.branches)
,(if cdr.branches
`(iflet ,var ,@cdr.branches))))
mac (iflet var x op test then ... branches) :case (op = :satisfies)
`(let ,var ,x
(if (,test ,var)
,then
(iflet ,var ,@branches)))
mac (aif expr ... branches)
`(iflet it ,expr ,@branches)
mac (whenlet var test ... body)
`(iflet ,var ,test
(do ,@body))
mac (awhen test ... body)
`(whenlet it ,test
,@body)
mac (aand ... args)
if no.args
1
~cdr.args
car.args
:else
`(aif ,car.args
(aand ,@cdr.args))
mac (while test ... body)
`(when ,test
,@body
(while ,test
,@body))
mac (whilet var test ... body)
`(let $var ,test
(when $var
(let ,var $var
,@body)
(whilet ,var ,test
,@body)))
mac (awhile test ... body)
`(whilet it ,test
,@body)
mac (for var start test update ... body)
`(let ,var ,start
(while ,test
,@body
,update))
mac (each var expr ... body)
`(for $i (as list ,expr) $i (zap! cdr $i)
(let ,var car.$i
,@body))
mac (on var expr ... body)
`(for ($i index) (list ,expr 0) $i (do (zap! cdr $i) ++index)
(let ,var car.$i
,@body))
mac (forlen var expr ... body)
`(for ,var 0 (< ,var (len ,expr)) (++ ,var)
,@body)
mac (repeat n ... body)
`(for $i 0 (< $i ,n) ++$i
,@body)
mac (repeat n ... body) :case (n = :forever)
`(while 1 ,@body)
# (f&g x) => (and (f x) (g x))
# multiply-evals by design to support macros
def (& ... fs)
(fn 'args
(eval `(and ,@(map :over fs
:with (fn(f) `(,f ,@args))))
caller_scope))
def (| ... fs)
(fn 'args
(eval `(or ,@(map :over fs
:with (fn(f) `(,f ,@args))))
caller_scope))
mac (&~ f g)
`(,f & (~ ,g))
def (functionize f)
if (and (cons?&car.f ~= 'fn)
(mem? '_ f))
`(fn(_) ,f)
f
mac (x -> f)
`(,functionize.f ,x)
mac (transform x ... fs|thru)
if no.fs
x
`(transform (,x -> ,car.fs) ,@cdr.fs)