Skip to content

tclForth from a traditional Forth point of view

Wolf Wejgaard edited this page Nov 22, 2020 · 1 revision

uh 2020-11-21

Redefinitions

In traditional Forth you can do redefinitions, i.e. if you define a word with an existing name, stil a new word is created and the old name is shadowed and no longer accessible in the dictionary. Other words that use the old word continue to do so.

In tclForth there is only one definition per name. If you define a word with an already existing name you substitute the old definition with the new one. Other words that use the old word now use the new one.

: old { -- } ." old" ;

: twice { -- }  old old ;

twice ( oldold )

: old { -- } ." new" ;

twice ( newnew )

You can make use of that tclForth behaviour to easily create and resolve forward references: In order to create a forward reference, you define an empty word. In order to resolve the forward reference you redefine the word with the same name and with the intended body:

: f {} ;  \ create forward reference

: g {} f f ; \ use not yet defined word f

: f {} ." *" ; \ resolve forward reference

g ( **)

Recursion

In traditional Forth the name of the currently defined word is hidden within the body of the definition so that an old word with the same name can be accessed.

In tclForth the name of the current definition is not hidden and can be directly used for recursion.

: fac { u -- } \ -- f
   u 0= if
     1
   else
     u  u 1- fac *
   then ;

Case Sensitity

tclForth is case sensitive with most words being defined in lowercase.

Arbitrary objects on the stack

In traditional Forth items on the data stack are cells of a fix size (e.g. 16, 32, 64 Bits) that represent different data values (characters, signed/unsigned integers, addresses, ...). In case of addresses the actual data is located somewhere in memory.

In tclForth any Tcl value can be on the data stack. As Tcl values can be integer of any size, floats, strings, lists, arrays all of these could be on the data stack.

10 20 30 { "ten" "twenty" "thirty" } 3.1415 
\ put three integers, a list with three srings, and a float on the tclForth stack

Traditional Forth represents strings as sequences of characters in memory either as counted strings or by managing the string length explicitly (c-addr len, c-addr the address of the first character and len the number of characters).

tclForth has a built in string type. String values are put on the data stack by enclosing them in quotation marks.


"SHOUT" tolower  ( shout)

Return Stack

Traditional Forth exposes a return stack to applications, that might be in shared use by the system to manage return addresses and loop parameters. Applications can access this return stack with words such as >R R@ R> but must follow strict rules when to access return stack items.

tclForth does not expose an explicit return stack and consequently none of the traditional return stack words are provided. Return addresses of word invocations are managed internally by Tcl.

Variables

The defining word VARIABLE to create a new variable has different syntax in traditional Forth. Some systems expect an initialization value on the stack, other don't and initialize the new variable with zero.

In tclForth an inital value is expected. variable is an Objecttype and creates words, that respone to the @ and ! methods to mimic the traditional Forth notation. Note that @ and ! do not address memory. Variables can also be used without @ and still put their value on the tclForth-Stack

Allocated dictionary

Traditional Forth manages main memory as an addressable area by means of the words DP HERE ALLOT , C, MOVE FILL. It names memory areas by CREATE.

tclForth does not use this kind of primitive memory but uses ObjectTypes in order to allocate data memory. ObjectTypes are inheritance free classes that define objects. These object react on messages by executing the corresponding method definiton of their ObjectType.

Predefined ObjectTypes are constants, variables, arrays, strings, lists, and files. Others can easily be defined using the ObjectType definer and implementing the methods in Tcl.

Formatted Number Output

Traditional Forth provides operators for formatted number output such as <# # #S HOLD #>. This small domain specific language can be used to create output words that do arbitrary formatting.

tclForth defines the single output word . that outputs arbitrary data items. Any additional specific formatted output has to be defined using Tcl.

Note that there is no notion of unsigned numbers in Tcl thus there is no unsigned number output such as U. U.R in traditionals Forth. Also tclForth supports arbitrary large numbers and does not suport double numbers that use two cells.

Single name space

Traditional Forth allow to structure the dictionary in named vocabularies (VOCABULARY) or unnamed word lists (WORDLIST) and manages the visibility of definitions by means of search order manipulation words (ONLY, ALSO, PREVIOUS or GET-ORDER, SET-ORDER). New definitions go to the current vocabulary or word list (DEFINITIONS, GET-CURRENT, SET-CURRENT)

tclForth has only a single name space for words.

Source Code specialities

Source code of traditional Forth is freely formatted. The input text is avaliable by the so called input stream.

tclForth expects source code to be formatted in a way that the defining words (such as : ObjectType variable are at the beginning of a line.

10 20 : hallo {} ;  \ : is undefined

As tclForth uses Tcl names for its words, the character # must not be used in word names as it would be considered the be ginning of a Tcl comment.

Clone this wiki locally