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
|
(defvar sys:*op-ctx*)
(sys:make-struct-type
'sys:op-ctx nil nil '(form gens up meta rec recvar nested) nil
(lambda (me)
(slotset me 'up sys:*op-ctx*)
(slotset me 'meta (gensym "meta-")))
nil nil)
(defun sys:ensure-op-arg (ctx n)
(let ((ag (slot ctx 'gens)))
(when (> n 1024)
['compile-error (slot ctx 'form)
"@~a calls for function with too many arguments" n])
(for ((i (len ag)) (l))
((<= i n)
(sys:setq ag (append ag (nreverse l)))
(slotset ctx 'gens ag)
[ag n])
((sys:setq i (succ i)))
(sys:setq l (cons (gensym `arg-@(if (plusp i) i "rest")-`) l)))))
(defun sys:op-meta-p (exp)
(tree-case exp
((x y . r) (and (null r)
(cond
((eq x 'sys:expr) (let ((depth (sys:op-meta-p y)))
(if depth (succ depth))))
((eq x 'sys:var) (if (or (integerp y)
(eq y 'rest))
0)))))))
(defun sys:op-rec-p (exp)
(or (tree-case exp
((x (y . t)) (and (eq x 'sys:expr) (eq y 'usr:rec))))
(equal exp '(sys:var usr:rec))))
(defun sys:op-ensure-rec (ctx : recvar)
(when recvar
(slotset ctx 'recvar t))
(or (slot ctx 'rec) (slotset ctx 'rec (gensym "rec-"))))
(defun sys:find-parent (ctx depth)
(for ((more t)) (more ctx) ((if (minusp (sys:setq depth (pred depth)))
(sys:setq more nil)))
(sys:setq ctx (slot ctx 'up))))
(defun sys:op-alpha-rename (e op-args do-nested-metas)
(let* ((ctx sys:*op-ctx*)
(code ^(macrolet ((sys:expr (:form f arg)
(let* ((ctx ,ctx)
(depth (sys:op-meta-p arg))
(rec (sys:op-rec-p arg))
(up (slot ctx 'up))
(par (cond
(depth (sys:find-parent ctx depth))
(rec up))))
(cond
((and par (or depth rec))
(slotset par 'nested t)
^(,(slot (slot ctx 'up) 'meta) (quote ,arg)))
((sys:op-rec-p f)
^(,(sys:op-ensure-rec ctx) ,*(rest arg)))
(t f))))
(sys:var (:form f arg . mods)
(cond
((sys:op-meta-p f)
(unless (integerp arg)
(sys:setq arg 0))
(sys:ensure-op-arg ,ctx arg))
((sys:op-rec-p f)
(sys:op-ensure-rec ,ctx t))
(t f)))
,*(if do-nested-metas
^((,(slot ctx 'meta) ((quote arg)) arg))))
,op-args)))
(expand code e)))
(eval-only
(defmacro op-ignerr (x)
^(sys:catch (error) ,x () (error (. args) (ignore args)))))
(defun sys:op-expand (f e args)
(unless args
['compile-error f "arguments required"])
(let* ((compat (and (plusp sys:compat) (<= sys:compat 225)))
(ctx (make-struct 'sys:op-ctx ^(form ,f)))
(sys:*op-ctx* ctx)
(sym (car f))
(do-gen (if (eq sym 'do) (gensym)))
(syntax-0 (if (eq sym 'do) args ^[,*args]))
(syntax-1 (if (or (null syntax-0) (neq sym 'do) compat)
(sys:op-alpha-rename e syntax-0 nil)
(let ((syn (op-ignerr (sys:op-alpha-rename e
syntax-0
nil))))
(if syn
(if (or (slot ctx 'gens) (slot ctx 'nested))
syn
(sys:op-alpha-rename e
(append syntax-0
(list do-gen))
nil))
(let ((syn (sys:op-alpha-rename
e (append syntax-0
(list do-gen))
nil)))
(when (or (slot ctx 'gens) (slot ctx 'nested))
(sys:op-alpha-rename e syntax-0 nil)
['compile-error f "internal error"])
syn)))))
(syntax-2 (sys:op-alpha-rename e syntax-1 t))
(metas (slot ctx 'gens))
(rec (slot ctx 'rec))
(recvar (slot ctx 'recvar))
(rest-sym (sys:ensure-op-arg ctx 0))
(lambda-interior (let ((fargs (tree-case syntax-2
((t t . fa) fa))))
(cond
((and (eq sym 'lop) fargs)
(let ((fargs-l1 (mapcar (lambda (farg)
^(sys:l1-val ,farg))
fargs)))
^[sys:apply ,(car (cdr syntax-2))
(append ,rest-sym (list ,*fargs-l1))]))
(metas syntax-2)
((eq sym 'do)
(let ((arg1 (sys:ensure-op-arg ctx 1)))
^(symacrolet ((,do-gen ,arg1))
,syntax-2)))
(t (append syntax-2 rest-sym))))))
(let ((metas (slot ctx 'gens)))
(cond
(recvar ^(sys:lbind ((,rec (lambda (,*(cdr metas) . ,rest-sym)
(let ((,rec (fun ,rec)))
,lambda-interior))))
(fun ,rec)))
(rec ^(sys:lbind ((,rec (lambda (,*(cdr metas) . ,rest-sym)
,lambda-interior)))
(fun ,rec)))
(t ^(lambda (,*(cdr metas) . ,rest-sym)
,lambda-interior))))))
(defmacro op (:form f :env e . args)
(sys:op-expand f e args))
(defmacro do (:form f :env e . args)
(sys:op-expand f e args))
(defmacro lop (:form f :env e . args)
(sys:op-expand f e args))
(defmacro ldo (op . args)
^(do ,op @1 ,*args))
(defmacro ap (. args)
^(apf (op ,*args)))
(defmacro ip (. args)
^(ipf (op ,*args)))
(defmacro ado (. args)
^(apf (do ,*args)))
(defmacro ido (. args)
^(ipf (do ,*args)))
(defmacro ret (arg)
^(op identity* ,arg))
(defmacro aret (arg)
^(ap identity* ,arg))
(defun sys:opip-single-let-p (c)
(tree-case c
((op sym)
(and (eq op 'let)
(atom sym)))
(t nil)))
(defun sys:opip-let-p (c)
(tree-case c
((op (sym t) . rest)
(and (eq op 'let)
(atom sym)
(listp rest)))
(t nil)))
(defun sys:opip-expand (e opsym dosym clauses)
(tree-case clauses
(nil nil)
((c . rest)
(if (atom c)
(cons c (sys:opip-expand e opsym dosym rest))
(let ((sym (car c)))
(cond
((memq sym '(dwim uref qref op do lop ldo ap ip ado ido ret aret))
(cons c (sys:opip-expand e opsym dosym rest)))
((sys:opip-single-let-p c)
(tree-bind (t sym) c
(sys:opip-expand e opsym dosym ^((let (,sym @1)) ,*rest))))
((sys:opip-let-p c)
(tree-bind (t . vars) c
^((do let* ,vars
[(opip ,*(sys:opip-expand e opsym dosym rest)) @1]))))
(t (let ((opdo (if (or (special-operator-p (car c))
(macro-form-p c e)) dosym opsym)))
(cons ^(,opdo ,*c) (sys:opip-expand e opsym dosym
rest))))))))))
(defmacro opip (:env e . clauses)
^[chain ,*(sys:opip-expand e 'op 'do clauses)])
(defmacro oand (:env e . clauses)
^[chand ,*(sys:opip-expand e 'op 'do clauses)])
(defmacro lopip (:env e . clauses)
^[chain ,*(sys:opip-expand e 'lop 'ldo clauses)])
(defmacro loand (:env e . clauses)
^[chand ,*(sys:opip-expand e 'lop 'ldo clauses)])
(defmacro opf (:env e fun . clauses)
^[,fun ,*(sys:opip-expand e 'op 'do clauses)])
(defmacro lopf (:env e fun . clauses)
^[,fun ,*(sys:opip-expand e 'lop 'ldo clauses)])
(defmacro flow (val . opip-args)
^(call (opip ,*opip-args) ,val))
(defmacro lflow (val . opip-args)
^(call (lopip ,*opip-args) ,val))
(defmacro tap (. args)
^(prog1 @1 ,args))
|