µEforth Internals

µEforth (micro-Eforth) simplifies EForth even futher, by building just enough of the core of the system in C to allow the rest to be be built in proper Forth.

A handful of "tricky" words that involve internal loops or many steps are built in their own functions:

FIND ( a n -- xt | 0 )
PARSE ( ch -- a n )
S>NUMBER? ( a n -- n f | 0 )
CREATE ( "name" -- )
EVALUATE1 ( -- )

This includes EVALUATE1 which parses a single word and interprets or compiles it (reusing PARSE, FIND, and S>NUMBER?).

See core.h.

A few global variables connect parsing and compilation state between C and Forth (by way of a memory region accessed via 'SYS):

'TIB      --- Pointer to the Translation Input Buffer
#TIB      --- Length of the Translation Input Buffer
>IN       --- Number of characters consumed from TIB

BASE      --- Numeric base for printing and parsing

STATE     --- State of compiling, -1 for compiling, 0 for interpreting
CURRENT   --- Pointer to pointer to last word of current vocabulary
CONTEXT   --- Pointer to pointer to last word of context vocabulary

'NOTFOUND --- Execution token of a handler to call on word not found

Error handling is routed via a deferred callback in 'NOTFOUND used when a word is absent from the dictionary. This is eventually directed to an error routing that prints a proper error, once I/O and exceptions are available.

X-Macros are then used to build up a small set of core opcodes defined in 1-3 lines each:

0= 0< + U/MOD */MOD   AND OR XOR
LSHIFT RSHIFT   DUP SWAP OVER DROP
@ SL@ SW@ C@ ! L! W! C!   SP@ SP! RP@ RP!
>R R> R@   : ; EXIT
EXECUTE BRANCH 0BRANCH DONEXT DOLIT
ALITERAL CELL DOES> IMMEDIATE 'SYS

NOTE: Later to reduce the use of the RAM heap and improve performance, additional non-essential extra opcodes were added in place of high-level words.

See tier0_opcodes.h.

I/O and access to systems outside Forth are connected via a few per platform words. Typically this set of words should be minimal, while still allowing relevant parts of the host system to be available to Forth.