Gforth for Version 0.7.9-20120212, November 17, 2011
Total Page:16
File Type:pdf, Size:1020Kb
Gforth for version 0.7.9-20120212, November 17, 2011 Neal Crook Anton Ertl David Kuehling Bernd Paysan Jens Wilke This manual is for Gforth (version 0.7.9-20120212, November 17, 2011), a fast and portable implementation 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,2009,2010,2011 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:::::::::::::::::::::::::::::::::::::: 13 3.8 Comments :::::::::::::::::::::::::::::::::::::::::::::::::::: 13 3.9 Colon Definitions ::::::::::::::::::::::::::::::::::::::::::::: 14 3.10 Decompilation ::::::::::::::::::::::::::::::::::::::::::::::: 14 3.11 Stack-Effect Comments :::::::::::::::::::::::::::::::::::::: 14 3.12 Types ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 16 3.13 Factoring :::::::::::::::::::::::::::::::::::::::::::::::::::: 16 3.14 Designing the stack effect :::::::::::::::::::::::::::::::::::: 17 3.15 Local Variables :::::::::::::::::::::::::::::::::::::::::::::: 17 3.16 Conditional execution :::::::::::::::::::::::::::::::::::::::: 18 3.17 Flags and Comparisons :::::::::::::::::::::::::::::::::::::: 19 3.18 General Loops ::::::::::::::::::::::::::::::::::::::::::::::: 20 3.19 Counted loops ::::::::::::::::::::::::::::::::::::::::::::::: 21 3.20 Recursion :::::::::::::::::::::::::::::::::::::::::::::::::::: 22 3.21 Leaving definitions or loops :::::::::::::::::::::::::::::::::: 22 3.22 Return Stack :::::::::::::::::::::::::::::::::::::::::::::::: 23 3.23 Memory ::::::::::::::::::::::::::::::::::::::::::::::::::::: 24 3.24 Characters and Strings ::::::::::::::::::::::::::::::::::::::: 25 3.25 Alignment ::::::::::::::::::::::::::::::::::::::::::::::::::: 26 3.26 Floating Point ::::::::::::::::::::::::::::::::::::::::::::::: 26 3.27 Files ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 27 3.27.1 Open file for input :::::::::::::::::::::::::::::::::::::: 28 ii 3.27.2 Create file for output:::::::::::::::::::::::::::::::::::: 28 3.27.3 Scan file for a particular line :::::::::::::::::::::::::::: 28 3.27.4 Copy input to output ::::::::::::::::::::::::::::::::::: 29 3.27.5 Close files ::::::::::::::::::::::::::::::::::::::::::::::: 29 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 :::::::::::::::::::::::::::::::::::::::::::: 36 3.36 Compilation Tokens :::::::::::::::::::::::::::::::::::::::::: 37 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 :::::::::::::::::::::::::::::::::::::::::::::::::::: 52 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:::::::::::::::::::::::::::::::::::::::: 54 5.5.4 Numeric comparison ::::::::::::::::::::::::::::::::::::: 54 5.5.5 Mixed precision :::::::::::::::::::::::::::::::::::::::::: 55 5.5.6 Floating Point ::::::::::::::::::::::::::::::::::::::::::: 56 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 :::::::::::::::::::::::::::::::::::::::::::::: 60 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 ::::::::::::::::::::::::::::::::::::::: 64 5.7.6 Memory Blocks :::::::::::::::::::::::::::::::::::::::::: 66 5.8 Control Structures :::::::::::::::::::::::::::::::::::::::::::: 67 5.8.1 Selection ::::::::::::::::::::::::::::::::::::::::::::::::: 67 5.8.2 Simple Loops :::::::::::::::::::::::::::::::::::::::::::: 68 5.8.3 Counted Loops ::::::::::::::::::::::::::::::::::::::::::: 69 5.8.4 Arbitrary control structures :::::::::::::::::::::::::::::: 71 5.8.4.1 Programming Style :::::::::::::::::::::::::::::::::: 72 5.8.5 Calls and returns::::::::::::::::::::::::::::::::::::::::: 73 5.8.6 Exception Handling :::::::::::::::::::::::::::::::::::::: 74 5.9 Defining Words ::::::::::::::::::::::::::::::::::::::::::::::: 77 5.9.1 CREATE::::::::::::::::::::::::::::::::::::::::::::::::::: 77 5.9.2 Variables::::::::::::::::::::::::::::::::::::::::::::::::: 78 5.9.3 Constants :::::::::::::::::::::::::::::::::::::::::::::::: 78 5.9.4 Values ::::::::::::::::::::::::::::::::::::::::::::::::::: 79 5.9.5 Colon Definitions::::::::::::::::::::::::::::::::::::::::: 80 5.9.6 Anonymous Definitions ::::::::::::::::::::::::::::::::::: 80 5.9.7 Quotations ::::::::::::::::::::::::::::::::::::::::::::::: 81 5.9.8 Supplying the name of a defined word :::::::::::::::::::: 81 5.9.9 User-defined Defining Words:::::::::::::::::::::::::::::: 81 5.9.9.1 Applications of CREATE..DOES> :::::::::::::::::::::: 84 5.9.9.2 The gory details of CREATE..DOES> :::::::::::::::::: 85 5.9.9.3 Advanced does> usage example :::::::::::::::::::::: 86 5.9.9.4 Const-does> :::::::::::::::::::::::::::::::::::::::: 87 5.9.10 Deferred Words ::::::::::::::::::::::::::::::::::::::::: 88 5.9.11 Aliases :::::::::::::::::::::::::::::::::::::::::::::::::: 89 5.10 Interpretation and Compilation Semantics :::::::::::::::::::: 90 5.10.1 Combined Words:::::::::::::::::::::::::::::::::::::::: 91 5.11 Tokens for Words :::::::::::::::::::::::::::::::::::::::::::: 92 5.11.1 Execution token::::::::::::::::::::::::::::::::::::::::: 92 5.11.2 Compilation token :::::::::::::::::::::::::::::::::::::: 93 5.11.3 Name token ::::::::::::::::::::::::::::::::::::::::::::: 94 5.12 Compiling words ::::::::::::::::::::::::::::::::::::::::::::: 95 5.12.1 Literals ::::::::::::::::::::::::::::::::::::::::::::::::: 95 5.12.2 Macros:::::::::::::::::::::::::::::::::::::::::::::::::: 96 5.13 The Text Interpreter ::::::::::::::::::::::::::::::::::::::::: 99 5.13.1 Input Sources :::::::::::::::::::::::::::::::::::::::::: 101 5.13.2 Number Conversion :::::::::::::::::::::::::::::::::::: 102 5.13.3 Interpret/Compile states ::::::::::::::::::::::::::::::: 104 5.13.4 Interpreter Directives ::::::::::::::::::::::::::::::::::