[ColorForth] How to escape from loops
- Subject: [ColorForth] How to escape from loops
- From: Richard Collins <rscollins@xxxxxxxxxxxxxxx>
- Date: Thu, 18 Jul 2002 10:12:19 +1200
Alex Thiel wrote:
Hello,
I can't seem to figure out how to escape from a FOR loop properly if some
exceptional condition occurs. I want to write something like this:
: loop for cond? if default next ; then exception ;
After "exception" has been executed, the loop should terminate. I guess I have
to shuffle the return stack a bit for this to work, but I don't know how.
Can anyone give me a hint?
Cheers,
Alex
------------------------
To Unsubscribe from this list, send mail to Mdaemon@xxxxxxxxxxxxxxxxxx with:
unsubscribe ColorForth
as the first and only line within the message body
Problems - List-Admin@xxxxxxxxxxxxxxxxxx
Main ColorForth site - http://www.colorforth.com
Wiki page http://kristopherjohnson.net/wiki/ColorForth
ColorForth index http://www.users.qwest.net/~loveall/c4index.htm
Your problem is that when both 'if' and 'for' compile they put the
address to jump back to onto the stack. When 'next' and 'then' compile,
they take the address to jump back to off the stack and compile a jump
back to that address. So you've got to get your nesting right.
In your example
: loop for cond? if default next ; then exception ;
They overlap so the 'next' will be jumping back to the 'if' and the then
will loop back to the 'for' which is not what you are trying to achieve.
This will work better.
: loop for cond? not if exception ; then exception next ;
As no doubt you know it still won't work since 'i' the counter is stored
on the return stack. You need to get rid of that counter before you do
the return. One way to do it is like this.
: loop for cond? not if exception pop drop ; then exception next ;
If you have to use a 'not' don't use the '-' as this doesn't update the
flags. You'll have to figure out some craftyness. Adding zero to
anything doesn't affect the value but updates the flags, though I'm sure
there is a better way.
Richard Collins
------------------------
To Unsubscribe from this list, send mail to Mdaemon@xxxxxxxxxxxxxxxxxx with:
unsubscribe ColorForth
as the first and only line within the message body
Problems - List-Admin@xxxxxxxxxxxxxxxxxx
Main ColorForth site - http://www.colorforth.com
Wiki page http://kristopherjohnson.net/wiki/ColorForth
ColorForth index http://www.users.qwest.net/~loveall/c4index.htm