2020-03-07 12:13:15 -05:00
|
|
|
Stack notation: "<stack before> -- <stack after>". Rightmost is top of stack
|
2020-03-09 15:12:44 -04:00
|
|
|
(TOS). For example, in "a b -- c d", b is TOS before, d is TOS after. "R:" means
|
2020-03-12 21:16:20 -04:00
|
|
|
that the Return Stack is modified. "I:" prefix means "IMMEDIATE", that is, that
|
|
|
|
this stack transformation is made at compile time.
|
2020-03-07 12:13:15 -05:00
|
|
|
|
2020-03-07 22:18:14 -05:00
|
|
|
DOES>: Used inside a colon definition that itself uses CREATE, DOES> transforms
|
2020-03-09 15:12:44 -04:00
|
|
|
that newly created word into a "does cell", that is, a regular cell ( when
|
|
|
|
called, puts the cell's addr on PS), but right after that, it executes words
|
|
|
|
that appear after the DOES>.
|
2020-03-07 22:18:14 -05:00
|
|
|
|
2020-03-09 15:12:44 -04:00
|
|
|
"does cells" always allocate 4 bytes (2 for the cell, 2 for the DOES> link) and
|
|
|
|
there is no need for ALLOT in colon definition.
|
2020-03-07 22:18:14 -05:00
|
|
|
|
2020-03-09 15:12:44 -04:00
|
|
|
At compile time, colon definition stops processing words when reaching the
|
|
|
|
DOES>.
|
2020-03-07 22:18:14 -05:00
|
|
|
|
2020-03-09 15:12:44 -04:00
|
|
|
Example: ": CONSTANT CREATE HERE @ ! DOES> @ ;"
|
|
|
|
|
|
|
|
Word references (wordref): When we say we have a "word reference", it's a
|
|
|
|
pointer to a words *code link*. For example, the label "PLUS:" in this unit is a
|
|
|
|
word reference. Why not refer to the beginning of the word struct? Because we
|
|
|
|
actually seldom refer to the name and prev link, except during compilation, so
|
|
|
|
defining "word reference" this way makes the code easier to understand.
|
|
|
|
|
|
|
|
Atom: A word of the type compiledWord contains, in its PF, a list of what we
|
|
|
|
call "atoms". Those atoms are most of the time word references, but they can
|
|
|
|
also be references to NUMBER and LIT.
|
2020-03-07 22:18:14 -05:00
|
|
|
|
2020-03-12 21:16:20 -04:00
|
|
|
Words between "()" are "support words" that aren't really meant to be used
|
|
|
|
directly, but as part of another word.
|
|
|
|
|
|
|
|
"*I*" in description indicates an IMMEDIATE word.
|
|
|
|
|
2020-03-09 22:13:11 -04:00
|
|
|
*** Defining words ***
|
2020-03-20 13:35:02 -04:00
|
|
|
(find) a -- a f Read at a and find it in dict. If found, f=1 and
|
2020-03-17 16:02:01 -04:00
|
|
|
a = wordref. If not found, f=0 and a = string addr.
|
2020-03-09 08:49:51 -04:00
|
|
|
: x ... -- Define a new word
|
|
|
|
; R:I -- Exit a colon definition
|
2020-03-12 13:52:27 -04:00
|
|
|
, n -- Write n in HERE and advance it.
|
2020-03-17 16:02:01 -04:00
|
|
|
' x -- a Push addr of word x to a. If not found, aborts
|
2020-03-13 19:33:16 -04:00
|
|
|
['] x -- *I* Like "'", but spits the addr as a number
|
2020-03-17 15:54:17 -04:00
|
|
|
literal. If not found, aborts.
|
2020-03-13 19:33:16 -04:00
|
|
|
( -- *I* Comment. Ignore rest of line until ")" is read.
|
2020-03-09 22:13:11 -04:00
|
|
|
ALLOT n -- Move HERE by n bytes
|
2020-03-12 13:52:27 -04:00
|
|
|
C, b -- Write byte b in HERE and advance it.
|
2020-03-07 22:23:08 -05:00
|
|
|
CREATE x -- Create cell named x. Doesn't allocate a PF.
|
2020-03-21 16:17:51 -04:00
|
|
|
[COMPILE] x -- Compile word x and write it to HERE. IMMEDIATE
|
|
|
|
words are *not* executed.
|
2020-03-16 21:31:43 -04:00
|
|
|
COMPILE x -- Meta compiles. Kind of blows the mind. See below.
|
2020-03-09 22:13:11 -04:00
|
|
|
CONSTANT x n -- Creates cell x that when called pushes its value
|
2020-03-07 22:18:14 -05:00
|
|
|
DOES> -- See description at top of file
|
2020-03-17 17:29:03 -04:00
|
|
|
IMMED? a -- f Checks whether wordref at a is immediate.
|
2020-03-09 22:13:11 -04:00
|
|
|
IMMEDIATE -- Flag the latest defined word as immediate.
|
2020-03-18 20:04:44 -04:00
|
|
|
LITN n -- Write number n as a literal.
|
2020-03-09 22:13:11 -04:00
|
|
|
VARIABLE c -- Creates cell x with 2 bytes allocation.
|
|
|
|
|
2020-03-16 21:31:43 -04:00
|
|
|
Compilation vs meta-compilation. When you compile a word with "[COMPILE] foo",
|
|
|
|
its straightforward: It writes down to HERE wither the address of the word or
|
|
|
|
a number literal.
|
|
|
|
|
|
|
|
When you *meta* compile, it's a bit more mind blowing. It fetches the address
|
|
|
|
of the word specified by the caller, then writes that number as a literal,
|
|
|
|
followed by a reference to ",".
|
|
|
|
|
|
|
|
Example: ": foo [COMPILE] bar;" is the equivalent of ": foo bar ;" if bar is
|
|
|
|
not an immediate. However, ": foo COMPILE bar ;" is the equivalent of
|
|
|
|
": foo ['] bar , ;". Got it?
|
|
|
|
|
|
|
|
Meta-compile only works with real words, not number literals.
|
|
|
|
|
2020-03-09 22:13:11 -04:00
|
|
|
*** Flow ***
|
2020-03-15 22:46:17 -04:00
|
|
|
Note about flow words: flow words can only be used in definitions. In the
|
|
|
|
INTERPRET loop, they don't have the desired effect because each word from the
|
|
|
|
input stream is executed immediately. In this context, branching doesn't work.
|
|
|
|
|
2020-03-12 21:16:20 -04:00
|
|
|
(fbr) -- Branches forward by the number specified in its
|
|
|
|
atom's cell.
|
2020-03-15 22:46:17 -04:00
|
|
|
(bbr) -- Branches backward by the number specified in its
|
|
|
|
atom's cell.
|
2020-03-17 15:30:57 -04:00
|
|
|
ABORT -- Resets PS and RS and returns to interpreter
|
|
|
|
ABORT" x" -- *I* Compiles a ." followed by a ABORT.
|
2020-03-15 22:46:17 -04:00
|
|
|
AGAIN I:a -- *I* Jump backwards to preceeding BEGIN.
|
|
|
|
BEGIN -- I:a *I* Marker for backward branching with AGAIN.
|
2020-03-12 21:16:20 -04:00
|
|
|
ELSE I:a -- *I* Compiles a (fbr) and set branching cell at a.
|
2020-03-09 15:12:44 -04:00
|
|
|
EXECUTE a -- Execute wordref at addr a
|
2020-03-12 21:16:20 -04:00
|
|
|
IF -- I:a *I* Compiles a (fbr?) and pushes its cell's addr
|
2020-03-07 12:13:15 -05:00
|
|
|
INTERPRET -- Get a line from stdin, compile it in tmp memory,
|
|
|
|
then execute the compiled contents.
|
2020-03-15 22:46:17 -04:00
|
|
|
QUIT R:drop -- Return to interpreter prompt immediately
|
2020-03-10 16:02:40 -04:00
|
|
|
RECURSE R:I -- R:I-2 Run the current word again.
|
2020-03-14 09:23:58 -04:00
|
|
|
SKIP? f -- If f is true, skip the execution of the next atom.
|
|
|
|
Use this right before ";" and you're gonna have a
|
|
|
|
bad time.
|
2020-03-12 21:16:20 -04:00
|
|
|
THEN I:a -- *I* Set branching cell at a.
|
2020-03-18 16:39:22 -04:00
|
|
|
UNTIL f -- *I* Jump backwards to BEGIN if f is *false*.
|
2020-03-07 21:12:30 -05:00
|
|
|
|
2020-03-13 16:40:55 -04:00
|
|
|
*** Parameter Stack ***
|
2020-03-14 17:48:24 -04:00
|
|
|
DROP a --
|
2020-03-09 22:13:11 -04:00
|
|
|
DUP a -- a a
|
|
|
|
OVER a b -- a b a
|
|
|
|
SWAP a b -- b a
|
2020-03-11 21:58:16 -04:00
|
|
|
2DUP a b -- a b a b
|
|
|
|
2OVER a b c d -- a b c d a b
|
|
|
|
2SWAP a b c d -- c d a b
|
2020-03-07 21:12:30 -05:00
|
|
|
|
2020-03-13 16:40:55 -04:00
|
|
|
*** Return Stack ***
|
|
|
|
>R n -- R:n Pops PS and push to RS
|
|
|
|
R> R:n -- n Pops RS and push to PS
|
|
|
|
I -- n Copy RS TOS to PS
|
|
|
|
I' -- n Copy RS second item to PS
|
|
|
|
J -- n Copy RS third item to PS
|
|
|
|
|
2020-03-09 22:13:11 -04:00
|
|
|
*** Memory ***
|
|
|
|
@ a -- n Set n to value at address a
|
|
|
|
! n a -- Store n in address a
|
2020-03-07 21:12:30 -05:00
|
|
|
? a -- Print value of addr a
|
|
|
|
+! n a -- Increase value of addr a by n
|
2020-03-11 22:11:54 -04:00
|
|
|
C@ a -- c Set c to byte at address a
|
|
|
|
C! c a -- Store byte c in address a
|
2020-03-13 16:40:55 -04:00
|
|
|
CURRENT -- a Set a to wordref of last added entry.
|
2020-03-09 22:13:11 -04:00
|
|
|
HERE -- a Push HERE's address
|
2020-03-12 21:49:10 -04:00
|
|
|
H -- a HERE @
|
2020-03-09 22:13:11 -04:00
|
|
|
|
2020-03-21 14:47:38 -04:00
|
|
|
*** Arithmetic / Bits ***
|
2020-03-09 22:13:11 -04:00
|
|
|
|
|
|
|
+ a b -- c a + b -> c
|
|
|
|
- a b -- c a - b -> c
|
2020-03-12 21:49:10 -04:00
|
|
|
-^ a b -- c b - a -> c
|
2020-03-09 22:13:11 -04:00
|
|
|
* a b -- c a * b -> c
|
|
|
|
/ a b -- c a / b -> c
|
2020-03-16 22:36:29 -04:00
|
|
|
MOD a b -- c a % b -> c
|
|
|
|
/MOD a b -- r q r:remainder q:quotient
|
2020-03-21 14:47:38 -04:00
|
|
|
AND a b -- c a & b -> c
|
|
|
|
OR a b -- c a | b -> c
|
|
|
|
XOR a b -- c a ^ b -> c
|
2020-03-09 22:13:11 -04:00
|
|
|
|
2020-03-10 16:02:40 -04:00
|
|
|
*** Logic ***
|
|
|
|
= n1 n2 -- f Push true if n1 == n2
|
|
|
|
< n1 n2 -- f Push true if n1 < n2
|
|
|
|
> n1 n2 -- f Push true if n1 > n2
|
|
|
|
CMP n1 n2 -- n Compare n1 and n2 and set n to -1, 0, or 1.
|
|
|
|
n=0: a1=a2. n=1: a1>a2. n=-1: a1<a2.
|
|
|
|
NOT f -- f Push the logical opposite of f
|
|
|
|
|
|
|
|
*** Strings ***
|
2020-03-21 16:27:21 -04:00
|
|
|
LIT< x -- Read following word and write to HERE as a string
|
|
|
|
literal.
|
2020-03-21 16:31:53 -04:00
|
|
|
LITS a -- Write word at addr a as a atring literal.
|
2020-03-13 16:40:55 -04:00
|
|
|
SCMP a1 a2 -- n Compare strings a1 and a2. See CMP
|
2020-03-18 21:52:55 -04:00
|
|
|
SLEN a -- n Push length of str at a.
|
2020-03-10 16:02:40 -04:00
|
|
|
|
2020-03-09 22:13:11 -04:00
|
|
|
*** I/O ***
|
2020-03-14 19:10:39 -04:00
|
|
|
|
|
|
|
A little word about inputs. There are two kind of inputs: direct and buffered.
|
|
|
|
As a general rule, we read line in a buffer, then feed words in it to the
|
|
|
|
interpreter. That's what "WORD" does. If it's at the End Of Line, it blocks and
|
|
|
|
wait until another line is entered.
|
|
|
|
|
|
|
|
KEY input, however, is direct. Regardless of the input buffer's state, KEY will
|
|
|
|
return the next typed key.
|
|
|
|
|
2020-03-17 17:46:58 -04:00
|
|
|
PARSING AND BOOTSTRAP: Parsing number literal is a very "core" activity of
|
|
|
|
Forth, and therefore generally seen as having to be implemented in native code.
|
|
|
|
However, Collapse OS' Forth supports many kinds of literals: decimal, hex, char,
|
|
|
|
binary. This incurs a significant complexity penalty.
|
|
|
|
|
|
|
|
What if we could implement those parsing routines in Forth? "But it's a core
|
|
|
|
routine!" you say. Yes, but here's the deal: at its native core, only decimal
|
|
|
|
parsing is supported. It lives in the "(parsed)" word. The interpreter's main
|
|
|
|
loop is initially set to simply call that word.
|
|
|
|
|
|
|
|
However, in core.fs, "(parsex)", "(parsec)" and "(parseb)" are implemented, in
|
|
|
|
Forth, then "(parse)", which goes through them all is defined. Then, "(parsef)",
|
|
|
|
which is the variable in which the interpreter's word pointer is set, is
|
|
|
|
updated to that new "(parse)" word.
|
|
|
|
|
|
|
|
This way, we have a full-featured (and extensible) parsing with a tiny native
|
|
|
|
core.
|
|
|
|
|
|
|
|
(parse) a -- n Parses string at a as a number and push the result
|
|
|
|
in n as well as whether parsing was a success in f
|
|
|
|
(false = failure, true = success)
|
|
|
|
(parse.) a -- n f Sub-parsing words. They all have the same signature.
|
|
|
|
Parses string at a as a number and push the result
|
|
|
|
in n as well as whether parsing was a success in f
|
|
|
|
(0 = failure, 1 = success)
|
2020-03-17 21:19:56 -04:00
|
|
|
(parse*) -- a Variable holding the current pointer for system
|
|
|
|
number parsing. By default, (parse).
|
2020-03-17 15:22:13 -04:00
|
|
|
(print) a -- Print string at addr a.
|
2020-03-09 22:13:11 -04:00
|
|
|
. n -- Print n in its decimal form
|
2020-03-17 14:05:53 -04:00
|
|
|
.X n -- Print n in its hexadecimal form. In hex, numbers
|
2020-03-17 15:22:13 -04:00
|
|
|
." xxx" -- *I* Compiles string literal xxx followed by a call
|
|
|
|
to (print)
|
2020-03-17 14:05:53 -04:00
|
|
|
are never considered negative. "-1 .X -> ffff"
|
2020-03-20 13:35:02 -04:00
|
|
|
C< -- c Read one char from buffered input.
|
2020-03-14 19:10:39 -04:00
|
|
|
EMIT c -- Spit char c to output stream
|
|
|
|
IN> -- a Address of variable containing current pos in input
|
|
|
|
buffer.
|
|
|
|
KEY -- c Get char c from direct input
|
2020-03-10 13:00:57 -04:00
|
|
|
PC! c a -- Spit c to port a
|
|
|
|
PC@ a -- c Fetch c from port a
|
2020-03-14 19:10:39 -04:00
|
|
|
WORD -- a Read one word from buffered input and push its addr
|
2020-03-09 22:13:11 -04:00
|
|
|
|
2020-03-19 15:43:48 -04:00
|
|
|
There are also ascii const emitters:
|
2020-03-20 13:35:02 -04:00
|
|
|
BS
|
|
|
|
CR
|
2020-03-19 15:43:48 -04:00
|
|
|
LF
|
|
|
|
SPC
|
|
|
|
|