|
![](/i/fill.gif) |
Patrick Elliott wrote:
>
> Actually the code for loops you had looked more
> C-ish. ;) Though, out of context it is hard to tell how cryptic it really is.
Well, here's some context. And it's straightforwardly-written code, so
anything that's "cryptic" is the fault of Forth, not me.
In Forth, \ to end-of-line is a comment. You can also enclose comments
in parens, which is how I will demonstrate what the stack looks like.
[For more on stack-based programming, please see
<URL:http://www-2.cs.cmu.edu/~koopman/stack_computers/contents.html>,
especially Section 1.2.]
N \ This puts the address of N (where the variable is stored) onto the
stack.
( 9238 ) \ Made-up memory address
@ \ This uses up the address of N, and fetches what was IN that address.
( 5 ) \ Made-up value of N.
0 \ This is zero. It gets put on the stack, too.
( 5 0 )
DO \ "DO" takes two numbers off of the stack and loops between them.
\ In this case, it will loop from 0 to N-1, inclusive.
( ) \ Nothing on the stack.
5 \ This puts 5 on the stack.
( 5 )
SQRT \ This replaces 5 with what happens when we run the SQRT word.
\ If we're lucky, it'll turn out to be 2.
\ (Stacks are usually integers, y'see.)
( 2 ) \ The SQRT of 5.
I \ This gives you the value of the looping variable, which is always
\ (for singly-nested loops) called I. It puts it on the stack.
( 2 0 ) \ First time through, it's 0. After that it's 1. And so on.
:)
17 \ Puts 17 on the stack.
( 2 0 17 )
* \ Multiply the top two numbers, and replace them with the result.
( 2 0 ) \ 17 * 0 == 0
23 \ Puts 23 on the stack.
( 2 0 23 )
+ \ Replace the top two numbers with their sum.
( 2 23 )
DOSOMETHING \ DOSOMETHING (notionally) takes the top two numbers, and
\ does something with them. We will also imagine that it
\ doesn't bother to return anything.
( )
LOOP \ Finish the loop started with "DO".
You know what? That's really not very helpful.
Here's a better idea. Go to
<URL:http://www.softsynth.com/pforth/pf_tut.htm> and poke around
"Arithmetic", "Variables" and "Loops". Phil Burk does a much better job
of explaining it than I just did. Sorry about that.
Also, since this has nothing more to do with scene files, please direct
all followups to povray.off-topic or to me personally, via email.
> Annoying thing being
> that the changed everything (including all new computer systems) the year after I
> graduated. :p
Always the way. That happened to me in elementary, junior high, and
college. In high school, they upgraded the last year I was there. Heh.
Deaken
Post a reply to this message
|
![](/i/fill.gif) |