# System dictionary
This is the list of words provided by the kernel and its boot sequence,
organized by topics.
You should read doc/usage first to understand concepts mentioned here.
Stack notation: "<stack before> -- <stack after>". Rightmost is top of stack
(TOS). For example, in "a b -- c d", b is TOS before, d is TOS after. "R:" means
that the Return Stack is modified.
Some words have a variable stack signature, most often in pair with a flag.
These are indicated with "?" to tell that the argument might not be there. For
example, "-- n? f" means that "n" might or might not be there.
Some elements are surrounded in "" quotes, for example ""name" --". This
indicates an element that isn't read from PS, but from the input stream (with
Letters have consistent meanings
a -- address
n -- number
f -- flag
s -- string
w -- word
e -- entry
m -- entry metadata
r -- range
u -- count
c -- character, a 8-bit value
a b c -- order of elements matter
The description of the words can contain letters in between "*" characters,
indicating a special attribute:
*I* indicates an immediate word.
*B* indicates that it is "binary modulable".
*C* indicates that the word can be compiled. Its description is its behavior in
interpret mode, but when in compile mode, it will "do the right thing" to
compile the same behvior.
Across words, different symbols are used in different contexts,
but we try to be consistent in their use. Here's their definitions:
! - Store
@ - Fetch
$ - Initialize
^ - Opposite
( - Lower boundary
) - Upper boundary
' - Address of
? - As a suffix, means "Is it ...?". As a prefix, "do ... if flag"
[...] - Indicates immediateness
## System variables and constants
sysdict Pointer to last word of the system dictionary. Is a linked list.
nextmeta Pointer to the metadata that will be assigned to the next entry to
be created. Reset to 0 after each entry. Is a linked list.
curword Address of last read word (with "word" word). Is a string.
[rcnt] Contains the current "R level" which is changed when compiling RS-
related words during compilation.
HERE Address containing the value of "here".
HEREMAX Address containing the upper bound for "here"
MAIN Address containing the value of "main".
ABORT Address containing the value of "abort".
EMIT Address containing the value of "emit".
IN< Address containing the value of "in<".
CELLSZ Size of a cell: 4
CALLSZ The size in bytes of a native call.
Obey "to" semantics
here Address where the "write" words write.
Obey "to" semantics. See description in sections below.
in< -- c
emit c --
Stack words move elements around in PS and RS.
drop a --
2drop a b --
dup a -- a a
?dup a -- a? a dup if a is nonzero.
2dup a b -- a b a b
swap a b -- b a
over a b -- a b a
nip a b -- b
tuck a b -- b a b
rot a b c -- b c a
rot> a b c -- c a b
rdrop -- *I* Compile a RS shrink of 4 bytes.
r@ -- *I* Compile a push of current RS top to PS.
r> -- *I* Equivalent to r@ rdrop
>r -- *I* Compiles a RS grow of 4 bytes followed by a pop
of PS into that new RS space.
rfree -- *I* Shrink RS by the current [rcnt] level and reset
[rcnt] to 0.
r+, n -- Compile a RS grow (n is negative) or shrink (n is
positive) operation by n bytes.
r', off -- Compile the yield of RSP with "off" offset applied to
it. At runtime, this number will be pushed to PS.
p+, n -- Same as r+, but for PS.
p', off -- Same as r', but for PS.
scnt -- n Number of elements in PS, excluding "n".
rcnt -- n Number of elementS in RS, excluding this call.
stack? -- Error out if scnt < 0.
Memory words fetch, store or write to an address in memory. "bw" means "binary
width", a value that can be 1, 2 or 4 depending of the width of the operation.
@ a -- n *B* Fetch n at address a.
! n a -- *B* Store n at address a.
+! n a -- *B* Add n to value stored at address a.
@! n1 a -- n2 *B* Fetch n2 at address a, then store n1 at address a.
@+ a -- a+bw n *B* Fetch n at address a and increase a.
!+ n a -- a+bw *B* Store n at address a and increase a.
@@+ a -- n *B* Do an indirect fetch from a and then increase the
pointer contained in a by "bw".
@!+ n a -- *B* Do an indirect store to a and then increase the
pointer contained in a by "bw".
, n -- *B* Write n to here and increase here by "bw".
8b -- *I* Set binary width to 8-bit
16b -- *I* Set binary width to 16-bit
allot u -- Increase here by u.
allot0 u -- Allot u and fill this space with zeroes.
move src dst u -- Copy u bytes from address src to address dst, moving
move, src u -- Copy u bytes to "here" and increase "here" by u.
fill a u c -- Fill range [a, a+u] with byte c.
align4 n -- Allot 0, 1, 2 or 3 bytes so that "here+n" is divisible
nc, n -- Parse n numbers from input stream and write them as
[c]? c a u -- i Search for character c in range [a, a+u] and yield its
index or -1 if not found.
c@ --> 8b @
c! --> 8b !
c, --> 8b ,
w@ --> 16b @
w! --> 16b !
c@+ --> 8b @+
c!+ --> 8b !+
1+ a -- a+1
1- a -- a-1
+ a b -- a+b
- a b -- a-b
-^ a b -- b-a
* a b -- a*b
/ a b -- a/b
/mod a b -- r q r is remainder, q is quotient
mod a b -- n n is the remainder of a divided by b
neg a -- -a
and a b -- n n is the result of a binary "and" of a and b
or a b -- n n is the result of a binary "or" of a and b
xor a b -- n n is the result of a binary "exclusive or" of a and b
^ a -- n n is the result of "a -1 xor", flipping all bits.
lshift n u -- n Shift n by u bits to the left
rshift n u -- n Shift n by u bits to the right
<< n -- n Shift n left by 1
>> n -- n Shift n right by 1
<<c n -- n c Shift n left by 1, yielding c as the 32-bit carry bit
>>c n -- n c Shift n right by 1, yielding c as the 32-bit carry bit
upcase c -- c If c is between "a" and "z", yield their upcase version.
Otherwise, yield c.
"f" is a flag that can be either 0 or 1. We describe conditions for "f" to be 1.
Other conditions yield f=0.
< a b -- f f=1 if a is lower than b
> a b -- f f=1 if a is higher than b
= a b -- f f=1 if a equals b
<> a b -- f f=1 if a is not equal to b
>= a b -- f f=1 if a is higher than or equal to b
<= a b -- f f=1 if a is lower than or equal to b
0< a -- f f=1 if a is negative
0>= a -- f f=1 if a is not negative
not a -- f f=1 if a is zero
bool a -- f f=1 if a is nonzero
min a b -- n n is the lowest number between a and b.
max a b -- n n is the highest number between a and b.
=><= n l h -- f f=1 if n >= l and n <= h.
?swap a b -- l h Sort a and b, putting the highest number on TOS.
= r1 r2 u -- f f=1 if memory ranges [r1, r1+u] and [r2, r2+u] have the
s= s1 s2 -- f f=1 if s1 and s2 have the same length and content.
noop -- Do nothing
bye -- Halt the machine.
quit -- Reset RS and return to the "main" loop.
abort -- Reset PS and quit.
execute a -- Call address a.
exit -- *I* Compile a return from call.
main -- The mainloop. Repeatedly call "word" and "runword".
leave -- *I* Set "next" counter so that we leave the loop at the
next "next" branch.
[if] f -- If f=0, skip all following words until a "[then]" word is
[then] -- No-op.
Structured flow words are better explained in their interaction together rather
than their individual effect and this is done in doc/usage. Below is a list of
all the flow words for reference. All these words are immediate.
if .. else .. then
begin .. again
begin .. until
n >r begin .. next
begin .. while .. repeat
begin .. while .. while .. repeat .. else .. then
n case .. of condition .. endof .. endcase
## Linked list
llnext ll -- ll Yield next LL element.
llend ll -- ll Iterate LL until we reach the last element.
llprev tgt ll -- ll From "ll", iterate LL until we reach the element when the
LL pointer points to "tgt".
lladd ll -- ll Write a new LL element to here, yield it, then write its
address to the last element of "ll".
llinsert 'll -- ll Given a *pointer* to a LL, write a new LL to here and
replace that first element in that pointer with the new
llcnt ll -- n Yield the number of elements in "ll".
find name 'dict -- word-or-0
Find name in dictionary pointer 'dict and yield the found
word or 0 if no match was found.
' "x" -- w Find x in system dictionary and error out if not found.
['] "x" -- *I* Find x and compile its address as a literal.
w>e w -- e Yield an entry (linked list pointer) from a word reference.
e>w e -- w Yield a word reference (executable) from an entry.
entry 'dict s --
Create entry with name s in dictionary 'dict.
code "x" -- Same as "entry", but reads name from input stream.
current -- w Yield the last word to be added to the system dictionary.
struct[ "x" -- Create new struct "x" and begin defining it.
]struct -- Exit current struct definition.
extends "x" -- Find struct "x" in system dictionary and make the next
defined struct extend it.
sfield "x" -- Add a new struct 4b field named "x".
sfieldw "x" -- Add a new struct 2b field named "x".
sfieldb "x" -- Add a new struct 1b field named "x".
sconst "x" -- Add a new struct read-only 4b field named "x".
sfield' sz "x" -- Add a new struct buffer of size sz named "x".
smethod "x" -- Add a new struct method named "x".
structbind 'data "x y" --
Create a new binding named "x" that binds 'data to struct
rebind 'data 'bind --
Bind 'data to structbind 'bind.
key -- c Read next character from system interactive input source.
in< -- c Read next character from system input source.
"< -- c Read from in< and apply literal escapes. c=-1 when " is
read (end of string).
emit c -- Emit character to system output destination.
nl> -- Emit CR then LF.
spc> -- Emit SPC.
rtype a u -- Emit characters in range [a, a+u].
stype s -- Emit all characters in s.
," x" -- Read from in< until " is reached and write it to here.
." x" -- *IC* Emit string x.
abort" x" -- *IC* Emit string x then abort.
maybeword -- s-or-0 Try to read word from system input source and yield it as
a string if it could be read. If EOF (c = -1) is reached,
word -- s Try to read a word and error out if EOF is reached.
\ -- Skip input stream until end of line (LF is the mark).
( -- Skip input stream until " ) " is read.
parse s -- n? f Try to parse string s as a number. f=1 and n exists if
parsing was successful.
compword s -- Compile s regardless of "compiling" flag. That is: try to
parse as a number. Write a literal if it's a number. Other-
wise, find word in system dict and check if it's an
immediate. If yes, execute, otherwise, write a call to its
runword s -- Execute string s according to our general logic: if
"compiling" flag is set, run "compword". Otherwise, try to
parse s as a number. If it is one, push in to PS. Otherwise
find in system dict and then execute.
Compiling words operate on a higher plane: they write native code to "here",
which can then be executed to have the desired effect.
[ -- *I* Stop compiling. The following words will be interpreted.
] -- Begin compiling. The following words will be compiled.
: "x" -- Create entry x and begin compiling.
; -- *I* Compile a return from call and then stop compiling.
litn n -- Compile a literal with value n.
execute, a -- Compile a call to address a.
exit, -- Compile a return from call.
compile "x" -- *I* Find word x and compile a compilation of a call to it.
[compile] "x" -- *I* Find immediate word x and instead of executing it
immediately as we would normally do, compile it as if it
compiling -- f f=1 if we're currently compiling.
immediate -- Make last added entry into an immediate entry.
create "x" -- Create a new entry named "x" of type "cell".
const n "x" -- Create a new constant named "x" of value n.
doer "x" -- Create a new "doer" word, to be paired with does>.
does> -- Begin compiling the runtime behavior of a doer word.
does' w -- a Yields the address of the "data" part of a doer word w.
value n "x" -- Create a new entry of type "value" with n as its initial
ivalue a "x" -- Create a new entry of type "indirect value" with a being the
address holding the pointer to the value.
alias "x y" -- Find word "x" in system dictionary and create entry "y" of
type "alias" pointing to it.
ialias a "x" -- Create a new entry of type "indirect alias" with a being the
address holding the pointer to the word to execute.
S" x" -- *IC* Yield string literal with contents "x".
chain "x y" -- Create a new "chain" targeting alias "x" and chaining "y" to
## "to" words
The way "to" words work is that they compile their associated word right
after a literal that yields the address of the word which obeys "to" semantics.
to --> !
to+ --> +!
to' --> noop
to@ --> @
to@! --> @!
to@+ --> @@+
to!+ --> @!+