Skip to content
wolfwejgaard edited this page Jan 5, 2015 · 47 revisions

###Why Tcl#

Tcl is a great software development environment, used and proven in many professional applications. See http://wiki.tcl.tk/299. Due to its long use and continuing care by a dedicated development team Tcl is a truly mature system. And now, with a little overlay, the Tcl universe is available in Forth.

###Tcl & Forth#

Tcl commands and Forth words are closely related. Both are called by their interpreters to compile or execute. Both in effect create the programming language, and both languages can be changed and extended by you, the programmer. The deciding difference is the notation. Tcl uses the formal prefix notation for command arguments and an infix interpreter for math expressions. If we add a way to pass arguments on a stack, Tcl would be postfix and a postfix Tcl is a Forth system, TclForth.

You can study it with a few commands in the Forth console and the resulting Tcl code in the Codewindow. (Open the Codewindow in the Setupmenu).

This is the definition of a Tcl command 'write':

tcl proc write {text} {printnl $text}

The word 'tcl' passes the following text to the Tcl interpreter.

tcl write "Hello World!"
Hello World!
ok

Now here is the same function written as a code word in Forth.

Code Write { text -- }  printnl $text

This is compiled to

proc Write {} {
set text [pop] ; printnl $text
}

and again passed to the Tcl interpreter.

Now test it the Forth way:

"Hello World!" Write
Hello World!
ok

####Discussion###

The only change needed to convert a Tcl command into a Forth word was the addition of "set text [pop] ;" in front of the code, which turns the stack argument 'text' into a local variable with this name.

And we need a stack for the parameter, of course.

####Forth Words###

TclForth provides the standard set of Forth words as far as they make sense. Lookup the words in the files forth.fth and tk.fth. And see how the words are used in console.fth.

####Knowing Tcl#

To really use TclForth you will want some [knowledge of Tcl](Tcl literature)

Clone this wiki locally