wfroth tutorial - Chap I

The Base of the language - Generalities


Base concepts


The first concept we will introduce is the concept of word. In wfroth, a word is represented by characters, any ascii character (7 bits code) different from space, even a digit number. Because, word can't contain space character, this character will be used to separate each word from others. A word correspond to a given action. Numbers are also words. Strings (enclosed by ") are a special case, and are not considered as several words, but as a whole entity.

Dictionnary, Vocabulary

All the defined words belong to the dictionnary. Because there is a lot of words (thousand), words are grouped by family into vocabularies. It's very similar to files, directories and sub-directories under an operating-system such NT or UNIX.

The stack

Because each defined word means a specific action, we can describe a state before calling the word, and a state after. A lot of words perform an action on a specific area: the parameters stack, let's name it simply the stack. Under wfroth, the stack is a data structure:

Think to a stack of plates on a table. You can only:

Stack operation is easy: you just push or pop objects. You don't have to say where, or to specify an item. In wfroth, the stack is said LIFO: Last In, First Out.

Many wfroth words just perform actions on the stack. It's a very easy way to give parameters to a word, and to have results in return. So, for each word, it will be given a stack protocol: stack state before, stack state after. Let's see the stack protocol for the word + : ( n1 n2 --- n3 )

The '---' means the action of the given word. On the left, this is the state before: two parameters (numbers) are popped from the stack (<n2> is the top value, <n1> the value under the top). On the right, the stack state is shown after word operation: a value is pushed onto stack. Of course, we have n3 = n1 + n2. + is a word available when we just start wfroth. Remember: we use here integer arithmetic on stack.

Yes, stack base objects are only numbers (32 bits numbers). But, a number can be interpreted with many ways:

Actually, parameters interpretation depends on each word. You must know the stack protocol to understand how input parameters are interpreted.

The stack is usually hidden in many high level languages (C, Pascal, ...), not in wfroth. wfroth programmers have to push and pop data to/from the stack.

Action of programming

In wfroth, action of programming consists into creating and adding new words. You just add news bricks. New words are defined with previous words (and only). At the top, we will found a word which is in fact the entire application. At the bottom, we will found words which perform a lot of basic and elementary actions. There is no real frontier between the words you'll find at startup (the library...), and the words that you'll define. We could say that programming in wfroth is to extand the language.

Because each word is a piece of program, we use often the more precise term of definition to design a word. We will see later that code and data are in the same manner also definitions.

In wfroth, you can stay under the integrated developement environment to execute your definitions you have defined. You could test individually each definition of a big program. This is really an interactive language. And do not think a wfroth program as a monolithic thing, but rather as a set of cooperative small definitions.

The extern interpret

The wfroth extern interpret let's you talk with the system. It allows you:

The primary job of the wfroth extern interpret is to parse each word met. There is no special syntax, just some rules: to design characters or hexadecimal numbers, to constant character, etc. The most important thing is to let a space between each word, and a <CR> at the end of the line. Input stream can come:

The wfroth extern interpret knows only one state: compiling state. But some words have a flag set which tells to the extern interpret to execute them instead of compiling them. The most important word to know is ; (semicolon), which means: end the current definition.

The system is always compiling. Two kind of definitions can be defined:

The word ; (semicolon) which ends the current definition is used to execute an anonymous definition. If the word compiled is a number (or a string), then the value (or the address) is pushed onto the stack.

We will find some words which allow to define others, to set the attribute 'immediate', etc.


The major characteristic of the Forth language is: