Let's go ahead and define afor because it will be less awkward to demo with:
; s/loop/aloop/ was the only change from for's definition
(mac afor (v init max . body)
(w/uniq (gi gm)
`(with (,v nil ,gi ,init ,gm (+ ,max 1))
(aloop (assign ,v ,gi) (< ,v ,gm) (assign ,v (+ ,v 1))
,@body))))
And now check it out!
; prints i from zero to ten, skipping odd values of i
arc> (afor i 0 10
(if (odd i)
(continue)
(pr i)))
*** redefining continue
0246810nil
(Note that since the continue macro gets generated anew on each call to aloop, you'll get the "redefining continue" message every time you call aloop or afor.) You could use the above definitions to spin off labeled versions of down, forlen, each and on, and a similar technique could be applied to redefine the while-family of macros.
I'm pretty sure the second (continue) form is expanded after the inner (afor ...) form is expanded, so that it tries to continue the wrong loop, but ends up referring to an unbound variable instead.
I recommend sticking to an anaphoric variable that has no macro wrapping it, sort of like this:
I'm pretty sure the second (continue) form is expanded after the inner (afor ...) form is expanded, so that it tries to continue the wrong loop, but ends up referring to an unbound variable instead.
Very good point, I hadn't tried to nest them before.
I will try your aloop definition when I get the chance.