Some of the main differences between 4tH and Forth:
1. The 4tH compiler is essentially the text interpreter. The 4tH VM is essentially the address interpreter;
2. You cannot switch between the 4tH text interpreter and the 4tH address interpreter;
3. Consequently, 4tH does not have a STATE variable;
4. All 4tH's immediate words cannot be overridden or changed - nor can you add new ones from within the bare 4tH system (the preprocessor can do some, though);
5. All dictionary related words have to be resolved during 4tH's compile time - since after compilation, the dictionary is gone;
6. There is no user controlled parsing during 4tH's compile time - since after compilation, the source is gone.
That means INTERPRET looks like this (compilation, execution, not at all)
: interpret ( -- )
BEGIN ( )
bl word dup c@ \ scan next token
WHILE ( c-addr ) \ another token found
find dup 1 = IF \ lookup in dictionary
drop execute \ immediate
ELSE
dup -1 = IF \ everything else
drop state @
IF compile, ELSE execute THEN
ELSE \ word not found, number?
drop 0 0 rot count over c@ [char] - = dup >r
IF 1- swap char+ swap THEN >number
IF #notfound throw THEN drop drop r>
IF negate THEN state @
IF postpone LITERAL THEN \ compile literal
THEN
THEN
REPEAT drop ;
The address interpreter in exec_4th.4th is quite simple:
for (;(Object->ErrLine < Object->CodeSiz); Object->ErrLine++)
switch (Object->CodeSeg [Object->ErrLine].Word) {
..
}
Yes, the IP is part of the 4tH applet - which makes it easy to trap the location of any errors, since it is saved in the applet itself. It's value is set upon creation (comp_4th.c, load_4th.c). Note the IP always increases by "one" - no matter which opcode has been executed. Even after BRANCH or CALL. Those instructions always seem to point to a location BEFORE the actual jump-location - and now you know why. It just made things easier. The GCC version of exec_4th.c is a bit more complex, but it works on the same principle.
Note a lot (but not all) of these limitations can be overcome by using the preprocessor - like IMMEDIATE words with POSTPONEs, special datatypes, renaming 4tH's internal words, adding or switching parameters (SYNONYM, BEGIN-STRUCTURE).
Now, it will never match the full capabilities of Forth, you can come quite close.. And in some cases even exceed it.
Hans Bezemer