Download presentation
Presentation is loading. Please wait.
Published byAgatha Rich Modified over 6 years ago
1
The Forth Language CSC 507 – Roy Ford November 22, 2005
2
Components of Language
Stack Stores intermediate results as 32 bit integers Dictionary Stores all command words for the language Interpreter Interpretively executes commands found in the dictionary using the stack to store calculations
3
Language History Charles Moore Started to work on Forth around 1970
First official Forth was FIG (Forth Interest Group) Forth in late 70’s Forth 79 was the basis for the first commercial implementations Forth 83 tried to fix Forth 79, but caused inconsistency ANS Forth defined in 1994 to consolidate Forth 79 and 83, and allow for things like 32 bit integers My testing was with Win32Forth, an extension of ANS Forth with OOP, floating point, file I/O, etc
4
Simple Forth Example The order of operations in Forth is Postfix (or Reverse Polish) Notation ok . 5 4 + Stack Ptr Stack Ptr Stack Ptr Stack Ptr 5 4 9 5 Other operations include + - * / mod 1+ 1-
5
Stack Operations As this is a stack machine, there are a few operations that focus on manipulating the stack DUP – duplicates the top of the stack SWAP – swaps the top 2 elements DROP – drops the top element of stack ROT – Rotate the top 3 numbers
6
Store & Fetch Variables
The variable <name> word allocates storage for an integer in the dictionary and assigns it to <name> When you use the variable name, the address of the variable is placed on the stack ! stores stack values into a variable variable year 2005 year ! (stores 2005 into year @ fetches variables and places them on the stack . (displays 2005
7
Adding a word : <name> <body>;
: defines the start of the word and ; ends the word : square dup *; When you type 2 square . 4 ok 2 is placed on the stack, duped and multiplied
8
Conditional Operations
In Forth, true has the numeric value of -1 and false has the numeric value of 0 Conditional operators pops the top 2 values on the stack and push the true or false value onto the stack Conditional operations remove values from the stack Operators include not, <, >, = <= is written as > not
9
Conditional Example 5 4 < 4 < 5 4 5 5
5 4 < 4 < 5 Stack Ptr Stack Ptr Stack Ptr 4 5 5 In this example, the < word removes 5 & 4, from the stack, does the comparison 5 < 4, and as this is false pushes 0 on the stack
10
Control Structures If/Else/Then Do/Loop Begin/Until Begin/While/Repeat
11
If/Else/Then If tests the top of the stack.
If TOS not equal 0, code from If to Else is executed If TOS = 0, then code from Else to Then is executed
12
If/Then/Else Normally, a conditional word is used before the If for the test : absolute dup 0 < if -1 * then ; Duplicates the variable on the stack (to save it), tests if less than 0 and multiplies stack by -1 if it is -1 absolute . 1 ok 1 absolute . 1 ok -10 absolute ok 10 absolute ok
13
Do/Loop Do loops are one of 3 looping structures in Forth
2 parameters need to be pushed on the stack for the Do loop Limit – The maximum number of iterations Index – The starting index for the loop (usually 0) Do loop copies Limit and Index to a special return stack for iteration I is a special word that pushes the value of index from the return stack back to the main stack J is like I and is used for nested Do loops Leave breaks out of a do loop (like a break in C)
14
Do/Loop example <limit> <index> do <forth words> loop : testdo 10 0 do i . loop ; testdo ok Note how i increments from the index of 0 to 9 and stops when i = limit. Index can be set to any start value In Win32Forth, if index is bigger than limit, the do becomes infinite
15
Do/+Loop Like a Do loop, except at the end of the loop, the value on the stack is added to the index : test+loop 0 do i . 3 +loop ; ok 9 test+loop ok
16
Begin/Until & Begin/While/Repeat
begin <forth words> <condition> until Similar to a do while loop begin <condition> while <forth words> repeat Similar to a while loop
17
Arrays create and allot
Arrays are created by extending the parameter area of the dictionary for the variable we want to create extensions to the parameter area are done with the allot word Indexing the array requires explicit pointer arithmetic
18
Creating an Array variable myarray 12 allot create myarray 12 allot
The variable myarray is created in the dictionary A parameter field is created to store the integer variable an additional 12 bytes is added to the end of the parameter field to store the rest of the array An integer is stored as 4 bytes, so the size of this array is 16 bytes total, or 4 integers create myarray 12 allot Similar to variable myarray, but a parameter field is not defined by the create command, making an array size of 3 integers (12 bytes)
19
Accessing an array Like pointer arithmetic, you need to calculate the index into the array create myarray 12 alloc 100 myarray ! (100 in index 0 200 myarray 4 + ! 300 myarray 8 + !
20
Strings Strings are allocated just like arrays
Instead of ! to store and retrieve integers from the array, c! and are used as they only store and retrieve 1 byte
21
Readability Forth is not very readable
The base language is simple, but as the dictionary is expandable it can go rapidly out of hand (4000+ words in Win32Forth) The only parameter passing mechanism to forth words is the stack, as such it is difficult to decipher what parameters are being passed to a forth word without explicit documentation do loops and if/then/else structures do exist, but they are convoluted due to having to place execution parameters on the stack. Forth makes you write programs that manipulate the stack, it does not encourage the creation of variables that help to document
22
Writeability Forth is a writable language
Charles Moore’s purpose for writing Forth was to rapidly write code Once you learn it, RPN is a natural way to do calculations By building applications up from smaller words, the program can take advantage of abstraction
23
Reliability Major weak point is the pointer arithmetic for arrays, but some of this can be hidden There is no type checking, because the type is integer Exception handling has been written into Win32Forth It has been used in a number of process control applications, where reliability is a must
24
Questions
25
References “The Complete Forth”, A. Winfield, Sigma Technical Press, Cheshire, UK, 1983 “The Evolution of Forth, and Unusual Language”, C. Moore, Byte, Volume 5, Number 8, August 1980, McGraw-Hill Publication “What is Forth? A Tutorial Introduction”, J. James, Byte, Volume 5, Number 8, August 1980, McGraw-Hill Publication “FORTH Extensibility, Or How to Write a Compiler in 25 Words or Less”, K. Harris, Byte, Volume 5, Number 8, August 1980, McGraw-Hill Publication
26
Additional Material
27
Forth Dictionary The Forth Dictionary is a table that contains all of the defined words, variables and constants used in the execution environment The table contains The length and name of the word A Link address to the previous table entry A code pointer to the actual code that is executed A variable length parameter field
28
Dictionary Example Name Executable code Associated with Link word Code
Parameter Name Link Code Executable code Associated with word Parameter Name Executable code Associated with word Link Code Parameter
29
Dictionary Entry for + + Name Ptr to Previous Link Word Code
Machine code to take 2 numbers off stack, and them and place results on stack Parameter field is null
30
Dictionary Entry for a Variable
Name Name of Variable Link Ptr to Prev Entry Code Machine code that takes the address of the parameter field and places it on the stack Parameter 32 bit storage area for variable
31
Dictionary Entry for : ;
: square dup *; Machine code for the : command Name square Link Ptr to Prev Word Code dup Parameter 1 Parameter 2 * Parameter 3 ;
32
Create/Does> Create/Does creates defining words, words that are used to create other words This is very similar to a crude class, in that it defines how to create the word, and what to do when the word is executed : array create 4 * allot does> swap 4 * + ; ok 10 array p ok 4 p ? 0 ok 5 p ? 0 ok 100 4 p ! ok 4 p ? 100 ok in this example, 100, 4 and the address of p are pushed onto the stack, then the does> statements swap 4 * + are executed Note : This is a modification of “The Complete Forth”, A. Winfield, Sigma Technical Press, Cheshire, UK, 1983, page 87
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.