Presentation is loading. Please wait.

Presentation is loading. Please wait.

Forth A stack language.

Similar presentations


Presentation on theme: "Forth A stack language."— Presentation transcript:

1 Forth A stack language

2 Forth syntax A word consists of a sequence of printable (non-whitespace) characters A program consists of a sequence of words, separated by whitespace Any questions?

3 Basic execution Forth uses a dictionary to hold function definitions, and a stack to hold data Words are evaluated left to right, top to bottom, in the usual order Forth is case insensitive Most documentation uses words in all caps, but lowercase is also ok If a word is in the dictionary, then the function is looked up and executed else Forth converts the word to a number, if it can, and places it on the stack

4 Manipulating the stack
s <-Top ok swap .s <-Top ok dup .s <-Top ok There are about 20 operations for moving, duplicating, or removing things on the stack

5 .(a single period) takes one thing off the stack and prints it
Seeing the stack .(a single period) takes one thing off the stack and prints it .s prints the entire stack, without changing it s <-Top ok ok .s 1 20 <-Top ok s <-Top ok

6 Arithmetic and logic This version of Forth has integers only, no floats ok ok 2 3 4 * 5 - dup * ok “False” is 0, “true” is -1 2 3 < ok 2 3 = . 0 ok 1 2 < and 3 4 < ok 20 10 > 5 > . 0 ok

7 Constants, variables, and strings
1000 constant oneThousand ok oneThousand ok Use ! to assign to a to get its value variable x ok 42 x ! ok x ok (not what we wanted) ok ." this is a string" type this is a string ok

8 Functions Functions take their arguments from the stack and leave their results on the stack : even ( n -- boolean ) dup 2 / 2 * = ; ok 7 even . 0 ok 8 even ok : odd even not ; ok 7 odd ok 8 odd . 0 ok

9 Conditionals and loops
: collatz1 dup even if 2 / else 3 * 1 + then ; ok 7 collatz ok 22 collatz ok : collatz ( n -- 1 ) begin dup . collatz1 dup 1 = until ; ok 7 collatz ok

10 Other stuff There’s a lot more to Forth than I’ve shown
Other kinds of loops “Switch” statements I/O, both file and console

11 99 Bottles of Beer on the Wall
\ Forth version of the 99 Bottles program. \ Dan Reish, : .bottles ( n -- n-1 ) dup 1 = IF ." One bottle of beer on the wall," CR " One bottle of beer," CR " Take it down," ELSE dup . ." bottles of beer on the wall," CR dup . ." bottles of beer," CR ." Take one down," THEN CR ." Pass it around," CR ?dup IF dup 1 = IF ." One bottle of beer on the wall;" ELSE dup . ." bottles of beer on the wall;" THEN ELSE ." No more bottles of beer on the wall." THEN CR ; : nbottles ( n -- ) BEGIN .bottles ?dup NOT UNTIL ; 99 nbottles

12 Why Forth? Very small, very simple language
Not like modern languages! You could write a complete interpreter and the most important library routines in a weekend Ideal when you have to fit a large program into a small amount like memory It’s actually kind of fun, in a puzzle-solving kind of way

13 The End The first time I combined the ideas I had been developing into a single entity, I was working on an IBM 1130, a “third-generation” computer. The result seemed so powerful that I considered it a “fourth generation computer language.” I would have called it Fourth, except that the 1130 permitted only five-character identifiers. So Fourth became Forth, a nicer play on words anyway Charles “Chuck” Moore


Download ppt "Forth A stack language."

Similar presentations


Ads by Google