Forth Programmer’s Handbook Edward K. Conklin Elizabeth D. Rather and the technical staff of FORTH, Inc. Software products and services since 1973 www.forth.com Forth Programmer’s Handbook FORTH, Inc. makes no warranty of any kind with regard to this material, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. FORTH, Inc. shall not be liable for errors contained herein or for incidental or consequential damages in connection with the furnishing, performance, or use of this material. All brand and product names are trademarks or registered trademarks of their respective companies. Copyright © 1997-2010 by FORTH, Inc. All rights reserved. First edition, September 1997 Second edition, August 1998 Third edition, August 2007 Latest revision, October 2010 ISBN 1-4196-7549-4 This document contains information proprietary to FORTH, Inc. Any reproduction, disclosure, or unauthorized use of this document, either in whole or in part, is expressly forbidden without prior permission in writing from: FORTH, Inc. Los Angeles, California www.forth.com 2 Forth Programmer’s Handbook Contents Preface to the Third Edition 11 Welcome! 13 About the Forth Programming Language 13 About This Book 13 How to Use This Book 14 Typographic Conventions 14 Reference Materials 15 How to Proceed 15 1. Introduction 17 1.1 Forth Language Features 18 1.1.1 Definitions of Terms 18 1.1.2 Dictionary 18 1.1.3 Data Stack 22 1.1.4 Return Stack 24 1.1.5 Text Interpreter 25 1.1.6 Numeric Input 29 1.1.7 Two-stack Virtual Machine 31 1.2 Forth Operating System Features 33 1.2.1 Disk I/O 34 1.2.2 Multitasking 34 1.3 The Forth Assembler 36 1.3.1 Notational Differences 36 1.3.2 Procedural Differences 37 1.4 Documentation and Programmer Aids 38 1.4.1 Comments 38 1.4.2 Locating Command Source 40 1.4.3 Cross-references 41 1.4.4 Decompiler and Disassembler 42 1.5 Interactive Programming—An Example 43 Contents 3 Forth Programmer’s Handbook 2. Forth Fundamentals 47 2.1 Stack Operations 47 2.1.1 Stack Notation 47 2.1.2 Data Stack Manipulation 49 2.1.2.1 Single-item operators 49 2.1.2.2 Two-item operators 50 2.1.3 Return Stack Manipulation 51 2.1.4 Programmer Conveniences 53 2.2 Arithmetic and Logical Operations 54 2.2.1 Arithmetic and Shift Operators 54 2.2.2 Logical Operations 58 2.3 Memory and Data Storage 60 2.3.1 Defining Words 60 2.3.2 Single Data Objects 61 2.3.2.1 Variables 62 2.3.2.2 Constants and Values 63 2.3.3 Arrays and Tables 65 2.3.4 Memory Stack Operations 69 2.3.5 Data Object and Memory Access Examples 71 3. String Handling 73 3.1 General String Topics 73 3.1.1 Single Characters 73 3.1.2 Scratch Storage for Strings 74 3.1.3 Internal String Format 75 3.2 Strings in Definitions 76 3.3 Strings in Data Structures 79 3.4 String Management Operations 80 3.5 Comparing Character Strings 82 3.6 Number Conversions 84 3.6.1 Input Number Conversion 84 3.6.2 Numeric Output 88 3.6.2.1 Standard Numeric Output Words 88 3.6.2.2 Pictured Number Conversion 89 4 Contents Forth Programmer’s Handbook 3.6.2.3 Using Pictured Numeric Output Words 90 3.6.2.4 Using Pictured Fill Characters 93 3.6.3 Processing Special Characters 94 4. Structured Programming 97 4.1 Controlling Program Flow 97 4.2 Comparison and Testing Operations 98 4.3 Conditionals 100 4.4 Indefinite Loops 102 4.4.1 Infinite loops 102 4.4.2 Post-testing loops 103 4.4.3 Pre-testing loops 104 4.5 Counting (Finite) Loops 105 4.6 Finite vs. Indefinite Loops 109 4.7 Case Statement 110 4.8 Nesting Structures 111 4.9 Nesting and Un-nesting Structures and Definitions 112 5. System Functions 115 5.1 Vectored Execution 115 5.1.1 Execution Tokens 115 5.1.2 Single Function Pointers 116 5.1.3 Execution Vector Tables 118 5.1.4 Vectored System Routines 119 5.2 System Environment 120 5.3 Exception Handling 123 5.4 Serial I/O 128 5.4.1 Terminal Input 128 5.4.2 Terminal Output 131 5.4.3 Support of Special Terminal Features 132 Contents 5 Forth Programmer’s Handbook 5.5 File-Based Disk Access 133 5.5.1 Overview 133 5.5.2 Global File Operations 134 5.5.3 File Reading and Writing 135 5.5.4 File Support Words 137 5.6 Time and Timing Functions 138 5.7 Dynamic Memory Management 139 5.8 Floating Point 140 5.8.1 Floating-Point System Guidelines 140 5.8.2 Input Number Conversion 142 5.8.3 Output Formats 143 5.8.4 Floating-Point Constants, Variables, and Literals 143 5.8.5 Memory Access 144 5.8.6 Floating-Point Stack Operators 146 5.8.7 Floating-Point Arithmetic 147 5.8.8 Floating-Point Conditionals 148 5.8.9 Logarithmic and Trigonometric Functions 149 5.8.10 Address Management 151 5.8.11 Custom I/O 153 6. The Forth Interpreter and Compiler 155 6.1 The Text Interpreter 155 6.1.1 Input Sources 155 6.1.2 Input Source Management 157 6.1.3 Parsing Text in the Input Stream 159 6.1.4 Dictionary Searches 161 6.1.5 Text Interpreter Conditionals 163 6.2 Defining Words 164 6.2.1 Creating a Dictionary Entry 165 6.2.2 Colon Definitions 166 6.2.3 Code Definitions 169 6.2.4 Custom Defining Words 170 6.2.4.1 Basic Principles of Defining Words 170 6.2.4.2 Constructing Custom Defining Words 171 6.2.4.3 High-level Defining Words 173 6.3 Compiling Words and Literals 175 6 Contents Forth Programmer’s Handbook 6.3.1 The Forth Compiler 175 6.3.2 Literals and Constants 178 6.3.3 Compiling Execution Tokens 180 6.3.4 Compiling Strings 180 6.4 Compiler Directives 182 6.4.1 Making Compiler Directives 182 6.4.2 The Control-flow Stack and Custom Compiling Structures 186 6.5 Overlays 188 6.6 Word Lists 190 6.6.1 Basic Principles 190 6.6.2 Managing Word Lists 192 6.6.3 Sealed Word Lists 193 7. Forth Cross compilers 195 7.1 Issues in Cross Development 196 7.2 Host and Target Roles and Functions 196 7.3 Managing Scopes 197 7.4 Data Space Management 199 7.4.1 Vectored Words 200 7.4.2 Data Types 201 7.4.3 Effects of Scoping on Data Object Defining Words 202 7.5 Interactive Programming 204 7.6 I/O Drivers for Embedded Systems 205 8. Programming Style and Editing Standards 209 8.1 FORTH, Inc. Editing Standards 209 8.1.1 Stack Effects 210 8.1.2 General Comments 211 8.1.3 Spacing Within Files 212 8.2 Open Firmware Coding Style 213 8.2.1 Typographic Conventions 213 8.2.2 Use of Spaces 213 Contents 7 Forth Programmer’s Handbook 8.2.3 Conditional Structures 214 8.2.4 Finite Loop Structures 215 8.2.5 Indefinite Pre-testing Loop Structures 215 8.2.6 Indefinite Post-testing Loop Structures 216 8.2.7 Block Comments 216 8.2.8 Stack Comments 216 8.2.9 Return Stack Comments 217 8.2.10 Numbers 217 8.3 Wong’s Rules for Readable Forth 218 8.3.1 Example: Magic Numbers 218 8.3.2 Example: Factoring 219 8.3.3 Example: Simplicity 220 8.3.4 Example: Testing Assumptions 221 8.3.5 Example: IF Avoidance 221 8.3.6 Example: Stack Music 222 8.3.7 Summary 224 8.4 Naming Conventions 224 Appendix A: Bibliography 229 Appendix B: Glossary & Notation 233 B.1 Glossary 234 B.2 Data Types in Stack Notation 238 B.3 Flags and IOR Codes 241 B.4 Forth Glossary Notation 241 Appendix C: Blocks for Disk Storage 243 C.1 Overview 243 C.2 Loading Forth Source Blocks 248 C.3 Block-based Programmer Aids and Utilities 252 C.4 Style Guidelines for Block-based Source 253 Appendix D: Index to Forth Words 257 General Index 271 8 Contents Forth Programmer’s Handbook List of Figures 1. Dictionary entry links 19 2. Logical structure of the Forth dictionary 20 3. Structural details of a typical dictionary entry 21 4. Items on the data stack 23 5. Flow diagram of the text interpreter 28 6. Example of a control program that runs a washing machine 44 7. Dictionary entry built by CONSTANT 64 8. Format of a counted string 75 9. Use of COUNT 76 10. Format of arguments for most two-string operators 80 11. Actions of string copy operators 81 12. String comparison 83 13. String search 83 14. Logical flow of a conditional structure 100 15. Logical flow of a post-test indefinite loop 103 16. Logical flow of a pre-test indefinite loop 104 17. Examples of legal and illegal nested structures. 112 18. Dictionary entry built by CREATE 165 19. Structures defined by using DOES> 174 20. Action of the Forth compiler 177 21. Compile-time action of IF 184 22. Hierarchy of data types 240 23. Relationship between blocks and block buffers 245 List of Tables 1. Integer precision and CPU data width 29 2. Valid numeric punctuation characters 30 3. Registers in the Forth virtual machine 31 4. Common stack notation 39 5. Order of arguments, Forth postfix vs. infix 55 6. String comparison examples 83 7. Conversion results from NUMBER? 87 8. Pre-processing for output number conversion 91 9. Environmental query strings and associated data 121 10. ANS Forth reserved throw codes 125 11. Identifying the input source 156 Contents 9 Forth Programmer’s Handbook 12. Summary of compile-time branch words 187 13. Availability of words defined in various scopes 198 14. Types of memory space in cross-compilers 199 15. Naming conventions 225 16. Notation for the data type of stack arguments 238 10 Contents