Saturday, May 3, 2008

Loops

I've implemented simple for/next loops now. The code below can be entered into a running Retro instance, then you can save the image to get them. (They will also be in the release due later today).

macro: for here 5 , ;
macro: next 6 , 27 , 25 , 8 , , ;

For those who are curious, this involves two compiler macros, which inline raw machine code into the words using the macros. As an example:

: foo 10 for 98 emit next ;

This will compile to:

foo:
lit 10

foo.1: ( 'for' )
push

lit 98

call emit

pop ( 'next' )

1-
0;
jump foo.1

;

There is a limitation that should be mentioned: when the loop counter reaches zero, the word will exit. Factor loops out if this is a problem for you.

No comments: