the forth language n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
The Forth Language PowerPoint Presentation
Download Presentation
The Forth Language

Loading in 2 Seconds...

play fullscreen
1 / 32

The Forth Language - PowerPoint PPT Presentation


  • 233 Views
  • Uploaded on

The Forth Language. CSC 507 – Roy Ford November 22, 2005. Components of Language. Stack Stores intermediate results as 32 bit integers Dictionary Stores all command words for the language Interpreter

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'The Forth Language' - holly


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the forth language

The Forth Language

CSC 507 – Roy Ford

November 22, 2005

components of language
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
language history
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
simple forth example

Stack Ptr

Stack Ptr

Stack Ptr

Stack Ptr

Simple Forth Example

The order of operations in Forth is Postfix (or Reverse Polish) Notation

5 4 + . 9 ok

.

5

4

+

5

4

9

5

Other operations include + - * / mod 1+ 1-

stack operations
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
store fetch variables
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

year @ . (displays 2005

adding a word
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

conditional operations
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
conditional example

Stack Ptr

Stack Ptr

Stack Ptr

Conditional Example

5 4 <

4

<

5

4

0

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

control structures
Control Structures
  • If/Else/Then
  • Do/Loop
  • Begin/Until
  • Begin/While/Repeat
if else then
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
if then else
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 . 10 ok

10 absolute . 10 ok

do loop
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)
do loop example
Do/Loop example

<limit> <index> do <forth words> loop

: testdo 10 0 do i . loop ;

testdo 0 1 2 3 4 5 6 7 8 9 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

do loop1
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 0 3 6 ok

begin until begin while repeat
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
arrays
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
creating an array
Creating an Array
  • variable 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)
accessing an array
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 + !

strings
Strings
  • Strings are allocated just like arrays
  • Instead of ! and @ to store and retrieve integers from the array, c! and c@ are used as they only store and retrieve 1 byte
readability
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
writeability
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
reliability
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
references
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
  • http://www.forth.org/
  • http://www.taygeta.com/forth/dpansd.htm#D.1
  • http://win32forth.sourceforge.net/
forth dictionary
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
dictionary example
Dictionary Example

Name

Executable code

Associated with

word

Link

Code

Parameter

Name

Link

Code

Executable code

Associated with

word

Parameter

Name

Executable code

Associated with

word

Link

Code

Parameter

dictionary entry for
Dictionary Entry for +

+

Name

Ptr to Previous

Word

Link

Machine code to

take 2 numbers off

stack, and them and

place results on stack

Code

Parameter field is null

dictionary entry for a variable
Dictionary Entry for a Variable

Name of Variable

Name

Ptr to Prev Entry

Link

Code

Machine code that

takes the address

of the parameter

field and places

it on the stack

32 bit storage

area for variable

Parameter

dictionary entry for1
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

;

create does
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