wejgaard / TclForth

Multi-platform desktop Forth based on Tcl/Tk
Other
64 stars 9 forks source link

Always compiling. Forth without STATE. #2

Open wejgaard opened 9 years ago

wejgaard commented 9 years ago

(Discussed in comp.lang.forth)

Classic (standard) Forth systems are remarkably consistent in using techniques invented 40 years ago. For instance, the classic interpreter still executes command lines word by word. I wonder why. Of course, it works and everyone is used to it. But it does not have to be that way.

TclForth compiles the command line and then executes the compiled line. If the command line is compiled it can handle immediate words. You can enter lines like this

1 begin dup 10 < if "*" else "&" then . 1+ dup 20 > until drop 

TclForth always compiles. Interpret is compile and execute.

If you need to calculate a value while compiling a definition, enclose the calculation in square brackets as usual. The code inside the brackets is compiled, then executed by ']' .

There are just two moments in the interpreter when code is executed: end-of-command-line and ']'. Thus, STATE is obsolete, it is not tested. And if there is no STATE to test, the immediate bit also makes little sense. TclForth replaces immediate by Compiler.

Immediate words are now Compiler words that execute native Tcl code. Example:

Compiler if 
    appendcode "if \[pop\]  \{\n" 

Code and Colon words compile a call to their execution semantics. Compiler words perform their compilation semantics.