This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
tutorials:forth [2022/01/06 12:03] papa |
tutorials:forth [2022/06/07 02:10] rcs correct lack of space after dot commands in examples |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | **FORTH-10** | + | [[http:// |
- | @FORTH | + | There are two Forth implementation available. |
- | MIDAS assembly language source is available in file '' | + | **FORTH-10** is a Forth language interpreter included in the Panda TOPS-20 distribution. The anonymous programmer describes the implementation as "NOT totally standard" |
- | ====== Primitive Dictionary ====== | + | **FORTH** is another interpreter originally written on the MIT AI lab DEC-20 called OZ, by John Wilson. |
- | The following Forth words are hard-coded in the FORTH-10 | + | ====== |
- | |[[# | + | Start the interpreter on TWENEX with the command |
- | |[[# | + | |
- | |[[# | + | |
- | |[[#'|']]|[[# | + | |
- | |[[#'#|'# | + | |
- | |[[# | + | |
- | |[[# | + | |
- | |[[# | + | |
- | |[[# | + | |
- | |[[# | + | |
- | |[[# | + | |
- | |[[# | + | |
- | |[[#,|, | + | |
- | |[[#,, | + | |
- | |[[#< | + | |
- | ===== Arithmetic ===== | + | @FORTH-10 |
+ | FORTH-10 | ||
+ | Ok | ||
+ | |||
+ | Exit FORTH-10 with the command '' | ||
- | == plus == | + | At the '' |
- | + ( n1 n2 -- n1+n2 ) | + | |
- | == one-plus == | + | 7 3 Ok ( Push 7 then 3 on stack. ) |
- | 1+ ( n -- n+1 ) | + | + Ok |
+ | . 10 Ok ( Word . pops top stack item and prints it. Stack is now empty. ) | ||
+ | |||
+ | Words '' | ||
- | == minus == | + | 3.14159 Ok |
- | - ( n1 n2 -- n1-n2 ) | + | f. 3.14159 Ok |
+ | |||
+ | Add new words to the dictionary with the defining words '':'' | ||
- | == one-minus == | + | '':'' |
- | 1- ( n -- n-1 ) | + | |
- | == star == | + | For example, |
- | * ( n1 n2 -- n1*n2 ) | + | |
- | == negate == | + | : TIMES5 5 * ; Ok ( Define word TIMES5: Push 5 on stack (previous top value pushed lower), pop 5 and ) |
- | MINUS ( n -- -n ) | + | |
+ | 7 TIMES5 Ok ( Execute the word. ) | ||
+ | . 35 Ok ( Pop and display result. ) | ||
+ | |||
+ | List words currently defined in the dictionary with '' | ||
- | == abs == | + | In general, Forth programs keep intermediate results on the stack instead of using variables like other languages. Words are provided to manipulate values on the stack so that they can be used in multiple procedures in the correct order. |
- | ABS ( n -- |n| ) | + | |
- | == max == | + | |DROP|Pop stack without printing |
- | MAX ( n1 n2 -- n ) //n// is the greater of //n1// or //n2// | + | |DUP|Copy top value and push it to stack.| |
+ | |SWAP|Exchange | ||
+ | |ROT|Rotate top three values on stack: [1 2 3] -> [2 3 1]| | ||
+ | |OVER|Copy second item on stack and push it to top: [1 2] -> [1 2 1]| | ||
- | == min == | + | Forth source files can be prepared with any editor. Load and interpret source files in FORTH-10 with the command '' |
- | MIN ( n1 n2 -- n ) //n// is the lesser of //n1// or //n2// | + | |
- | == slash == | + | ====== Hello, World! ====== |
- | / ( n1 n2 -- n1/n2 ) integer division | + | |
- | == mod == | + | : HELLO CR ." " |
- | MOD ( n1 n2 -- n ) //n// is the modulus of //n1/n2// | + | |
+ | HELLO, WORLD! | ||
+ | Ok | ||
- | == slash-mod == | + | Insert comments in Forth source code with the word '' |
- | /MOD ( n1 n2 -- n3 n4 ) //n3// is the modulus of //n1/n2//, //n4// is the integer quotient (//n1// = // | + | |
- | == f-plus | + | ====== Primitive Dictionary ====== |
- | F+ ( r1 r2 -- r1+r2 ) floating point addition | + | |
- | == f-minus == | + | The following Forth words are hard-coded in the FORTH-10 dictionary. |
- | F- ( r1 r2 -- r1-r2 ) floating point subtraction | + | |
- | == f-star | + | DUP SWAP ROLL PICK DROP OVER ROT -DUP ?DUP LEVEL DEPTH FLOAT + - |
- | F* ( r1 r2 -- r1*r2 ) floating point multiplication | + | * / ^ F+ F- F* F/ FIX MOD /MOD 0= 0=_ 0< 0<= |
+ | | ||
+ | <# # HOLD #N SIGN #S #> HOME CR CLEAR SPACE SPACES | ||
+ | [TYPE] | ||
+ | | ||
+ | < | ||
+ | , ALLOT LOAD [LOAD] | ||
+ | LOOP +LOOP I J IJ..N RUNT REPEAT | ||
+ | ERROR [NUMBER] | ||
- | == f-slash == | ||
- | F/ ( r1 r2 -- r1/r2 ) floating point division | ||
- | ===== Stack manipulation | + | ====== Start-Up File ====== |
- | == drop == | + | At start-up, FORTH-10 searches the user's log-in directory for a file named '' |
- | DROP ( n -- ) | + | |
- | == dup == | + | ====== FORTH ====== |
- | DUP ( n -- n n ) | + | |
- | == over == | + | Start the John Wilson' |
- | OVER ( n1 n2 -- n1 n2 n1 ) | + | |
- | == pick == | + | @FORTH |
- | PICK ( n1 -- n2 ) //n2// is the //n1//th item from the top of the stack | + | |
+ | ok | ||
+ | |||
+ | Exit FORTH with the command '' | ||
- | == swap == | ||
- | SWAP ( n1 n2 -- n2 n1 ) | ||
- | == rot == | + | ====== Reference ====== |
- | ROT ( n1 n2 n3 -- n2 n3 n1 ) rotate | + | |
- | == question-dup == | + | //Starting FORTH// is the standard introductory text for the Forth language. |
- | ?DUP ( n -- 0 | n n ) duplicate | + | |
- | == roll == | + | * Leo Brody, |
- | ROLL ( -- ) //(BROKEN)// | + | |
- | ===== Defining words ===== | ||
- | |||
- | == colon == | ||
- | : ( -- ) colon definition | ||
- | |||
- | : name ( ... -- ... ) word1 word2 ... ; | ||
- | | ||
- | Create a word //name// that executes //word1 word2 ...//. | ||
- | |||
- | == semicolon == | ||
- | ; ( -- ) close colon definition | ||
- | |||
- | ====== Start-Up File ====== | ||
- | |||
- | At start-up, FORTH-10 searches the user's log-in directory for a file named '' |