herkforth words
herkforth has a lot of the same core words as other forths. If you are unfamiliar with basic forth words, I recommend reading some of the forth documentation.
It's very useful to know the herkforth naming conventions. They make it very much easier to read code, and sometimes to find the word you're looking for.
Speaking of finding words, check out the search commands in herkforth command keys.
New words in herkforth
These words don't (normally) apear in other forths.
Flow Control
herkforth doesn't have normal comparison operators (e.g. < > =
) Instead it
checks conditions as part of the control flow words if:
<if >if <>if <=if >=if 0if
and ;
(exit) words:
0=; <; >; <>; if;
<;
is equivalent to (in ans forths): < if exit then
There are two more which are like the above (they exit on a condition) except
that they only drop
if if TOS is zero:
0; ?;
0;
is like 0=;
except that it does not drop TOS unless it's zero. So
it's equivalent to the following ans code: dup 0 = if drop exit then
?;
is like if;
except it does not drop TOS unless it's zero, So it is
equivalent to the following in ans code: dup if exit then drop
Loops
herkforth has two types of loops. Counted (for
/next
) and tail
recursion.
Counted loops:
:
5stars
5 for $2a emit next ;
Note: 5 for
will cause the code between for
and next
to execute
5
times. I would like to emphesise that it's 5
times, because gforth
and probably other forths would execute it 6 times (go figure.)
Tail recursion: You don't have to do anything fancy to do tail recursion.
herkforth automagically turns a call just before a ;
into a branch. So
you can simply:
:
stars
$2a emit 1- 0; stars ;
Memory stores/fetches
In addition to the normal @
and !
herkforth also has an A (address)
register. You may have heard of this from colorforth and some other forths,
but here's a quick intro:
>a
pops TOS into the A register.
!a
stores TOS to the address in the A register
@a
fetches from the address in the A register
a
pushes A onto the stack
The A register is very useful for incrementing stores/fetches.
+!
increments the address in the A register (by one word) and stores to
that address.
h+!
increments the address in the A register (by a half-word) and
stores a half-word to that address.
b+!
increments the address in the A register (by one) and stores a byte
to that address.
As PPC has no post-increment or post-decrement stores or fetches, it's usually
best to decrement the pointer before you use it. Herkforth has these words:
b>a h>a w>a
which push TOS into the A register, and decrement it by 1, 2
and 4 bytes respectively.
While we're on the subject of stores and fetches, you can add an h
to make
it store/fetch a Half word (two bytes), or a b
prefix makes it just one
Byte. Thus, herkforth has the following words: b! b@ h! h@ b+! b-! h+!
h+@
. An a
postfix indicates that the store/fetch uses the A register,
thus we have: !a b!a
and h!a
Read the herkforth naming conventions for more details.
Words conspiciously absent from herkforth
rot else do loop immediate does> begin until while word parse
These are missing on purpose, and I intend not to ever add them.
herkforth immediates work without the word immediate
.
You don't need else.
herkforth variables work without the word variable
.