Gforth for Version 0.7.0, November 2, 2008
Total Page:16
File Type:pdf, Size:1020Kb
Gforth for version 0.7.0, November 2, 2008 Neal Crook Anton Ertl David Kuehling Bernd Paysan Jens Wilke This manual is for Gforth (version 0.7.0, November 2, 2008), a fast and portable implemen- tation of the ANS Forth language. It serves as reference manual, but it also contains an introduction to Forth and a Forth tutorial. Copyright c 1995, 1996, 1997, 1998, 2000, 2003, 2004,2005,2006,2007,2008 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover texts being “A GNU Manual,” and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled “GNU Free Documentation License.” (a) The FSF’s Back-Cover Text is: “You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.” i Table of Contents Preface ....................................... 1 1 Goals of Gforth ............................ 2 2 Gforth Environment ....................... 3 2.1 Invoking Gforth ............................................. 3 2.2 Leaving Gforth.............................................. 6 2.3 Command-line editing ....................................... 6 2.4 Environment variables ....................................... 7 2.5 Gforth files ................................................. 7 2.6 Gforth in pipes.............................................. 8 2.7 Startup speed ............................................... 8 3 Forth Tutorial ............................ 10 3.1 Starting Gforth ............................................ 10 3.2 Syntax ................................................... 10 3.3 Crash Course .............................................. 11 3.4 Stack................................................... ... 11 3.5 Arithmetics ................................................ 11 3.6 Stack Manipulation......................................... 12 3.7 Using files for Forth code ................................... 12 3.8 Comments ................................................. 13 3.9 Colon Definitions........................................... 13 3.10 Decompilation ............................................ 14 3.11 Stack-Effect Comments .................................... 14 3.12 Types ................................................... 15 3.13 Factoring ................................................. 16 3.14 Designing the stack effect .................................. 16 3.15 Local Variables ........................................... 17 3.16 Conditional execution ..................................... 18 3.17 Flags and Comparisons .................................... 18 3.18 General Loops ............................................ 19 3.19 Counted loops ............................................ 20 3.20 Recursion................................................. 21 3.21 Leaving definitions or loops ................................ 22 3.22 Return Stack ............................................. 23 3.23 Memory .................................................. 23 3.24 Characters and Strings .................................... 25 3.25 Alignment ................................................ 25 3.26 Floating Point ............................................ 26 3.27 Files ................................................... .. 27 3.27.1 Open file for input .................................... 27 ii 3.27.2 Create file for output ................................. 27 3.27.3 Scan file for a particular line........................... 28 3.27.4 Copy input to output ................................. 28 3.27.5 Close files ............................................ 28 3.28 Interpretation and Compilation Semantics and Immediacy .... 29 3.29 Execution Tokens ......................................... 30 3.30 Exceptions................................................ 31 3.31 Defining Words ........................................... 32 3.32 Arrays and Records ....................................... 34 3.33 POSTPONE ................................................. 34 3.34 Literal .................................................. 35 3.35 Advanced macros ......................................... 35 3.36 Compilation Tokens ....................................... 36 3.37 Wordlists and Search Order ................................ 37 4 An Introduction to ANS Forth ............ 39 4.1 Introducing the Text Interpreter............................. 39 4.2 Stacks, postfix notation and parameter passing ............... 41 4.3 Your first Forth definition................................... 44 4.4 How does that work? ....................................... 45 4.5 Forth is written in Forth .................................... 47 4.6 Review - elements of a Forth system ......................... 48 4.7 Where To Go Next ......................................... 48 4.8 Exercises .................................................. 49 5 Forth Words.............................. 50 5.1 Notation................................................... 50 5.2 Case insensitivity........................................... 51 5.3 Comments ................................................. 51 5.4 Boolean Flags.............................................. 52 5.5 Arithmetic................................................. 52 5.5.1 Single precision ........................................ 52 5.5.2 Double precision ....................................... 53 5.5.3 Bitwise operations ..................................... 53 5.5.4 Numeric comparison ................................... 54 5.5.5 Mixed precision........................................ 55 5.5.6 Floating Point ......................................... 55 5.6 Stack Manipulation......................................... 58 5.6.1 Data stack ............................................ 58 5.6.2 Floating point stack.................................... 59 5.6.3 Return stack .......................................... 59 5.6.4 Locals stack ........................................... 59 5.6.5 Stack pointer manipulation ............................. 60 5.7 Memory ................................................... 60 5.7.1 ANS Forth and Gforth memory models .................. 60 5.7.2 Dictionary allocation................................... 61 5.7.3 Heap allocation ........................................ 62 5.7.4 Memory Access ........................................ 62 iii 5.7.5 Address arithmetic..................................... 63 5.7.6 Memory Blocks ........................................ 65 5.8 Control Structures ......................................... 67 5.8.1 Selection .............................................. 67 5.8.2 Simple Loops .......................................... 68 5.8.3 Counted Loops ........................................ 68 5.8.4 Arbitrary control structures ............................ 70 5.8.4.1 Programming Style ................................ 72 5.8.5 Calls and returns ...................................... 72 5.8.6 Exception Handling .................................... 73 5.9 Defining Words ............................................ 76 5.9.1 CREATE................................................ 76 5.9.2 Variables .............................................. 77 5.9.3 Constants ............................................. 78 5.9.4 Values ................................................ 79 5.9.5 Colon Definitions ...................................... 79 5.9.6 Anonymous Definitions................................. 79 5.9.7 Supplying the name of a defined word ................... 80 5.9.8 User-defined Defining Words............................ 80 5.9.8.1 Applications of CREATE..DOES>..................... 83 5.9.8.2 The gory details of CREATE..DOES> ................. 84 5.9.8.3 Advanced does> usage example ..................... 84 5.9.8.4 Const-does> ..................................... 86 5.9.9 Deferred Words ........................................ 87 5.9.10 Aliases ............................................... 88 5.10 Interpretation and Compilation Semantics................... 89 5.10.1 Combined Words ..................................... 90 5.11 Tokens for Words ......................................... 91 5.11.1 Execution token ...................................... 91 5.11.2 Compilation token .................................... 92 5.11.3 Name token .......................................... 93 5.12 Compiling words .......................................... 94 5.12.1 Literals .............................................. 94 5.12.2 Macros .............................................. 95 5.13 The Text Interpreter ...................................... 97 5.13.1 Input Sources ........................................ 99 5.13.2 Number Conversion .................................. 100 5.13.3 Interpret/Compile states ............................. 102 5.13.4 Interpreter Directives ................................ 102 5.14 The Input Stream ........................................ 104 5.15 Word Lists .............................................. 105 5.15.1 Vocabularies ........................................ 107 5.15.2 Why use word lists? ................................. 108 5.15.3 Word list example ................................... 108 5.16 Environmental Queries ................................... 109 5.17 Files ..................................................