Skip to content

Word Types

Wolf Wejgaard edited this page Feb 22, 2018 · 8 revisions

The TclForth compiler handles the following defining words.

proc

The proc is the "assembler word" of TclForth. It is defined in Tcl commands.

proc push {p} {
    lappend ::stack $p
}

Code

Code words interface Forth with Tcl, handle I/O, events, GUI, etc. They are defined as Tcl procs without formal arguments. TclForth replaces the formal argument by a stack diagram.

The compiler uses the diagram to add the appropriate stack handling. The stack arguments are converted to local variables in the proc.

The definition is ended by an empty line. (You can also add an "end-code", it is optional.)

Example: The Forth word

Code int { n1 -- n2 }
    set n2 [expr int($n1)]

is compiled to

proc int {} {
    set n1 [pop];  set n2 [expr int($n1)]; push $n2 ;
}

Colon (:)

Colon words are defined by other Forth code and colon words. They are converted to procs that call "Forth procs". The execution engine of Tcl is the inner interpreter of Forth.

: <name> { -- } <Forth source> ;

Again, arguments are accepted and results are returned on the parameter stack.

{ -- } is stack notation.

The Forth ; is optional. The definition is ended by an empty line.

Example:

: Prompt { -- }
     depth 0> withStack and
     if   "$::stack ok>"
     else "ok>"
     then Console append update
;

is compiled to

proc Prompt {} {
     depth;  0>; push $::withStack; and;
     if [pop]  {
          push "$::stack ok>";
     } else {
          push "ok>";
     }
     $::Console insert end [pop]; update;
}

Compiler

As in Forth, flow control in Tcl is handled by the flow words themselves: IF THEN etc are words that are executed when the source is compiled. They are known as immediate words in Forth, TclForth calls them compilers. A compiler word executes when the source is loaded.

Compiler <name> <host action>

The compiler words define action in the host and are written in Tcl code.

Objecttype

TclForth handles data as objecttypes.

Objecttype <name>  <array of messages and methods>

The Objecttypes replace CREATE ... DOES ...

tcl

tcl set x 0

The text after tcl is passed unchanged to the Tcl interpreter.


Note: The defining words are case tolerant. You can also write code, compiler, objecttype, Tcl. - However, the defined word names are case sensitive.

Clone this wiki locally