Re: acceptance of forth
- From: Josh Grams <josh@xxxxxxxxxxx>
- Date: Mon, 30 Nov 2009 13:38:08 GMT
Tinkerer Atlarge wrote: <1j9zm4j.qzwbt6zit6moN%tinkerer@xxxxxxxxxxxxxxx>
The secret might be to forget about constructs and consider the action
of WHILE and REPEAT as entities in their own right which only need
BEGIN as an end-marker for their own purposes. But that is just me
thinking aloud. I don't claim to know yet.
You could do that. It's probably better to start by just using the
basic three constructions, but I do somewhat think of them as individual
pieces.
My understanding goes something like this:
Forth has an unconditional branch and a branch-if-false. It also
differentiates between forward and backward branches. So we have six
basic words to compile these branches, and three convenience words which
combine the basic operations. These words use a stack at compile-time:
Forward Branches
----------------
IF/AHEAD ( -- branch )
Compile a forward (conditional?) branch, leaving a reference so we
can point it to the right place later.
THEN ( branch -- )
Resolve `branch` to jump here.
ELSE ( branch1 -- branch2 ) AHEAD SWAP THEN ; IMMEDIATE
-------------------->
Usually `IF <true-action> ELSE <false-action> THEN`.
---------------------->
It ends the <true-action> section with an unconditional `branch2`
which skips over the <false-action>, and then resolves the
branch-if-false from the IF (so that it jumps here to begin the
<false-action> section).
Backward Branches
-----------------
BEGIN ( -- addr )
Push a marker so that we can branch (loop) back to it.
UNTIL/AGAIN ( addr -- )
Loop (conditially?) back to the most recent BEGIN.
WHILE ( addr -- branch addr ) IF SWAP ; IMMEDIATE
--------------------->
Usually `BEGIN <test> WHILE <action> REPEAT`
<-----------------------
Branch forward out of a loop (putting the branch *under* the BEGIN
addr).
: REPEAT ( branch addr -- ) AGAIN THEN ; IMMEDIATE
loop back to the most recent BEGIN, then resolve the WHILE branch to
jump just outside the loop.
Then you can do crazy things like:
BEGIN <test1> WHILE <test2> WHILE
<action>
REPEAT <failed-test2> ELSE <failed-test1> THEN
:-)
--Josh
.
- References:
- continued fractions
- From: Hugh Aguilar
- Re: continued fractions
- From: Albert van der Horst
- Re: continued fractions
- From: Hugh Aguilar
- Re: continued fractions
- From: Coos Haak
- Re: continued fractions
- From: Hugh Aguilar
- Re: continued fractions
- From: Coos Haak
- Re: continued fractions
- From: Hugh Aguilar
- Re: continued fractions
- From: idknow
- Re: continued fractions
- From: Elizabeth D Rather
- acceptance of forth (was: Re: continued fractions)
- From: Uwe Kloß
- Re: acceptance of forth
- From: Tinkerer Atlarge
- Re: acceptance of forth
- From: Doug Hoffman
- Re: acceptance of forth
- From: Tinkerer Atlarge
- Re: acceptance of forth
- From: Stephen Pelc
- Re: acceptance of forth
- From: Tinkerer Atlarge
- Re: acceptance of forth
- From: Marcel Hendrix
- Re: acceptance of forth
- From: Tinkerer Atlarge
- continued fractions
- Prev by Date: Re: acceptance of forth
- Next by Date: Re: Open Firmware (was: acceptance of forth)
- Previous by thread: Re: acceptance of forth
- Next by thread: Re: acceptance of forth
- Index(es):
Relevant Pages
|