User Tools

Site Tools


tutorials:forth

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorials:forth [2022/01/06 09:01]
papa [Primitive Dictionary]
tutorials:forth [2022/06/07 02:10] (current)
rcs correct lack of space after dot commands in examples
Line 1: Line 1:
-**FORTH-10** is a Forth language interpreter included in the Panda TOPS-20 distributionThe anonymous programmer describes the implementation as "NOT totally standard"The interpreter can be started with the following command on TWENEX:+[[http://forth.org/whatis.html|Forth]] is a stack-based, RPN-format programming language created by [[https://colorforth.github.io/|Charles H. Moore]] and first released in 1970
  
-  @FORTH+There are two Forth implementation available.
  
-MIDAS assembly language source is available in file ''TOPS20:<UTILITIES>FORTH.MID''.+**FORTH-10** is a Forth language interpreter included in the Panda TOPS-20 distribution. The anonymous programmer describes the implementation as "NOT totally standard"MIDAS assembly language source is available in file ''TOPS20:<UTILITIES>FORTH.MID''
 + 
 +**FORTH** is another interpreter originally written on the MIT AI lab DEC-20 called OZ, by John Wilson. 
 + 
 +====== FORTH-10 Survival Guide ====== 
 + 
 +Start the interpreter on TWENEX with the command ''FORTH-10''. If you have not created an [[#start-up_file|auto-load file]], FORTH-10 will respond with the default greeting and prompt. 
 + 
 +  @FORTH-10 
 +  FORTH-10   Type QUIT to exit. 
 +   Ok 
 +   
 +Exit FORTH-10 with the command ''QUIT''
 + 
 +At the ''Ok'' prompt, enter data and executable words separated by whitespace. The interpreter pushes each data item onto the top of the stack. Executable words are executed by the interpreter, removing required arguments from the top of the stack and replacing them with output results. 
 + 
 +  7 3 Ok  ( Push 7 then 3 on stack. ) 
 +  + Ok    ( Remove two items from stack (3 and 7), push the sum on stack, so 10 is only item. )  
 +  . 10 Ok  ( Word . pops top stack item and prints it. Stack is now empty. ) 
 +   
 +Words ''.'', ''+'', ''-'', ''*'', and ''/'' interpret stack items as integer values. For floating-point values, use ''f.'', ''f+'', ''f-'', ''f*'', and ''f/''  
 + 
 +  3.14159 Ok 
 +  f. 3.14159 Ok      ( f. pops stack and displays as floating-point. ) 
 +   
 +Add new words to the dictionary with the defining words '':'' and '';''
 + 
 +'':'' name word1 word2 ... '';''  create word //name// that executes //word1 word2 ...//.  
 + 
 +For example, 
 + 
 +  : TIMES5 5 * ; Ok  ( Define word TIMES5: Push 5 on stack (previous top value pushed lower), pop 5 and ) 
 +                     ( previous top value, multiply and push product on stack. 
 +  7 TIMES5 Ok        ( Execute the word. ) 
 +  . 35  Ok            ( Pop and display result. ) 
 +   
 +List words currently defined in the dictionary with ''VLIST''
 + 
 +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. 
 + 
 +|DROP|Pop stack without printing (discards top value).| 
 +|DUP|Copy top value and push it to stack.| 
 +|SWAP|Exchange the top two stack values.| 
 +|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]| 
 + 
 +Forth source files can be prepared with any editor. Load and interpret source files in FORTH-10 with the command ''LOAD "//file//"''
 + 
 +====== Hello, World! ====== 
 + 
 +  : HELLO CR ." "HELLO, WORLD!" CR ; Ok 
 +  HELLO 
 +  HELLO, WORLD! 
 +   Ok 
 + 
 +Insert comments in Forth source code with the word ''(''. The comment continues until the next '')''.
  
 ====== Primitive Dictionary ====== ====== Primitive Dictionary ======
Line 19: Line 74:
   LOOP  +LOOP  I  J  IJ..N  RUNT  REPEAT  UNTIL  CMOVE  [CMOVE]  HERE  LEAVE   LOOP  +LOOP  I  J  IJ..N  RUNT  REPEAT  UNTIL  CMOVE  [CMOVE]  HERE  LEAVE
   ERROR  [NUMBER]  WHILE  BEGIN  END   ERROR  [NUMBER]  WHILE  BEGIN  END
-   
-|!|-|>=|BINARY|DROP|F.|KEY|[NUMBER]|SPACE| 
-|#|-!|@|<BUILDS|DUP|F/|LEAVE|OCTAL|SPACES| 
-|#>|.|]|C!|-DUP|FILL|LEVEL|OR|SW,,AP| 
-|'|."|0<|C<|?DUP|FIX|LN|OVER|SWAP| 
-|'#|/|0<=|C>|ELSE|FLOAT|LOAD|PICK|?TERMINAL| 
-|(|^|0=|C@|EMIT|FLUSH|[LOAD]|QUIT|THEN| 
-|(")|:|0=_|CLEAR|END|FORGET|LOOP|.R|TRACE| 
-|["]|:"|0>|CMOVE|ERROR|HERE|+LOOP|REPEAT|TYPE| 
-|*|;|0>=|[CMOVE]|EXCHANGE|HOLD|MAX|ROLL|[TYPE]| 
-|+|<|1+|COSINE|EXECUTE|HOME|MIN|ROOT|UNLOAD| 
-|+!|<#|1-|CR|EXPECT|I|MINUS|ROT|UNTIL| 
-|+-|<=|ABS|DECIMAL|[EXPECT]|IF|MOD|RUNT|VLIST| 
-|,|=|ALLOT|DEPTH|F*|IJ..N|/MOD|#S|WHILE| 
-|,,->|=_|AND|DO|F+|J|#N|SIGN|XOR| 
-|<-,,|>|BEGIN|DOES>|F-|JSYS|NOT|SINE|   | 
  
  
Line 40: Line 79:
  
 At start-up, FORTH-10 searches the user's log-in directory for a file named ''AUTO-LOAD.4TH''. If such a file exists, it is loaded automatically. If the file does not exist, FORTH-10 displays a standard greeting message. At start-up, FORTH-10 searches the user's log-in directory for a file named ''AUTO-LOAD.4TH''. If such a file exists, it is loaded automatically. If the file does not exist, FORTH-10 displays a standard greeting message.
 +
 +====== FORTH ======
 +
 +Start the John Wilson's interpreter on TWENEX with the command ''FORTH''. It will respond with this greeting and prompt.
 +
 +  @FORTH
 +  FORTH/Oz v1 (JohnW)
 +  ok
 +  
 +Exit FORTH with the command ''BYE''.
 +
 +
 +====== Reference ======
 +
 +//Starting FORTH// is the standard introductory text for the Forth language.
 +
 +* Leo Brody, //Starting FORTH//. Forth, Inc. https://www.forth.com/starting-forth/. [Accessed January 8, 2022.]
 +
tutorials/forth.1641459685.txt.gz · Last modified: 2022/01/06 09:01 by papa