| 1 : | anton | 1.1 | \input texinfo @c -*-texinfo-*- |
| 2 : | @comment The source is gforth.ds, from which gforth.texi is generated | ||
| 3 : | crook | 1.28 | |
| 4 : | crook | 1.21 | @comment TODO: nac29jan99 - a list of things to add in the next edit: |
| 5 : | crook | 1.28 | @comment 1. x-ref all ambiguous or implementation-defined features? |
| 6 : | @comment 2. Describe the use of Auser Avariable AConstant A, etc. | ||
| 7 : | @comment 3. words in miscellaneous section need a home. | ||
| 8 : | @comment 4. search for TODO for other minor and major works required. | ||
| 9 : | @comment 5. [rats] change all @var to @i in Forth source so that info | ||
| 10 : | @comment file looks decent. | ||
| 11 : | anton | 1.36 | @c Not an improvement IMO - anton |
| 12 : | @c and anyway, this should be taken up | ||
| 13 : | @c with Karl Berry (the texinfo guy) - anton | ||
| 14 : | anton | 1.113 | @c |
| 15 : | @c Karl Berry writes: | ||
| 16 : | @c If they don't like the all-caps for @var Info output, all I can say is | ||
| 17 : | @c that it's always been that way, and the usage of all-caps for | ||
| 18 : | @c metavariables has a long tradition. I think it's best to just let it be | ||
| 19 : | @c what it is, for the sake of consistency among manuals. | ||
| 20 : | @c | ||
| 21 : | crook | 1.29 | @comment .. would be useful to have a word that identified all deferred words |
| 22 : | @comment should semantics stuff in intro be moved to another section | ||
| 23 : | |||
| 24 : | anton | 1.66 | @c POSTPONE, COMPILE, [COMPILE], LITERAL should have their own section |
| 25 : | crook | 1.28 | |
| 26 : | anton | 1.1 | @comment %**start of header (This is for running Texinfo on a region.) |
| 27 : | @setfilename gforth.info | ||
| 28 : | anton | 1.113 | @include version.texi |
| 29 : | anton | 1.1 | @settitle Gforth Manual |
| 30 : | anton | 1.113 | @c @syncodeindex pg cp |
| 31 : | anton | 1.49 | |
| 32 : | anton | 1.12 | @macro progstyle {} |
| 33 : | Programming style note: | ||
| 34 : | anton | 1.3 | @end macro |
| 35 : | anton | 1.48 | |
| 36 : | @macro assignment {} | ||
| 37 : | @table @i | ||
| 38 : | @item Assignment: | ||
| 39 : | @end macro | ||
| 40 : | @macro endassignment {} | ||
| 41 : | @end table | ||
| 42 : | @end macro | ||
| 43 : | |||
| 44 : | crook | 1.29 | @comment macros for beautifying glossary entries |
| 45 : | @macro GLOSS-START {} | ||
| 46 : | @iftex | ||
| 47 : | @ninerm | ||
| 48 : | @end iftex | ||
| 49 : | @end macro | ||
| 50 : | |||
| 51 : | @macro GLOSS-END {} | ||
| 52 : | @iftex | ||
| 53 : | @rm | ||
| 54 : | @end iftex | ||
| 55 : | @end macro | ||
| 56 : | |||
| 57 : | anton | 1.113 | @comment %**end of header (This is for running Texinfo on a region.) |
| 58 : | @copying | ||
| 59 : | anton | 1.125 | This manual is for Gforth (version @value{VERSION}, @value{UPDATED}), |
| 60 : | a fast and portable implementation of the ANS Forth language. It | ||
| 61 : | serves as reference manual, but it also contains an introduction to | ||
| 62 : | Forth and a Forth tutorial. | ||
| 63 : | crook | 1.29 | |
| 64 : | anton | 1.113 | Copyright @copyright{} 1995, 1996, 1997, 1998, 2000, 2003 Free Software Foundation, Inc. |
| 65 : | crook | 1.29 | |
| 66 : | anton | 1.113 | @quotation |
| 67 : | Permission is granted to copy, distribute and/or modify this document | ||
| 68 : | under the terms of the GNU Free Documentation License, Version 1.1 or | ||
| 69 : | any later version published by the Free Software Foundation; with no | ||
| 70 : | Invariant Sections, with the Front-Cover texts being ``A GNU Manual,'' | ||
| 71 : | and with the Back-Cover Texts as in (a) below. A copy of the | ||
| 72 : | license is included in the section entitled ``GNU Free Documentation | ||
| 73 : | License.'' | ||
| 74 : | |||
| 75 : | (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify | ||
| 76 : | this GNU Manual, like GNU software. Copies published by the Free | ||
| 77 : | Software Foundation raise funds for GNU development.'' | ||
| 78 : | @end quotation | ||
| 79 : | @end copying | ||
| 80 : | anton | 1.10 | |
| 81 : | anton | 1.113 | @dircategory Software development |
| 82 : | @direntry | ||
| 83 : | * Gforth: (gforth). A fast interpreter for the Forth language. | ||
| 84 : | @end direntry | ||
| 85 : | @c The Texinfo manual also recommends doing this, but for Gforth it may | ||
| 86 : | @c not make much sense | ||
| 87 : | @c @dircategory Individual utilities | ||
| 88 : | @c @direntry | ||
| 89 : | @c * Gforth: (gforth)Invoking Gforth. gforth, gforth-fast, gforthmi | ||
| 90 : | @c @end direntry | ||
| 91 : | anton | 1.1 | |
| 92 : | @titlepage | ||
| 93 : | anton | 1.113 | @title Gforth |
| 94 : | @subtitle for version @value{VERSION}, @value{UPDATED} | ||
| 95 : | @author Neal Crook | ||
| 96 : | @author Anton Ertl | ||
| 97 : | anton | 1.114 | @author David Kuehling |
| 98 : | anton | 1.113 | @author Bernd Paysan |
| 99 : | @author Jens Wilke | ||
| 100 : | anton | 1.1 | @page |
| 101 : | @vskip 0pt plus 1filll | ||
| 102 : | anton | 1.113 | @insertcopying |
| 103 : | @end titlepage | ||
| 104 : | anton | 1.1 | |
| 105 : | anton | 1.113 | @contents |
| 106 : | anton | 1.1 | |
| 107 : | anton | 1.113 | @ifnottex |
| 108 : | @node Top, Goals, (dir), (dir) | ||
| 109 : | @top Gforth | ||
| 110 : | anton | 1.1 | |
| 111 : | anton | 1.113 | @insertcopying |
| 112 : | anton | 1.49 | @end ifnottex |
| 113 : | anton | 1.1 | |
| 114 : | @menu | ||
| 115 : | crook | 1.26 | * Goals:: About the Gforth Project |
| 116 : | crook | 1.29 | * Gforth Environment:: Starting (and exiting) Gforth |
| 117 : | anton | 1.48 | * Tutorial:: Hands-on Forth Tutorial |
| 118 : | crook | 1.21 | * Introduction:: An introduction to ANS Forth |
| 119 : | anton | 1.1 | * Words:: Forth words available in Gforth |
| 120 : | anton | 1.24 | * Error messages:: How to interpret them |
| 121 : | anton | 1.1 | * Tools:: Programming tools |
| 122 : | * ANS conformance:: Implementation-defined options etc. | ||
| 123 : | anton | 1.65 | * Standard vs Extensions:: Should I use extensions? |
| 124 : | anton | 1.1 | * Model:: The abstract machine of Gforth |
| 125 : | * Integrating Gforth:: Forth as scripting language for applications | ||
| 126 : | * Emacs and Gforth:: The Gforth Mode | ||
| 127 : | * Image Files:: @code{.fi} files contain compiled code | ||
| 128 : | * Engine:: The inner interpreter and the primitives | ||
| 129 : | pazsan | 1.13 | * Cross Compiler:: The Cross Compiler |
| 130 : | anton | 1.1 | * Bugs:: How to report them |
| 131 : | * Origin:: Authors and ancestors of Gforth | ||
| 132 : | crook | 1.21 | * Forth-related information:: Books and places to look on the WWW |
| 133 : | anton | 1.113 | * Licenses:: |
| 134 : | anton | 1.1 | * Word Index:: An item for each Forth word |
| 135 : | * Concept Index:: A menu covering many topics | ||
| 136 : | anton | 1.12 | |
| 137 : | anton | 1.91 | @detailmenu |
| 138 : | --- The Detailed Node Listing --- | ||
| 139 : | anton | 1.12 | |
| 140 : | crook | 1.29 | Gforth Environment |
| 141 : | |||
| 142 : | anton | 1.32 | * Invoking Gforth:: Getting in |
| 143 : | * Leaving Gforth:: Getting out | ||
| 144 : | * Command-line editing:: | ||
| 145 : | anton | 1.48 | * Environment variables:: that affect how Gforth starts up |
| 146 : | anton | 1.32 | * Gforth Files:: What gets installed and where |
| 147 : | anton | 1.112 | * Gforth in pipes:: |
| 148 : | anton | 1.48 | * Startup speed:: When 35ms is not fast enough ... |
| 149 : | |||
| 150 : | Forth Tutorial | ||
| 151 : | |||
| 152 : | * Starting Gforth Tutorial:: | ||
| 153 : | * Syntax Tutorial:: | ||
| 154 : | * Crash Course Tutorial:: | ||
| 155 : | * Stack Tutorial:: | ||
| 156 : | * Arithmetics Tutorial:: | ||
| 157 : | * Stack Manipulation Tutorial:: | ||
| 158 : | * Using files for Forth code Tutorial:: | ||
| 159 : | * Comments Tutorial:: | ||
| 160 : | * Colon Definitions Tutorial:: | ||
| 161 : | * Decompilation Tutorial:: | ||
| 162 : | * Stack-Effect Comments Tutorial:: | ||
| 163 : | * Types Tutorial:: | ||
| 164 : | * Factoring Tutorial:: | ||
| 165 : | * Designing the stack effect Tutorial:: | ||
| 166 : | * Local Variables Tutorial:: | ||
| 167 : | * Conditional execution Tutorial:: | ||
| 168 : | * Flags and Comparisons Tutorial:: | ||
| 169 : | * General Loops Tutorial:: | ||
| 170 : | * Counted loops Tutorial:: | ||
| 171 : | * Recursion Tutorial:: | ||
| 172 : | * Leaving definitions or loops Tutorial:: | ||
| 173 : | * Return Stack Tutorial:: | ||
| 174 : | * Memory Tutorial:: | ||
| 175 : | * Characters and Strings Tutorial:: | ||
| 176 : | * Alignment Tutorial:: | ||
| 177 : | anton | 1.87 | * Files Tutorial:: |
| 178 : | anton | 1.48 | * Interpretation and Compilation Semantics and Immediacy Tutorial:: |
| 179 : | * Execution Tokens Tutorial:: | ||
| 180 : | * Exceptions Tutorial:: | ||
| 181 : | * Defining Words Tutorial:: | ||
| 182 : | * Arrays and Records Tutorial:: | ||
| 183 : | * POSTPONE Tutorial:: | ||
| 184 : | * Literal Tutorial:: | ||
| 185 : | * Advanced macros Tutorial:: | ||
| 186 : | * Compilation Tokens Tutorial:: | ||
| 187 : | * Wordlists and Search Order Tutorial:: | ||
| 188 : | crook | 1.29 | |
| 189 : | anton | 1.24 | An Introduction to ANS Forth |
| 190 : | |||
| 191 : | anton | 1.67 | * Introducing the Text Interpreter:: |
| 192 : | * Stacks and Postfix notation:: | ||
| 193 : | * Your first definition:: | ||
| 194 : | * How does that work?:: | ||
| 195 : | * Forth is written in Forth:: | ||
| 196 : | * Review - elements of a Forth system:: | ||
| 197 : | * Where to go next:: | ||
| 198 : | * Exercises:: | ||
| 199 : | anton | 1.24 | |
| 200 : | anton | 1.12 | Forth Words |
| 201 : | |||
| 202 : | * Notation:: | ||
| 203 : | anton | 1.65 | * Case insensitivity:: |
| 204 : | * Comments:: | ||
| 205 : | * Boolean Flags:: | ||
| 206 : | anton | 1.12 | * Arithmetic:: |
| 207 : | * Stack Manipulation:: | ||
| 208 : | * Memory:: | ||
| 209 : | * Control Structures:: | ||
| 210 : | * Defining Words:: | ||
| 211 : | anton | 1.65 | * Interpretation and Compilation Semantics:: |
| 212 : | crook | 1.47 | * Tokens for Words:: |
| 213 : | anton | 1.81 | * Compiling words:: |
| 214 : | anton | 1.65 | * The Text Interpreter:: |
| 215 : | anton | 1.111 | * The Input Stream:: |
| 216 : | anton | 1.65 | * Word Lists:: |
| 217 : | * Environmental Queries:: | ||
| 218 : | anton | 1.12 | * Files:: |
| 219 : | * Blocks:: | ||
| 220 : | * Other I/O:: | ||
| 221 : | anton | 1.121 | * OS command line arguments:: |
| 222 : | anton | 1.78 | * Locals:: |
| 223 : | * Structures:: | ||
| 224 : | * Object-oriented Forth:: | ||
| 225 : | anton | 1.12 | * Programming Tools:: |
| 226 : | * Assembler and Code Words:: | ||
| 227 : | * Threading Words:: | ||
| 228 : | anton | 1.65 | * Passing Commands to the OS:: |
| 229 : | * Keeping track of Time:: | ||
| 230 : | * Miscellaneous Words:: | ||
| 231 : | anton | 1.12 | |
| 232 : | Arithmetic | ||
| 233 : | |||
| 234 : | * Single precision:: | ||
| 235 : | anton | 1.67 | * Double precision:: Double-cell integer arithmetic |
| 236 : | anton | 1.12 | * Bitwise operations:: |
| 237 : | anton | 1.67 | * Numeric comparison:: |
| 238 : | anton | 1.32 | * Mixed precision:: Operations with single and double-cell integers |
| 239 : | anton | 1.12 | * Floating Point:: |
| 240 : | |||
| 241 : | Stack Manipulation | ||
| 242 : | |||
| 243 : | * Data stack:: | ||
| 244 : | * Floating point stack:: | ||
| 245 : | * Return stack:: | ||
| 246 : | * Locals stack:: | ||
| 247 : | * Stack pointer manipulation:: | ||
| 248 : | |||
| 249 : | Memory | ||
| 250 : | |||
| 251 : | anton | 1.32 | * Memory model:: |
| 252 : | * Dictionary allocation:: | ||
| 253 : | * Heap Allocation:: | ||
| 254 : | * Memory Access:: | ||
| 255 : | * Address arithmetic:: | ||
| 256 : | * Memory Blocks:: | ||
| 257 : | anton | 1.12 | |
| 258 : | Control Structures | ||
| 259 : | |||
| 260 : | anton | 1.41 | * Selection:: IF ... ELSE ... ENDIF |
| 261 : | * Simple Loops:: BEGIN ... | ||
| 262 : | anton | 1.32 | * Counted Loops:: DO |
| 263 : | anton | 1.67 | * Arbitrary control structures:: |
| 264 : | * Calls and returns:: | ||
| 265 : | anton | 1.12 | * Exception Handling:: |
| 266 : | |||
| 267 : | Defining Words | ||
| 268 : | |||
| 269 : | anton | 1.67 | * CREATE:: |
| 270 : | crook | 1.44 | * Variables:: Variables and user variables |
| 271 : | anton | 1.67 | * Constants:: |
| 272 : | crook | 1.44 | * Values:: Initialised variables |
| 273 : | anton | 1.67 | * Colon Definitions:: |
| 274 : | crook | 1.44 | * Anonymous Definitions:: Definitions without names |
| 275 : | anton | 1.71 | * Supplying names:: Passing definition names as strings |
| 276 : | anton | 1.67 | * User-defined Defining Words:: |
| 277 : | crook | 1.44 | * Deferred words:: Allow forward references |
| 278 : | anton | 1.67 | * Aliases:: |
| 279 : | crook | 1.47 | |
| 280 : | anton | 1.63 | User-defined Defining Words |
| 281 : | |||
| 282 : | * CREATE..DOES> applications:: | ||
| 283 : | * CREATE..DOES> details:: | ||
| 284 : | * Advanced does> usage example:: | ||
| 285 : | anton | 1.91 | * @code{Const-does>}:: |
| 286 : | anton | 1.63 | |
| 287 : | crook | 1.47 | Interpretation and Compilation Semantics |
| 288 : | |||
| 289 : | anton | 1.67 | * Combined words:: |
| 290 : | anton | 1.12 | |
| 291 : | anton | 1.71 | Tokens for Words |
| 292 : | |||
| 293 : | * Execution token:: represents execution/interpretation semantics | ||
| 294 : | * Compilation token:: represents compilation semantics | ||
| 295 : | * Name token:: represents named words | ||
| 296 : | |||
| 297 : | anton | 1.82 | Compiling words |
| 298 : | |||
| 299 : | * Literals:: Compiling data values | ||
| 300 : | * Macros:: Compiling words | ||
| 301 : | |||
| 302 : | crook | 1.21 | The Text Interpreter |
| 303 : | |||
| 304 : | anton | 1.67 | * Input Sources:: |
| 305 : | * Number Conversion:: | ||
| 306 : | * Interpret/Compile states:: | ||
| 307 : | * Interpreter Directives:: | ||
| 308 : | crook | 1.21 | |
| 309 : | crook | 1.26 | Word Lists |
| 310 : | |||
| 311 : | anton | 1.75 | * Vocabularies:: |
| 312 : | anton | 1.67 | * Why use word lists?:: |
| 313 : | anton | 1.75 | * Word list example:: |
| 314 : | crook | 1.26 | |
| 315 : | Files | ||
| 316 : | |||
| 317 : | anton | 1.48 | * Forth source files:: |
| 318 : | * General files:: | ||
| 319 : | * Search Paths:: | ||
| 320 : | |||
| 321 : | Search Paths | ||
| 322 : | |||
| 323 : | anton | 1.75 | * Source Search Paths:: |
| 324 : | crook | 1.26 | * General Search Paths:: |
| 325 : | |||
| 326 : | Other I/O | ||
| 327 : | |||
| 328 : | anton | 1.32 | * Simple numeric output:: Predefined formats |
| 329 : | * Formatted numeric output:: Formatted (pictured) output | ||
| 330 : | * String Formats:: How Forth stores strings in memory | ||
| 331 : | anton | 1.67 | * Displaying characters and strings:: Other stuff |
| 332 : | anton | 1.32 | * Input:: Input |
| 333 : | anton | 1.112 | * Pipes:: How to create your own pipes |
| 334 : | crook | 1.26 | |
| 335 : | Locals | ||
| 336 : | |||
| 337 : | * Gforth locals:: | ||
| 338 : | * ANS Forth locals:: | ||
| 339 : | |||
| 340 : | Gforth locals | ||
| 341 : | |||
| 342 : | * Where are locals visible by name?:: | ||
| 343 : | * How long do locals live?:: | ||
| 344 : | anton | 1.78 | * Locals programming style:: |
| 345 : | * Locals implementation:: | ||
| 346 : | crook | 1.26 | |
| 347 : | anton | 1.12 | Structures |
| 348 : | |||
| 349 : | * Why explicit structure support?:: | ||
| 350 : | * Structure Usage:: | ||
| 351 : | * Structure Naming Convention:: | ||
| 352 : | * Structure Implementation:: | ||
| 353 : | * Structure Glossary:: | ||
| 354 : | |||
| 355 : | Object-oriented Forth | ||
| 356 : | |||
| 357 : | anton | 1.48 | * Why object-oriented programming?:: |
| 358 : | * Object-Oriented Terminology:: | ||
| 359 : | * Objects:: | ||
| 360 : | * OOF:: | ||
| 361 : | * Mini-OOF:: | ||
| 362 : | crook | 1.23 | * Comparison with other object models:: |
| 363 : | anton | 1.12 | |
| 364 : | anton | 1.24 | The @file{objects.fs} model |
| 365 : | anton | 1.12 | |
| 366 : | * Properties of the Objects model:: | ||
| 367 : | * Basic Objects Usage:: | ||
| 368 : | anton | 1.41 | * The Objects base class:: |
| 369 : | anton | 1.12 | * Creating objects:: |
| 370 : | * Object-Oriented Programming Style:: | ||
| 371 : | * Class Binding:: | ||
| 372 : | * Method conveniences:: | ||
| 373 : | * Classes and Scoping:: | ||
| 374 : | anton | 1.41 | * Dividing classes:: |
| 375 : | anton | 1.12 | * Object Interfaces:: |
| 376 : | * Objects Implementation:: | ||
| 377 : | * Objects Glossary:: | ||
| 378 : | |||
| 379 : | anton | 1.24 | The @file{oof.fs} model |
| 380 : | anton | 1.12 | |
| 381 : | anton | 1.67 | * Properties of the OOF model:: |
| 382 : | * Basic OOF Usage:: | ||
| 383 : | * The OOF base class:: | ||
| 384 : | * Class Declaration:: | ||
| 385 : | * Class Implementation:: | ||
| 386 : | anton | 1.12 | |
| 387 : | anton | 1.24 | The @file{mini-oof.fs} model |
| 388 : | crook | 1.23 | |
| 389 : | anton | 1.48 | * Basic Mini-OOF Usage:: |
| 390 : | * Mini-OOF Example:: | ||
| 391 : | * Mini-OOF Implementation:: | ||
| 392 : | crook | 1.23 | |
| 393 : | anton | 1.78 | Programming Tools |
| 394 : | |||
| 395 : | * Examining:: | ||
| 396 : | * Forgetting words:: | ||
| 397 : | * Debugging:: Simple and quick. | ||
| 398 : | * Assertions:: Making your programs self-checking. | ||
| 399 : | * Singlestep Debugger:: Executing your program word by word. | ||
| 400 : | |||
| 401 : | Assembler and Code Words | ||
| 402 : | |||
| 403 : | * Code and ;code:: | ||
| 404 : | * Common Assembler:: Assembler Syntax | ||
| 405 : | * Common Disassembler:: | ||
| 406 : | * 386 Assembler:: Deviations and special cases | ||
| 407 : | * Alpha Assembler:: Deviations and special cases | ||
| 408 : | * MIPS assembler:: Deviations and special cases | ||
| 409 : | * Other assemblers:: How to write them | ||
| 410 : | |||
| 411 : | anton | 1.12 | Tools |
| 412 : | |||
| 413 : | * ANS Report:: Report the words used, sorted by wordset. | ||
| 414 : | anton | 1.127 | * Stack depth changes:: Where does this stack item come from? |
| 415 : | anton | 1.12 | |
| 416 : | ANS conformance | ||
| 417 : | |||
| 418 : | * The Core Words:: | ||
| 419 : | * The optional Block word set:: | ||
| 420 : | * The optional Double Number word set:: | ||
| 421 : | * The optional Exception word set:: | ||
| 422 : | * The optional Facility word set:: | ||
| 423 : | * The optional File-Access word set:: | ||
| 424 : | * The optional Floating-Point word set:: | ||
| 425 : | * The optional Locals word set:: | ||
| 426 : | * The optional Memory-Allocation word set:: | ||
| 427 : | * The optional Programming-Tools word set:: | ||
| 428 : | * The optional Search-Order word set:: | ||
| 429 : | |||
| 430 : | The Core Words | ||
| 431 : | |||
| 432 : | * core-idef:: Implementation Defined Options | ||
| 433 : | * core-ambcond:: Ambiguous Conditions | ||
| 434 : | * core-other:: Other System Documentation | ||
| 435 : | |||
| 436 : | The optional Block word set | ||
| 437 : | |||
| 438 : | * block-idef:: Implementation Defined Options | ||
| 439 : | * block-ambcond:: Ambiguous Conditions | ||
| 440 : | * block-other:: Other System Documentation | ||
| 441 : | |||
| 442 : | The optional Double Number word set | ||
| 443 : | |||
| 444 : | * double-ambcond:: Ambiguous Conditions | ||
| 445 : | |||
| 446 : | The optional Exception word set | ||
| 447 : | |||
| 448 : | * exception-idef:: Implementation Defined Options | ||
| 449 : | |||
| 450 : | The optional Facility word set | ||
| 451 : | |||
| 452 : | * facility-idef:: Implementation Defined Options | ||
| 453 : | * facility-ambcond:: Ambiguous Conditions | ||
| 454 : | |||
| 455 : | The optional File-Access word set | ||
| 456 : | |||
| 457 : | * file-idef:: Implementation Defined Options | ||
| 458 : | * file-ambcond:: Ambiguous Conditions | ||
| 459 : | |||
| 460 : | The optional Floating-Point word set | ||
| 461 : | |||
| 462 : | * floating-idef:: Implementation Defined Options | ||
| 463 : | * floating-ambcond:: Ambiguous Conditions | ||
| 464 : | |||
| 465 : | The optional Locals word set | ||
| 466 : | |||
| 467 : | * locals-idef:: Implementation Defined Options | ||
| 468 : | * locals-ambcond:: Ambiguous Conditions | ||
| 469 : | |||
| 470 : | The optional Memory-Allocation word set | ||
| 471 : | |||
| 472 : | * memory-idef:: Implementation Defined Options | ||
| 473 : | |||
| 474 : | The optional Programming-Tools word set | ||
| 475 : | |||
| 476 : | * programming-idef:: Implementation Defined Options | ||
| 477 : | * programming-ambcond:: Ambiguous Conditions | ||
| 478 : | |||
| 479 : | The optional Search-Order word set | ||
| 480 : | |||
| 481 : | * search-idef:: Implementation Defined Options | ||
| 482 : | * search-ambcond:: Ambiguous Conditions | ||
| 483 : | |||
| 484 : | anton | 1.109 | Emacs and Gforth |
| 485 : | |||
| 486 : | * Installing gforth.el:: Making Emacs aware of Forth. | ||
| 487 : | * Emacs Tags:: Viewing the source of a word in Emacs. | ||
| 488 : | * Hilighting:: Making Forth code look prettier. | ||
| 489 : | * Auto-Indentation:: Customizing auto-indentation. | ||
| 490 : | * Blocks Files:: Reading and writing blocks files. | ||
| 491 : | |||
| 492 : | anton | 1.12 | Image Files |
| 493 : | |||
| 494 : | anton | 1.24 | * Image Licensing Issues:: Distribution terms for images. |
| 495 : | * Image File Background:: Why have image files? | ||
| 496 : | anton | 1.67 | * Non-Relocatable Image Files:: don't always work. |
| 497 : | anton | 1.24 | * Data-Relocatable Image Files:: are better. |
| 498 : | anton | 1.67 | * Fully Relocatable Image Files:: better yet. |
| 499 : | anton | 1.24 | * Stack and Dictionary Sizes:: Setting the default sizes for an image. |
| 500 : | anton | 1.32 | * Running Image Files:: @code{gforth -i @i{file}} or @i{file}. |
| 501 : | anton | 1.24 | * Modifying the Startup Sequence:: and turnkey applications. |
| 502 : | anton | 1.12 | |
| 503 : | Fully Relocatable Image Files | ||
| 504 : | |||
| 505 : | crook | 1.27 | * gforthmi:: The normal way |
| 506 : | anton | 1.12 | * cross.fs:: The hard way |
| 507 : | |||
| 508 : | Engine | ||
| 509 : | |||
| 510 : | * Portability:: | ||
| 511 : | * Threading:: | ||
| 512 : | * Primitives:: | ||
| 513 : | * Performance:: | ||
| 514 : | |||
| 515 : | Threading | ||
| 516 : | |||
| 517 : | * Scheduling:: | ||
| 518 : | * Direct or Indirect Threaded?:: | ||
| 519 : | anton | 1.109 | * Dynamic Superinstructions:: |
| 520 : | anton | 1.12 | * DOES>:: |
| 521 : | |||
| 522 : | Primitives | ||
| 523 : | |||
| 524 : | * Automatic Generation:: | ||
| 525 : | * TOS Optimization:: | ||
| 526 : | * Produced code:: | ||
| 527 : | pazsan | 1.13 | |
| 528 : | Cross Compiler | ||
| 529 : | |||
| 530 : | anton | 1.67 | * Using the Cross Compiler:: |
| 531 : | * How the Cross Compiler Works:: | ||
| 532 : | pazsan | 1.13 | |
| 533 : | anton | 1.113 | Licenses |
| 534 : | |||
| 535 : | * GNU Free Documentation License:: License for copying this manual. | ||
| 536 : | * Copying:: GPL (for copying this software). | ||
| 537 : | |||
| 538 : | anton | 1.24 | @end detailmenu |
| 539 : | anton | 1.1 | @end menu |
| 540 : | |||
| 541 : | anton | 1.113 | @c ---------------------------------------------------------- |
| 542 : | anton | 1.1 | @iftex |
| 543 : | @unnumbered Preface | ||
| 544 : | @cindex Preface | ||
| 545 : | crook | 1.21 | This manual documents Gforth. Some introductory material is provided for |
| 546 : | readers who are unfamiliar with Forth or who are migrating to Gforth | ||
| 547 : | from other Forth compilers. However, this manual is primarily a | ||
| 548 : | reference manual. | ||
| 549 : | anton | 1.1 | @end iftex |
| 550 : | |||
| 551 : | crook | 1.28 | @comment TODO much more blurb here. |
| 552 : | crook | 1.26 | |
| 553 : | @c ****************************************************************** | ||
| 554 : | anton | 1.113 | @node Goals, Gforth Environment, Top, Top |
| 555 : | crook | 1.26 | @comment node-name, next, previous, up |
| 556 : | @chapter Goals of Gforth | ||
| 557 : | @cindex goals of the Gforth project | ||
| 558 : | The goal of the Gforth Project is to develop a standard model for | ||
| 559 : | ANS Forth. This can be split into several subgoals: | ||
| 560 : | |||
| 561 : | @itemize @bullet | ||
| 562 : | @item | ||
| 563 : | Gforth should conform to the ANS Forth Standard. | ||
| 564 : | @item | ||
| 565 : | It should be a model, i.e. it should define all the | ||
| 566 : | implementation-dependent things. | ||
| 567 : | @item | ||
| 568 : | It should become standard, i.e. widely accepted and used. This goal | ||
| 569 : | is the most difficult one. | ||
| 570 : | @end itemize | ||
| 571 : | |||
| 572 : | To achieve these goals Gforth should be | ||
| 573 : | @itemize @bullet | ||
| 574 : | @item | ||
| 575 : | Similar to previous models (fig-Forth, F83) | ||
| 576 : | @item | ||
| 577 : | Powerful. It should provide for all the things that are considered | ||
| 578 : | necessary today and even some that are not yet considered necessary. | ||
| 579 : | @item | ||
| 580 : | Efficient. It should not get the reputation of being exceptionally | ||
| 581 : | slow. | ||
| 582 : | @item | ||
| 583 : | Free. | ||
| 584 : | @item | ||
| 585 : | Available on many machines/easy to port. | ||
| 586 : | @end itemize | ||
| 587 : | |||
| 588 : | Have we achieved these goals? Gforth conforms to the ANS Forth | ||
| 589 : | standard. It may be considered a model, but we have not yet documented | ||
| 590 : | which parts of the model are stable and which parts we are likely to | ||
| 591 : | change. It certainly has not yet become a de facto standard, but it | ||
| 592 : | appears to be quite popular. It has some similarities to and some | ||
| 593 : | differences from previous models. It has some powerful features, but not | ||
| 594 : | yet everything that we envisioned. We certainly have achieved our | ||
| 595 : | anton | 1.65 | execution speed goals (@pxref{Performance})@footnote{However, in 1998 |
| 596 : | the bar was raised when the major commercial Forth vendors switched to | ||
| 597 : | native code compilers.}. It is free and available on many machines. | ||
| 598 : | crook | 1.29 | |
| 599 : | crook | 1.26 | @c ****************************************************************** |
| 600 : | anton | 1.48 | @node Gforth Environment, Tutorial, Goals, Top |
| 601 : | crook | 1.29 | @chapter Gforth Environment |
| 602 : | @cindex Gforth environment | ||
| 603 : | crook | 1.21 | |
| 604 : | crook | 1.45 | Note: ultimately, the Gforth man page will be auto-generated from the |
| 605 : | crook | 1.29 | material in this chapter. |
| 606 : | crook | 1.21 | |
| 607 : | @menu | ||
| 608 : | crook | 1.29 | * Invoking Gforth:: Getting in |
| 609 : | * Leaving Gforth:: Getting out | ||
| 610 : | * Command-line editing:: | ||
| 611 : | anton | 1.48 | * Environment variables:: that affect how Gforth starts up |
| 612 : | crook | 1.29 | * Gforth Files:: What gets installed and where |
| 613 : | anton | 1.112 | * Gforth in pipes:: |
| 614 : | anton | 1.48 | * Startup speed:: When 35ms is not fast enough ... |
| 615 : | crook | 1.21 | @end menu |
| 616 : | |||
| 617 : | anton | 1.49 | For related information about the creation of images see @ref{Image Files}. |
| 618 : | crook | 1.29 | |
| 619 : | crook | 1.21 | @comment ---------------------------------------------- |
| 620 : | anton | 1.48 | @node Invoking Gforth, Leaving Gforth, Gforth Environment, Gforth Environment |
| 621 : | crook | 1.29 | @section Invoking Gforth |
| 622 : | @cindex invoking Gforth | ||
| 623 : | @cindex running Gforth | ||
| 624 : | @cindex command-line options | ||
| 625 : | @cindex options on the command line | ||
| 626 : | @cindex flags on the command line | ||
| 627 : | crook | 1.21 | |
| 628 : | anton | 1.30 | Gforth is made up of two parts; an executable ``engine'' (named |
| 629 : | anton | 1.109 | @command{gforth} or @command{gforth-fast}) and an image file. To start it, you |
| 630 : | anton | 1.30 | will usually just say @code{gforth} -- this automatically loads the |
| 631 : | default image file @file{gforth.fi}. In many other cases the default | ||
| 632 : | Gforth image will be invoked like this: | ||
| 633 : | crook | 1.21 | @example |
| 634 : | anton | 1.30 | gforth [file | -e forth-code] ... |
| 635 : | crook | 1.21 | @end example |
| 636 : | crook | 1.29 | @noindent |
| 637 : | This interprets the contents of the files and the Forth code in the order they | ||
| 638 : | are given. | ||
| 639 : | crook | 1.21 | |
| 640 : | anton | 1.109 | In addition to the @command{gforth} engine, there is also an engine |
| 641 : | called @command{gforth-fast}, which is faster, but gives less | ||
| 642 : | informative error messages (@pxref{Error messages}) and may catch some | ||
| 643 : | stack underflows later or not at all. You should use it for debugged, | ||
| 644 : | performance-critical programs. | ||
| 645 : | |||
| 646 : | Moreover, there is an engine called @command{gforth-itc}, which is | ||
| 647 : | useful in some backwards-compatibility situations (@pxref{Direct or | ||
| 648 : | Indirect Threaded?}). | ||
| 649 : | anton | 1.30 | |
| 650 : | crook | 1.29 | In general, the command line looks like this: |
| 651 : | crook | 1.21 | |
| 652 : | @example | ||
| 653 : | anton | 1.30 | gforth[-fast] [engine options] [image options] |
| 654 : | crook | 1.21 | @end example |
| 655 : | |||
| 656 : | anton | 1.30 | The engine options must come before the rest of the command |
| 657 : | crook | 1.29 | line. They are: |
| 658 : | crook | 1.26 | |
| 659 : | crook | 1.29 | @table @code |
| 660 : | @cindex -i, command-line option | ||
| 661 : | @cindex --image-file, command-line option | ||
| 662 : | @item --image-file @i{file} | ||
| 663 : | @itemx -i @i{file} | ||
| 664 : | Loads the Forth image @i{file} instead of the default | ||
| 665 : | @file{gforth.fi} (@pxref{Image Files}). | ||
| 666 : | crook | 1.21 | |
| 667 : | anton | 1.39 | @cindex --appl-image, command-line option |
| 668 : | @item --appl-image @i{file} | ||
| 669 : | Loads the image @i{file} and leaves all further command-line arguments | ||
| 670 : | anton | 1.65 | to the image (instead of processing them as engine options). This is |
| 671 : | useful for building executable application images on Unix, built with | ||
| 672 : | anton | 1.39 | @code{gforthmi --application ...}. |
| 673 : | |||
| 674 : | crook | 1.29 | @cindex --path, command-line option |
| 675 : | @cindex -p, command-line option | ||
| 676 : | @item --path @i{path} | ||
| 677 : | @itemx -p @i{path} | ||
| 678 : | Uses @i{path} for searching the image file and Forth source code files | ||
| 679 : | instead of the default in the environment variable @code{GFORTHPATH} or | ||
| 680 : | the path specified at installation time (e.g., | ||
| 681 : | @file{/usr/local/share/gforth/0.2.0:.}). A path is given as a list of | ||
| 682 : | directories, separated by @samp{:} (on Unix) or @samp{;} (on other OSs). | ||
| 683 : | crook | 1.21 | |
| 684 : | crook | 1.29 | @cindex --dictionary-size, command-line option |
| 685 : | @cindex -m, command-line option | ||
| 686 : | @cindex @i{size} parameters for command-line options | ||
| 687 : | @cindex size of the dictionary and the stacks | ||
| 688 : | @item --dictionary-size @i{size} | ||
| 689 : | @itemx -m @i{size} | ||
| 690 : | Allocate @i{size} space for the Forth dictionary space instead of | ||
| 691 : | using the default specified in the image (typically 256K). The | ||
| 692 : | @i{size} specification for this and subsequent options consists of | ||
| 693 : | an integer and a unit (e.g., | ||
| 694 : | @code{4M}). The unit can be one of @code{b} (bytes), @code{e} (element | ||
| 695 : | size, in this case Cells), @code{k} (kilobytes), @code{M} (Megabytes), | ||
| 696 : | @code{G} (Gigabytes), and @code{T} (Terabytes). If no unit is specified, | ||
| 697 : | @code{e} is used. | ||
| 698 : | crook | 1.21 | |
| 699 : | crook | 1.29 | @cindex --data-stack-size, command-line option |
| 700 : | @cindex -d, command-line option | ||
| 701 : | @item --data-stack-size @i{size} | ||
| 702 : | @itemx -d @i{size} | ||
| 703 : | Allocate @i{size} space for the data stack instead of using the | ||
| 704 : | default specified in the image (typically 16K). | ||
| 705 : | crook | 1.21 | |
| 706 : | crook | 1.29 | @cindex --return-stack-size, command-line option |
| 707 : | @cindex -r, command-line option | ||
| 708 : | @item --return-stack-size @i{size} | ||
| 709 : | @itemx -r @i{size} | ||
| 710 : | Allocate @i{size} space for the return stack instead of using the | ||
| 711 : | default specified in the image (typically 15K). | ||
| 712 : | crook | 1.21 | |
| 713 : | crook | 1.29 | @cindex --fp-stack-size, command-line option |
| 714 : | @cindex -f, command-line option | ||
| 715 : | @item --fp-stack-size @i{size} | ||
| 716 : | @itemx -f @i{size} | ||
| 717 : | Allocate @i{size} space for the floating point stack instead of | ||
| 718 : | using the default specified in the image (typically 15.5K). In this case | ||
| 719 : | the unit specifier @code{e} refers to floating point numbers. | ||
| 720 : | crook | 1.21 | |
| 721 : | anton | 1.48 | @cindex --locals-stack-size, command-line option |
| 722 : | @cindex -l, command-line option | ||
| 723 : | @item --locals-stack-size @i{size} | ||
| 724 : | @itemx -l @i{size} | ||
| 725 : | Allocate @i{size} space for the locals stack instead of using the | ||
| 726 : | default specified in the image (typically 14.5K). | ||
| 727 : | |||
| 728 : | @cindex -h, command-line option | ||
| 729 : | @cindex --help, command-line option | ||
| 730 : | @item --help | ||
| 731 : | @itemx -h | ||
| 732 : | Print a message about the command-line options | ||
| 733 : | |||
| 734 : | @cindex -v, command-line option | ||
| 735 : | @cindex --version, command-line option | ||
| 736 : | @item --version | ||
| 737 : | @itemx -v | ||
| 738 : | Print version and exit | ||
| 739 : | |||
| 740 : | @cindex --debug, command-line option | ||
| 741 : | @item --debug | ||
| 742 : | Print some information useful for debugging on startup. | ||
| 743 : | |||
| 744 : | @cindex --offset-image, command-line option | ||
| 745 : | @item --offset-image | ||
| 746 : | Start the dictionary at a slightly different position than would be used | ||
| 747 : | otherwise (useful for creating data-relocatable images, | ||
| 748 : | @pxref{Data-Relocatable Image Files}). | ||
| 749 : | |||
| 750 : | @cindex --no-offset-im, command-line option | ||
| 751 : | @item --no-offset-im | ||
| 752 : | Start the dictionary at the normal position. | ||
| 753 : | |||
| 754 : | @cindex --clear-dictionary, command-line option | ||
| 755 : | @item --clear-dictionary | ||
| 756 : | Initialize all bytes in the dictionary to 0 before loading the image | ||
| 757 : | (@pxref{Data-Relocatable Image Files}). | ||
| 758 : | |||
| 759 : | @cindex --die-on-signal, command-line-option | ||
| 760 : | @item --die-on-signal | ||
| 761 : | Normally Gforth handles most signals (e.g., the user interrupt SIGINT, | ||
| 762 : | or the segmentation violation SIGSEGV) by translating it into a Forth | ||
| 763 : | @code{THROW}. With this option, Gforth exits if it receives such a | ||
| 764 : | signal. This option is useful when the engine and/or the image might be | ||
| 765 : | severely broken (such that it causes another signal before recovering | ||
| 766 : | from the first); this option avoids endless loops in such cases. | ||
| 767 : | anton | 1.109 | |
| 768 : | anton | 1.119 | @cindex --no-dynamic, command-line option |
| 769 : | @cindex --dynamic, command-line option | ||
| 770 : | anton | 1.109 | @item --no-dynamic |
| 771 : | @item --dynamic | ||
| 772 : | Disable or enable dynamic superinstructions with replication | ||
| 773 : | (@pxref{Dynamic Superinstructions}). | ||
| 774 : | |||
| 775 : | anton | 1.119 | @cindex --no-super, command-line option |
| 776 : | anton | 1.109 | @item --no-super |
| 777 : | anton | 1.110 | Disable dynamic superinstructions, use just dynamic replication; this is |
| 778 : | useful if you want to patch threaded code (@pxref{Dynamic | ||
| 779 : | Superinstructions}). | ||
| 780 : | anton | 1.119 | |
| 781 : | @cindex --ss-number, command-line option | ||
| 782 : | @item --ss-number=@var{N} | ||
| 783 : | Use only the first @var{N} static superinstructions compiled into the | ||
| 784 : | engine (default: use them all; note that only @code{gforth-fast} has | ||
| 785 : | any). This option is useful for measuring the performance impact of | ||
| 786 : | static superinstructions. | ||
| 787 : | |||
| 788 : | @cindex --ss-min-..., command-line options | ||
| 789 : | @item --ss-min-codesize | ||
| 790 : | @item --ss-min-ls | ||
| 791 : | @item --ss-min-lsu | ||
| 792 : | @item --ss-min-nexts | ||
| 793 : | Use specified metric for determining the cost of a primitive or static | ||
| 794 : | superinstruction for static superinstruction selection. @code{Codesize} | ||
| 795 : | is the native code size of the primive or static superinstruction, | ||
| 796 : | @code{ls} is the number of loads and stores, @code{lsu} is the number of | ||
| 797 : | loads, stores, and updates, and @code{nexts} is the number of dispatches | ||
| 798 : | (not taking dynamic superinstructions into account), i.e. every | ||
| 799 : | primitive or static superinstruction has cost 1. Default: | ||
| 800 : | @code{codesize} if you use dynamic code generation, otherwise | ||
| 801 : | @code{nexts}. | ||
| 802 : | |||
| 803 : | @cindex --ss-greedy, command-line option | ||
| 804 : | @item --ss-greedy | ||
| 805 : | This option is useful for measuring the performance impact of static | ||
| 806 : | superinstructions. By default, an optimal shortest-path algorithm is | ||
| 807 : | used for selecting static superinstructions. With @option{--ss-greedy} | ||
| 808 : | this algorithm is modified to assume that anything after the static | ||
| 809 : | superinstruction currently under consideration is not combined into | ||
| 810 : | static superinstructions. With @option{--ss-min-nexts} this produces | ||
| 811 : | the same result as a greedy algorithm that always selects the longest | ||
| 812 : | superinstruction available at the moment. E.g., if there are | ||
| 813 : | superinstructions AB and BCD, then for the sequence A B C D the optimal | ||
| 814 : | algorithm will select A BCD and the greedy algorithm will select AB C D. | ||
| 815 : | |||
| 816 : | @cindex --print-metrics, command-line option | ||
| 817 : | @item --print-metrics | ||
| 818 : | Prints some metrics used during static superinstruction selection: | ||
| 819 : | @code{code size} is the actual size of the dynamically generated code. | ||
| 820 : | @code{Metric codesize} is the sum of the codesize metrics as seen by | ||
| 821 : | static superinstruction selection; there is a difference from @code{code | ||
| 822 : | size}, because not all primitives and static superinstructions are | ||
| 823 : | compiled into dynamically generated code, and because of markers. The | ||
| 824 : | other metrics correspond to the @option{ss-min-...} options. This | ||
| 825 : | option is useful for evaluating the effects of the @option{--ss-...} | ||
| 826 : | options. | ||
| 827 : | anton | 1.109 | |
| 828 : | anton | 1.48 | @end table |
| 829 : | |||
| 830 : | @cindex loading files at startup | ||
| 831 : | @cindex executing code on startup | ||
| 832 : | @cindex batch processing with Gforth | ||
| 833 : | As explained above, the image-specific command-line arguments for the | ||
| 834 : | default image @file{gforth.fi} consist of a sequence of filenames and | ||
| 835 : | @code{-e @var{forth-code}} options that are interpreted in the sequence | ||
| 836 : | in which they are given. The @code{-e @var{forth-code}} or | ||
| 837 : | anton | 1.121 | @code{--evaluate @var{forth-code}} option evaluates the Forth code. This |
| 838 : | option takes only one argument; if you want to evaluate more Forth | ||
| 839 : | words, you have to quote them or use @code{-e} several times. To exit | ||
| 840 : | anton | 1.48 | after processing the command line (instead of entering interactive mode) |
| 841 : | anton | 1.121 | append @code{-e bye} to the command line. You can also process the |
| 842 : | command-line arguments with a Forth program (@pxref{OS command line | ||
| 843 : | arguments}). | ||
| 844 : | anton | 1.48 | |
| 845 : | @cindex versions, invoking other versions of Gforth | ||
| 846 : | If you have several versions of Gforth installed, @code{gforth} will | ||
| 847 : | invoke the version that was installed last. @code{gforth-@i{version}} | ||
| 848 : | invokes a specific version. If your environment contains the variable | ||
| 849 : | @code{GFORTHPATH}, you may want to override it by using the | ||
| 850 : | @code{--path} option. | ||
| 851 : | |||
| 852 : | Not yet implemented: | ||
| 853 : | On startup the system first executes the system initialization file | ||
| 854 : | (unless the option @code{--no-init-file} is given; note that the system | ||
| 855 : | resulting from using this option may not be ANS Forth conformant). Then | ||
| 856 : | the user initialization file @file{.gforth.fs} is executed, unless the | ||
| 857 : | crook | 1.62 | option @code{--no-rc} is given; this file is searched for in @file{.}, |
| 858 : | anton | 1.48 | then in @file{~}, then in the normal path (see above). |
| 859 : | |||
| 860 : | |||
| 861 : | |||
| 862 : | @comment ---------------------------------------------- | ||
| 863 : | @node Leaving Gforth, Command-line editing, Invoking Gforth, Gforth Environment | ||
| 864 : | @section Leaving Gforth | ||
| 865 : | @cindex Gforth - leaving | ||
| 866 : | @cindex leaving Gforth | ||
| 867 : | |||
| 868 : | You can leave Gforth by typing @code{bye} or @kbd{Ctrl-d} (at the start | ||
| 869 : | of a line) or (if you invoked Gforth with the @code{--die-on-signal} | ||
| 870 : | option) @kbd{Ctrl-c}. When you leave Gforth, all of your definitions and | ||
| 871 : | anton | 1.49 | data are discarded. For ways of saving the state of the system before |
| 872 : | leaving Gforth see @ref{Image Files}. | ||
| 873 : | anton | 1.48 | |
| 874 : | doc-bye | ||
| 875 : | |||
| 876 : | |||
| 877 : | @comment ---------------------------------------------- | ||
| 878 : | anton | 1.65 | @node Command-line editing, Environment variables, Leaving Gforth, Gforth Environment |
| 879 : | anton | 1.48 | @section Command-line editing |
| 880 : | @cindex command-line editing | ||
| 881 : | |||
| 882 : | Gforth maintains a history file that records every line that you type to | ||
| 883 : | the text interpreter. This file is preserved between sessions, and is | ||
| 884 : | used to provide a command-line recall facility; if you type @kbd{Ctrl-P} | ||
| 885 : | repeatedly you can recall successively older commands from this (or | ||
| 886 : | previous) session(s). The full list of command-line editing facilities is: | ||
| 887 : | |||
| 888 : | @itemize @bullet | ||
| 889 : | @item | ||
| 890 : | @kbd{Ctrl-p} (``previous'') (or up-arrow) to recall successively older | ||
| 891 : | commands from the history buffer. | ||
| 892 : | @item | ||
| 893 : | @kbd{Ctrl-n} (``next'') (or down-arrow) to recall successively newer commands | ||
| 894 : | from the history buffer. | ||
| 895 : | @item | ||
| 896 : | @kbd{Ctrl-f} (or right-arrow) to move the cursor right, non-destructively. | ||
| 897 : | @item | ||
| 898 : | @kbd{Ctrl-b} (or left-arrow) to move the cursor left, non-destructively. | ||
| 899 : | @item | ||
| 900 : | @kbd{Ctrl-h} (backspace) to delete the character to the left of the cursor, | ||
| 901 : | closing up the line. | ||
| 902 : | @item | ||
| 903 : | @kbd{Ctrl-k} to delete (``kill'') from the cursor to the end of the line. | ||
| 904 : | @item | ||
| 905 : | @kbd{Ctrl-a} to move the cursor to the start of the line. | ||
| 906 : | @item | ||
| 907 : | @kbd{Ctrl-e} to move the cursor to the end of the line. | ||
| 908 : | @item | ||
| 909 : | @key{RET} (@kbd{Ctrl-m}) or @key{LFD} (@kbd{Ctrl-j}) to submit the current | ||
| 910 : | line. | ||
| 911 : | @item | ||
| 912 : | @key{TAB} to step through all possible full-word completions of the word | ||
| 913 : | currently being typed. | ||
| 914 : | @item | ||
| 915 : | anton | 1.65 | @kbd{Ctrl-d} on an empty line line to terminate Gforth (gracefully, |
| 916 : | using @code{bye}). | ||
| 917 : | @item | ||
| 918 : | @kbd{Ctrl-x} (or @code{Ctrl-d} on a non-empty line) to delete the | ||
| 919 : | character under the cursor. | ||
| 920 : | anton | 1.48 | @end itemize |
| 921 : | |||
| 922 : | When editing, displayable characters are inserted to the left of the | ||
| 923 : | cursor position; the line is always in ``insert'' (as opposed to | ||
| 924 : | ``overstrike'') mode. | ||
| 925 : | |||
| 926 : | @cindex history file | ||
| 927 : | @cindex @file{.gforth-history} | ||
| 928 : | On Unix systems, the history file is @file{~/.gforth-history} by | ||
| 929 : | default@footnote{i.e. it is stored in the user's home directory.}. You | ||
| 930 : | can find out the name and location of your history file using: | ||
| 931 : | |||
| 932 : | @example | ||
| 933 : | history-file type \ Unix-class systems | ||
| 934 : | |||
| 935 : | history-file type \ Other systems | ||
| 936 : | history-dir type | ||
| 937 : | @end example | ||
| 938 : | |||
| 939 : | If you enter long definitions by hand, you can use a text editor to | ||
| 940 : | paste them out of the history file into a Forth source file for reuse at | ||
| 941 : | a later time. | ||
| 942 : | |||
| 943 : | Gforth never trims the size of the history file, so you should do this | ||
| 944 : | periodically, if necessary. | ||
| 945 : | |||
| 946 : | @comment this is all defined in history.fs | ||
| 947 : | @comment NAC TODO the ctrl-D behaviour can either do a bye or a beep.. how is that option | ||
| 948 : | @comment chosen? | ||
| 949 : | |||
| 950 : | |||
| 951 : | @comment ---------------------------------------------- | ||
| 952 : | anton | 1.65 | @node Environment variables, Gforth Files, Command-line editing, Gforth Environment |
| 953 : | anton | 1.48 | @section Environment variables |
| 954 : | @cindex environment variables | ||
| 955 : | |||
| 956 : | Gforth uses these environment variables: | ||
| 957 : | |||
| 958 : | @itemize @bullet | ||
| 959 : | @item | ||
| 960 : | @cindex @code{GFORTHHIST} -- environment variable | ||
| 961 : | @code{GFORTHHIST} -- (Unix systems only) specifies the directory in which to | ||
| 962 : | open/create the history file, @file{.gforth-history}. Default: | ||
| 963 : | @code{$HOME}. | ||
| 964 : | |||
| 965 : | @item | ||
| 966 : | @cindex @code{GFORTHPATH} -- environment variable | ||
| 967 : | @code{GFORTHPATH} -- specifies the path used when searching for the gforth image file and | ||
| 968 : | for Forth source-code files. | ||
| 969 : | |||
| 970 : | @item | ||
| 971 : | anton | 1.129 | @cindex @code{GFORTHSYSTEMPREFIX} -- environment variable |
| 972 : | |||
| 973 : | @code{GFORTHSYSTEMPREFIX} -- specifies what to prepend to the argument | ||
| 974 : | of @code{system} before passing it to C's @code{system()}. Default: | ||
| 975 : | anton | 1.130 | @code{"./$COMSPEC /c "} on Windows, @code{""} on other OSs. The prefix |
| 976 : | anton | 1.129 | and the command are directly concatenated, so if a space between them is |
| 977 : | necessary, append it to the prefix. | ||
| 978 : | |||
| 979 : | @item | ||
| 980 : | anton | 1.48 | @cindex @code{GFORTH} -- environment variable |
| 981 : | anton | 1.49 | @code{GFORTH} -- used by @file{gforthmi}, @xref{gforthmi}. |
| 982 : | anton | 1.48 | |
| 983 : | @item | ||
| 984 : | @cindex @code{GFORTHD} -- environment variable | ||
| 985 : | crook | 1.62 | @code{GFORTHD} -- used by @file{gforthmi}, @xref{gforthmi}. |
| 986 : | anton | 1.48 | |
| 987 : | @item | ||
| 988 : | @cindex @code{TMP}, @code{TEMP} - environment variable | ||
| 989 : | @code{TMP}, @code{TEMP} - (non-Unix systems only) used as a potential | ||
| 990 : | location for the history file. | ||
| 991 : | @end itemize | ||
| 992 : | |||
| 993 : | @comment also POSIXELY_CORRECT LINES COLUMNS HOME but no interest in | ||
| 994 : | @comment mentioning these. | ||
| 995 : | |||
| 996 : | All the Gforth environment variables default to sensible values if they | ||
| 997 : | are not set. | ||
| 998 : | |||
| 999 : | |||
| 1000 : | @comment ---------------------------------------------- | ||
| 1001 : | anton | 1.112 | @node Gforth Files, Gforth in pipes, Environment variables, Gforth Environment |
| 1002 : | anton | 1.48 | @section Gforth files |
| 1003 : | @cindex Gforth files | ||
| 1004 : | |||
| 1005 : | When you install Gforth on a Unix system, it installs files in these | ||
| 1006 : | locations by default: | ||
| 1007 : | |||
| 1008 : | @itemize @bullet | ||
| 1009 : | @item | ||
| 1010 : | @file{/usr/local/bin/gforth} | ||
| 1011 : | @item | ||
| 1012 : | @file{/usr/local/bin/gforthmi} | ||
| 1013 : | @item | ||
| 1014 : | @file{/usr/local/man/man1/gforth.1} - man page. | ||
| 1015 : | @item | ||
| 1016 : | @file{/usr/local/info} - the Info version of this manual. | ||
| 1017 : | @item | ||
| 1018 : | @file{/usr/local/lib/gforth/<version>/...} - Gforth @file{.fi} files. | ||
| 1019 : | @item | ||
| 1020 : | @file{/usr/local/share/gforth/<version>/TAGS} - Emacs TAGS file. | ||
| 1021 : | @item | ||
| 1022 : | @file{/usr/local/share/gforth/<version>/...} - Gforth source files. | ||
| 1023 : | @item | ||
| 1024 : | @file{.../emacs/site-lisp/gforth.el} - Emacs gforth mode. | ||
| 1025 : | @end itemize | ||
| 1026 : | |||
| 1027 : | You can select different places for installation by using | ||
| 1028 : | @code{configure} options (listed with @code{configure --help}). | ||
| 1029 : | |||
| 1030 : | @comment ---------------------------------------------- | ||
| 1031 : | anton | 1.112 | @node Gforth in pipes, Startup speed, Gforth Files, Gforth Environment |
| 1032 : | @section Gforth in pipes | ||
| 1033 : | @cindex pipes, Gforth as part of | ||
| 1034 : | |||
| 1035 : | Gforth can be used in pipes created elsewhere (described here). It can | ||
| 1036 : | also create pipes on its own (@pxref{Pipes}). | ||
| 1037 : | |||
| 1038 : | @cindex input from pipes | ||
| 1039 : | If you pipe into Gforth, your program should read with @code{read-file} | ||
| 1040 : | or @code{read-line} from @code{stdin} (@pxref{General files}). | ||
| 1041 : | @code{Key} does not recognize the end of input. Words like | ||
| 1042 : | @code{accept} echo the input and are therefore usually not useful for | ||
| 1043 : | reading from a pipe. You have to invoke the Forth program with an OS | ||
| 1044 : | command-line option, as you have no chance to use the Forth command line | ||
| 1045 : | (the text interpreter would try to interpret the pipe input). | ||
| 1046 : | |||
| 1047 : | @cindex output in pipes | ||
| 1048 : | You can output to a pipe with @code{type}, @code{emit}, @code{cr} etc. | ||
| 1049 : | |||
| 1050 : | @cindex silent exiting from Gforth | ||
| 1051 : | When you write to a pipe that has been closed at the other end, Gforth | ||
| 1052 : | receives a SIGPIPE signal (``pipe broken''). Gforth translates this | ||
| 1053 : | into the exception @code{broken-pipe-error}. If your application does | ||
| 1054 : | not catch that exception, the system catches it and exits, usually | ||
| 1055 : | silently (unless you were working on the Forth command line; then it | ||
| 1056 : | prints an error message and exits). This is usually the desired | ||
| 1057 : | behaviour. | ||
| 1058 : | |||
| 1059 : | If you do not like this behaviour, you have to catch the exception | ||
| 1060 : | yourself, and react to it. | ||
| 1061 : | |||
| 1062 : | Here's an example of an invocation of Gforth that is usable in a pipe: | ||
| 1063 : | |||
| 1064 : | @example | ||
| 1065 : | gforth -e ": foo begin pad dup 10 stdin read-file throw dup while \ | ||
| 1066 : | type repeat ; foo bye" | ||
| 1067 : | @end example | ||
| 1068 : | |||
| 1069 : | This example just copies the input verbatim to the output. A very | ||
| 1070 : | simple pipe containing this example looks like this: | ||
| 1071 : | |||
| 1072 : | @example | ||
| 1073 : | cat startup.fs | | ||
| 1074 : | gforth -e ": foo begin pad dup 80 stdin read-file throw dup while \ | ||
| 1075 : | type repeat ; foo bye"| | ||
| 1076 : | head | ||
| 1077 : | @end example | ||
| 1078 : | |||
| 1079 : | @cindex stderr and pipes | ||
| 1080 : | Pipes involving Gforth's @code{stderr} output do not work. | ||
| 1081 : | |||
| 1082 : | @comment ---------------------------------------------- | ||
| 1083 : | @node Startup speed, , Gforth in pipes, Gforth Environment | ||
| 1084 : | anton | 1.48 | @section Startup speed |
| 1085 : | @cindex Startup speed | ||
| 1086 : | @cindex speed, startup | ||
| 1087 : | |||
| 1088 : | If Gforth is used for CGI scripts or in shell scripts, its startup | ||
| 1089 : | speed may become a problem. On a 300MHz 21064a under Linux-2.2.13 with | ||
| 1090 : | glibc-2.0.7, @code{gforth -e bye} takes about 24.6ms user and 11.3ms | ||
| 1091 : | system time. | ||
| 1092 : | |||
| 1093 : | If startup speed is a problem, you may consider the following ways to | ||
| 1094 : | improve it; or you may consider ways to reduce the number of startups | ||
| 1095 : | crook | 1.62 | (for example, by using Fast-CGI). |
| 1096 : | anton | 1.48 | |
| 1097 : | anton | 1.112 | An easy step that influences Gforth startup speed is the use of the |
| 1098 : | @option{--no-dynamic} option; this decreases image loading speed, but | ||
| 1099 : | increases compile-time and run-time. | ||
| 1100 : | |||
| 1101 : | Another step to improve startup speed is to statically link Gforth, by | ||
| 1102 : | anton | 1.48 | building it with @code{XLDFLAGS=-static}. This requires more memory for |
| 1103 : | the code and will therefore slow down the first invocation, but | ||
| 1104 : | subsequent invocations avoid the dynamic linking overhead. Another | ||
| 1105 : | disadvantage is that Gforth won't profit from library upgrades. As a | ||
| 1106 : | result, @code{gforth-static -e bye} takes about 17.1ms user and | ||
| 1107 : | 8.2ms system time. | ||
| 1108 : | |||
| 1109 : | The next step to improve startup speed is to use a non-relocatable image | ||
| 1110 : | anton | 1.65 | (@pxref{Non-Relocatable Image Files}). You can create this image with |
| 1111 : | anton | 1.48 | @code{gforth -e "savesystem gforthnr.fi bye"} and later use it with |
| 1112 : | @code{gforth -i gforthnr.fi ...}. This avoids the relocation overhead | ||
| 1113 : | and a part of the copy-on-write overhead. The disadvantage is that the | ||
| 1114 : | crook | 1.62 | non-relocatable image does not work if the OS gives Gforth a different |
| 1115 : | anton | 1.48 | address for the dictionary, for whatever reason; so you better provide a |
| 1116 : | fallback on a relocatable image. @code{gforth-static -i gforthnr.fi -e | ||
| 1117 : | bye} takes about 15.3ms user and 7.5ms system time. | ||
| 1118 : | |||
| 1119 : | The final step is to disable dictionary hashing in Gforth. Gforth | ||
| 1120 : | builds the hash table on startup, which takes much of the startup | ||
| 1121 : | overhead. You can do this by commenting out the @code{include hash.fs} | ||
| 1122 : | in @file{startup.fs} and everything that requires @file{hash.fs} (at the | ||
| 1123 : | moment @file{table.fs} and @file{ekey.fs}) and then doing @code{make}. | ||
| 1124 : | The disadvantages are that functionality like @code{table} and | ||
| 1125 : | @code{ekey} is missing and that text interpretation (e.g., compiling) | ||
| 1126 : | now takes much longer. So, you should only use this method if there is | ||
| 1127 : | no significant text interpretation to perform (the script should be | ||
| 1128 : | crook | 1.62 | compiled into the image, amongst other things). @code{gforth-static -i |
| 1129 : | anton | 1.48 | gforthnrnh.fi -e bye} takes about 2.1ms user and 6.1ms system time. |
| 1130 : | |||
| 1131 : | @c ****************************************************************** | ||
| 1132 : | @node Tutorial, Introduction, Gforth Environment, Top | ||
| 1133 : | @chapter Forth Tutorial | ||
| 1134 : | @cindex Tutorial | ||
| 1135 : | @cindex Forth Tutorial | ||
| 1136 : | |||
| 1137 : | anton | 1.67 | @c Topics from nac's Introduction that could be mentioned: |
| 1138 : | @c press <ret> after each line | ||
| 1139 : | @c Prompt | ||
| 1140 : | @c numbers vs. words in dictionary on text interpretation | ||
| 1141 : | @c what happens on redefinition | ||
| 1142 : | @c parsing words (in particular, defining words) | ||
| 1143 : | |||
| 1144 : | anton | 1.83 | The difference of this chapter from the Introduction |
| 1145 : | (@pxref{Introduction}) is that this tutorial is more fast-paced, should | ||
| 1146 : | be used while sitting in front of a computer, and covers much more | ||
| 1147 : | material, but does not explain how the Forth system works. | ||
| 1148 : | |||
| 1149 : | crook | 1.62 | This tutorial can be used with any ANS-compliant Forth; any |
| 1150 : | Gforth-specific features are marked as such and you can skip them if you | ||
| 1151 : | work with another Forth. This tutorial does not explain all features of | ||
| 1152 : | Forth, just enough to get you started and give you some ideas about the | ||
| 1153 : | facilities available in Forth. Read the rest of the manual and the | ||
| 1154 : | standard when you are through this. | ||
| 1155 : | anton | 1.48 | |
| 1156 : | The intended way to use this tutorial is that you work through it while | ||
| 1157 : | sitting in front of the console, take a look at the examples and predict | ||
| 1158 : | what they will do, then try them out; if the outcome is not as expected, | ||
| 1159 : | find out why (e.g., by trying out variations of the example), so you | ||
| 1160 : | understand what's going on. There are also some assignments that you | ||
| 1161 : | should solve. | ||
| 1162 : | |||
| 1163 : | This tutorial assumes that you have programmed before and know what, | ||
| 1164 : | e.g., a loop is. | ||
| 1165 : | |||
| 1166 : | @c !! explain compat library | ||
| 1167 : | |||
| 1168 : | @menu | ||
| 1169 : | * Starting Gforth Tutorial:: | ||
| 1170 : | * Syntax Tutorial:: | ||
| 1171 : | * Crash Course Tutorial:: | ||
| 1172 : | * Stack Tutorial:: | ||
| 1173 : | * Arithmetics Tutorial:: | ||
| 1174 : | * Stack Manipulation Tutorial:: | ||
| 1175 : | * Using files for Forth code Tutorial:: | ||
| 1176 : | * Comments Tutorial:: | ||
| 1177 : | * Colon Definitions Tutorial:: | ||
| 1178 : | * Decompilation Tutorial:: | ||
| 1179 : | * Stack-Effect Comments Tutorial:: | ||
| 1180 : | * Types Tutorial:: | ||
| 1181 : | * Factoring Tutorial:: | ||
| 1182 : | * Designing the stack effect Tutorial:: | ||
| 1183 : | * Local Variables Tutorial:: | ||
| 1184 : | * Conditional execution Tutorial:: | ||
| 1185 : | * Flags and Comparisons Tutorial:: | ||
| 1186 : | * General Loops Tutorial:: | ||
| 1187 : | * Counted loops Tutorial:: | ||
| 1188 : | * Recursion Tutorial:: | ||
| 1189 : | * Leaving definitions or loops Tutorial:: | ||
| 1190 : | * Return Stack Tutorial:: | ||
| 1191 : | * Memory Tutorial:: | ||
| 1192 : | * Characters and Strings Tutorial:: | ||
| 1193 : | * Alignment Tutorial:: | ||
| 1194 : | anton | 1.87 | * Files Tutorial:: |
| 1195 : | anton | 1.48 | * Interpretation and Compilation Semantics and Immediacy Tutorial:: |
| 1196 : | * Execution Tokens Tutorial:: | ||
| 1197 : | * Exceptions Tutorial:: | ||
| 1198 : | * Defining Words Tutorial:: | ||
| 1199 : | * Arrays and Records Tutorial:: | ||
| 1200 : | * POSTPONE Tutorial:: | ||
| 1201 : | * Literal Tutorial:: | ||
| 1202 : | * Advanced macros Tutorial:: | ||
| 1203 : | * Compilation Tokens Tutorial:: | ||
| 1204 : | * Wordlists and Search Order Tutorial:: | ||
| 1205 : | @end menu | ||
| 1206 : | |||
| 1207 : | @node Starting Gforth Tutorial, Syntax Tutorial, Tutorial, Tutorial | ||
| 1208 : | @section Starting Gforth | ||
| 1209 : | anton | 1.66 | @cindex starting Gforth tutorial |
| 1210 : | anton | 1.48 | You can start Gforth by typing its name: |
| 1211 : | |||
| 1212 : | @example | ||
| 1213 : | gforth | ||
| 1214 : | @end example | ||
| 1215 : | |||
| 1216 : | That puts you into interactive mode; you can leave Gforth by typing | ||
| 1217 : | @code{bye}. While in Gforth, you can edit the command line and access | ||
| 1218 : | the command line history with cursor keys, similar to bash. | ||
| 1219 : | |||
| 1220 : | |||
| 1221 : | @node Syntax Tutorial, Crash Course Tutorial, Starting Gforth Tutorial, Tutorial | ||
| 1222 : | @section Syntax | ||
| 1223 : | anton | 1.66 | @cindex syntax tutorial |
| 1224 : | anton | 1.48 | |
| 1225 : | A @dfn{word} is a sequence of arbitrary characters (expcept white | ||
| 1226 : | space). Words are separated by white space. E.g., each of the | ||
| 1227 : | following lines contains exactly one word: | ||
| 1228 : | |||
| 1229 : | @example | ||
| 1230 : | word | ||
| 1231 : | !@@#$%^&*() | ||
| 1232 : | 1234567890 | ||
| 1233 : | 5!a | ||
| 1234 : | @end example | ||
| 1235 : | |||
| 1236 : | A frequent beginner's error is to leave away necessary white space, | ||
| 1237 : | resulting in an error like @samp{Undefined word}; so if you see such an | ||
| 1238 : | error, check if you have put spaces wherever necessary. | ||
| 1239 : | |||
| 1240 : | @example | ||
| 1241 : | ." hello, world" \ correct | ||
| 1242 : | ."hello, world" \ gives an "Undefined word" error | ||
| 1243 : | @end example | ||
| 1244 : | |||
| 1245 : | anton | 1.65 | Gforth and most other Forth systems ignore differences in case (they are |
| 1246 : | anton | 1.48 | case-insensitive), i.e., @samp{word} is the same as @samp{Word}. If |
| 1247 : | your system is case-sensitive, you may have to type all the examples | ||
| 1248 : | given here in upper case. | ||
| 1249 : | |||
| 1250 : | |||
| 1251 : | @node Crash Course Tutorial, Stack Tutorial, Syntax Tutorial, Tutorial | ||
| 1252 : | @section Crash Course | ||
| 1253 : | |||
| 1254 : | Type | ||
| 1255 : | |||
| 1256 : | @example | ||
| 1257 : | 0 0 ! | ||
| 1258 : | here execute | ||
| 1259 : | ' catch >body 20 erase abort | ||
| 1260 : | ' (quit) >body 20 erase | ||
| 1261 : | @end example | ||
| 1262 : | |||
| 1263 : | The last two examples are guaranteed to destroy parts of Gforth (and | ||
| 1264 : | most other systems), so you better leave Gforth afterwards (if it has | ||
| 1265 : | not finished by itself). On some systems you may have to kill gforth | ||
| 1266 : | from outside (e.g., in Unix with @code{kill}). | ||
| 1267 : | |||
| 1268 : | Now that you know how to produce crashes (and that there's not much to | ||
| 1269 : | them), let's learn how to produce meaningful programs. | ||
| 1270 : | |||
| 1271 : | |||
| 1272 : | @node Stack Tutorial, Arithmetics Tutorial, Crash Course Tutorial, Tutorial | ||
| 1273 : | @section Stack | ||
| 1274 : | anton | 1.66 | @cindex stack tutorial |
| 1275 : | anton | 1.48 | |
| 1276 : | The most obvious feature of Forth is the stack. When you type in a | ||
| 1277 : | number, it is pushed on the stack. You can display the content of the | ||
| 1278 : | stack with @code{.s}. | ||
| 1279 : | |||
| 1280 : | @example | ||
| 1281 : | 1 2 .s | ||
| 1282 : | 3 .s | ||
| 1283 : | @end example | ||
| 1284 : | |||
| 1285 : | @code{.s} displays the top-of-stack to the right, i.e., the numbers | ||
| 1286 : | appear in @code{.s} output as they appeared in the input. | ||
| 1287 : | |||
| 1288 : | You can print the top of stack element with @code{.}. | ||
| 1289 : | |||
| 1290 : | @example | ||
| 1291 : | 1 2 3 . . . | ||
| 1292 : | @end example | ||
| 1293 : | |||
| 1294 : | In general, words consume their stack arguments (@code{.s} is an | ||
| 1295 : | exception). | ||
| 1296 : | |||
| 1297 : | @assignment | ||
| 1298 : | What does the stack contain after @code{5 6 7 .}? | ||
| 1299 : | @endassignment | ||
| 1300 : | |||
| 1301 : | |||
| 1302 : | @node Arithmetics Tutorial, Stack Manipulation Tutorial, Stack Tutorial, Tutorial | ||
| 1303 : | @section Arithmetics | ||
| 1304 : | anton | 1.66 | @cindex arithmetics tutorial |
| 1305 : | anton | 1.48 | |
| 1306 : | The words @code{+}, @code{-}, @code{*}, @code{/}, and @code{mod} always | ||
| 1307 : | operate on the top two stack items: | ||
| 1308 : | |||
| 1309 : | @example | ||
| 1310 : | anton | 1.67 | 2 2 .s |
| 1311 : | + .s | ||
| 1312 : | . | ||
| 1313 : | anton | 1.48 | 2 1 - . |
| 1314 : | 7 3 mod . | ||
| 1315 : | @end example | ||
| 1316 : | |||
| 1317 : | The operands of @code{-}, @code{/}, and @code{mod} are in the same order | ||
| 1318 : | as in the corresponding infix expression (this is generally the case in | ||
| 1319 : | Forth). | ||
| 1320 : | |||
| 1321 : | Parentheses are superfluous (and not available), because the order of | ||
| 1322 : | the words unambiguously determines the order of evaluation and the | ||
| 1323 : | operands: | ||
| 1324 : | |||
| 1325 : | @example | ||
| 1326 : | 3 4 + 5 * . | ||
| 1327 : | 3 4 5 * + . | ||
| 1328 : | @end example | ||
| 1329 : | |||
| 1330 : | @assignment | ||
| 1331 : | What are the infix expressions corresponding to the Forth code above? | ||
| 1332 : | Write @code{6-7*8+9} in Forth notation@footnote{This notation is also | ||
| 1333 : | known as Postfix or RPN (Reverse Polish Notation).}. | ||
| 1334 : | @endassignment | ||
| 1335 : | |||
| 1336 : | To change the sign, use @code{negate}: | ||
| 1337 : | |||
| 1338 : | @example | ||
| 1339 : | 2 negate . | ||
| 1340 : | @end example | ||
| 1341 : | |||
| 1342 : | @assignment | ||
| 1343 : | Convert -(-3)*4-5 to Forth. | ||
| 1344 : | @endassignment | ||
| 1345 : | |||
| 1346 : | @code{/mod} performs both @code{/} and @code{mod}. | ||
| 1347 : | |||
| 1348 : | @example | ||
| 1349 : | 7 3 /mod . . | ||
| 1350 : | @end example | ||
| 1351 : | |||
| 1352 : | anton | 1.66 | Reference: @ref{Arithmetic}. |
| 1353 : | |||
| 1354 : | |||
| 1355 : | anton | 1.48 | @node Stack Manipulation Tutorial, Using files for Forth code Tutorial, Arithmetics Tutorial, Tutorial |
| 1356 : | @section Stack Manipulation | ||
| 1357 : | anton | 1.66 | @cindex stack manipulation tutorial |
| 1358 : | anton | 1.48 | |
| 1359 : | Stack manipulation words rearrange the data on the stack. | ||
| 1360 : | |||
| 1361 : | @example | ||
| 1362 : | 1 .s drop .s | ||
| 1363 : | 1 .s dup .s drop drop .s | ||
| 1364 : | 1 2 .s over .s drop drop drop | ||
| 1365 : | 1 2 .s swap .s drop drop | ||
| 1366 : | 1 2 3 .s rot .s drop drop drop | ||
| 1367 : | @end example | ||
| 1368 : | |||
| 1369 : | These are the most important stack manipulation words. There are also | ||
| 1370 : | variants that manipulate twice as many stack items: | ||
| 1371 : | |||
| 1372 : | @example | ||
| 1373 : | 1 2 3 4 .s 2swap .s 2drop 2drop | ||
| 1374 : | @end example | ||
| 1375 : | |||
| 1376 : | Two more stack manipulation words are: | ||
| 1377 : | |||
| 1378 : | @example | ||
| 1379 : | 1 2 .s nip .s drop | ||
| 1380 : | 1 2 .s tuck .s 2drop drop | ||
| 1381 : | @end example | ||
| 1382 : | |||
| 1383 : | @assignment | ||
| 1384 : | Replace @code{nip} and @code{tuck} with combinations of other stack | ||
| 1385 : | manipulation words. | ||
| 1386 : | |||
| 1387 : | @example | ||
| 1388 : | Given: How do you get: | ||
| 1389 : | 1 2 3 3 2 1 | ||
| 1390 : | 1 2 3 1 2 3 2 | ||
| 1391 : | 1 2 3 1 2 3 3 | ||
| 1392 : | 1 2 3 1 3 3 | ||
| 1393 : | 1 2 3 2 1 3 | ||
| 1394 : | 1 2 3 4 4 3 2 1 | ||
| 1395 : | 1 2 3 1 2 3 1 2 3 | ||
| 1396 : | 1 2 3 4 1 2 3 4 1 2 | ||
| 1397 : | 1 2 3 | ||
| 1398 : | 1 2 3 1 2 3 4 | ||
| 1399 : | 1 2 3 1 3 | ||
| 1400 : | @end example | ||
| 1401 : | @endassignment | ||
| 1402 : | |||
| 1403 : | @example | ||
| 1404 : | 5 dup * . | ||
| 1405 : | @end example | ||
| 1406 : | |||
| 1407 : | @assignment | ||
| 1408 : | Write 17^3 and 17^4 in Forth, without writing @code{17} more than once. | ||
| 1409 : | Write a piece of Forth code that expects two numbers on the stack | ||
| 1410 : | (@var{a} and @var{b}, with @var{b} on top) and computes | ||
| 1411 : | @code{(a-b)(a+1)}. | ||
| 1412 : | @endassignment | ||
| 1413 : | |||
| 1414 : | anton | 1.66 | Reference: @ref{Stack Manipulation}. |
| 1415 : | |||
| 1416 : | |||
| 1417 : | anton | 1.48 | @node Using files for Forth code Tutorial, Comments Tutorial, Stack Manipulation Tutorial, Tutorial |
| 1418 : | @section Using files for Forth code | ||
| 1419 : | anton | 1.66 | @cindex loading Forth code, tutorial |
| 1420 : | @cindex files containing Forth code, tutorial | ||
| 1421 : | anton | 1.48 | |
| 1422 : | While working at the Forth command line is convenient for one-line | ||
| 1423 : | examples and short one-off code, you probably want to store your source | ||
| 1424 : | code in files for convenient editing and persistence. You can use your | ||
| 1425 : | favourite editor (Gforth includes Emacs support, @pxref{Emacs and | ||
| 1426 : | anton | 1.102 | Gforth}) to create @var{file.fs} and use |
| 1427 : | anton | 1.48 | |
| 1428 : | @example | ||
| 1429 : | anton | 1.102 | s" @var{file.fs}" included |
| 1430 : | anton | 1.48 | @end example |
| 1431 : | |||
| 1432 : | to load it into your Forth system. The file name extension I use for | ||
| 1433 : | Forth files is @samp{.fs}. | ||
| 1434 : | |||
| 1435 : | You can easily start Gforth with some files loaded like this: | ||
| 1436 : | |||
| 1437 : | @example | ||
| 1438 : | anton | 1.102 | gforth @var{file1.fs} @var{file2.fs} |
| 1439 : | anton | 1.48 | @end example |
| 1440 : | |||
| 1441 : | If an error occurs during loading these files, Gforth terminates, | ||
| 1442 : | whereas an error during @code{INCLUDED} within Gforth usually gives you | ||
| 1443 : | a Gforth command line. Starting the Forth system every time gives you a | ||
| 1444 : | clean start every time, without interference from the results of earlier | ||
| 1445 : | tries. | ||
| 1446 : | |||
| 1447 : | I often put all the tests in a file, then load the code and run the | ||
| 1448 : | tests with | ||
| 1449 : | |||
| 1450 : | @example | ||
| 1451 : | anton | 1.102 | gforth @var{code.fs} @var{tests.fs} -e bye |
| 1452 : | anton | 1.48 | @end example |
| 1453 : | |||
| 1454 : | (often by performing this command with @kbd{C-x C-e} in Emacs). The | ||
| 1455 : | @code{-e bye} ensures that Gforth terminates afterwards so that I can | ||
| 1456 : | restart this command without ado. | ||
| 1457 : | |||
| 1458 : | The advantage of this approach is that the tests can be repeated easily | ||
| 1459 : | every time the program ist changed, making it easy to catch bugs | ||
| 1460 : | introduced by the change. | ||
| 1461 : | |||
| 1462 : | anton | 1.66 | Reference: @ref{Forth source files}. |
| 1463 : | |||
| 1464 : | anton | 1.48 | |
| 1465 : | @node Comments Tutorial, Colon Definitions Tutorial, Using files for Forth code Tutorial, Tutorial | ||
| 1466 : | @section Comments | ||
| 1467 : | anton | 1.66 | @cindex comments tutorial |
| 1468 : | anton | 1.48 | |
| 1469 : | @example | ||
| 1470 : | \ That's a comment; it ends at the end of the line | ||
| 1471 : | ( Another comment; it ends here: ) .s | ||
| 1472 : | @end example | ||
| 1473 : | |||
| 1474 : | @code{\} and @code{(} are ordinary Forth words and therefore have to be | ||
| 1475 : | separated with white space from the following text. | ||
| 1476 : | |||
| 1477 : | @example | ||
| 1478 : | \This gives an "Undefined word" error | ||
| 1479 : | @end example | ||
| 1480 : | |||
| 1481 : | The first @code{)} ends a comment started with @code{(}, so you cannot | ||
| 1482 : | nest @code{(}-comments; and you cannot comment out text containing a | ||
| 1483 : | @code{)} with @code{( ... )}@footnote{therefore it's a good idea to | ||
| 1484 : | avoid @code{)} in word names.}. | ||
| 1485 : | |||
| 1486 : | I use @code{\}-comments for descriptive text and for commenting out code | ||
| 1487 : | of one or more line; I use @code{(}-comments for describing the stack | ||
| 1488 : | effect, the stack contents, or for commenting out sub-line pieces of | ||
| 1489 : | code. | ||
| 1490 : | |||
| 1491 : | The Emacs mode @file{gforth.el} (@pxref{Emacs and Gforth}) supports | ||
| 1492 : | these uses by commenting out a region with @kbd{C-x \}, uncommenting a | ||
| 1493 : | region with @kbd{C-u C-x \}, and filling a @code{\}-commented region | ||
| 1494 : | with @kbd{M-q}. | ||
| 1495 : | |||
| 1496 : | anton | 1.66 | Reference: @ref{Comments}. |
| 1497 : | |||
| 1498 : | anton | 1.48 | |
| 1499 : | @node Colon Definitions Tutorial, Decompilation Tutorial, Comments Tutorial, Tutorial | ||
| 1500 : | @section Colon Definitions | ||
| 1501 : | anton | 1.66 | @cindex colon definitions, tutorial |
| 1502 : | @cindex definitions, tutorial | ||
| 1503 : | @cindex procedures, tutorial | ||
| 1504 : | @cindex functions, tutorial | ||
| 1505 : | anton | 1.48 | |
| 1506 : | are similar to procedures and functions in other programming languages. | ||
| 1507 : | |||
| 1508 : | @example | ||
| 1509 : | : squared ( n -- n^2 ) | ||
| 1510 : | dup * ; | ||
| 1511 : | 5 squared . | ||
| 1512 : | 7 squared . | ||
| 1513 : | @end example | ||
| 1514 : | |||
| 1515 : | @code{:} starts the colon definition; its name is @code{squared}. The | ||
| 1516 : | following comment describes its stack effect. The words @code{dup *} | ||
| 1517 : | are not executed, but compiled into the definition. @code{;} ends the | ||
| 1518 : | colon definition. | ||
| 1519 : | |||
| 1520 : | The newly-defined word can be used like any other word, including using | ||
| 1521 : | it in other definitions: | ||
| 1522 : | |||
| 1523 : | @example | ||
| 1524 : | : cubed ( n -- n^3 ) | ||
| 1525 : | dup squared * ; | ||
| 1526 : | -5 cubed . | ||
| 1527 : | : fourth-power ( n -- n^4 ) | ||
| 1528 : | squared squared ; | ||
| 1529 : | 3 fourth-power . | ||
| 1530 : | @end example | ||
| 1531 : | |||
| 1532 : | @assignment | ||
| 1533 : | Write colon definitions for @code{nip}, @code{tuck}, @code{negate}, and | ||
| 1534 : | @code{/mod} in terms of other Forth words, and check if they work (hint: | ||
| 1535 : | test your tests on the originals first). Don't let the | ||
| 1536 : | @samp{redefined}-Messages spook you, they are just warnings. | ||
| 1537 : | @endassignment | ||
| 1538 : | |||
| 1539 : | anton | 1.66 | Reference: @ref{Colon Definitions}. |
| 1540 : | |||
| 1541 : | anton | 1.48 | |
| 1542 : | @node Decompilation Tutorial, Stack-Effect Comments Tutorial, Colon Definitions Tutorial, Tutorial | ||
| 1543 : | @section Decompilation | ||
| 1544 : | anton | 1.66 | @cindex decompilation tutorial |
| 1545 : | @cindex see tutorial | ||
| 1546 : | anton | 1.48 | |
| 1547 : | You can decompile colon definitions with @code{see}: | ||
| 1548 : | |||
| 1549 : | @example | ||
| 1550 : | see squared | ||
| 1551 : | see cubed | ||
| 1552 : | @end example | ||
| 1553 : | |||
| 1554 : | In Gforth @code{see} shows you a reconstruction of the source code from | ||
| 1555 : | the executable code. Informations that were present in the source, but | ||
| 1556 : | not in the executable code, are lost (e.g., comments). | ||
| 1557 : | |||
| 1558 : | anton | 1.65 | You can also decompile the predefined words: |
| 1559 : | |||
| 1560 : | @example | ||
| 1561 : | see . | ||
| 1562 : | see + | ||
| 1563 : | @end example | ||
| 1564 : | |||
| 1565 : | |||
| 1566 : | anton | 1.48 | @node Stack-Effect Comments Tutorial, Types Tutorial, Decompilation Tutorial, Tutorial |
| 1567 : | @section Stack-Effect Comments | ||
| 1568 : | anton | 1.66 | @cindex stack-effect comments, tutorial |
| 1569 : | @cindex --, tutorial | ||
| 1570 : | anton | 1.48 | By convention the comment after the name of a definition describes the |
| 1571 : | stack effect: The part in from of the @samp{--} describes the state of | ||
| 1572 : | the stack before the execution of the definition, i.e., the parameters | ||
| 1573 : | that are passed into the colon definition; the part behind the @samp{--} | ||
| 1574 : | is the state of the stack after the execution of the definition, i.e., | ||
| 1575 : | the results of the definition. The stack comment only shows the top | ||
| 1576 : | stack items that the definition accesses and/or changes. | ||
| 1577 : | |||
| 1578 : | You should put a correct stack effect on every definition, even if it is | ||
| 1579 : | just @code{( -- )}. You should also add some descriptive comment to | ||
| 1580 : | more complicated words (I usually do this in the lines following | ||
| 1581 : | @code{:}). If you don't do this, your code becomes unreadable (because | ||
| 1582 : | anton | 1.117 | you have to work through every definition before you can understand |
| 1583 : | anton | 1.48 | any). |
| 1584 : | |||
| 1585 : | @assignment | ||
| 1586 : | The stack effect of @code{swap} can be written like this: @code{x1 x2 -- | ||
| 1587 : | x2 x1}. Describe the stack effect of @code{-}, @code{drop}, @code{dup}, | ||
| 1588 : | @code{over}, @code{rot}, @code{nip}, and @code{tuck}. Hint: When you | ||
| 1589 : | anton | 1.65 | are done, you can compare your stack effects to those in this manual |
| 1590 : | anton | 1.48 | (@pxref{Word Index}). |
| 1591 : | @endassignment | ||
| 1592 : | |||
| 1593 : | Sometimes programmers put comments at various places in colon | ||
| 1594 : | definitions that describe the contents of the stack at that place (stack | ||
| 1595 : | comments); i.e., they are like the first part of a stack-effect | ||
| 1596 : | comment. E.g., | ||
| 1597 : | |||
| 1598 : | @example | ||
| 1599 : | : cubed ( n -- n^3 ) | ||
| 1600 : | dup squared ( n n^2 ) * ; | ||
| 1601 : | @end example | ||
| 1602 : | |||
| 1603 : | In this case the stack comment is pretty superfluous, because the word | ||
| 1604 : | is simple enough. If you think it would be a good idea to add such a | ||
| 1605 : | comment to increase readability, you should also consider factoring the | ||
| 1606 : | word into several simpler words (@pxref{Factoring Tutorial,, | ||
| 1607 : | anton | 1.60 | Factoring}), which typically eliminates the need for the stack comment; |
| 1608 : | anton | 1.48 | however, if you decide not to refactor it, then having such a comment is |
| 1609 : | better than not having it. | ||
| 1610 : | |||
| 1611 : | The names of the stack items in stack-effect and stack comments in the | ||
| 1612 : | standard, in this manual, and in many programs specify the type through | ||
| 1613 : | a type prefix, similar to Fortran and Hungarian notation. The most | ||
| 1614 : | frequent prefixes are: | ||
| 1615 : | |||
| 1616 : | @table @code | ||
| 1617 : | @item n | ||
| 1618 : | signed integer | ||
| 1619 : | @item u | ||
| 1620 : | unsigned integer | ||
| 1621 : | @item c | ||
| 1622 : | character | ||
| 1623 : | @item f | ||
| 1624 : | Boolean flags, i.e. @code{false} or @code{true}. | ||
| 1625 : | @item a-addr,a- | ||
| 1626 : | Cell-aligned address | ||
| 1627 : | @item c-addr,c- | ||
| 1628 : | Char-aligned address (note that a Char may have two bytes in Windows NT) | ||
| 1629 : | @item xt | ||
| 1630 : | Execution token, same size as Cell | ||
| 1631 : | @item w,x | ||
| 1632 : | Cell, can contain an integer or an address. It usually takes 32, 64 or | ||
| 1633 : | 16 bits (depending on your platform and Forth system). A cell is more | ||
| 1634 : | commonly known as machine word, but the term @emph{word} already means | ||
| 1635 : | something different in Forth. | ||
| 1636 : | @item d | ||
| 1637 : | signed double-cell integer | ||
| 1638 : | @item ud | ||
| 1639 : | unsigned double-cell integer | ||
| 1640 : | @item r | ||
| 1641 : | Float (on the FP stack) | ||
| 1642 : | @end table | ||
| 1643 : | |||
| 1644 : | You can find a more complete list in @ref{Notation}. | ||
| 1645 : | |||
| 1646 : | @assignment | ||
| 1647 : | Write stack-effect comments for all definitions you have written up to | ||
| 1648 : | now. | ||
| 1649 : | @endassignment | ||
| 1650 : | |||
| 1651 : | |||
| 1652 : | @node Types Tutorial, Factoring Tutorial, Stack-Effect Comments Tutorial, Tutorial | ||
| 1653 : | @section Types | ||
| 1654 : | anton | 1.66 | @cindex types tutorial |
| 1655 : | anton | 1.48 | |
| 1656 : | In Forth the names of the operations are not overloaded; so similar | ||
| 1657 : | operations on different types need different names; e.g., @code{+} adds | ||
| 1658 : | integers, and you have to use @code{f+} to add floating-point numbers. | ||
| 1659 : | The following prefixes are often used for related operations on | ||
| 1660 : | different types: | ||
| 1661 : | |||
| 1662 : | @table @code | ||
| 1663 : | @item (none) | ||
| 1664 : | signed integer | ||
| 1665 : | @item u | ||
| 1666 : | unsigned integer | ||
| 1667 : | @item c | ||
| 1668 : | character | ||
| 1669 : | @item d | ||
| 1670 : | signed double-cell integer | ||
| 1671 : | @item ud, du | ||
| 1672 : | unsigned double-cell integer | ||
| 1673 : | @item 2 | ||
| 1674 : | two cells (not-necessarily double-cell numbers) | ||
| 1675 : | @item m, um | ||
| 1676 : | mixed single-cell and double-cell operations | ||
| 1677 : | @item f | ||
| 1678 : | floating-point (note that in stack comments @samp{f} represents flags, | ||
| 1679 : | anton | 1.66 | and @samp{r} represents FP numbers). |
| 1680 : | anton | 1.48 | @end table |
| 1681 : | |||
| 1682 : | If there are no differences between the signed and the unsigned variant | ||
| 1683 : | (e.g., for @code{+}), there is only the prefix-less variant. | ||
| 1684 : | |||
| 1685 : | Forth does not perform type checking, neither at compile time, nor at | ||
| 1686 : | run time. If you use the wrong oeration, the data are interpreted | ||
| 1687 : | incorrectly: | ||
| 1688 : | |||
| 1689 : | @example | ||
| 1690 : | -1 u. | ||
| 1691 : | @end example | ||
| 1692 : | |||
| 1693 : | If you have only experience with type-checked languages until now, and | ||
| 1694 : | have heard how important type-checking is, don't panic! In my | ||
| 1695 : | experience (and that of other Forthers), type errors in Forth code are | ||
| 1696 : | usually easy to find (once you get used to it), the increased vigilance | ||
| 1697 : | of the programmer tends to catch some harder errors in addition to most | ||
| 1698 : | type errors, and you never have to work around the type system, so in | ||
| 1699 : | most situations the lack of type-checking seems to be a win (projects to | ||
| 1700 : | add type checking to Forth have not caught on). | ||
| 1701 : | |||
| 1702 : | |||
| 1703 : | @node Factoring Tutorial, Designing the stack effect Tutorial, Types Tutorial, Tutorial | ||
| 1704 : | @section Factoring | ||
| 1705 : | anton | 1.66 | @cindex factoring tutorial |
| 1706 : | anton | 1.48 | |
| 1707 : | If you try to write longer definitions, you will soon find it hard to | ||
| 1708 : | keep track of the stack contents. Therefore, good Forth programmers | ||
| 1709 : | tend to write only short definitions (e.g., three lines). The art of | ||
| 1710 : | finding meaningful short definitions is known as factoring (as in | ||
| 1711 : | factoring polynomials). | ||
| 1712 : | |||
| 1713 : | Well-factored programs offer additional advantages: smaller, more | ||
| 1714 : | general words, are easier to test and debug and can be reused more and | ||
| 1715 : | better than larger, specialized words. | ||
| 1716 : | |||
| 1717 : | So, if you run into difficulties with stack management, when writing | ||
| 1718 : | code, try to define meaningful factors for the word, and define the word | ||
| 1719 : | in terms of those. Even if a factor contains only two words, it is | ||
| 1720 : | often helpful. | ||
| 1721 : | |||
| 1722 : | anton | 1.65 | Good factoring is not easy, and it takes some practice to get the knack |
| 1723 : | for it; but even experienced Forth programmers often don't find the | ||
| 1724 : | right solution right away, but only when rewriting the program. So, if | ||
| 1725 : | you don't come up with a good solution immediately, keep trying, don't | ||
| 1726 : | despair. | ||
| 1727 : | anton | 1.48 | |
| 1728 : | @c example !! | ||
| 1729 : | |||
| 1730 : | |||
| 1731 : | @node Designing the stack effect Tutorial, Local Variables Tutorial, Factoring Tutorial, Tutorial | ||
| 1732 : | @section Designing the stack effect | ||
| 1733 : | anton | 1.66 | @cindex Stack effect design, tutorial |
| 1734 : | @cindex design of stack effects, tutorial | ||
| 1735 : | anton | 1.48 | |
| 1736 : | In other languages you can use an arbitrary order of parameters for a | ||
| 1737 : | anton | 1.65 | function; and since there is only one result, you don't have to deal with |
| 1738 : | anton | 1.48 | the order of results, either. |
| 1739 : | |||
| 1740 : | anton | 1.117 | In Forth (and other stack-based languages, e.g., PostScript) the |
| 1741 : | anton | 1.48 | parameter and result order of a definition is important and should be |
| 1742 : | designed well. The general guideline is to design the stack effect such | ||
| 1743 : | that the word is simple to use in most cases, even if that complicates | ||
| 1744 : | the implementation of the word. Some concrete rules are: | ||
| 1745 : | |||
| 1746 : | @itemize @bullet | ||
| 1747 : | |||
| 1748 : | @item | ||
| 1749 : | Words consume all of their parameters (e.g., @code{.}). | ||
| 1750 : | |||
| 1751 : | @item | ||
| 1752 : | If there is a convention on the order of parameters (e.g., from | ||
| 1753 : | mathematics or another programming language), stick with it (e.g., | ||
| 1754 : | @code{-}). | ||
| 1755 : | |||
| 1756 : | @item | ||
| 1757 : | If one parameter usually requires only a short computation (e.g., it is | ||
| 1758 : | a constant), pass it on the top of the stack. Conversely, parameters | ||
| 1759 : | that usually require a long sequence of code to compute should be passed | ||
| 1760 : | as the bottom (i.e., first) parameter. This makes the code easier to | ||
| 1761 : | read, because reader does not need to keep track of the bottom item | ||
| 1762 : | through a long sequence of code (or, alternatively, through stack | ||
| 1763 : | anton | 1.49 | manipulations). E.g., @code{!} (store, @pxref{Memory}) expects the |
| 1764 : | anton | 1.48 | address on top of the stack because it is usually simpler to compute |
| 1765 : | than the stored value (often the address is just a variable). | ||
| 1766 : | |||
| 1767 : | @item | ||
| 1768 : | Similarly, results that are usually consumed quickly should be returned | ||
| 1769 : | on the top of stack, whereas a result that is often used in long | ||
| 1770 : | computations should be passed as bottom result. E.g., the file words | ||
| 1771 : | like @code{open-file} return the error code on the top of stack, because | ||
| 1772 : | it is usually consumed quickly by @code{throw}; moreover, the error code | ||
| 1773 : | has to be checked before doing anything with the other results. | ||
| 1774 : | |||
| 1775 : | @end itemize | ||
| 1776 : | |||
| 1777 : | These rules are just general guidelines, don't lose sight of the overall | ||
| 1778 : | goal to make the words easy to use. E.g., if the convention rule | ||
| 1779 : | conflicts with the computation-length rule, you might decide in favour | ||
| 1780 : | of the convention if the word will be used rarely, and in favour of the | ||
| 1781 : | computation-length rule if the word will be used frequently (because | ||
| 1782 : | with frequent use the cost of breaking the computation-length rule would | ||
| 1783 : | be quite high, and frequent use makes it easier to remember an | ||
| 1784 : | unconventional order). | ||
| 1785 : | |||
| 1786 : | @c example !! structure package | ||
| 1787 : | |||
| 1788 : | anton | 1.65 | |
| 1789 : | anton | 1.48 | @node Local Variables Tutorial, Conditional execution Tutorial, Designing the stack effect Tutorial, Tutorial |
| 1790 : | @section Local Variables | ||
| 1791 : | anton | 1.66 | @cindex local variables, tutorial |
| 1792 : | anton | 1.48 | |
| 1793 : | You can define local variables (@emph{locals}) in a colon definition: | ||
| 1794 : | |||
| 1795 : | @example | ||
| 1796 : | : swap @{ a b -- b a @} | ||
| 1797 : | b a ; | ||
| 1798 : | 1 2 swap .s 2drop | ||
| 1799 : | @end example | ||
| 1800 : | |||
| 1801 : | (If your Forth system does not support this syntax, include | ||
| 1802 : | @file{compat/anslocals.fs} first). | ||
| 1803 : | |||
| 1804 : | In this example @code{@{ a b -- b a @}} is the locals definition; it | ||
| 1805 : | takes two cells from the stack, puts the top of stack in @code{b} and | ||
| 1806 : | the next stack element in @code{a}. @code{--} starts a comment ending | ||
| 1807 : | with @code{@}}. After the locals definition, using the name of the | ||
| 1808 : | local will push its value on the stack. You can leave the comment | ||
| 1809 : | part (@code{-- b a}) away: | ||
| 1810 : | |||
| 1811 : | @example | ||
| 1812 : | : swap ( x1 x2 -- x2 x1 ) | ||
| 1813 : | @{ a b @} b a ; | ||
| 1814 : | @end example | ||
| 1815 : | |||
| 1816 : | In Gforth you can have several locals definitions, anywhere in a colon | ||
| 1817 : | definition; in contrast, in a standard program you can have only one | ||
| 1818 : | locals definition per colon definition, and that locals definition must | ||
| 1819 : | be outside any controll structure. | ||
| 1820 : | |||
| 1821 : | With locals you can write slightly longer definitions without running | ||
| 1822 : | into stack trouble. However, I recommend trying to write colon | ||
| 1823 : | definitions without locals for exercise purposes to help you gain the | ||
| 1824 : | essential factoring skills. | ||
| 1825 : | |||
| 1826 : | @assignment | ||
| 1827 : | Rewrite your definitions until now with locals | ||
| 1828 : | @endassignment | ||
| 1829 : | |||
| 1830 : | anton | 1.66 | Reference: @ref{Locals}. |
| 1831 : | |||
| 1832 : | anton | 1.48 | |
| 1833 : | @node Conditional execution Tutorial, Flags and Comparisons Tutorial, Local Variables Tutorial, Tutorial | ||
| 1834 : | @section Conditional execution | ||
| 1835 : | anton | 1.66 | @cindex conditionals, tutorial |
| 1836 : | @cindex if, tutorial | ||
| 1837 : | anton | 1.48 | |
| 1838 : | In Forth you can use control structures only inside colon definitions. | ||
| 1839 : | An @code{if}-structure looks like this: | ||
| 1840 : | |||
| 1841 : | @example | ||
| 1842 : | : abs ( n1 -- +n2 ) | ||
| 1843 : | dup 0 < if | ||
| 1844 : | negate | ||
| 1845 : | endif ; | ||
| 1846 : | 5 abs . | ||
| 1847 : | -5 abs . | ||
| 1848 : | @end example | ||
| 1849 : | |||
| 1850 : | @code{if} takes a flag from the stack. If the flag is non-zero (true), | ||
| 1851 : | the following code is performed, otherwise execution continues after the | ||
| 1852 : | pazsan | 1.51 | @code{endif} (or @code{else}). @code{<} compares the top two stack |
| 1853 : | anton | 1.48 | elements and prioduces a flag: |
| 1854 : | |||
| 1855 : | @example | ||
| 1856 : | 1 2 < . | ||
| 1857 : | 2 1 < . | ||
| 1858 : | 1 1 < . | ||
| 1859 : | @end example | ||
| 1860 : | |||
| 1861 : | Actually the standard name for @code{endif} is @code{then}. This | ||
| 1862 : | tutorial presents the examples using @code{endif}, because this is often | ||
| 1863 : | less confusing for people familiar with other programming languages | ||
| 1864 : | where @code{then} has a different meaning. If your system does not have | ||
| 1865 : | @code{endif}, define it with | ||
| 1866 : | |||
| 1867 : | @example | ||
| 1868 : | : endif postpone then ; immediate | ||
| 1869 : | @end example | ||
| 1870 : | |||
| 1871 : | You can optionally use an @code{else}-part: | ||
| 1872 : | |||
| 1873 : | @example | ||
| 1874 : | : min ( n1 n2 -- n ) | ||
| 1875 : | 2dup < if | ||
| 1876 : | drop | ||
| 1877 : | else | ||
| 1878 : | nip | ||
| 1879 : | endif ; | ||
| 1880 : | 2 3 min . | ||
| 1881 : | 3 2 min . | ||
| 1882 : | @end example | ||
| 1883 : | |||
| 1884 : | @assignment | ||
| 1885 : | Write @code{min} without @code{else}-part (hint: what's the definition | ||
| 1886 : | of @code{nip}?). | ||
| 1887 : | @endassignment | ||
| 1888 : | |||
| 1889 : | anton | 1.66 | Reference: @ref{Selection}. |
| 1890 : | |||
| 1891 : | anton | 1.48 | |
| 1892 : | @node Flags and Comparisons Tutorial, General Loops Tutorial, Conditional execution Tutorial, Tutorial | ||
| 1893 : | @section Flags and Comparisons | ||
| 1894 : | anton | 1.66 | @cindex flags tutorial |
| 1895 : | @cindex comparison tutorial | ||
| 1896 : | anton | 1.48 | |
| 1897 : | In a false-flag all bits are clear (0 when interpreted as integer). In | ||
| 1898 : | a canonical true-flag all bits are set (-1 as a twos-complement signed | ||
| 1899 : | integer); in many contexts (e.g., @code{if}) any non-zero value is | ||
| 1900 : | treated as true flag. | ||
| 1901 : | |||
| 1902 : | @example | ||
| 1903 : | false . | ||
| 1904 : | true . | ||
| 1905 : | true hex u. decimal | ||
| 1906 : | @end example | ||
| 1907 : | |||
| 1908 : | Comparison words produce canonical flags: | ||
| 1909 : | |||
| 1910 : | @example | ||
| 1911 : | 1 1 = . | ||
| 1912 : | 1 0= . | ||
| 1913 : | 0 1 < . | ||
| 1914 : | 0 0 < . | ||
| 1915 : | -1 1 u< . \ type error, u< interprets -1 as large unsigned number | ||
| 1916 : | -1 1 < . | ||
| 1917 : | @end example | ||
| 1918 : | |||
| 1919 : | anton | 1.66 | Gforth supports all combinations of the prefixes @code{0 u d d0 du f f0} |
| 1920 : | (or none) and the comparisons @code{= <> < > <= >=}. Only a part of | ||
| 1921 : | these combinations are standard (for details see the standard, | ||
| 1922 : | @ref{Numeric comparison}, @ref{Floating Point} or @ref{Word Index}). | ||
| 1923 : | anton | 1.48 | |
| 1924 : | You can use @code{and or xor invert} can be used as operations on | ||
| 1925 : | canonical flags. Actually they are bitwise operations: | ||
| 1926 : | |||
| 1927 : | @example | ||
| 1928 : | 1 2 and . | ||
| 1929 : | 1 2 or . | ||
| 1930 : | 1 3 xor . | ||
| 1931 : | 1 invert . | ||
| 1932 : | @end example | ||
| 1933 : | |||
| 1934 : | You can convert a zero/non-zero flag into a canonical flag with | ||
| 1935 : | @code{0<>} (and complement it on the way with @code{0=}). | ||
| 1936 : | |||
| 1937 : | @example | ||
| 1938 : | 1 0= . | ||
| 1939 : | 1 0<> . | ||
| 1940 : | @end example | ||
| 1941 : | |||
| 1942 : | anton | 1.65 | You can use the all-bits-set feature of canonical flags and the bitwise |
| 1943 : | anton | 1.48 | operation of the Boolean operations to avoid @code{if}s: |
| 1944 : | |||
| 1945 : | @example | ||
| 1946 : | : foo ( n1 -- n2 ) | ||
| 1947 : | 0= if | ||
| 1948 : | 14 | ||
| 1949 : | else | ||
| 1950 : | 0 | ||
| 1951 : | endif ; | ||
| 1952 : | 0 foo . | ||
| 1953 : | 1 foo . | ||
| 1954 : | |||
| 1955 : | : foo ( n1 -- n2 ) | ||
| 1956 : | 0= 14 and ; | ||
| 1957 : | 0 foo . | ||
| 1958 : | 1 foo . | ||
| 1959 : | @end example | ||
| 1960 : | |||
| 1961 : | @assignment | ||
| 1962 : | Write @code{min} without @code{if}. | ||
| 1963 : | @endassignment | ||
| 1964 : | |||
| 1965 : | anton | 1.66 | For reference, see @ref{Boolean Flags}, @ref{Numeric comparison}, and |
| 1966 : | @ref{Bitwise operations}. | ||
| 1967 : | |||
| 1968 : | anton | 1.48 | |
| 1969 : | @node General Loops Tutorial, Counted loops Tutorial, Flags and Comparisons Tutorial, Tutorial | ||
| 1970 : | @section General Loops | ||
| 1971 : | anton | 1.66 | @cindex loops, indefinite, tutorial |
| 1972 : | anton | 1.48 | |
| 1973 : | The endless loop is the most simple one: | ||
| 1974 : | |||
| 1975 : | @example | ||
| 1976 : | : endless ( -- ) | ||
| 1977 : | 0 begin | ||
| 1978 : | dup . 1+ | ||
| 1979 : | again ; | ||
| 1980 : | endless | ||
| 1981 : | @end example | ||
| 1982 : | |||
| 1983 : | Terminate this loop by pressing @kbd{Ctrl-C} (in Gforth). @code{begin} | ||
| 1984 : | does nothing at run-time, @code{again} jumps back to @code{begin}. | ||
| 1985 : | |||
| 1986 : | A loop with one exit at any place looks like this: | ||
| 1987 : | |||
| 1988 : | @example | ||
| 1989 : | : log2 ( +n1 -- n2 ) | ||
| 1990 : | \ logarithmus dualis of n1>0, rounded down to the next integer | ||
| 1991 : | assert( dup 0> ) | ||
| 1992 : | 2/ 0 begin | ||
| 1993 : | over 0> while | ||
| 1994 : | 1+ swap 2/ swap | ||
| 1995 : | repeat | ||
| 1996 : | nip ; | ||
| 1997 : | 7 log2 . | ||
| 1998 : | 8 log2 . | ||
| 1999 : | @end example | ||
| 2000 : | |||
| 2001 : | At run-time @code{while} consumes a flag; if it is 0, execution | ||
| 2002 : | pazsan | 1.51 | continues behind the @code{repeat}; if the flag is non-zero, execution |
| 2003 : | anton | 1.48 | continues behind the @code{while}. @code{Repeat} jumps back to |
| 2004 : | @code{begin}, just like @code{again}. | ||
| 2005 : | |||
| 2006 : | In Forth there are many combinations/abbreviations, like @code{1+}. | ||
| 2007 : | anton | 1.90 | However, @code{2/} is not one of them; it shifts its argument right by |
| 2008 : | anton | 1.48 | one bit (arithmetic shift right): |
| 2009 : | |||
| 2010 : | @example | ||
| 2011 : | -5 2 / . | ||
| 2012 : | -5 2/ . | ||
| 2013 : | @end example | ||
| 2014 : | |||
| 2015 : | @code{assert(} is no standard word, but you can get it on systems other | ||
| 2016 : | then Gforth by including @file{compat/assert.fs}. You can see what it | ||
| 2017 : | does by trying | ||
| 2018 : | |||
| 2019 : | @example | ||
| 2020 : | 0 log2 . | ||
| 2021 : | @end example | ||
| 2022 : | |||
| 2023 : | Here's a loop with an exit at the end: | ||
| 2024 : | |||
| 2025 : | @example | ||
| 2026 : | : log2 ( +n1 -- n2 ) | ||
| 2027 : | \ logarithmus dualis of n1>0, rounded down to the next integer | ||
| 2028 : | assert( dup 0 > ) | ||
| 2029 : | -1 begin | ||
| 2030 : | 1+ swap 2/ swap | ||
| 2031 : | over 0 <= | ||
| 2032 : | until | ||
| 2033 : | nip ; | ||
| 2034 : | @end example | ||
| 2035 : | |||
| 2036 : | @code{Until} consumes a flag; if it is non-zero, execution continues at | ||
| 2037 : | the @code{begin}, otherwise after the @code{until}. | ||
| 2038 : | |||
| 2039 : | @assignment | ||
| 2040 : | Write a definition for computing the greatest common divisor. | ||
| 2041 : | @endassignment | ||
| 2042 : | |||
| 2043 : | anton | 1.66 | Reference: @ref{Simple Loops}. |
| 2044 : | |||
| 2045 : | anton | 1.48 | |
| 2046 : | @node Counted loops Tutorial, Recursion Tutorial, General Loops Tutorial, Tutorial | ||
| 2047 : | @section Counted loops | ||
| 2048 : | anton | 1.66 | @cindex loops, counted, tutorial |
| 2049 : | anton | 1.48 | |
| 2050 : | @example | ||
| 2051 : | : ^ ( n1 u -- n ) | ||
| 2052 : | \ n = the uth power of u1 | ||
| 2053 : | 1 swap 0 u+do | ||
| 2054 : | over * | ||
| 2055 : | loop | ||
| 2056 : | nip ; | ||
| 2057 : | 3 2 ^ . | ||
| 2058 : | 4 3 ^ . | ||
| 2059 : | @end example | ||
| 2060 : | |||
| 2061 : | @code{U+do} (from @file{compat/loops.fs}, if your Forth system doesn't | ||
| 2062 : | have it) takes two numbers of the stack @code{( u3 u4 -- )}, and then | ||
| 2063 : | performs the code between @code{u+do} and @code{loop} for @code{u3-u4} | ||
| 2064 : | times (or not at all, if @code{u3-u4<0}). | ||
| 2065 : | |||
| 2066 : | You can see the stack effect design rules at work in the stack effect of | ||
| 2067 : | the loop start words: Since the start value of the loop is more | ||
| 2068 : | frequently constant than the end value, the start value is passed on | ||
| 2069 : | the top-of-stack. | ||
| 2070 : | |||
| 2071 : | You can access the counter of a counted loop with @code{i}: | ||
| 2072 : | |||
| 2073 : | @example | ||
| 2074 : | : fac ( u -- u! ) | ||
| 2075 : | 1 swap 1+ 1 u+do | ||
| 2076 : | i * | ||
| 2077 : | loop ; | ||
| 2078 : | 5 fac . | ||
| 2079 : | 7 fac . | ||
| 2080 : | @end example | ||
| 2081 : | |||
| 2082 : | There is also @code{+do}, which expects signed numbers (important for | ||
| 2083 : | deciding whether to enter the loop). | ||
| 2084 : | |||
| 2085 : | @assignment | ||
| 2086 : | Write a definition for computing the nth Fibonacci number. | ||
| 2087 : | @endassignment | ||
| 2088 : | |||
| 2089 : | anton | 1.65 | You can also use increments other than 1: |
| 2090 : | |||
| 2091 : | @example | ||
| 2092 : | : up2 ( n1 n2 -- ) | ||
| 2093 : | +do | ||
| 2094 : | i . | ||
| 2095 : | 2 +loop ; | ||
| 2096 : | 10 0 up2 | ||
| 2097 : | |||
| 2098 : | : down2 ( n1 n2 -- ) | ||
| 2099 : | -do | ||
| 2100 : | i . | ||
| 2101 : | 2 -loop ; | ||
| 2102 : | 0 10 down2 | ||
| 2103 : | @end example | ||
| 2104 : | anton | 1.48 | |
| 2105 : | anton | 1.66 | Reference: @ref{Counted Loops}. |
| 2106 : | |||
| 2107 : | anton | 1.48 | |
| 2108 : | @node Recursion Tutorial, Leaving definitions or loops Tutorial, Counted loops Tutorial, Tutorial | ||
| 2109 : | @section Recursion | ||
| 2110 : | anton | 1.66 | @cindex recursion tutorial |
| 2111 : | anton | 1.48 | |
| 2112 : | Usually the name of a definition is not visible in the definition; but | ||
| 2113 : | earlier definitions are usually visible: | ||
| 2114 : | |||
| 2115 : | @example | ||
| 2116 : | 1 0 / . \ "Floating-point unidentified fault" in Gforth on most platforms | ||
| 2117 : | : / ( n1 n2 -- n ) | ||
| 2118 : | dup 0= if | ||
| 2119 : | -10 throw \ report division by zero | ||
| 2120 : | endif | ||
| 2121 : | / \ old version | ||
| 2122 : | ; | ||
| 2123 : | 1 0 / | ||
| 2124 : | @end example | ||
| 2125 : | |||
| 2126 : | For recursive definitions you can use @code{recursive} (non-standard) or | ||
| 2127 : | @code{recurse}: | ||
| 2128 : | |||
| 2129 : | @example | ||
| 2130 : | : fac1 ( n -- n! ) recursive | ||
| 2131 : | dup 0> if | ||
| 2132 : | dup 1- fac1 * | ||
| 2133 : | else | ||
| 2134 : | drop 1 | ||
| 2135 : | endif ; | ||
| 2136 : | 7 fac1 . | ||
| 2137 : | |||
| 2138 : | : fac2 ( n -- n! ) | ||
| 2139 : | dup 0> if | ||
| 2140 : | dup 1- recurse * | ||
| 2141 : | else | ||
| 2142 : | drop 1 | ||
| 2143 : | endif ; | ||
| 2144 : | 8 fac2 . | ||
| 2145 : | @end example | ||
| 2146 : | |||
| 2147 : | @assignment | ||
| 2148 : | Write a recursive definition for computing the nth Fibonacci number. | ||
| 2149 : | @endassignment | ||
| 2150 : | |||
| 2151 : | anton | 1.66 | Reference (including indirect recursion): @xref{Calls and returns}. |
| 2152 : | |||
| 2153 : | anton | 1.48 | |
| 2154 : | @node Leaving definitions or loops Tutorial, Return Stack Tutorial, Recursion Tutorial, Tutorial | ||
| 2155 : | @section Leaving definitions or loops | ||
| 2156 : | anton | 1.66 | @cindex leaving definitions, tutorial |
| 2157 : | @cindex leaving loops, tutorial | ||
| 2158 : | anton | 1.48 | |
| 2159 : | @code{EXIT} exits the current definition right away. For every counted | ||
| 2160 : | loop that is left in this way, an @code{UNLOOP} has to be performed | ||
| 2161 : | before the @code{EXIT}: | ||
| 2162 : | |||
| 2163 : | @c !! real examples | ||
| 2164 : | @example | ||
| 2165 : | : ... | ||
| 2166 : | ... u+do | ||
| 2167 : | ... if | ||
| 2168 : | ... unloop exit | ||
| 2169 : | endif | ||
| 2170 : | ... | ||
| 2171 : | loop | ||
| 2172 : | ... ; | ||
| 2173 : | @end example | ||
| 2174 : | |||
| 2175 : | @code{LEAVE} leaves the innermost counted loop right away: | ||
| 2176 : | |||
| 2177 : | @example | ||
| 2178 : | : ... | ||
| 2179 : | ... u+do | ||
| 2180 : | ... if | ||
| 2181 : | ... leave | ||
| 2182 : | endif | ||
| 2183 : | ... | ||
| 2184 : | loop | ||
| 2185 : | ... ; | ||
| 2186 : | @end example | ||
| 2187 : | |||
| 2188 : | anton | 1.65 | @c !! example |
| 2189 : | anton | 1.48 | |
| 2190 : | anton | 1.66 | Reference: @ref{Calls and returns}, @ref{Counted Loops}. |
| 2191 : | |||
| 2192 : | |||
| 2193 : | anton | 1.48 | @node Return Stack Tutorial, Memory Tutorial, Leaving definitions or loops Tutorial, Tutorial |
| 2194 : | @section Return Stack | ||
| 2195 : | anton | 1.66 | @cindex return stack tutorial |
| 2196 : | anton | 1.48 | |
| 2197 : | In addition to the data stack Forth also has a second stack, the return | ||
| 2198 : | stack; most Forth systems store the return addresses of procedure calls | ||
| 2199 : | there (thus its name). Programmers can also use this stack: | ||
| 2200 : | |||
| 2201 : | @example | ||
| 2202 : | : foo ( n1 n2 -- ) | ||
| 2203 : | .s | ||
| 2204 : | >r .s | ||
| 2205 : | anton | 1.50 | r@@ . |
| 2206 : | anton | 1.48 | >r .s |
| 2207 : | anton | 1.50 | r@@ . |
| 2208 : | anton | 1.48 | r> . |
| 2209 : | anton | 1.50 | r@@ . |
| 2210 : | anton | 1.48 | r> . ; |
| 2211 : | 1 2 foo | ||
| 2212 : | @end example | ||
| 2213 : | |||
| 2214 : | @code{>r} takes an element from the data stack and pushes it onto the | ||
| 2215 : | return stack; conversely, @code{r>} moves an elementm from the return to | ||
| 2216 : | the data stack; @code{r@@} pushes a copy of the top of the return stack | ||
| 2217 : | on the return stack. | ||
| 2218 : | |||
| 2219 : | Forth programmers usually use the return stack for storing data | ||
| 2220 : | temporarily, if using the data stack alone would be too complex, and | ||
| 2221 : | factoring and locals are not an option: | ||
| 2222 : | |||
| 2223 : | @example | ||
| 2224 : | : 2swap ( x1 x2 x3 x4 -- x3 x4 x1 x2 ) | ||
| 2225 : | rot >r rot r> ; | ||
| 2226 : | @end example | ||
| 2227 : | |||
| 2228 : | The return address of the definition and the loop control parameters of | ||
| 2229 : | counted loops usually reside on the return stack, so you have to take | ||
| 2230 : | all items, that you have pushed on the return stack in a colon | ||
| 2231 : | definition or counted loop, from the return stack before the definition | ||
| 2232 : | or loop ends. You cannot access items that you pushed on the return | ||
| 2233 : | stack outside some definition or loop within the definition of loop. | ||
| 2234 : | |||
| 2235 : | If you miscount the return stack items, this usually ends in a crash: | ||
| 2236 : | |||
| 2237 : | @example | ||
| 2238 : | : crash ( n -- ) | ||
| 2239 : | >r ; | ||
| 2240 : | 5 crash | ||
| 2241 : | @end example | ||
| 2242 : | |||
| 2243 : | You cannot mix using locals and using the return stack (according to the | ||
| 2244 : | standard; Gforth has no problem). However, they solve the same | ||
| 2245 : | problems, so this shouldn't be an issue. | ||
| 2246 : | |||
| 2247 : | @assignment | ||
| 2248 : | Can you rewrite any of the definitions you wrote until now in a better | ||
| 2249 : | way using the return stack? | ||
| 2250 : | @endassignment | ||
| 2251 : | |||
| 2252 : | anton | 1.66 | Reference: @ref{Return stack}. |
| 2253 : | |||
| 2254 : | anton | 1.48 | |
| 2255 : | @node Memory Tutorial, Characters and Strings Tutorial, Return Stack Tutorial, Tutorial | ||
| 2256 : | @section Memory | ||
| 2257 : | anton | 1.66 | @cindex memory access/allocation tutorial |
| 2258 : | anton | 1.48 | |
| 2259 : | You can create a global variable @code{v} with | ||
| 2260 : | |||
| 2261 : | @example | ||
| 2262 : | variable v ( -- addr ) | ||
| 2263 : | @end example | ||
| 2264 : | |||
| 2265 : | @code{v} pushes the address of a cell in memory on the stack. This cell | ||
| 2266 : | was reserved by @code{variable}. You can use @code{!} (store) to store | ||
| 2267 : | values into this cell and @code{@@} (fetch) to load the value from the | ||
| 2268 : | stack into memory: | ||
| 2269 : | |||
| 2270 : | @example | ||
| 2271 : | v . | ||
| 2272 : | 5 v ! .s | ||
| 2273 : | anton | 1.50 | v @@ . |
| 2274 : | anton | 1.48 | @end example |
| 2275 : | |||
| 2276 : | anton | 1.65 | You can see a raw dump of memory with @code{dump}: |
| 2277 : | |||
| 2278 : | @example | ||
| 2279 : | v 1 cells .s dump | ||
| 2280 : | @end example | ||
| 2281 : | |||
| 2282 : | @code{Cells ( n1 -- n2 )} gives you the number of bytes (or, more | ||
| 2283 : | generally, address units (aus)) that @code{n1 cells} occupy. You can | ||
| 2284 : | also reserve more memory: | ||
| 2285 : | anton | 1.48 | |
| 2286 : | @example | ||
| 2287 : | create v2 20 cells allot | ||
| 2288 : | anton | 1.65 | v2 20 cells dump |
| 2289 : | anton | 1.48 | @end example |
| 2290 : | |||
| 2291 : | anton | 1.65 | creates a word @code{v2} and reserves 20 uninitialized cells; the |
| 2292 : | address pushed by @code{v2} points to the start of these 20 cells. You | ||
| 2293 : | can use address arithmetic to access these cells: | ||
| 2294 : | anton | 1.48 | |
| 2295 : | @example | ||
| 2296 : | 3 v2 5 cells + ! | ||
| 2297 : | anton | 1.65 | v2 20 cells dump |
| 2298 : | anton | 1.48 | @end example |
| 2299 : | |||
| 2300 : | You can reserve and initialize memory with @code{,}: | ||
| 2301 : | |||
| 2302 : | @example | ||
| 2303 : | create v3 | ||
| 2304 : | 5 , 4 , 3 , 2 , 1 , | ||
| 2305 : | anton | 1.50 | v3 @@ . |
| 2306 : | v3 cell+ @@ . | ||
| 2307 : | v3 2 cells + @@ . | ||
| 2308 : | anton | 1.65 | v3 5 cells dump |
| 2309 : | anton | 1.48 | @end example |
| 2310 : | |||
| 2311 : | @assignment | ||
| 2312 : | Write a definition @code{vsum ( addr u -- n )} that computes the sum of | ||
| 2313 : | @code{u} cells, with the first of these cells at @code{addr}, the next | ||
| 2314 : | one at @code{addr cell+} etc. | ||
| 2315 : | @endassignment | ||
| 2316 : | |||
| 2317 : | You can also reserve memory without creating a new word: | ||
| 2318 : | |||
| 2319 : | @example | ||
| 2320 : | anton | 1.60 | here 10 cells allot . |
| 2321 : | here . | ||
| 2322 : | anton | 1.48 | @end example |
| 2323 : | |||
| 2324 : | @code{Here} pushes the start address of the memory area. You should | ||
| 2325 : | store it somewhere, or you will have a hard time finding the memory area | ||
| 2326 : | again. | ||
| 2327 : | |||
| 2328 : | @code{Allot} manages dictionary memory. The dictionary memory contains | ||
| 2329 : | the system's data structures for words etc. on Gforth and most other | ||
| 2330 : | Forth systems. It is managed like a stack: You can free the memory that | ||
| 2331 : | you have just @code{allot}ed with | ||
| 2332 : | |||
| 2333 : | @example | ||
| 2334 : | -10 cells allot | ||
| 2335 : | anton | 1.60 | here . |
| 2336 : | anton | 1.48 | @end example |
| 2337 : | |||
| 2338 : | Note that you cannot do this if you have created a new word in the | ||
| 2339 : | meantime (because then your @code{allot}ed memory is no longer on the | ||
| 2340 : | top of the dictionary ``stack''). | ||
| 2341 : | |||
| 2342 : | Alternatively, you can use @code{allocate} and @code{free} which allow | ||
| 2343 : | freeing memory in any order: | ||
| 2344 : | |||
| 2345 : | @example | ||
| 2346 : | 10 cells allocate throw .s | ||
| 2347 : | 20 cells allocate throw .s | ||
| 2348 : | swap | ||
| 2349 : | free throw | ||
| 2350 : | free throw | ||
| 2351 : | @end example | ||
| 2352 : | |||
| 2353 : | The @code{throw}s deal with errors (e.g., out of memory). | ||
| 2354 : | |||
| 2355 : | anton | 1.65 | And there is also a |
| 2356 : | @uref{http://www.complang.tuwien.ac.at/forth/garbage-collection.zip, | ||
| 2357 : | garbage collector}, which eliminates the need to @code{free} memory | ||
| 2358 : | explicitly. | ||
| 2359 : | anton | 1.48 | |
| 2360 : | anton | 1.66 | Reference: @ref{Memory}. |
| 2361 : | |||
| 2362 : | anton | 1.48 | |
| 2363 : | @node Characters and Strings Tutorial, Alignment Tutorial, Memory Tutorial, Tutorial | ||
| 2364 : | @section Characters and Strings | ||
| 2365 : | anton | 1.66 | @cindex strings tutorial |
| 2366 : | @cindex characters tutorial | ||
| 2367 : | anton | 1.48 | |
| 2368 : | On the stack characters take up a cell, like numbers. In memory they | ||
| 2369 : | have their own size (one 8-bit byte on most systems), and therefore | ||
| 2370 : | require their own words for memory access: | ||
| 2371 : | |||
| 2372 : | @example | ||
| 2373 : | create v4 | ||
| 2374 : | 104 c, 97 c, 108 c, 108 c, 111 c, | ||
| 2375 : | anton | 1.50 | v4 4 chars + c@@ . |
| 2376 : | anton | 1.65 | v4 5 chars dump |
| 2377 : | anton | 1.48 | @end example |
| 2378 : | |||
| 2379 : | The preferred representation of strings on the stack is @code{addr | ||
| 2380 : | u-count}, where @code{addr} is the address of the first character and | ||
| 2381 : | @code{u-count} is the number of characters in the string. | ||
| 2382 : | |||
| 2383 : | @example | ||
| 2384 : | v4 5 type | ||
| 2385 : | @end example | ||
| 2386 : | |||
| 2387 : | You get a string constant with | ||
| 2388 : | |||
| 2389 : | @example | ||
| 2390 : | s" hello, world" .s | ||
| 2391 : | type | ||
| 2392 : | @end example | ||
| 2393 : | |||
| 2394 : | Make sure you have a space between @code{s"} and the string; @code{s"} | ||
| 2395 : | is a normal Forth word and must be delimited with white space (try what | ||
| 2396 : | happens when you remove the space). | ||
| 2397 : | |||
| 2398 : | However, this interpretive use of @code{s"} is quite restricted: the | ||
| 2399 : | string exists only until the next call of @code{s"} (some Forth systems | ||
| 2400 : | keep more than one of these strings, but usually they still have a | ||
| 2401 : | crook | 1.62 | limited lifetime). |
| 2402 : | anton | 1.48 | |
| 2403 : | @example | ||
| 2404 : | s" hello," s" world" .s | ||
| 2405 : | type | ||
| 2406 : | type | ||
| 2407 : | @end example | ||
| 2408 : | |||
| 2409 : | crook | 1.62 | You can also use @code{s"} in a definition, and the resulting |
| 2410 : | strings then live forever (well, for as long as the definition): | ||
| 2411 : | anton | 1.48 | |
| 2412 : | @example | ||
| 2413 : | : foo s" hello," s" world" ; | ||
| 2414 : | foo .s | ||
| 2415 : | type | ||
| 2416 : | type | ||
| 2417 : | @end example | ||
| 2418 : | |||
| 2419 : | @assignment | ||
| 2420 : | @code{Emit ( c -- )} types @code{c} as character (not a number). | ||
| 2421 : | Implement @code{type ( addr u -- )}. | ||
| 2422 : | @endassignment | ||
| 2423 : | |||
| 2424 : | anton | 1.66 | Reference: @ref{Memory Blocks}. |
| 2425 : | |||
| 2426 : | |||
| 2427 : | pazsan | 1.84 | @node Alignment Tutorial, Files Tutorial, Characters and Strings Tutorial, Tutorial |
| 2428 : | anton | 1.48 | @section Alignment |
| 2429 : | anton | 1.66 | @cindex alignment tutorial |
| 2430 : | @cindex memory alignment tutorial | ||
| 2431 : | anton | 1.48 | |
| 2432 : | On many processors cells have to be aligned in memory, if you want to | ||
| 2433 : | access them with @code{@@} and @code{!} (and even if the processor does | ||
| 2434 : | crook | 1.62 | not require alignment, access to aligned cells is faster). |
| 2435 : | anton | 1.48 | |
| 2436 : | @code{Create} aligns @code{here} (i.e., the place where the next | ||
| 2437 : | allocation will occur, and that the @code{create}d word points to). | ||
| 2438 : | Likewise, the memory produced by @code{allocate} starts at an aligned | ||
| 2439 : | address. Adding a number of @code{cells} to an aligned address produces | ||
| 2440 : | another aligned address. | ||
| 2441 : | |||
| 2442 : | However, address arithmetic involving @code{char+} and @code{chars} can | ||
| 2443 : | create an address that is not cell-aligned. @code{Aligned ( addr -- | ||
| 2444 : | a-addr )} produces the next aligned address: | ||
| 2445 : | |||
| 2446 : | @example | ||
| 2447 : | anton | 1.50 | v3 char+ aligned .s @@ . |
| 2448 : | v3 char+ .s @@ . | ||
| 2449 : | anton | 1.48 | @end example |
| 2450 : | |||
| 2451 : | Similarly, @code{align} advances @code{here} to the next aligned | ||
| 2452 : | address: | ||
| 2453 : | |||
| 2454 : | @example | ||
| 2455 : | create v5 97 c, | ||
| 2456 : | here . | ||
| 2457 : | align here . | ||
| 2458 : | 1000 , | ||
| 2459 : | @end example | ||
| 2460 : | |||
| 2461 : | Note that you should use aligned addresses even if your processor does | ||
| 2462 : | not require them, if you want your program to be portable. | ||
| 2463 : | |||
| 2464 : | anton | 1.66 | Reference: @ref{Address arithmetic}. |
| 2465 : | |||
| 2466 : | anton | 1.48 | |
| 2467 : | pazsan | 1.84 | @node Files Tutorial, Interpretation and Compilation Semantics and Immediacy Tutorial, Alignment Tutorial, Tutorial |
| 2468 : | @section Files | ||
| 2469 : | @cindex files tutorial | ||
| 2470 : | |||
| 2471 : | This section gives a short introduction into how to use files inside | ||
| 2472 : | Forth. It's broken up into five easy steps: | ||
| 2473 : | |||
| 2474 : | @enumerate 1 | ||
| 2475 : | @item Opened an ASCII text file for input | ||
| 2476 : | @item Opened a file for output | ||
| 2477 : | @item Read input file until string matched (or some other condition matched) | ||
| 2478 : | @item Wrote some lines from input ( modified or not) to output | ||
| 2479 : | @item Closed the files. | ||
| 2480 : | @end enumerate | ||
| 2481 : | |||
| 2482 : | @subsection Open file for input | ||
| 2483 : | |||
| 2484 : | @example | ||
| 2485 : | s" foo.in" r/o open-file throw Value fd-in | ||
| 2486 : | @end example | ||
| 2487 : | |||
| 2488 : | @subsection Create file for output | ||
| 2489 : | |||
| 2490 : | @example | ||
| 2491 : | s" foo.out" w/o create-file throw Value fd-out | ||
| 2492 : | @end example | ||
| 2493 : | |||
| 2494 : | The available file modes are r/o for read-only access, r/w for | ||
| 2495 : | read-write access, and w/o for write-only access. You could open both | ||
| 2496 : | files with r/w, too, if you like. All file words return error codes; for | ||
| 2497 : | most applications, it's best to pass there error codes with @code{throw} | ||
| 2498 : | to the outer error handler. | ||
| 2499 : | |||
| 2500 : | If you want words for opening and assigning, define them as follows: | ||
| 2501 : | |||
| 2502 : | @example | ||
| 2503 : | 0 Value fd-in | ||
| 2504 : | 0 Value fd-out | ||
| 2505 : | : open-input ( addr u -- ) r/o open-file throw to fd-in ; | ||
| 2506 : | : open-output ( addr u -- ) w/o create-file throw to fd-out ; | ||
| 2507 : | @end example | ||
| 2508 : | |||
| 2509 : | Usage example: | ||
| 2510 : | |||
| 2511 : | @example | ||
| 2512 : | s" foo.in" open-input | ||
| 2513 : | s" foo.out" open-output | ||
| 2514 : | @end example | ||
| 2515 : | |||
| 2516 : | @subsection Scan file for a particular line | ||
| 2517 : | |||
| 2518 : | @example | ||
| 2519 : | 256 Constant max-line | ||
| 2520 : | Create line-buffer max-line 2 + allot | ||
| 2521 : | |||
| 2522 : | : scan-file ( addr u -- ) | ||
| 2523 : | begin | ||
| 2524 : | line-buffer max-line fd-in read-line throw | ||
| 2525 : | while | ||
| 2526 : | >r 2dup line-buffer r> compare 0= | ||
| 2527 : | until | ||
| 2528 : | else | ||
| 2529 : | drop | ||
| 2530 : | then | ||
| 2531 : | 2drop ; | ||
| 2532 : | @end example | ||
| 2533 : | |||
| 2534 : | @code{read-line ( addr u1 fd -- u2 flag ior )} reads up to u1 bytes into | ||
| 2535 : | anton | 1.94 | the buffer at addr, and returns the number of bytes read, a flag that is |
| 2536 : | false when the end of file is reached, and an error code. | ||
| 2537 : | pazsan | 1.84 | |
| 2538 : | @code{compare ( addr1 u1 addr2 u2 -- n )} compares two strings and | ||
| 2539 : | returns zero if both strings are equal. It returns a positive number if | ||
| 2540 : | the first string is lexically greater, a negative if the second string | ||
| 2541 : | is lexically greater. | ||
| 2542 : | |||
| 2543 : | We haven't seen this loop here; it has two exits. Since the @code{while} | ||
| 2544 : | exits with the number of bytes read on the stack, we have to clean up | ||
| 2545 : | that separately; that's after the @code{else}. | ||
| 2546 : | |||
| 2547 : | Usage example: | ||
| 2548 : | |||
| 2549 : | @example | ||
| 2550 : | s" The text I search is here" scan-file | ||
| 2551 : | @end example | ||
| 2552 : | |||
| 2553 : | @subsection Copy input to output | ||
| 2554 : | |||
| 2555 : | @example | ||
| 2556 : | : copy-file ( -- ) | ||
| 2557 : | begin | ||
| 2558 : | line-buffer max-line fd-in read-line throw | ||
| 2559 : | while | ||
| 2560 : | line-buffer swap fd-out write-file throw | ||
| 2561 : | repeat ; | ||
| 2562 : | @end example | ||
| 2563 : | |||
| 2564 : | @subsection Close files | ||
| 2565 : | |||
| 2566 : | @example | ||
| 2567 : | fd-in close-file throw | ||
| 2568 : | fd-out close-file throw | ||
| 2569 : | @end example | ||
| 2570 : | |||
| 2571 : | Likewise, you can put that into definitions, too: | ||
| 2572 : | |||
| 2573 : | @example | ||
| 2574 : | : close-input ( -- ) fd-in close-file throw ; | ||
| 2575 : | : close-output ( -- ) fd-out close-file throw ; | ||
| 2576 : | @end example | ||
| 2577 : | |||
| 2578 : | @assignment | ||
| 2579 : | How could you modify @code{copy-file} so that it copies until a second line is | ||
| 2580 : | matched? Can you write a program that extracts a section of a text file, | ||
| 2581 : | given the line that starts and the line that terminates that section? | ||
| 2582 : | @endassignment | ||
| 2583 : | |||
| 2584 : | @node Interpretation and Compilation Semantics and Immediacy Tutorial, Execution Tokens Tutorial, Files Tutorial, Tutorial | ||
| 2585 : | anton | 1.48 | @section Interpretation and Compilation Semantics and Immediacy |
| 2586 : | anton | 1.66 | @cindex semantics tutorial |
| 2587 : | @cindex interpretation semantics tutorial | ||
| 2588 : | @cindex compilation semantics tutorial | ||
| 2589 : | @cindex immediate, tutorial | ||
| 2590 : | anton | 1.48 | |
| 2591 : | When a word is compiled, it behaves differently from being interpreted. | ||
| 2592 : | E.g., consider @code{+}: | ||
| 2593 : | |||
| 2594 : | @example | ||
| 2595 : | 1 2 + . | ||
| 2596 : | : foo + ; | ||
| 2597 : | @end example | ||
| 2598 : | |||
| 2599 : | These two behaviours are known as compilation and interpretation | ||
| 2600 : | semantics. For normal words (e.g., @code{+}), the compilation semantics | ||
| 2601 : | is to append the interpretation semantics to the currently defined word | ||
| 2602 : | (@code{foo} in the example above). I.e., when @code{foo} is executed | ||
| 2603 : | later, the interpretation semantics of @code{+} (i.e., adding two | ||
| 2604 : | numbers) will be performed. | ||
| 2605 : | |||
| 2606 : | However, there are words with non-default compilation semantics, e.g., | ||
| 2607 : | the control-flow words like @code{if}. You can use @code{immediate} to | ||
| 2608 : | change the compilation semantics of the last defined word to be equal to | ||
| 2609 : | the interpretation semantics: | ||
| 2610 : | |||
| 2611 : | @example | ||
| 2612 : | : [FOO] ( -- ) | ||
| 2613 : | 5 . ; immediate | ||
| 2614 : | |||
| 2615 : | [FOO] | ||
| 2616 : | : bar ( -- ) | ||
| 2617 : | [FOO] ; | ||
| 2618 : | bar | ||
| 2619 : | see bar | ||
| 2620 : | @end example | ||
| 2621 : | |||
| 2622 : | Two conventions to mark words with non-default compilation semnatics are | ||
| 2623 : | names with brackets (more frequently used) and to write them all in | ||
| 2624 : | upper case (less frequently used). | ||
| 2625 : | |||
| 2626 : | In Gforth (and many other systems) you can also remove the | ||
| 2627 : | interpretation semantics with @code{compile-only} (the compilation | ||
| 2628 : | semantics is derived from the original interpretation semantics): | ||
| 2629 : | |||
| 2630 : | @example | ||
| 2631 : | : flip ( -- ) | ||
| 2632 : | 6 . ; compile-only \ but not immediate | ||
| 2633 : | flip | ||
| 2634 : | |||
| 2635 : | : flop ( -- ) | ||
| 2636 : | flip ; | ||
| 2637 : | flop | ||
| 2638 : | @end example | ||
| 2639 : | |||
| 2640 : | In this example the interpretation semantics of @code{flop} is equal to | ||
| 2641 : | the original interpretation semantics of @code{flip}. | ||
| 2642 : | |||
| 2643 : | The text interpreter has two states: in interpret state, it performs the | ||
| 2644 : | interpretation semantics of words it encounters; in compile state, it | ||
| 2645 : | performs the compilation semantics of these words. | ||
| 2646 : | |||
| 2647 : | Among other things, @code{:} switches into compile state, and @code{;} | ||
| 2648 : | switches back to interpret state. They contain the factors @code{]} | ||
| 2649 : | (switch to compile state) and @code{[} (switch to interpret state), that | ||
| 2650 : | do nothing but switch the state. | ||
| 2651 : | |||
| 2652 : | @example | ||
| 2653 : | : xxx ( -- ) | ||
| 2654 : | [ 5 . ] | ||
| 2655 : | ; | ||
| 2656 : | |||
| 2657 : | xxx | ||
| 2658 : | see xxx | ||
| 2659 : | @end example | ||
| 2660 : | |||
| 2661 : | These brackets are also the source of the naming convention mentioned | ||
| 2662 : | above. | ||
| 2663 : | |||
| 2664 : | anton | 1.66 | Reference: @ref{Interpretation and Compilation Semantics}. |
| 2665 : | |||
| 2666 : | anton | 1.48 | |
| 2667 : | @node Execution Tokens Tutorial, Exceptions Tutorial, Interpretation and Compilation Semantics and Immediacy Tutorial, Tutorial | ||
| 2668 : | @section Execution Tokens | ||
| 2669 : | anton | 1.66 | @cindex execution tokens tutorial |
| 2670 : | @cindex XT tutorial | ||
| 2671 : | anton | 1.48 | |
| 2672 : | @code{' word} gives you the execution token (XT) of a word. The XT is a | ||
| 2673 : | cell representing the interpretation semantics of a word. You can | ||
| 2674 : | execute this semantics with @code{execute}: | ||
| 2675 : | |||
| 2676 : | @example | ||
| 2677 : | ' + .s | ||
| 2678 : | 1 2 rot execute . | ||
| 2679 : | @end example | ||
| 2680 : | |||
| 2681 : | The XT is similar to a function pointer in C. However, parameter | ||
| 2682 : | passing through the stack makes it a little more flexible: | ||
| 2683 : | |||
| 2684 : | @example | ||
| 2685 : | : map-array ( ... addr u xt -- ... ) | ||
| 2686 : | anton | 1.50 | \ executes xt ( ... x -- ... ) for every element of the array starting |
| 2687 : | \ at addr and containing u elements | ||
| 2688 : | anton | 1.48 | @{ xt @} |
| 2689 : | cells over + swap ?do | ||
| 2690 : | anton | 1.50 | i @@ xt execute |
| 2691 : | anton | 1.48 | 1 cells +loop ; |
| 2692 : | |||
| 2693 : | create a 3 , 4 , 2 , -1 , 4 , | ||
| 2694 : | a 5 ' . map-array .s | ||
| 2695 : | 0 a 5 ' + map-array . | ||
| 2696 : | s" max-n" environment? drop .s | ||
| 2697 : | a 5 ' min map-array . | ||
| 2698 : | @end example | ||
| 2699 : | |||
| 2700 : | You can use map-array with the XTs of words that consume one element | ||
| 2701 : | more than they produce. In theory you can also use it with other XTs, | ||
| 2702 : | but the stack effect then depends on the size of the array, which is | ||
| 2703 : | hard to understand. | ||
| 2704 : | |||
| 2705 : | pazsan | 1.51 | Since XTs are cell-sized, you can store them in memory and manipulate |
| 2706 : | them on the stack like other cells. You can also compile the XT into a | ||
| 2707 : | anton | 1.48 | word with @code{compile,}: |
| 2708 : | |||
| 2709 : | @example | ||
| 2710 : | : foo1 ( n1 n2 -- n ) | ||
| 2711 : | [ ' + compile, ] ; | ||
| 2712 : | see foo | ||
| 2713 : | @end example | ||
| 2714 : | |||
| 2715 : | This is non-standard, because @code{compile,} has no compilation | ||
| 2716 : | semantics in the standard, but it works in good Forth systems. For the | ||
| 2717 : | broken ones, use | ||
| 2718 : | |||
| 2719 : | @example | ||
| 2720 : | : [compile,] compile, ; immediate | ||
| 2721 : | |||
| 2722 : | : foo1 ( n1 n2 -- n ) | ||
| 2723 : | [ ' + ] [compile,] ; | ||
| 2724 : | see foo | ||
| 2725 : | @end example | ||
| 2726 : | |||
| 2727 : | @code{'} is a word with default compilation semantics; it parses the | ||
| 2728 : | next word when its interpretation semantics are executed, not during | ||
| 2729 : | compilation: | ||
| 2730 : | |||
| 2731 : | @example | ||
| 2732 : | : foo ( -- xt ) | ||
| 2733 : | ' ; | ||
| 2734 : | see foo | ||
| 2735 : | : bar ( ... "word" -- ... ) | ||
| 2736 : | ' execute ; | ||
| 2737 : | see bar | ||
| 2738 : | anton | 1.60 | 1 2 bar + . |
| 2739 : | anton | 1.48 | @end example |
| 2740 : | |||
| 2741 : | You often want to parse a word during compilation and compile its XT so | ||
| 2742 : | it will be pushed on the stack at run-time. @code{[']} does this: | ||
| 2743 : | |||
| 2744 : | @example | ||
| 2745 : | : xt-+ ( -- xt ) | ||
| 2746 : | ['] + ; | ||
| 2747 : | see xt-+ | ||
| 2748 : | 1 2 xt-+ execute . | ||
| 2749 : | @end example | ||
| 2750 : | |||
| 2751 : | Many programmers tend to see @code{'} and the word it parses as one | ||
| 2752 : | unit, and expect it to behave like @code{[']} when compiled, and are | ||
| 2753 : | confused by the actual behaviour. If you are, just remember that the | ||
| 2754 : | Forth system just takes @code{'} as one unit and has no idea that it is | ||
| 2755 : | a parsing word (attempts to convenience programmers in this issue have | ||
| 2756 : | usually resulted in even worse pitfalls, see | ||
| 2757 : | anton | 1.66 | @uref{http://www.complang.tuwien.ac.at/papers/ertl98.ps.gz, |
| 2758 : | @code{State}-smartness---Why it is evil and How to Exorcise it}). | ||
| 2759 : | anton | 1.48 | |
| 2760 : | Note that the state of the interpreter does not come into play when | ||
| 2761 : | pazsan | 1.51 | creating and executing XTs. I.e., even when you execute @code{'} in |
| 2762 : | anton | 1.48 | compile state, it still gives you the interpretation semantics. And |
| 2763 : | whatever that state is, @code{execute} performs the semantics | ||
| 2764 : | anton | 1.66 | represented by the XT (i.e., for XTs produced with @code{'} the |
| 2765 : | interpretation semantics). | ||
| 2766 : | |||
| 2767 : | Reference: @ref{Tokens for Words}. | ||
| 2768 : | anton | 1.48 | |
| 2769 : | |||
| 2770 : | @node Exceptions Tutorial, Defining Words Tutorial, Execution Tokens Tutorial, Tutorial | ||
| 2771 : | @section Exceptions | ||
| 2772 : | anton | 1.66 | @cindex exceptions tutorial |
| 2773 : | anton | 1.48 | |
| 2774 : | @code{throw ( n -- )} causes an exception unless n is zero. | ||
| 2775 : | |||
| 2776 : | @example | ||
| 2777 : | 100 throw .s | ||
| 2778 : | 0 throw .s | ||
| 2779 : | @end example | ||
| 2780 : | |||
| 2781 : | @code{catch ( ... xt -- ... n )} behaves similar to @code{execute}, but | ||
| 2782 : | it catches exceptions and pushes the number of the exception on the | ||
| 2783 : | stack (or 0, if the xt executed without exception). If there was an | ||
| 2784 : | exception, the stacks have the same depth as when entering @code{catch}: | ||
| 2785 : | |||
| 2786 : | @example | ||
| 2787 : | .s | ||
| 2788 : | 3 0 ' / catch .s | ||
| 2789 : | 3 2 ' / catch .s | ||
| 2790 : | @end example | ||
| 2791 : | |||
| 2792 : | @assignment | ||
| 2793 : | Try the same with @code{execute} instead of @code{catch}. | ||
| 2794 : | @endassignment | ||
| 2795 : | |||
| 2796 : | @code{Throw} always jumps to the dynamically next enclosing | ||
| 2797 : | @code{catch}, even if it has to leave several call levels to achieve | ||
| 2798 : | this: | ||
| 2799 : | |||
| 2800 : | @example | ||
| 2801 : | : foo 100 throw ; | ||
| 2802 : | : foo1 foo ." after foo" ; | ||
| 2803 : | pazsan | 1.51 | : bar ['] foo1 catch ; |
| 2804 : | anton | 1.60 | bar . |
| 2805 : | anton | 1.48 | @end example |
| 2806 : | |||
| 2807 : | It is often important to restore a value upon leaving a definition, even | ||
| 2808 : | if the definition is left through an exception. You can ensure this | ||
| 2809 : | like this: | ||
| 2810 : | |||
| 2811 : | @example | ||
| 2812 : | : ... | ||
| 2813 : | save-x | ||
| 2814 : | pazsan | 1.51 | ['] word-changing-x catch ( ... n ) |
| 2815 : | anton | 1.48 | restore-x |
| 2816 : | ( ... n ) throw ; | ||
| 2817 : | @end example | ||
| 2818 : | |||
| 2819 : | anton | 1.55 | Gforth provides an alternative syntax in addition to @code{catch}: |
| 2820 : | anton | 1.48 | @code{try ... recover ... endtry}. If the code between @code{try} and |
| 2821 : | @code{recover} has an exception, the stack depths are restored, the | ||
| 2822 : | exception number is pushed on the stack, and the code between | ||
| 2823 : | @code{recover} and @code{endtry} is performed. E.g., the definition for | ||
| 2824 : | @code{catch} is | ||
| 2825 : | |||
| 2826 : | @example | ||
| 2827 : | : catch ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception | ||
| 2828 : | try | ||
| 2829 : | execute 0 | ||
| 2830 : | recover | ||
| 2831 : | nip | ||
| 2832 : | endtry ; | ||
| 2833 : | @end example | ||
| 2834 : | |||
| 2835 : | The equivalent to the restoration code above is | ||
| 2836 : | |||
| 2837 : | @example | ||
| 2838 : | : ... | ||
| 2839 : | save-x | ||
| 2840 : | try | ||
| 2841 : | anton | 1.92 | word-changing-x 0 |
| 2842 : | recover endtry | ||
| 2843 : | anton | 1.48 | restore-x |
| 2844 : | throw ; | ||
| 2845 : | @end example | ||
| 2846 : | |||
| 2847 : | anton | 1.92 | This works if @code{word-changing-x} does not change the stack depth, |
| 2848 : | otherwise you should add some code between @code{recover} and | ||
| 2849 : | @code{endtry} to balance the stack. | ||
| 2850 : | anton | 1.48 | |
| 2851 : | anton | 1.66 | Reference: @ref{Exception Handling}. |
| 2852 : | |||
| 2853 : | anton | 1.48 | |
| 2854 : | @node Defining Words Tutorial, Arrays and Records Tutorial, Exceptions Tutorial, Tutorial | ||
| 2855 : | @section Defining Words | ||
| 2856 : | anton | 1.66 | @cindex defining words tutorial |
| 2857 : | @cindex does> tutorial | ||
| 2858 : | @cindex create...does> tutorial | ||
| 2859 : | |||
| 2860 : | @c before semantics? | ||
| 2861 : | anton | 1.48 | |
| 2862 : | @code{:}, @code{create}, and @code{variable} are definition words: They | ||
| 2863 : | define other words. @code{Constant} is another definition word: | ||
| 2864 : | |||
| 2865 : | @example | ||
| 2866 : | 5 constant foo | ||
| 2867 : | foo . | ||
| 2868 : | @end example | ||
| 2869 : | |||
| 2870 : | You can also use the prefixes @code{2} (double-cell) and @code{f} | ||
| 2871 : | (floating point) with @code{variable} and @code{constant}. | ||
| 2872 : | |||
| 2873 : | You can also define your own defining words. E.g.: | ||
| 2874 : | |||
| 2875 : | @example | ||
| 2876 : | : variable ( "name" -- ) | ||
| 2877 : | create 0 , ; | ||
| 2878 : | @end example | ||
| 2879 : | |||
| 2880 : | You can also define defining words that create words that do something | ||
| 2881 : | other than just producing their address: | ||
| 2882 : | |||
| 2883 : | @example | ||
| 2884 : | : constant ( n "name" -- ) | ||
| 2885 : | create , | ||
| 2886 : | does> ( -- n ) | ||
| 2887 : | anton | 1.50 | ( addr ) @@ ; |
| 2888 : | anton | 1.48 | |
| 2889 : | 5 constant foo | ||
| 2890 : | foo . | ||
| 2891 : | @end example | ||
| 2892 : | |||
| 2893 : | The definition of @code{constant} above ends at the @code{does>}; i.e., | ||
| 2894 : | @code{does>} replaces @code{;}, but it also does something else: It | ||
| 2895 : | changes the last defined word such that it pushes the address of the | ||
| 2896 : | body of the word and then performs the code after the @code{does>} | ||
| 2897 : | whenever it is called. | ||
| 2898 : | |||
| 2899 : | In the example above, @code{constant} uses @code{,} to store 5 into the | ||
| 2900 : | body of @code{foo}. When @code{foo} executes, it pushes the address of | ||
| 2901 : | the body onto the stack, then (in the code after the @code{does>}) | ||
| 2902 : | fetches the 5 from there. | ||
| 2903 : | |||
| 2904 : | The stack comment near the @code{does>} reflects the stack effect of the | ||
| 2905 : | defined word, not the stack effect of the code after the @code{does>} | ||
| 2906 : | (the difference is that the code expects the address of the body that | ||
| 2907 : | the stack comment does not show). | ||
| 2908 : | |||
| 2909 : | You can use these definition words to do factoring in cases that involve | ||
| 2910 : | (other) definition words. E.g., a field offset is always added to an | ||
| 2911 : | address. Instead of defining | ||
| 2912 : | |||
| 2913 : | @example | ||
| 2914 : | 2 cells constant offset-field1 | ||
| 2915 : | @end example | ||
| 2916 : | |||
| 2917 : | and using this like | ||
| 2918 : | |||
| 2919 : | @example | ||
| 2920 : | ( addr ) offset-field1 + | ||
| 2921 : | @end example | ||
| 2922 : | |||
| 2923 : | you can define a definition word | ||
| 2924 : | |||
| 2925 : | @example | ||
| 2926 : | : simple-field ( n "name" -- ) | ||
| 2927 : | create , | ||
| 2928 : | does> ( n1 -- n1+n ) | ||
| 2929 : | anton | 1.50 | ( addr ) @@ + ; |
| 2930 : | anton | 1.48 | @end example |
| 2931 : | crook | 1.21 | |
| 2932 : | anton | 1.48 | Definition and use of field offsets now look like this: |
| 2933 : | crook | 1.21 | |
| 2934 : | anton | 1.48 | @example |
| 2935 : | 2 cells simple-field field1 | ||
| 2936 : | anton | 1.60 | create mystruct 4 cells allot |
| 2937 : | mystruct .s field1 .s drop | ||
| 2938 : | anton | 1.48 | @end example |
| 2939 : | crook | 1.21 | |
| 2940 : | anton | 1.48 | If you want to do something with the word without performing the code |
| 2941 : | after the @code{does>}, you can access the body of a @code{create}d word | ||
| 2942 : | with @code{>body ( xt -- addr )}: | ||
| 2943 : | crook | 1.21 | |
| 2944 : | anton | 1.48 | @example |
| 2945 : | : value ( n "name" -- ) | ||
| 2946 : | create , | ||
| 2947 : | does> ( -- n1 ) | ||
| 2948 : | anton | 1.50 | @@ ; |
| 2949 : | anton | 1.48 | : to ( n "name" -- ) |
| 2950 : | ' >body ! ; | ||
| 2951 : | crook | 1.21 | |
| 2952 : | anton | 1.48 | 5 value foo |
| 2953 : | foo . | ||
| 2954 : | 7 to foo | ||
| 2955 : | foo . | ||
| 2956 : | @end example | ||
| 2957 : | crook | 1.21 | |
| 2958 : | anton | 1.48 | @assignment |
| 2959 : | Define @code{defer ( "name" -- )}, which creates a word that stores an | ||
| 2960 : | XT (at the start the XT of @code{abort}), and upon execution | ||
| 2961 : | @code{execute}s the XT. Define @code{is ( xt "name" -- )} that stores | ||
| 2962 : | @code{xt} into @code{name}, a word defined with @code{defer}. Indirect | ||
| 2963 : | recursion is one application of @code{defer}. | ||
| 2964 : | @endassignment | ||
| 2965 : | crook | 1.29 | |
| 2966 : | anton | 1.66 | Reference: @ref{User-defined Defining Words}. |
| 2967 : | |||
| 2968 : | |||
| 2969 : | anton | 1.48 | @node Arrays and Records Tutorial, POSTPONE Tutorial, Defining Words Tutorial, Tutorial |
| 2970 : | @section Arrays and Records | ||
| 2971 : | anton | 1.66 | @cindex arrays tutorial |
| 2972 : | @cindex records tutorial | ||
| 2973 : | @cindex structs tutorial | ||
| 2974 : | crook | 1.29 | |
| 2975 : | anton | 1.48 | Forth has no standard words for defining data structures such as arrays |
| 2976 : | and records (structs in C terminology), but you can build them yourself | ||
| 2977 : | based on address arithmetic. You can also define words for defining | ||
| 2978 : | arrays and records (@pxref{Defining Words Tutorial,, Defining Words}). | ||
| 2979 : | crook | 1.29 | |
| 2980 : | anton | 1.48 | One of the first projects a Forth newcomer sets out upon when learning |
| 2981 : | about defining words is an array defining word (possibly for | ||
| 2982 : | n-dimensional arrays). Go ahead and do it, I did it, too; you will | ||
| 2983 : | learn something from it. However, don't be disappointed when you later | ||
| 2984 : | learn that you have little use for these words (inappropriate use would | ||
| 2985 : | be even worse). I have not yet found a set of useful array words yet; | ||
| 2986 : | the needs are just too diverse, and named, global arrays (the result of | ||
| 2987 : | naive use of defining words) are often not flexible enough (e.g., | ||
| 2988 : | anton | 1.66 | consider how to pass them as parameters). Another such project is a set |
| 2989 : | of words to help dealing with strings. | ||
| 2990 : | crook | 1.29 | |
| 2991 : | anton | 1.48 | On the other hand, there is a useful set of record words, and it has |
| 2992 : | been defined in @file{compat/struct.fs}; these words are predefined in | ||
| 2993 : | Gforth. They are explained in depth elsewhere in this manual (see | ||
| 2994 : | @pxref{Structures}). The @code{simple-field} example above is | ||
| 2995 : | simplified variant of fields in this package. | ||
| 2996 : | crook | 1.21 | |
| 2997 : | |||
| 2998 : | anton | 1.48 | @node POSTPONE Tutorial, Literal Tutorial, Arrays and Records Tutorial, Tutorial |
| 2999 : | @section @code{POSTPONE} | ||
| 3000 : | anton | 1.66 | @cindex postpone tutorial |
| 3001 : | crook | 1.21 | |
| 3002 : | anton | 1.48 | You can compile the compilation semantics (instead of compiling the |
| 3003 : | interpretation semantics) of a word with @code{POSTPONE}: | ||
| 3004 : | crook | 1.21 | |
| 3005 : | anton | 1.48 | @example |
| 3006 : | : MY-+ ( Compilation: -- ; Run-time of compiled code: n1 n2 -- n ) | ||
| 3007 : | pazsan | 1.51 | POSTPONE + ; immediate |
| 3008 : | anton | 1.48 | : foo ( n1 n2 -- n ) |
| 3009 : | MY-+ ; | ||
| 3010 : | 1 2 foo . | ||
| 3011 : | see foo | ||
| 3012 : | @end example | ||
| 3013 : | crook | 1.21 | |
| 3014 : | anton | 1.48 | During the definition of @code{foo} the text interpreter performs the |
| 3015 : | compilation semantics of @code{MY-+}, which performs the compilation | ||
| 3016 : | semantics of @code{+}, i.e., it compiles @code{+} into @code{foo}. | ||
| 3017 : | |||
| 3018 : | This example also displays separate stack comments for the compilation | ||
| 3019 : | semantics and for the stack effect of the compiled code. For words with | ||
| 3020 : | default compilation semantics these stack effects are usually not | ||
| 3021 : | displayed; the stack effect of the compilation semantics is always | ||
| 3022 : | @code{( -- )} for these words, the stack effect for the compiled code is | ||
| 3023 : | the stack effect of the interpretation semantics. | ||
| 3024 : | |||
| 3025 : | Note that the state of the interpreter does not come into play when | ||
| 3026 : | performing the compilation semantics in this way. You can also perform | ||
| 3027 : | it interpretively, e.g.: | ||
| 3028 : | |||
| 3029 : | @example | ||
| 3030 : | : foo2 ( n1 n2 -- n ) | ||
| 3031 : | [ MY-+ ] ; | ||
| 3032 : | 1 2 foo . | ||
| 3033 : | see foo | ||
| 3034 : | @end example | ||
| 3035 : | crook | 1.21 | |
| 3036 : | anton | 1.48 | However, there are some broken Forth systems where this does not always |
| 3037 : | crook | 1.62 | work, and therefore this practice was been declared non-standard in |
| 3038 : | anton | 1.48 | 1999. |
| 3039 : | @c !! repair.fs | ||
| 3040 : | |||
| 3041 : | Here is another example for using @code{POSTPONE}: | ||
| 3042 : | crook | 1.44 | |
| 3043 : | anton | 1.48 | @example |
| 3044 : | : MY-- ( Compilation: -- ; Run-time of compiled code: n1 n2 -- n ) | ||
| 3045 : | POSTPONE negate POSTPONE + ; immediate compile-only | ||
| 3046 : | : bar ( n1 n2 -- n ) | ||
| 3047 : | MY-- ; | ||
| 3048 : | 2 1 bar . | ||
| 3049 : | see bar | ||
| 3050 : | @end example | ||
| 3051 : | crook | 1.21 | |
| 3052 : | anton | 1.48 | You can define @code{ENDIF} in this way: |
| 3053 : | crook | 1.21 | |
| 3054 : | anton | 1.48 | @example |
| 3055 : | : ENDIF ( Compilation: orig -- ) | ||
| 3056 : | POSTPONE then ; immediate | ||
| 3057 : | @end example | ||
| 3058 : | crook | 1.21 | |
| 3059 : | anton | 1.48 | @assignment |
| 3060 : | Write @code{MY-2DUP} that has compilation semantics equivalent to | ||
| 3061 : | @code{2dup}, but compiles @code{over over}. | ||
| 3062 : | @endassignment | ||
| 3063 : | crook | 1.29 | |
| 3064 : | anton | 1.66 | @c !! @xref{Macros} for reference |
| 3065 : | |||
| 3066 : | |||
| 3067 : | anton | 1.48 | @node Literal Tutorial, Advanced macros Tutorial, POSTPONE Tutorial, Tutorial |
| 3068 : | @section @code{Literal} | ||
| 3069 : | anton | 1.66 | @cindex literal tutorial |
| 3070 : | crook | 1.29 | |
| 3071 : | anton | 1.48 | You cannot @code{POSTPONE} numbers: |
| 3072 : | crook | 1.21 | |
| 3073 : | anton | 1.48 | @example |
| 3074 : | : [FOO] POSTPONE 500 ; immediate | ||
| 3075 : | crook | 1.21 | @end example |
| 3076 : | |||
| 3077 : | anton | 1.48 | Instead, you can use @code{LITERAL (compilation: n --; run-time: -- n )}: |
| 3078 : | crook | 1.29 | |
| 3079 : | anton | 1.48 | @example |
| 3080 : | : [FOO] ( compilation: --; run-time: -- n ) | ||
| 3081 : | 500 POSTPONE literal ; immediate | ||
| 3082 : | crook | 1.29 | |
| 3083 : | anton | 1.60 | : flip [FOO] ; |
| 3084 : | anton | 1.48 | flip . |
| 3085 : | see flip | ||
| 3086 : | @end example | ||
| 3087 : | crook | 1.29 | |
| 3088 : | anton | 1.48 | @code{LITERAL} consumes a number at compile-time (when it's compilation |
| 3089 : | semantics are executed) and pushes it at run-time (when the code it | ||
| 3090 : | compiled is executed). A frequent use of @code{LITERAL} is to compile a | ||
| 3091 : | number computed at compile time into the current word: | ||
| 3092 : | crook | 1.29 | |
| 3093 : | anton | 1.48 | @example |
| 3094 : | : bar ( -- n ) | ||
| 3095 : | [ 2 2 + ] literal ; | ||
| 3096 : | see bar | ||
| 3097 : | @end example | ||
| 3098 : | crook | 1.29 | |
| 3099 : | anton | 1.48 | @assignment |
| 3100 : | Write @code{]L} which allows writing the example above as @code{: bar ( | ||
| 3101 : | -- n ) [ 2 2 + ]L ;} | ||
| 3102 : | @endassignment | ||
| 3103 : | |||
| 3104 : | anton | 1.66 | @c !! @xref{Macros} for reference |
| 3105 : | |||
| 3106 : | anton | 1.48 | |
| 3107 : | @node Advanced macros Tutorial, Compilation Tokens Tutorial, Literal Tutorial, Tutorial | ||
| 3108 : | @section Advanced macros | ||
| 3109 : | anton | 1.66 | @cindex macros, advanced tutorial |
| 3110 : | @cindex run-time code generation, tutorial | ||
| 3111 : | anton | 1.48 | |
| 3112 : | anton | 1.66 | Reconsider @code{map-array} from @ref{Execution Tokens Tutorial,, |
| 3113 : | Execution Tokens}. It frequently performs @code{execute}, a relatively | ||
| 3114 : | expensive operation in some Forth implementations. You can use | ||
| 3115 : | anton | 1.48 | @code{compile,} and @code{POSTPONE} to eliminate these @code{execute}s |
| 3116 : | and produce a word that contains the word to be performed directly: | ||
| 3117 : | |||
| 3118 : | @c use ]] ... [[ | ||
| 3119 : | @example | ||
| 3120 : | : compile-map-array ( compilation: xt -- ; run-time: ... addr u -- ... ) | ||
| 3121 : | \ at run-time, execute xt ( ... x -- ... ) for each element of the | ||
| 3122 : | \ array beginning at addr and containing u elements | ||
| 3123 : | @{ xt @} | ||
| 3124 : | POSTPONE cells POSTPONE over POSTPONE + POSTPONE swap POSTPONE ?do | ||
| 3125 : | anton | 1.50 | POSTPONE i POSTPONE @@ xt compile, |
| 3126 : | anton | 1.48 | 1 cells POSTPONE literal POSTPONE +loop ; |
| 3127 : | |||
| 3128 : | : sum-array ( addr u -- n ) | ||
| 3129 : | 0 rot rot [ ' + compile-map-array ] ; | ||
| 3130 : | see sum-array | ||
| 3131 : | a 5 sum-array . | ||
| 3132 : | @end example | ||
| 3133 : | |||
| 3134 : | You can use the full power of Forth for generating the code; here's an | ||
| 3135 : | example where the code is generated in a loop: | ||
| 3136 : | |||
| 3137 : | @example | ||
| 3138 : | : compile-vmul-step ( compilation: n --; run-time: n1 addr1 -- n2 addr2 ) | ||
| 3139 : | \ n2=n1+(addr1)*n, addr2=addr1+cell | ||
| 3140 : | anton | 1.50 | POSTPONE tuck POSTPONE @@ |
| 3141 : | anton | 1.48 | POSTPONE literal POSTPONE * POSTPONE + |
| 3142 : | POSTPONE swap POSTPONE cell+ ; | ||
| 3143 : | |||
| 3144 : | : compile-vmul ( compilation: addr1 u -- ; run-time: addr2 -- n ) | ||
| 3145 : | pazsan | 1.51 | \ n=v1*v2 (inner product), where the v_i are represented as addr_i u |
| 3146 : | anton | 1.48 | 0 postpone literal postpone swap |
| 3147 : | [ ' compile-vmul-step compile-map-array ] | ||
| 3148 : | postpone drop ; | ||
| 3149 : | see compile-vmul | ||
| 3150 : | |||
| 3151 : | : a-vmul ( addr -- n ) | ||
| 3152 : | pazsan | 1.51 | \ n=a*v, where v is a vector that's as long as a and starts at addr |
| 3153 : | anton | 1.48 | [ a 5 compile-vmul ] ; |
| 3154 : | see a-vmul | ||
| 3155 : | a a-vmul . | ||
| 3156 : | @end example | ||
| 3157 : | |||
| 3158 : | This example uses @code{compile-map-array} to show off, but you could | ||
| 3159 : | anton | 1.66 | also use @code{map-array} instead (try it now!). |
| 3160 : | anton | 1.48 | |
| 3161 : | You can use this technique for efficient multiplication of large | ||
| 3162 : | matrices. In matrix multiplication, you multiply every line of one | ||
| 3163 : | matrix with every column of the other matrix. You can generate the code | ||
| 3164 : | for one line once, and use it for every column. The only downside of | ||
| 3165 : | this technique is that it is cumbersome to recover the memory consumed | ||
| 3166 : | by the generated code when you are done (and in more complicated cases | ||
| 3167 : | it is not possible portably). | ||
| 3168 : | |||
| 3169 : | anton | 1.66 | @c !! @xref{Macros} for reference |
| 3170 : | |||
| 3171 : | |||
| 3172 : | anton | 1.48 | @node Compilation Tokens Tutorial, Wordlists and Search Order Tutorial, Advanced macros Tutorial, Tutorial |
| 3173 : | @section Compilation Tokens | ||
| 3174 : | anton | 1.66 | @cindex compilation tokens, tutorial |
| 3175 : | @cindex CT, tutorial | ||
| 3176 : | anton | 1.48 | |
| 3177 : | This section is Gforth-specific. You can skip it. | ||
| 3178 : | |||
| 3179 : | @code{' word compile,} compiles the interpretation semantics. For words | ||
| 3180 : | with default compilation semantics this is the same as performing the | ||
| 3181 : | compilation semantics. To represent the compilation semantics of other | ||
| 3182 : | words (e.g., words like @code{if} that have no interpretation | ||
| 3183 : | semantics), Gforth has the concept of a compilation token (CT, | ||
| 3184 : | consisting of two cells), and words @code{comp'} and @code{[comp']}. | ||
| 3185 : | You can perform the compilation semantics represented by a CT with | ||
| 3186 : | @code{execute}: | ||
| 3187 : | crook | 1.29 | |
| 3188 : | anton | 1.48 | @example |
| 3189 : | : foo2 ( n1 n2 -- n ) | ||
| 3190 : | [ comp' + execute ] ; | ||
| 3191 : | see foo | ||
| 3192 : | @end example | ||
| 3193 : | crook | 1.29 | |
| 3194 : | anton | 1.48 | You can compile the compilation semantics represented by a CT with |
| 3195 : | @code{postpone,}: | ||
| 3196 : | anton | 1.30 | |
| 3197 : | anton | 1.48 | @example |
| 3198 : | : foo3 ( -- ) | ||
| 3199 : | [ comp' + postpone, ] ; | ||
| 3200 : | see foo3 | ||
| 3201 : | @end example | ||
| 3202 : | anton | 1.30 | |
| 3203 : | pazsan | 1.51 | @code{[ comp' word postpone, ]} is equivalent to @code{POSTPONE word}. |
| 3204 : | anton | 1.48 | @code{comp'} is particularly useful for words that have no |
| 3205 : | interpretation semantics: | ||
| 3206 : | crook | 1.29 | |
| 3207 : | anton | 1.30 | @example |
| 3208 : | anton | 1.48 | ' if |
| 3209 : | anton | 1.60 | comp' if .s 2drop |
| 3210 : | anton | 1.30 | @end example |
| 3211 : | |||
| 3212 : | anton | 1.66 | Reference: @ref{Tokens for Words}. |
| 3213 : | |||
| 3214 : | crook | 1.29 | |
| 3215 : | anton | 1.48 | @node Wordlists and Search Order Tutorial, , Compilation Tokens Tutorial, Tutorial |
| 3216 : | @section Wordlists and Search Order | ||
| 3217 : | anton | 1.66 | @cindex wordlists tutorial |
| 3218 : | @cindex search order, tutorial | ||
| 3219 : | anton | 1.48 | |
| 3220 : | The dictionary is not just a memory area that allows you to allocate | ||
| 3221 : | memory with @code{allot}, it also contains the Forth words, arranged in | ||
| 3222 : | several wordlists. When searching for a word in a wordlist, | ||
| 3223 : | conceptually you start searching at the youngest and proceed towards | ||
| 3224 : | older words (in reality most systems nowadays use hash-tables); i.e., if | ||
| 3225 : | you define a word with the same name as an older word, the new word | ||
| 3226 : | shadows the older word. | ||
| 3227 : | |||
| 3228 : | Which wordlists are searched in which order is determined by the search | ||
| 3229 : | order. You can display the search order with @code{order}. It displays | ||
| 3230 : | first the search order, starting with the wordlist searched first, then | ||
| 3231 : | it displays the wordlist that will contain newly defined words. | ||
| 3232 : | crook | 1.21 | |
| 3233 : | anton | 1.48 | You can create a new, empty wordlist with @code{wordlist ( -- wid )}: |
| 3234 : | crook | 1.21 | |
| 3235 : | anton | 1.48 | @example |
| 3236 : | wordlist constant mywords | ||
| 3237 : | @end example | ||
| 3238 : | crook | 1.21 | |
| 3239 : | anton | 1.48 | @code{Set-current ( wid -- )} sets the wordlist that will contain newly |
| 3240 : | defined words (the @emph{current} wordlist): | ||
| 3241 : | crook | 1.21 | |
| 3242 : | anton | 1.48 | @example |
| 3243 : | mywords set-current | ||
| 3244 : | order | ||
| 3245 : | @end example | ||
| 3246 : | crook | 1.26 | |
| 3247 : | anton | 1.48 | Gforth does not display a name for the wordlist in @code{mywords} |
| 3248 : | because this wordlist was created anonymously with @code{wordlist}. | ||
| 3249 : | crook | 1.21 | |
| 3250 : | anton | 1.48 | You can get the current wordlist with @code{get-current ( -- wid)}. If |
| 3251 : | you want to put something into a specific wordlist without overall | ||
| 3252 : | effect on the current wordlist, this typically looks like this: | ||
| 3253 : | crook | 1.21 | |
| 3254 : | anton | 1.48 | @example |
| 3255 : | get-current mywords set-current ( wid ) | ||
| 3256 : | create someword | ||
| 3257 : | ( wid ) set-current | ||
| 3258 : | @end example | ||
| 3259 : | crook | 1.21 | |
| 3260 : | anton | 1.48 | You can write the search order with @code{set-order ( wid1 .. widn n -- |
| 3261 : | )} and read it with @code{get-order ( -- wid1 .. widn n )}. The first | ||
| 3262 : | searched wordlist is topmost. | ||
| 3263 : | crook | 1.21 | |
| 3264 : | anton | 1.48 | @example |
| 3265 : | get-order mywords swap 1+ set-order | ||
| 3266 : | order | ||
| 3267 : | @end example | ||
| 3268 : | crook | 1.21 | |
| 3269 : | anton | 1.48 | Yes, the order of wordlists in the output of @code{order} is reversed |
| 3270 : | from stack comments and the output of @code{.s} and thus unintuitive. | ||
| 3271 : | crook | 1.21 | |
| 3272 : | anton | 1.48 | @assignment |
| 3273 : | Define @code{>order ( wid -- )} with adds @code{wid} as first searched | ||
| 3274 : | wordlist to the search order. Define @code{previous ( -- )}, which | ||
| 3275 : | removes the first searched wordlist from the search order. Experiment | ||
| 3276 : | with boundary conditions (you will see some crashes or situations that | ||
| 3277 : | are hard or impossible to leave). | ||
| 3278 : | @endassignment | ||
| 3279 : | crook | 1.21 | |
| 3280 : | anton | 1.48 | The search order is a powerful foundation for providing features similar |
| 3281 : | to Modula-2 modules and C++ namespaces. However, trying to modularize | ||
| 3282 : | programs in this way has disadvantages for debugging and reuse/factoring | ||
| 3283 : | that overcome the advantages in my experience (I don't do huge projects, | ||
| 3284 : | anton | 1.55 | though). These disadvantages are not so clear in other |
| 3285 : | anton | 1.82 | languages/programming environments, because these languages are not so |
| 3286 : | anton | 1.48 | strong in debugging and reuse. |
| 3287 : | crook | 1.21 | |
| 3288 : | anton | 1.66 | @c !! example |
| 3289 : | |||
| 3290 : | Reference: @ref{Word Lists}. | ||
| 3291 : | crook | 1.21 | |
| 3292 : | crook | 1.29 | @c ****************************************************************** |
| 3293 : | anton | 1.48 | @node Introduction, Words, Tutorial, Top |
| 3294 : | crook | 1.29 | @comment node-name, next, previous, up |
| 3295 : | @chapter An Introduction to ANS Forth | ||
| 3296 : | @cindex Forth - an introduction | ||
| 3297 : | crook | 1.21 | |
| 3298 : | anton | 1.83 | The difference of this chapter from the Tutorial (@pxref{Tutorial}) is |
| 3299 : | that it is slower-paced in its examples, but uses them to dive deep into | ||
| 3300 : | explaining Forth internals (not covered by the Tutorial). Apart from | ||
| 3301 : | that, this chapter covers far less material. It is suitable for reading | ||
| 3302 : | without using a computer. | ||
| 3303 : | |||
| 3304 : | crook | 1.29 | The primary purpose of this manual is to document Gforth. However, since |
| 3305 : | Forth is not a widely-known language and there is a lack of up-to-date | ||
| 3306 : | teaching material, it seems worthwhile to provide some introductory | ||
| 3307 : | anton | 1.49 | material. For other sources of Forth-related |
| 3308 : | information, see @ref{Forth-related information}. | ||
| 3309 : | crook | 1.21 | |
| 3310 : | crook | 1.29 | The examples in this section should work on any ANS Forth; the |
| 3311 : | output shown was produced using Gforth. Each example attempts to | ||
| 3312 : | reproduce the exact output that Gforth produces. If you try out the | ||
| 3313 : | examples (and you should), what you should type is shown @kbd{like this} | ||
| 3314 : | and Gforth's response is shown @code{like this}. The single exception is | ||
| 3315 : | anton | 1.30 | that, where the example shows @key{RET} it means that you should |
| 3316 : | crook | 1.29 | press the ``carriage return'' key. Unfortunately, some output formats for |
| 3317 : | this manual cannot show the difference between @kbd{this} and | ||
| 3318 : | @code{this} which will make trying out the examples harder (but not | ||
| 3319 : | impossible). | ||
| 3320 : | crook | 1.21 | |
| 3321 : | crook | 1.29 | Forth is an unusual language. It provides an interactive development |
| 3322 : | environment which includes both an interpreter and compiler. Forth | ||
| 3323 : | programming style encourages you to break a problem down into many | ||
| 3324 : | @cindex factoring | ||
| 3325 : | small fragments (@dfn{factoring}), and then to develop and test each | ||
| 3326 : | fragment interactively. Forth advocates assert that breaking the | ||
| 3327 : | edit-compile-test cycle used by conventional programming languages can | ||
| 3328 : | lead to great productivity improvements. | ||
| 3329 : | crook | 1.21 | |
| 3330 : | crook | 1.29 | @menu |
| 3331 : | anton | 1.67 | * Introducing the Text Interpreter:: |
| 3332 : | * Stacks and Postfix notation:: | ||
| 3333 : | * Your first definition:: | ||
| 3334 : | * How does that work?:: | ||
| 3335 : | * Forth is written in Forth:: | ||
| 3336 : | * Review - elements of a Forth system:: | ||
| 3337 : | * Where to go next:: | ||
| 3338 : | * Exercises:: | ||
| 3339 : | crook | 1.29 | @end menu |
| 3340 : | crook | 1.21 | |
| 3341 : | crook | 1.29 | @comment ---------------------------------------------- |
| 3342 : | @node Introducing the Text Interpreter, Stacks and Postfix notation, Introduction, Introduction | ||
| 3343 : | @section Introducing the Text Interpreter | ||
| 3344 : | @cindex text interpreter | ||
| 3345 : | @cindex outer interpreter | ||
| 3346 : | crook | 1.21 | |
| 3347 : | anton | 1.30 | @c IMO this is too detailed and the pace is too slow for |
| 3348 : | @c an introduction. If you know German, take a look at | ||
| 3349 : | @c http://www.complang.tuwien.ac.at/anton/lvas/skriptum-stack.html | ||
| 3350 : | @c to see how I do it - anton | ||
| 3351 : | |||
| 3352 : | crook | 1.44 | @c nac-> Where I have accepted your comments 100% and modified the text |
| 3353 : | @c accordingly, I have deleted your comments. Elsewhere I have added a | ||
| 3354 : | @c response like this to attempt to rationalise what I have done. Of | ||
| 3355 : | @c course, this is a very clumsy mechanism for something that would be | ||
| 3356 : | @c done far more efficiently over a beer. Please delete any dialogue | ||
| 3357 : | @c you consider closed. | ||
| 3358 : | |||
| 3359 : | crook | 1.29 | When you invoke the Forth image, you will see a startup banner printed |
| 3360 : | and nothing else (if you have Gforth installed on your system, try | ||
| 3361 : | anton | 1.30 | invoking it now, by typing @kbd{gforth@key{RET}}). Forth is now running |
| 3362 : | crook | 1.29 | its command line interpreter, which is called the @dfn{Text Interpreter} |
| 3363 : | (also known as the @dfn{Outer Interpreter}). (You will learn a lot | ||
| 3364 : | anton | 1.49 | about the text interpreter as you read through this chapter, for more |
| 3365 : | detail @pxref{The Text Interpreter}). | ||
| 3366 : | crook | 1.21 | |
| 3367 : | crook | 1.29 | Although it's not obvious, Forth is actually waiting for your |
| 3368 : | anton | 1.30 | input. Type a number and press the @key{RET} key: |
| 3369 : | crook | 1.21 | |
| 3370 : | crook | 1.26 | @example |
| 3371 : | anton | 1.30 | @kbd{45@key{RET}} ok |
| 3372 : | crook | 1.26 | @end example |
| 3373 : | crook | 1.21 | |
| 3374 : | crook | 1.29 | Rather than give you a prompt to invite you to input something, the text |
| 3375 : | interpreter prints a status message @i{after} it has processed a line | ||
| 3376 : | of input. The status message in this case (``@code{ ok}'' followed by | ||
| 3377 : | carriage-return) indicates that the text interpreter was able to process | ||
| 3378 : | all of your input successfully. Now type something illegal: | ||
| 3379 : | |||
| 3380 : | @example | ||
| 3381 : | anton | 1.30 | @kbd{qwer341@key{RET}} |
| 3382 : | crook | 1.29 | :1: Undefined word |
| 3383 : | qwer341 | ||
| 3384 : | ^^^^^^^ | ||
| 3385 : | $400D2BA8 Bounce | ||
| 3386 : | $400DBDA8 no.extensions | ||
| 3387 : | @end example | ||
| 3388 : | crook | 1.23 | |
| 3389 : | crook | 1.29 | The exact text, other than the ``Undefined word'' may differ slightly on |
| 3390 : | your system, but the effect is the same; when the text interpreter | ||
| 3391 : | detects an error, it discards any remaining text on a line, resets | ||
| 3392 : | anton | 1.49 | certain internal state and prints an error message. For a detailed description of error messages see @ref{Error |
| 3393 : | messages}. | ||
| 3394 : | crook | 1.23 | |
| 3395 : | crook | 1.29 | The text interpreter waits for you to press carriage-return, and then |
| 3396 : | processes your input line. Starting at the beginning of the line, it | ||
| 3397 : | breaks the line into groups of characters separated by spaces. For each | ||
| 3398 : | group of characters in turn, it makes two attempts to do something: | ||
| 3399 : | crook | 1.23 | |
| 3400 : | crook | 1.29 | @itemize @bullet |
| 3401 : | @item | ||
| 3402 : | crook | 1.44 | @cindex name dictionary |
| 3403 : | crook | 1.29 | It tries to treat it as a command. It does this by searching a @dfn{name |
| 3404 : | dictionary}. If the group of characters matches an entry in the name | ||
| 3405 : | dictionary, the name dictionary provides the text interpreter with | ||
| 3406 : | information that allows the text interpreter perform some actions. In | ||
| 3407 : | Forth jargon, we say that the group | ||
| 3408 : | @cindex word | ||
| 3409 : | @cindex definition | ||
| 3410 : | @cindex execution token | ||
| 3411 : | @cindex xt | ||
| 3412 : | of characters names a @dfn{word}, that the dictionary search returns an | ||
| 3413 : | @dfn{execution token (xt)} corresponding to the @dfn{definition} of the | ||
| 3414 : | word, and that the text interpreter executes the xt. Often, the terms | ||
| 3415 : | @dfn{word} and @dfn{definition} are used interchangeably. | ||
| 3416 : | @item | ||
| 3417 : | If the text interpreter fails to find a match in the name dictionary, it | ||
| 3418 : | tries to treat the group of characters as a number in the current number | ||
| 3419 : | base (when you start up Forth, the current number base is base 10). If | ||
| 3420 : | the group of characters legitimately represents a number, the text | ||
| 3421 : | interpreter pushes the number onto a stack (we'll learn more about that | ||
| 3422 : | in the next section). | ||
| 3423 : | @end itemize | ||
| 3424 : | crook | 1.23 | |
| 3425 : | crook | 1.29 | If the text interpreter is unable to do either of these things with any |
| 3426 : | group of characters, it discards the group of characters and the rest of | ||
| 3427 : | the line, then prints an error message. If the text interpreter reaches | ||
| 3428 : | the end of the line without error, it prints the status message ``@code{ ok}'' | ||
| 3429 : | followed by carriage-return. | ||
| 3430 : | crook | 1.21 | |
| 3431 : | crook | 1.29 | This is the simplest command we can give to the text interpreter: |
| 3432 : | crook | 1.23 | |
| 3433 : | @example | ||
| 3434 : | anton | 1.30 | @key{RET} ok |
| 3435 : | crook | 1.23 | @end example |
| 3436 : | crook | 1.21 | |
| 3437 : | crook | 1.29 | The text interpreter did everything we asked it to do (nothing) without |
| 3438 : | an error, so it said that everything is ``@code{ ok}''. Try a slightly longer | ||
| 3439 : | command: | ||
| 3440 : | crook | 1.21 | |
| 3441 : | crook | 1.23 | @example |
| 3442 : | anton | 1.30 | @kbd{12 dup fred dup@key{RET}} |
| 3443 : | crook | 1.29 | :1: Undefined word |
| 3444 : | 12 dup fred dup | ||
| 3445 : | ^^^^ | ||
| 3446 : | $400D2BA8 Bounce | ||
| 3447 : | $400DBDA8 no.extensions | ||
| 3448 : | crook | 1.23 | @end example |
| 3449 : | crook | 1.21 | |
| 3450 : | crook | 1.29 | When you press the carriage-return key, the text interpreter starts to |
| 3451 : | work its way along the line: | ||
| 3452 : | crook | 1.21 | |
| 3453 : | crook | 1.29 | @itemize @bullet |
| 3454 : | @item | ||
| 3455 : | When it gets to the space after the @code{2}, it takes the group of | ||
| 3456 : | characters @code{12} and looks them up in the name | ||
| 3457 : | dictionary@footnote{We can't tell if it found them or not, but assume | ||
| 3458 : | for now that it did not}. There is no match for this group of characters | ||
| 3459 : | in the name dictionary, so it tries to treat them as a number. It is | ||
| 3460 : | able to do this successfully, so it puts the number, 12, ``on the stack'' | ||
| 3461 : | (whatever that means). | ||
| 3462 : | @item | ||
| 3463 : | The text interpreter resumes scanning the line and gets the next group | ||
| 3464 : | of characters, @code{dup}. It looks it up in the name dictionary and | ||
| 3465 : | (you'll have to take my word for this) finds it, and executes the word | ||
| 3466 : | @code{dup} (whatever that means). | ||
| 3467 : | @item | ||
| 3468 : | Once again, the text interpreter resumes scanning the line and gets the | ||
| 3469 : | group of characters @code{fred}. It looks them up in the name | ||
| 3470 : | dictionary, but can't find them. It tries to treat them as a number, but | ||
| 3471 : | they don't represent any legal number. | ||
| 3472 : | @end itemize | ||
| 3473 : | crook | 1.21 | |
| 3474 : | crook | 1.29 | At this point, the text interpreter gives up and prints an error |
| 3475 : | message. The error message shows exactly how far the text interpreter | ||
| 3476 : | got in processing the line. In particular, it shows that the text | ||
| 3477 : | interpreter made no attempt to do anything with the final character | ||
| 3478 : | group, @code{dup}, even though we have good reason to believe that the | ||
| 3479 : | text interpreter would have no problem looking that word up and | ||
| 3480 : | executing it a second time. | ||
| 3481 : | crook | 1.21 | |
| 3482 : | |||
| 3483 : | crook | 1.29 | @comment ---------------------------------------------- |
| 3484 : | @node Stacks and Postfix notation, Your first definition, Introducing the Text Interpreter, Introduction | ||
| 3485 : | @section Stacks, postfix notation and parameter passing | ||
| 3486 : | @cindex text interpreter | ||
| 3487 : | @cindex outer interpreter | ||
| 3488 : | crook | 1.21 | |
| 3489 : | crook | 1.29 | In procedural programming languages (like C and Pascal), the |
| 3490 : | building-block of programs is the @dfn{function} or @dfn{procedure}. These | ||
| 3491 : | functions or procedures are called with @dfn{explicit parameters}. For | ||
| 3492 : | example, in C we might write: | ||
| 3493 : | crook | 1.21 | |
| 3494 : | crook | 1.23 | @example |
| 3495 : | crook | 1.29 | total = total + new_volume(length,height,depth); |
| 3496 : | crook | 1.23 | @end example |
| 3497 : | crook | 1.21 | |
| 3498 : | crook | 1.23 | @noindent |
| 3499 : | crook | 1.29 | where new_volume is a function-call to another piece of code, and total, |
| 3500 : | length, height and depth are all variables. length, height and depth are | ||
| 3501 : | parameters to the function-call. | ||
| 3502 : | crook | 1.21 | |
| 3503 : | crook | 1.29 | In Forth, the equivalent of the function or procedure is the |
| 3504 : | @dfn{definition} and parameters are implicitly passed between | ||
| 3505 : | definitions using a shared stack that is visible to the | ||
| 3506 : | programmer. Although Forth does support variables, the existence of the | ||
| 3507 : | stack means that they are used far less often than in most other | ||
| 3508 : | programming languages. When the text interpreter encounters a number, it | ||
| 3509 : | will place (@dfn{push}) it on the stack. There are several stacks (the | ||
| 3510 : | anton | 1.30 | actual number is implementation-dependent ...) and the particular stack |
| 3511 : | crook | 1.29 | used for any operation is implied unambiguously by the operation being |
| 3512 : | performed. The stack used for all integer operations is called the @dfn{data | ||
| 3513 : | stack} and, since this is the stack used most commonly, references to | ||
| 3514 : | ``the data stack'' are often abbreviated to ``the stack''. | ||
| 3515 : | crook | 1.21 | |
| 3516 : | crook | 1.29 | The stacks have a last-in, first-out (LIFO) organisation. If you type: |
| 3517 : | crook | 1.21 | |
| 3518 : | crook | 1.23 | @example |
| 3519 : | anton | 1.30 | @kbd{1 2 3@key{RET}} ok |
| 3520 : | crook | 1.23 | @end example |
| 3521 : | crook | 1.21 | |
| 3522 : | crook | 1.29 | Then this instructs the text interpreter to placed three numbers on the |
| 3523 : | (data) stack. An analogy for the behaviour of the stack is to take a | ||
| 3524 : | pack of playing cards and deal out the ace (1), 2 and 3 into a pile on | ||
| 3525 : | the table. The 3 was the last card onto the pile (``last-in'') and if | ||
| 3526 : | you take a card off the pile then, unless you're prepared to fiddle a | ||
| 3527 : | bit, the card that you take off will be the 3 (``first-out''). The | ||
| 3528 : | number that will be first-out of the stack is called the @dfn{top of | ||
| 3529 : | stack}, which | ||
| 3530 : | @cindex TOS definition | ||
| 3531 : | is often abbreviated to @dfn{TOS}. | ||
| 3532 : | crook | 1.21 | |
| 3533 : | crook | 1.29 | To understand how parameters are passed in Forth, consider the |
| 3534 : | behaviour of the definition @code{+} (pronounced ``plus''). You will not | ||
| 3535 : | be surprised to learn that this definition performs addition. More | ||
| 3536 : | precisely, it adds two number together and produces a result. Where does | ||
| 3537 : | it get the two numbers from? It takes the top two numbers off the | ||
| 3538 : | stack. Where does it place the result? On the stack. You can act-out the | ||
| 3539 : | behaviour of @code{+} with your playing cards like this: | ||
| 3540 : | crook | 1.21 | |
| 3541 : | @itemize @bullet | ||
| 3542 : | @item | ||
| 3543 : | crook | 1.29 | Pick up two cards from the stack on the table |
| 3544 : | crook | 1.21 | @item |
| 3545 : | crook | 1.29 | Stare at them intently and ask yourself ``what @i{is} the sum of these two |
| 3546 : | numbers'' | ||
| 3547 : | crook | 1.21 | @item |
| 3548 : | crook | 1.29 | Decide that the answer is 5 |
| 3549 : | crook | 1.21 | @item |
| 3550 : | crook | 1.29 | Shuffle the two cards back into the pack and find a 5 |
| 3551 : | crook | 1.21 | @item |
| 3552 : | crook | 1.29 | Put a 5 on the remaining ace that's on the table. |
| 3553 : | crook | 1.21 | @end itemize |
| 3554 : | |||
| 3555 : | crook | 1.29 | If you don't have a pack of cards handy but you do have Forth running, |
| 3556 : | you can use the definition @code{.s} to show the current state of the stack, | ||
| 3557 : | without affecting the stack. Type: | ||
| 3558 : | crook | 1.21 | |
| 3559 : | @example | ||
| 3560 : | anton | 1.124 | @kbd{clearstacks 1 2 3@key{RET}} ok |
| 3561 : | anton | 1.30 | @kbd{.s@key{RET}} <3> 1 2 3 ok |
| 3562 : | crook | 1.23 | @end example |
| 3563 : | |||
| 3564 : | anton | 1.124 | The text interpreter looks up the word @code{clearstacks} and executes |
| 3565 : | it; it tidies up the stacks and removes any entries that may have been | ||
| 3566 : | crook | 1.29 | left on it by earlier examples. The text interpreter pushes each of the |
| 3567 : | three numbers in turn onto the stack. Finally, the text interpreter | ||
| 3568 : | looks up the word @code{.s} and executes it. The effect of executing | ||
| 3569 : | @code{.s} is to print the ``<3>'' (the total number of items on the stack) | ||
| 3570 : | followed by a list of all the items on the stack; the item on the far | ||
| 3571 : | right-hand side is the TOS. | ||
| 3572 : | crook | 1.21 | |
| 3573 : | crook | 1.29 | You can now type: |
| 3574 : | crook | 1.21 | |
| 3575 : | crook | 1.29 | @example |
| 3576 : | anton | 1.30 | @kbd{+ .s@key{RET}} <2> 1 5 ok |
| 3577 : | crook | 1.29 | @end example |
| 3578 : | crook | 1.21 | |
| 3579 : | crook | 1.29 | @noindent |
| 3580 : | which is correct; there are now 2 items on the stack and the result of | ||
| 3581 : | the addition is 5. | ||
| 3582 : | crook | 1.23 | |
| 3583 : | crook | 1.29 | If you're playing with cards, try doing a second addition: pick up the |
| 3584 : | two cards, work out that their sum is 6, shuffle them into the pack, | ||
| 3585 : | look for a 6 and place that on the table. You now have just one item on | ||
| 3586 : | the stack. What happens if you try to do a third addition? Pick up the | ||
| 3587 : | first card, pick up the second card -- ah! There is no second card. This | ||
| 3588 : | is called a @dfn{stack underflow} and consitutes an error. If you try to | ||
| 3589 : | anton | 1.95 | do the same thing with Forth it often reports an error (probably a Stack |
| 3590 : | crook | 1.29 | Underflow or an Invalid Memory Address error). |
| 3591 : | crook | 1.23 | |
| 3592 : | crook | 1.29 | The opposite situation to a stack underflow is a @dfn{stack overflow}, |
| 3593 : | which simply accepts that there is a finite amount of storage space | ||
| 3594 : | reserved for the stack. To stretch the playing card analogy, if you had | ||
| 3595 : | enough packs of cards and you piled the cards up on the table, you would | ||
| 3596 : | eventually be unable to add another card; you'd hit the ceiling. Gforth | ||
| 3597 : | allows you to set the maximum size of the stacks. In general, the only | ||
| 3598 : | time that you will get a stack overflow is because a definition has a | ||
| 3599 : | bug in it and is generating data on the stack uncontrollably. | ||
| 3600 : | crook | 1.23 | |
| 3601 : | crook | 1.29 | There's one final use for the playing card analogy. If you model your |
| 3602 : | stack using a pack of playing cards, the maximum number of items on | ||
| 3603 : | your stack will be 52 (I assume you didn't use the Joker). The maximum | ||
| 3604 : | @i{value} of any item on the stack is 13 (the King). In fact, the only | ||
| 3605 : | possible numbers are positive integer numbers 1 through 13; you can't | ||
| 3606 : | have (for example) 0 or 27 or 3.52 or -2. If you change the way you | ||
| 3607 : | think about some of the cards, you can accommodate different | ||
| 3608 : | numbers. For example, you could think of the Jack as representing 0, | ||
| 3609 : | the Queen as representing -1 and the King as representing -2. Your | ||
| 3610 : | crook | 1.45 | @i{range} remains unchanged (you can still only represent a total of 13 |
| 3611 : | crook | 1.29 | numbers) but the numbers that you can represent are -2 through 10. |
| 3612 : | crook | 1.28 | |
| 3613 : | crook | 1.29 | In that analogy, the limit was the amount of information that a single |
| 3614 : | stack entry could hold, and Forth has a similar limit. In Forth, the | ||
| 3615 : | size of a stack entry is called a @dfn{cell}. The actual size of a cell is | ||
| 3616 : | implementation dependent and affects the maximum value that a stack | ||
| 3617 : | entry can hold. A Standard Forth provides a cell size of at least | ||
| 3618 : | 16-bits, and most desktop systems use a cell size of 32-bits. | ||
| 3619 : | crook | 1.21 | |
| 3620 : | crook | 1.29 | Forth does not do any type checking for you, so you are free to |
| 3621 : | manipulate and combine stack items in any way you wish. A convenient way | ||
| 3622 : | of treating stack items is as 2's complement signed integers, and that | ||
| 3623 : | is what Standard words like @code{+} do. Therefore you can type: | ||
| 3624 : | crook | 1.21 | |
| 3625 : | crook | 1.29 | @example |
| 3626 : | anton | 1.30 | @kbd{-5 12 + .s@key{RET}} <1> 7 ok |
| 3627 : | crook | 1.29 | @end example |
| 3628 : | crook | 1.21 | |
| 3629 : | crook | 1.29 | If you use numbers and definitions like @code{+} in order to turn Forth |
| 3630 : | into a great big pocket calculator, you will realise that it's rather | ||
| 3631 : | different from a normal calculator. Rather than typing 2 + 3 = you had | ||
| 3632 : | to type 2 3 + (ignore the fact that you had to use @code{.s} to see the | ||
| 3633 : | result). The terminology used to describe this difference is to say that | ||
| 3634 : | your calculator uses @dfn{Infix Notation} (parameters and operators are | ||
| 3635 : | mixed) whilst Forth uses @dfn{Postfix Notation} (parameters and | ||
| 3636 : | operators are separate), also called @dfn{Reverse Polish Notation}. | ||
| 3637 : | crook | 1.21 | |
| 3638 : | crook | 1.29 | Whilst postfix notation might look confusing to begin with, it has |
| 3639 : | several important advantages: | ||
| 3640 : | crook | 1.21 | |
| 3641 : | crook | 1.23 | @itemize @bullet |
| 3642 : | @item | ||
| 3643 : | crook | 1.29 | it is unambiguous |
| 3644 : | crook | 1.23 | @item |
| 3645 : | crook | 1.29 | it is more concise |
| 3646 : | crook | 1.23 | @item |
| 3647 : | crook | 1.29 | it fits naturally with a stack-based system |
| 3648 : | crook | 1.23 | @end itemize |
| 3649 : | crook | 1.21 | |
| 3650 : | crook | 1.29 | To examine these claims in more detail, consider these sums: |
| 3651 : | crook | 1.21 | |
| 3652 : | crook | 1.29 | @example |
| 3653 : | 6 + 5 * 4 = | ||
| 3654 : | 4 * 5 + 6 = | ||
| 3655 : | @end example | ||
| 3656 : | crook | 1.21 | |
| 3657 : | crook | 1.29 | If you're just learning maths or your maths is very rusty, you will |
| 3658 : | probably come up with the answer 44 for the first and 26 for the | ||
| 3659 : | second. If you are a bit of a whizz at maths you will remember the | ||
| 3660 : | @i{convention} that multiplication takes precendence over addition, and | ||
| 3661 : | you'd come up with the answer 26 both times. To explain the answer 26 | ||
| 3662 : | to someone who got the answer 44, you'd probably rewrite the first sum | ||
| 3663 : | like this: | ||
| 3664 : | crook | 1.21 | |
| 3665 : | crook | 1.29 | @example |
| 3666 : | 6 + (5 * 4) = | ||
| 3667 : | @end example | ||
| 3668 : | crook | 1.21 | |
| 3669 : | crook | 1.29 | If what you really wanted was to perform the addition before the |
| 3670 : | multiplication, you would have to use parentheses to force it. | ||
| 3671 : | crook | 1.21 | |
| 3672 : | crook | 1.29 | If you did the first two sums on a pocket calculator you would probably |
| 3673 : | get the right answers, unless you were very cautious and entered them using | ||
| 3674 : | these keystroke sequences: | ||
| 3675 : | crook | 1.21 | |
| 3676 : | crook | 1.29 | 6 + 5 = * 4 = |
| 3677 : | 4 * 5 = + 6 = | ||
| 3678 : | crook | 1.21 | |
| 3679 : | crook | 1.29 | Postfix notation is unambiguous because the order that the operators |
| 3680 : | are applied is always explicit; that also means that parentheses are | ||
| 3681 : | never required. The operators are @i{active} (the act of quoting the | ||
| 3682 : | operator makes the operation occur) which removes the need for ``=''. | ||
| 3683 : | crook | 1.28 | |
| 3684 : | crook | 1.29 | The sum 6 + 5 * 4 can be written (in postfix notation) in two |
| 3685 : | equivalent ways: | ||
| 3686 : | crook | 1.26 | |
| 3687 : | @example | ||
| 3688 : | crook | 1.29 | 6 5 4 * + or: |
| 3689 : | 5 4 * 6 + | ||
| 3690 : | crook | 1.26 | @end example |
| 3691 : | crook | 1.23 | |
| 3692 : | crook | 1.29 | An important thing that you should notice about this notation is that |
| 3693 : | the @i{order} of the numbers does not change; if you want to subtract | ||
| 3694 : | 2 from 10 you type @code{10 2 -}. | ||
| 3695 : | anton | 1.1 | |
| 3696 : | crook | 1.29 | The reason that Forth uses postfix notation is very simple to explain: it |
| 3697 : | makes the implementation extremely simple, and it follows naturally from | ||
| 3698 : | using the stack as a mechanism for passing parameters. Another way of | ||
| 3699 : | thinking about this is to realise that all Forth definitions are | ||
| 3700 : | @i{active}; they execute as they are encountered by the text | ||
| 3701 : | interpreter. The result of this is that the syntax of Forth is trivially | ||
| 3702 : | simple. | ||
| 3703 : | anton | 1.1 | |
| 3704 : | |||
| 3705 : | |||
| 3706 : | crook | 1.29 | @comment ---------------------------------------------- |
| 3707 : | @node Your first definition, How does that work?, Stacks and Postfix notation, Introduction | ||
| 3708 : | @section Your first Forth definition | ||
| 3709 : | @cindex first definition | ||
| 3710 : | anton | 1.1 | |
| 3711 : | crook | 1.29 | Until now, the examples we've seen have been trivial; we've just been |
| 3712 : | using Forth as a bigger-than-pocket calculator. Also, each calculation | ||
| 3713 : | we've shown has been a ``one-off'' -- to repeat it we'd need to type it in | ||
| 3714 : | again@footnote{That's not quite true. If you press the up-arrow key on | ||
| 3715 : | your keyboard you should be able to scroll back to any earlier command, | ||
| 3716 : | edit it and re-enter it.} In this section we'll see how to add new | ||
| 3717 : | words to Forth's vocabulary. | ||
| 3718 : | anton | 1.1 | |
| 3719 : | crook | 1.29 | The easiest way to create a new word is to use a @dfn{colon |
| 3720 : | definition}. We'll define a few and try them out before worrying too | ||
| 3721 : | much about how they work. Try typing in these examples; be careful to | ||
| 3722 : | copy the spaces accurately: | ||
| 3723 : | anton | 1.1 | |
| 3724 : | crook | 1.29 | @example |
| 3725 : | : add-two 2 + . ; | ||
| 3726 : | : greet ." Hello and welcome" ; | ||
| 3727 : | : demo 5 add-two ; | ||
| 3728 : | @end example | ||
| 3729 : | anton | 1.1 | |
| 3730 : | crook | 1.29 | @noindent |
| 3731 : | Now try them out: | ||
| 3732 : | anton | 1.1 | |
| 3733 : | crook | 1.29 | @example |
| 3734 : | anton | 1.30 | @kbd{greet@key{RET}} Hello and welcome ok |
| 3735 : | @kbd{greet greet@key{RET}} Hello and welcomeHello and welcome ok | ||
| 3736 : | @kbd{4 add-two@key{RET}} 6 ok | ||
| 3737 : | @kbd{demo@key{RET}} 7 ok | ||
| 3738 : | @kbd{9 greet demo add-two@key{RET}} Hello and welcome7 11 ok | ||
| 3739 : | crook | 1.29 | @end example |
| 3740 : | anton | 1.1 | |
| 3741 : | crook | 1.29 | The first new thing that we've introduced here is the pair of words |
| 3742 : | @code{:} and @code{;}. These are used to start and terminate a new | ||
| 3743 : | definition, respectively. The first word after the @code{:} is the name | ||
| 3744 : | for the new definition. | ||
| 3745 : | anton | 1.1 | |
| 3746 : | crook | 1.29 | As you can see from the examples, a definition is built up of words that |
| 3747 : | have already been defined; Forth makes no distinction between | ||
| 3748 : | definitions that existed when you started the system up, and those that | ||
| 3749 : | you define yourself. | ||
| 3750 : | anton | 1.1 | |
| 3751 : | crook | 1.29 | The examples also introduce the words @code{.} (dot), @code{."} |
| 3752 : | (dot-quote) and @code{dup} (dewp). Dot takes the value from the top of | ||
| 3753 : | the stack and displays it. It's like @code{.s} except that it only | ||
| 3754 : | displays the top item of the stack and it is destructive; after it has | ||
| 3755 : | executed, the number is no longer on the stack. There is always one | ||
| 3756 : | space printed after the number, and no spaces before it. Dot-quote | ||
| 3757 : | defines a string (a sequence of characters) that will be printed when | ||
| 3758 : | the word is executed. The string can contain any printable characters | ||
| 3759 : | except @code{"}. A @code{"} has a special function; it is not a Forth | ||
| 3760 : | word but it acts as a delimiter (the way that delimiters work is | ||
| 3761 : | described in the next section). Finally, @code{dup} duplicates the value | ||
| 3762 : | at the top of the stack. Try typing @code{5 dup .s} to see what it does. | ||
| 3763 : | anton | 1.1 | |
| 3764 : | crook | 1.29 | We already know that the text interpreter searches through the |
| 3765 : | dictionary to locate names. If you've followed the examples earlier, you | ||
| 3766 : | will already have a definition called @code{add-two}. Lets try modifying | ||
| 3767 : | it by typing in a new definition: | ||
| 3768 : | anton | 1.1 | |
| 3769 : | crook | 1.29 | @example |
| 3770 : | anton | 1.30 | @kbd{: add-two dup . ." + 2 =" 2 + . ;@key{RET}} redefined add-two ok |
| 3771 : | crook | 1.29 | @end example |
| 3772 : | anton | 1.5 | |
| 3773 : | crook | 1.29 | Forth recognised that we were defining a word that already exists, and |
| 3774 : | printed a message to warn us of that fact. Let's try out the new | ||
| 3775 : | definition: | ||
| 3776 : | anton | 1.5 | |
| 3777 : | crook | 1.29 | @example |
| 3778 : | anton | 1.30 | @kbd{9 add-two@key{RET}} 9 + 2 =11 ok |
| 3779 : | crook | 1.29 | @end example |
| 3780 : | anton | 1.1 | |
| 3781 : | crook | 1.29 | @noindent |
| 3782 : | All that we've actually done here, though, is to create a new | ||
| 3783 : | definition, with a particular name. The fact that there was already a | ||
| 3784 : | definition with the same name did not make any difference to the way | ||
| 3785 : | that the new definition was created (except that Forth printed a warning | ||
| 3786 : | message). The old definition of add-two still exists (try @code{demo} | ||
| 3787 : | again to see that this is true). Any new definition will use the new | ||
| 3788 : | definition of @code{add-two}, but old definitions continue to use the | ||
| 3789 : | version that already existed at the time that they were @code{compiled}. | ||
| 3790 : | anton | 1.1 | |
| 3791 : | crook | 1.29 | Before you go on to the next section, try defining and redefining some |
| 3792 : | words of your own. | ||
| 3793 : | anton | 1.1 | |
| 3794 : | crook | 1.29 | @comment ---------------------------------------------- |
| 3795 : | @node How does that work?, Forth is written in Forth, Your first definition, Introduction | ||
| 3796 : | @section How does that work? | ||
| 3797 : | @cindex parsing words | ||
| 3798 : | anton | 1.1 | |
| 3799 : | anton | 1.30 | @c That's pretty deep (IMO way too deep) for an introduction. - anton |
| 3800 : | |||
| 3801 : | @c Is it a good idea to talk about the interpretation semantics of a | ||
| 3802 : | @c number? We don't have an xt to go along with it. - anton | ||
| 3803 : | |||
| 3804 : | @c Now that I have eliminated execution semantics, I wonder if it would not | ||
| 3805 : | @c be better to keep them (or add run-time semantics), to make it easier to | ||
| 3806 : | @c explain what compilation semantics usually does. - anton | ||
| 3807 : | |||
| 3808 : | crook | 1.44 | @c nac-> I removed the term ``default compilation sematics'' from the |
| 3809 : | @c introductory chapter. Removing ``execution semantics'' was making | ||
| 3810 : | @c everything simpler to explain, then I think the use of this term made | ||
| 3811 : | @c everything more complex again. I replaced it with ``default | ||
| 3812 : | @c semantics'' (which is used elsewhere in the manual) by which I mean | ||
| 3813 : | @c ``a definition that has neither the immediate nor the compile-only | ||
| 3814 : | anton | 1.83 | @c flag set''. |
| 3815 : | |||
| 3816 : | @c anton: I have eliminated default semantics (except in one place where it | ||
| 3817 : | @c means "default interpretation and compilation semantics"), because it | ||
| 3818 : | @c makes no sense in the presence of combined words. I reverted to | ||
| 3819 : | @c "execution semantics" where necessary. | ||
| 3820 : | |||
| 3821 : | @c nac-> I reworded big chunks of the ``how does that work'' | ||
| 3822 : | crook | 1.44 | @c section (and, unusually for me, I think I even made it shorter!). See |
| 3823 : | @c what you think -- I know I have not addressed your primary concern | ||
| 3824 : | @c that it is too heavy-going for an introduction. From what I understood | ||
| 3825 : | @c of your course notes it looks as though they might be a good framework. | ||
| 3826 : | @c Things that I've tried to capture here are some things that came as a | ||
| 3827 : | @c great revelation here when I first understood them. Also, I like the | ||
| 3828 : | @c fact that a very simple code example shows up almost all of the issues | ||
| 3829 : | @c that you need to understand to see how Forth works. That's unique and | ||
| 3830 : | @c worthwhile to emphasise. | ||
| 3831 : | |||
| 3832 : | anton | 1.83 | @c anton: I think it's a good idea to present the details, especially those |
| 3833 : | @c that you found to be a revelation, and probably the tutorial tries to be | ||
| 3834 : | @c too superficial and does not get some of the things across that make | ||
| 3835 : | @c Forth special. I do believe that most of the time these things should | ||
| 3836 : | @c be discussed at the end of a section or in separate sections instead of | ||
| 3837 : | @c in the middle of a section (e.g., the stuff you added in "User-defined | ||
| 3838 : | @c defining words" leads in a completely different direction from the rest | ||
| 3839 : | @c of the section). | ||
| 3840 : | |||
| 3841 : | crook | 1.29 | Now we're going to take another look at the definition of @code{add-two} |
| 3842 : | from the previous section. From our knowledge of the way that the text | ||
| 3843 : | interpreter works, we would have expected this result when we tried to | ||
| 3844 : | define @code{add-two}: | ||
| 3845 : | crook | 1.21 | |
| 3846 : | crook | 1.29 | @example |
| 3847 : | crook | 1.44 | @kbd{: add-two 2 + . ;@key{RET}} |
| 3848 : | crook | 1.29 | ^^^^^^^ |
| 3849 : | Error: Undefined word | ||
| 3850 : | @end example | ||
| 3851 : | crook | 1.28 | |
| 3852 : | crook | 1.29 | The reason that this didn't happen is bound up in the way that @code{:} |
| 3853 : | works. The word @code{:} does two special things. The first special | ||
| 3854 : | thing that it does prevents the text interpreter from ever seeing the | ||
| 3855 : | characters @code{add-two}. The text interpreter uses a variable called | ||
| 3856 : | @cindex modifying >IN | ||
| 3857 : | crook | 1.44 | @code{>IN} (pronounced ``to-in'') to keep track of where it is in the |
| 3858 : | crook | 1.29 | input line. When it encounters the word @code{:} it behaves in exactly |
| 3859 : | the same way as it does for any other word; it looks it up in the name | ||
| 3860 : | dictionary, finds its xt and executes it. When @code{:} executes, it | ||
| 3861 : | looks at the input buffer, finds the word @code{add-two} and advances the | ||
| 3862 : | value of @code{>IN} to point past it. It then does some other stuff | ||
| 3863 : | associated with creating the new definition (including creating an entry | ||
| 3864 : | for @code{add-two} in the name dictionary). When the execution of @code{:} | ||
| 3865 : | completes, control returns to the text interpreter, which is oblivious | ||
| 3866 : | to the fact that it has been tricked into ignoring part of the input | ||
| 3867 : | line. | ||
| 3868 : | crook | 1.21 | |
| 3869 : | crook | 1.29 | @cindex parsing words |
| 3870 : | Words like @code{:} -- words that advance the value of @code{>IN} and so | ||
| 3871 : | prevent the text interpreter from acting on the whole of the input line | ||
| 3872 : | -- are called @dfn{parsing words}. | ||
| 3873 : | crook | 1.21 | |
| 3874 : | crook | 1.29 | @cindex @code{state} - effect on the text interpreter |
| 3875 : | @cindex text interpreter - effect of state | ||
| 3876 : | The second special thing that @code{:} does is change the value of a | ||
| 3877 : | variable called @code{state}, which affects the way that the text | ||
| 3878 : | interpreter behaves. When Gforth starts up, @code{state} has the value | ||
| 3879 : | 0, and the text interpreter is said to be @dfn{interpreting}. During a | ||
| 3880 : | colon definition (started with @code{:}), @code{state} is set to -1 and | ||
| 3881 : | crook | 1.44 | the text interpreter is said to be @dfn{compiling}. |
| 3882 : | |||
| 3883 : | In this example, the text interpreter is compiling when it processes the | ||
| 3884 : | string ``@code{2 + . ;}''. It still breaks the string down into | ||
| 3885 : | character sequences in the same way. However, instead of pushing the | ||
| 3886 : | number @code{2} onto the stack, it lays down (@dfn{compiles}) some magic | ||
| 3887 : | into the definition of @code{add-two} that will make the number @code{2} get | ||
| 3888 : | pushed onto the stack when @code{add-two} is @dfn{executed}. Similarly, | ||
| 3889 : | the behaviours of @code{+} and @code{.} are also compiled into the | ||
| 3890 : | definition. | ||
| 3891 : | |||
| 3892 : | One category of words don't get compiled. These so-called @dfn{immediate | ||
| 3893 : | words} get executed (performed @i{now}) regardless of whether the text | ||
| 3894 : | interpreter is interpreting or compiling. The word @code{;} is an | ||
| 3895 : | immediate word. Rather than being compiled into the definition, it | ||
| 3896 : | executes. Its effect is to terminate the current definition, which | ||
| 3897 : | includes changing the value of @code{state} back to 0. | ||
| 3898 : | |||
| 3899 : | When you execute @code{add-two}, it has a @dfn{run-time effect} that is | ||
| 3900 : | exactly the same as if you had typed @code{2 + . @key{RET}} outside of a | ||
| 3901 : | definition. | ||
| 3902 : | crook | 1.28 | |
| 3903 : | anton | 1.30 | In Forth, every word or number can be described in terms of two |
| 3904 : | crook | 1.29 | properties: |
| 3905 : | crook | 1.28 | |
| 3906 : | @itemize @bullet | ||
| 3907 : | @item | ||
| 3908 : | crook | 1.29 | @cindex interpretation semantics |
| 3909 : | crook | 1.44 | Its @dfn{interpretation semantics} describe how it will behave when the |
| 3910 : | text interpreter encounters it in @dfn{interpret} state. The | ||
| 3911 : | interpretation semantics of a word are represented by an @dfn{execution | ||
| 3912 : | token}. | ||
| 3913 : | crook | 1.28 | @item |
| 3914 : | crook | 1.29 | @cindex compilation semantics |
| 3915 : | crook | 1.44 | Its @dfn{compilation semantics} describe how it will behave when the |
| 3916 : | text interpreter encounters it in @dfn{compile} state. The compilation | ||
| 3917 : | semantics of a word are represented in an implementation-dependent way; | ||
| 3918 : | Gforth uses a @dfn{compilation token}. | ||
| 3919 : | crook | 1.29 | @end itemize |
| 3920 : | |||
| 3921 : | @noindent | ||
| 3922 : | Numbers are always treated in a fixed way: | ||
| 3923 : | |||
| 3924 : | @itemize @bullet | ||
| 3925 : | crook | 1.28 | @item |
| 3926 : | crook | 1.44 | When the number is @dfn{interpreted}, its behaviour is to push the |
| 3927 : | number onto the stack. | ||
| 3928 : | crook | 1.28 | @item |
| 3929 : | anton | 1.30 | When the number is @dfn{compiled}, a piece of code is appended to the |
| 3930 : | current definition that pushes the number when it runs. (In other words, | ||
| 3931 : | the compilation semantics of a number are to postpone its interpretation | ||
| 3932 : | semantics until the run-time of the definition that it is being compiled | ||
| 3933 : | into.) | ||
| 3934 : | crook | 1.29 | @end itemize |
| 3935 : | |||
| 3936 : | crook | 1.44 | Words don't behave in such a regular way, but most have @i{default |
| 3937 : | semantics} which means that they behave like this: | ||
| 3938 : | crook | 1.29 | |
| 3939 : | @itemize @bullet | ||
| 3940 : | crook | 1.28 | @item |
| 3941 : | anton | 1.30 | The @dfn{interpretation semantics} of the word are to do something useful. |
| 3942 : | @item | ||
| 3943 : | crook | 1.29 | The @dfn{compilation semantics} of the word are to append its |
| 3944 : | anton | 1.30 | @dfn{interpretation semantics} to the current definition (so that its |
| 3945 : | run-time behaviour is to do something useful). | ||
| 3946 : | crook | 1.28 | @end itemize |
| 3947 : | |||
| 3948 : | anton | 1.30 | @cindex immediate words |
| 3949 : | crook | 1.44 | The actual behaviour of any particular word can be controlled by using |
| 3950 : | the words @code{immediate} and @code{compile-only} when the word is | ||
| 3951 : | defined. These words set flags in the name dictionary entry of the most | ||
| 3952 : | recently defined word, and these flags are retrieved by the text | ||
| 3953 : | interpreter when it finds the word in the name dictionary. | ||
| 3954 : | |||
| 3955 : | A word that is marked as @dfn{immediate} has compilation semantics that | ||
| 3956 : | are identical to its interpretation semantics. In other words, it | ||
| 3957 : | behaves like this: | ||
| 3958 : | crook | 1.29 | |
| 3959 : | @itemize @bullet | ||
| 3960 : | @item | ||
| 3961 : | anton | 1.30 | The @dfn{interpretation semantics} of the word are to do something useful. |
| 3962 : | crook | 1.29 | @item |
| 3963 : | anton | 1.30 | The @dfn{compilation semantics} of the word are to do something useful |
| 3964 : | (and actually the same thing); i.e., it is executed during compilation. | ||
| 3965 : | crook | 1.29 | @end itemize |
| 3966 : | crook | 1.28 | |
| 3967 : | crook | 1.44 | Marking a word as @dfn{compile-only} prohibits the text interpreter from |
| 3968 : | performing the interpretation semantics of the word directly; an attempt | ||
| 3969 : | to do so will generate an error. It is never necessary to use | ||
| 3970 : | @code{compile-only} (and it is not even part of ANS Forth, though it is | ||
| 3971 : | provided by many implementations) but it is good etiquette to apply it | ||
| 3972 : | to a word that will not behave correctly (and might have unexpected | ||
| 3973 : | side-effects) in interpret state. For example, it is only legal to use | ||
| 3974 : | the conditional word @code{IF} within a definition. If you forget this | ||
| 3975 : | and try to use it elsewhere, the fact that (in Gforth) it is marked as | ||
| 3976 : | @code{compile-only} allows the text interpreter to generate a helpful | ||
| 3977 : | error message rather than subjecting you to the consequences of your | ||
| 3978 : | folly. | ||
| 3979 : | |||
| 3980 : | crook | 1.29 | This example shows the difference between an immediate and a |
| 3981 : | non-immediate word: | ||
| 3982 : | crook | 1.28 | |
| 3983 : | crook | 1.29 | @example |
| 3984 : | : show-state state @@ . ; | ||
| 3985 : | : show-state-now show-state ; immediate | ||
| 3986 : | : word1 show-state ; | ||
| 3987 : | : word2 show-state-now ; | ||
| 3988 : | crook | 1.28 | @end example |
| 3989 : | crook | 1.23 | |
| 3990 : | crook | 1.29 | The word @code{immediate} after the definition of @code{show-state-now} |
| 3991 : | makes that word an immediate word. These definitions introduce a new | ||
| 3992 : | word: @code{@@} (pronounced ``fetch''). This word fetches the value of a | ||
| 3993 : | variable, and leaves it on the stack. Therefore, the behaviour of | ||
| 3994 : | @code{show-state} is to print a number that represents the current value | ||
| 3995 : | of @code{state}. | ||
| 3996 : | crook | 1.28 | |
| 3997 : | crook | 1.29 | When you execute @code{word1}, it prints the number 0, indicating that |
| 3998 : | the system is interpreting. When the text interpreter compiled the | ||
| 3999 : | definition of @code{word1}, it encountered @code{show-state} whose | ||
| 4000 : | anton | 1.30 | compilation semantics are to append its interpretation semantics to the |
| 4001 : | crook | 1.29 | current definition. When you execute @code{word1}, it performs the |
| 4002 : | anton | 1.30 | interpretation semantics of @code{show-state}. At the time that @code{word1} |
| 4003 : | crook | 1.29 | (and therefore @code{show-state}) are executed, the system is |
| 4004 : | interpreting. | ||
| 4005 : | crook | 1.28 | |
| 4006 : | anton | 1.30 | When you pressed @key{RET} after entering the definition of @code{word2}, |
| 4007 : | crook | 1.29 | you should have seen the number -1 printed, followed by ``@code{ |
| 4008 : | ok}''. When the text interpreter compiled the definition of | ||
| 4009 : | @code{word2}, it encountered @code{show-state-now}, an immediate word, | ||
| 4010 : | anton | 1.30 | whose compilation semantics are therefore to perform its interpretation |
| 4011 : | crook | 1.29 | semantics. It is executed straight away (even before the text |
| 4012 : | interpreter has moved on to process another group of characters; the | ||
| 4013 : | @code{;} in this example). The effect of executing it are to display the | ||
| 4014 : | value of @code{state} @i{at the time that the definition of} | ||
| 4015 : | @code{word2} @i{is being defined}. Printing -1 demonstrates that the | ||
| 4016 : | system is compiling at this time. If you execute @code{word2} it does | ||
| 4017 : | nothing at all. | ||
| 4018 : | crook | 1.28 | |
| 4019 : | crook | 1.29 | @cindex @code{."}, how it works |
| 4020 : | Before leaving the subject of immediate words, consider the behaviour of | ||
| 4021 : | @code{."} in the definition of @code{greet}, in the previous | ||
| 4022 : | section. This word is both a parsing word and an immediate word. Notice | ||
| 4023 : | that there is a space between @code{."} and the start of the text | ||
| 4024 : | @code{Hello and welcome}, but that there is no space between the last | ||
| 4025 : | letter of @code{welcome} and the @code{"} character. The reason for this | ||
| 4026 : | is that @code{."} is a Forth word; it must have a space after it so that | ||
| 4027 : | the text interpreter can identify it. The @code{"} is not a Forth word; | ||
| 4028 : | it is a @dfn{delimiter}. The examples earlier show that, when the string | ||
| 4029 : | is displayed, there is neither a space before the @code{H} nor after the | ||
| 4030 : | @code{e}. Since @code{."} is an immediate word, it executes at the time | ||
| 4031 : | that @code{greet} is defined. When it executes, its behaviour is to | ||
| 4032 : | search forward in the input line looking for the delimiter. When it | ||
| 4033 : | finds the delimiter, it updates @code{>IN} to point past the | ||
| 4034 : | delimiter. It also compiles some magic code into the definition of | ||
| 4035 : | @code{greet}; the xt of a run-time routine that prints a text string. It | ||
| 4036 : | compiles the string @code{Hello and welcome} into memory so that it is | ||
| 4037 : | available to be printed later. When the text interpreter gains control, | ||
| 4038 : | the next word it finds in the input stream is @code{;} and so it | ||
| 4039 : | terminates the definition of @code{greet}. | ||
| 4040 : | crook | 1.28 | |
| 4041 : | |||
| 4042 : | @comment ---------------------------------------------- | ||
| 4043 : | crook | 1.29 | @node Forth is written in Forth, Review - elements of a Forth system, How does that work?, Introduction |
| 4044 : | @section Forth is written in Forth | ||
| 4045 : | @cindex structure of Forth programs | ||
| 4046 : | |||
| 4047 : | When you start up a Forth compiler, a large number of definitions | ||
| 4048 : | already exist. In Forth, you develop a new application using bottom-up | ||
| 4049 : | programming techniques to create new definitions that are defined in | ||
| 4050 : | terms of existing definitions. As you create each definition you can | ||
| 4051 : | test and debug it interactively. | ||
| 4052 : | |||
| 4053 : | If you have tried out the examples in this section, you will probably | ||
| 4054 : | have typed them in by hand; when you leave Gforth, your definitions will | ||
| 4055 : | be lost. You can avoid this by using a text editor to enter Forth source | ||
| 4056 : | code into a file, and then loading code from the file using | ||
| 4057 : | anton | 1.49 | @code{include} (@pxref{Forth source files}). A Forth source file is |
| 4058 : | crook | 1.29 | processed by the text interpreter, just as though you had typed it in by |
| 4059 : | hand@footnote{Actually, there are some subtle differences -- see | ||
| 4060 : | @ref{The Text Interpreter}.}. | ||
| 4061 : | |||
| 4062 : | Gforth also supports the traditional Forth alternative to using text | ||
| 4063 : | anton | 1.49 | files for program entry (@pxref{Blocks}). |
| 4064 : | crook | 1.28 | |
| 4065 : | crook | 1.29 | In common with many, if not most, Forth compilers, most of Gforth is |
| 4066 : | actually written in Forth. All of the @file{.fs} files in the | ||
| 4067 : | installation directory@footnote{For example, | ||
| 4068 : | anton | 1.30 | @file{/usr/local/share/gforth...}} are Forth source files, which you can |
| 4069 : | crook | 1.29 | study to see examples of Forth programming. |
| 4070 : | crook | 1.28 | |
| 4071 : | crook | 1.29 | Gforth maintains a history file that records every line that you type to |
| 4072 : | the text interpreter. This file is preserved between sessions, and is | ||
| 4073 : | used to provide a command-line recall facility. If you enter long | ||
| 4074 : | definitions by hand, you can use a text editor to paste them out of the | ||
| 4075 : | history file into a Forth source file for reuse at a later time | ||
| 4076 : | anton | 1.49 | (for more information @pxref{Command-line editing}). |
| 4077 : | crook | 1.28 | |
| 4078 : | |||
| 4079 : | @comment ---------------------------------------------- | ||
| 4080 : | crook | 1.29 | @node Review - elements of a Forth system, Where to go next, Forth is written in Forth, Introduction |
| 4081 : | @section Review - elements of a Forth system | ||
| 4082 : | @cindex elements of a Forth system | ||
| 4083 : | crook | 1.28 | |
| 4084 : | crook | 1.29 | To summarise this chapter: |
| 4085 : | crook | 1.28 | |
| 4086 : | @itemize @bullet | ||
| 4087 : | @item | ||
| 4088 : | crook | 1.29 | Forth programs use @dfn{factoring} to break a problem down into small |
| 4089 : | fragments called @dfn{words} or @dfn{definitions}. | ||
| 4090 : | @item | ||
| 4091 : | Forth program development is an interactive process. | ||
| 4092 : | @item | ||
| 4093 : | The main command loop that accepts input, and controls both | ||
| 4094 : | interpretation and compilation, is called the @dfn{text interpreter} | ||
| 4095 : | (also known as the @dfn{outer interpreter}). | ||
| 4096 : | @item | ||
| 4097 : | Forth has a very simple syntax, consisting of words and numbers | ||
| 4098 : | separated by spaces or carriage-return characters. Any additional syntax | ||
| 4099 : | is imposed by @dfn{parsing words}. | ||
| 4100 : | @item | ||
| 4101 : | Forth uses a stack to pass parameters between words. As a result, it | ||
| 4102 : | uses postfix notation. | ||
| 4103 : | @item | ||
| 4104 : | To use a word that has previously been defined, the text interpreter | ||
| 4105 : | searches for the word in the @dfn{name dictionary}. | ||
| 4106 : | @item | ||
| 4107 : | anton | 1.30 | Words have @dfn{interpretation semantics} and @dfn{compilation semantics}. |
| 4108 : | crook | 1.28 | @item |
| 4109 : | crook | 1.29 | The text interpreter uses the value of @code{state} to select between |
| 4110 : | the use of the @dfn{interpretation semantics} and the @dfn{compilation | ||
| 4111 : | semantics} of a word that it encounters. | ||
| 4112 : | crook | 1.28 | @item |
| 4113 : | anton | 1.30 | The relationship between the @dfn{interpretation semantics} and |
| 4114 : | @dfn{compilation semantics} for a word | ||
| 4115 : | crook | 1.29 | depend upon the way in which the word was defined (for example, whether |
| 4116 : | it is an @dfn{immediate} word). | ||
| 4117 : | crook | 1.28 | @item |
| 4118 : | crook | 1.29 | Forth definitions can be implemented in Forth (called @dfn{high-level |
| 4119 : | definitions}) or in some other way (usually a lower-level language and | ||
| 4120 : | as a result often called @dfn{low-level definitions}, @dfn{code | ||
| 4121 : | definitions} or @dfn{primitives}). | ||
| 4122 : | crook | 1.28 | @item |
| 4123 : | crook | 1.29 | Many Forth systems are implemented mainly in Forth. |
| 4124 : | crook | 1.28 | @end itemize |
| 4125 : | |||
| 4126 : | |||
| 4127 : | crook | 1.29 | @comment ---------------------------------------------- |
| 4128 : | anton | 1.48 | @node Where to go next, Exercises, Review - elements of a Forth system, Introduction |
| 4129 : | crook | 1.29 | @section Where To Go Next |
| 4130 : | @cindex where to go next | ||
| 4131 : | crook | 1.28 | |
| 4132 : | crook | 1.29 | Amazing as it may seem, if you have read (and understood) this far, you |
| 4133 : | know almost all the fundamentals about the inner workings of a Forth | ||
| 4134 : | system. You certainly know enough to be able to read and understand the | ||
| 4135 : | rest of this manual and the ANS Forth document, to learn more about the | ||
| 4136 : | facilities that Forth in general and Gforth in particular provide. Even | ||
| 4137 : | scarier, you know almost enough to implement your own Forth system. | ||
| 4138 : | anton | 1.30 | However, that's not a good idea just yet... better to try writing some |
| 4139 : | crook | 1.29 | programs in Gforth. |
| 4140 : | crook | 1.28 | |
| 4141 : | crook | 1.29 | Forth has such a rich vocabulary that it can be hard to know where to |
| 4142 : | start in learning it. This section suggests a few sets of words that are | ||
| 4143 : | enough to write small but useful programs. Use the word index in this | ||
| 4144 : | document to learn more about each word, then try it out and try to write | ||
| 4145 : | small definitions using it. Start by experimenting with these words: | ||
| 4146 : | crook | 1.28 | |
| 4147 : | @itemize @bullet | ||
| 4148 : | @item | ||
| 4149 : | crook | 1.29 | Arithmetic: @code{+ - * / /MOD */ ABS INVERT} |
| 4150 : | @item | ||
| 4151 : | Comparison: @code{MIN MAX =} | ||
| 4152 : | @item | ||
| 4153 : | Logic: @code{AND OR XOR NOT} | ||
| 4154 : | @item | ||
| 4155 : | Stack manipulation: @code{DUP DROP SWAP OVER} | ||
| 4156 : | crook | 1.28 | @item |
| 4157 : | crook | 1.29 | Loops and decisions: @code{IF ELSE ENDIF ?DO I LOOP} |
| 4158 : | crook | 1.28 | @item |
| 4159 : | crook | 1.29 | Input/Output: @code{. ." EMIT CR KEY} |
| 4160 : | crook | 1.28 | @item |
| 4161 : | crook | 1.29 | Defining words: @code{: ; CREATE} |
| 4162 : | crook | 1.28 | @item |
| 4163 : | crook | 1.29 | Memory allocation words: @code{ALLOT ,} |
| 4164 : | crook | 1.28 | @item |
| 4165 : | crook | 1.29 | Tools: @code{SEE WORDS .S MARKER} |
| 4166 : | @end itemize | ||
| 4167 : | |||
| 4168 : | When you have mastered those, go on to: | ||
| 4169 : | |||
| 4170 : | @itemize @bullet | ||
| 4171 : | crook | 1.28 | @item |
| 4172 : | crook | 1.29 | More defining words: @code{VARIABLE CONSTANT VALUE TO CREATE DOES>} |
| 4173 : | crook | 1.28 | @item |
| 4174 : | crook | 1.29 | Memory access: @code{@@ !} |
| 4175 : | crook | 1.28 | @end itemize |
| 4176 : | crook | 1.23 | |
| 4177 : | crook | 1.29 | When you have mastered these, there's nothing for it but to read through |
| 4178 : | the whole of this manual and find out what you've missed. | ||
| 4179 : | |||
| 4180 : | @comment ---------------------------------------------- | ||
| 4181 : | anton | 1.48 | @node Exercises, , Where to go next, Introduction |
| 4182 : | crook | 1.29 | @section Exercises |
| 4183 : | @cindex exercises | ||
| 4184 : | |||
| 4185 : | TODO: provide a set of programming excercises linked into the stuff done | ||
| 4186 : | already and into other sections of the manual. Provide solutions to all | ||
| 4187 : | the exercises in a .fs file in the distribution. | ||
| 4188 : | |||
| 4189 : | @c Get some inspiration from Starting Forth and Kelly&Spies. | ||
| 4190 : | |||
| 4191 : | @c excercises: | ||
| 4192 : | @c 1. take inches and convert to feet and inches. | ||
| 4193 : | @c 2. take temperature and convert from fahrenheight to celcius; | ||
| 4194 : | @c may need to care about symmetric vs floored?? | ||
| 4195 : | @c 3. take input line and do character substitution | ||
| 4196 : | @c to encipher or decipher | ||
| 4197 : | @c 4. as above but work on a file for in and out | ||
| 4198 : | @c 5. take input line and convert to pig-latin | ||
| 4199 : | @c | ||
| 4200 : | @c thing of sets of things to exercise then come up with | ||
| 4201 : | @c problems that need those things. | ||
| 4202 : | |||
| 4203 : | |||
| 4204 : | crook | 1.26 | @c ****************************************************************** |
| 4205 : | crook | 1.29 | @node Words, Error messages, Introduction, Top |
| 4206 : | anton | 1.1 | @chapter Forth Words |
| 4207 : | crook | 1.26 | @cindex words |
| 4208 : | anton | 1.1 | |
| 4209 : | @menu | ||
| 4210 : | * Notation:: | ||
| 4211 : | anton | 1.65 | * Case insensitivity:: |
| 4212 : | * Comments:: | ||
| 4213 : | * Boolean Flags:: | ||
| 4214 : | anton | 1.1 | * Arithmetic:: |
| 4215 : | * Stack Manipulation:: | ||
| 4216 : | anton | 1.5 | * Memory:: |
| 4217 : | anton | 1.1 | * Control Structures:: |
| 4218 : | * Defining Words:: | ||
| 4219 : | anton | 1.65 | * Interpretation and Compilation Semantics:: |
| 4220 : | crook | 1.47 | * Tokens for Words:: |
| 4221 : | anton | 1.81 | * Compiling words:: |
| 4222 : | anton | 1.65 | * The Text Interpreter:: |
| 4223 : | anton | 1.111 | * The Input Stream:: |
| 4224 : | anton | 1.65 | * Word Lists:: |
| 4225 : | * Environmental Queries:: | ||
| 4226 : | anton | 1.12 | * Files:: |
| 4227 : | * Blocks:: | ||
| 4228 : | * Other I/O:: | ||
| 4229 : | anton | 1.121 | * OS command line arguments:: |
| 4230 : | anton | 1.78 | * Locals:: |
| 4231 : | * Structures:: | ||
| 4232 : | * Object-oriented Forth:: | ||
| 4233 : | anton | 1.12 | * Programming Tools:: |
| 4234 : | * Assembler and Code Words:: | ||
| 4235 : | * Threading Words:: | ||
| 4236 : | anton | 1.65 | * Passing Commands to the OS:: |
| 4237 : | * Keeping track of Time:: | ||
| 4238 : | * Miscellaneous Words:: | ||
| 4239 : | anton | 1.1 | @end menu |
| 4240 : | |||
| 4241 : | anton | 1.65 | @node Notation, Case insensitivity, Words, Words |
| 4242 : | anton | 1.1 | @section Notation |
| 4243 : | @cindex notation of glossary entries | ||
| 4244 : | @cindex format of glossary entries | ||
| 4245 : | @cindex glossary notation format | ||
| 4246 : | @cindex word glossary entry format | ||
| 4247 : | |||
| 4248 : | The Forth words are described in this section in the glossary notation | ||
| 4249 : | anton | 1.67 | that has become a de-facto standard for Forth texts: |
| 4250 : | anton | 1.1 | |
| 4251 : | @format | ||
| 4252 : | crook | 1.29 | @i{word} @i{Stack effect} @i{wordset} @i{pronunciation} |
| 4253 : | anton | 1.1 | @end format |
| 4254 : | crook | 1.29 | @i{Description} |
| 4255 : | anton | 1.1 | |
| 4256 : | @table @var | ||
| 4257 : | @item word | ||
| 4258 : | crook | 1.28 | The name of the word. |
| 4259 : | anton | 1.1 | |
| 4260 : | @item Stack effect | ||
| 4261 : | @cindex stack effect | ||
| 4262 : | crook | 1.29 | The stack effect is written in the notation @code{@i{before} -- |
| 4263 : | @i{after}}, where @i{before} and @i{after} describe the top of | ||
| 4264 : | anton | 1.1 | stack entries before and after the execution of the word. The rest of |
| 4265 : | the stack is not touched by the word. The top of stack is rightmost, | ||
| 4266 : | i.e., a stack sequence is written as it is typed in. Note that Gforth | ||
| 4267 : | uses a separate floating point stack, but a unified stack | ||
| 4268 : | crook | 1.29 | notation. Also, return stack effects are not shown in @i{stack |
| 4269 : | effect}, but in @i{Description}. The name of a stack item describes | ||
| 4270 : | anton | 1.1 | the type and/or the function of the item. See below for a discussion of |
| 4271 : | the types. | ||
| 4272 : | |||
| 4273 : | All words have two stack effects: A compile-time stack effect and a | ||
| 4274 : | run-time stack effect. The compile-time stack-effect of most words is | ||
| 4275 : | crook | 1.29 | @i{ -- }. If the compile-time stack-effect of a word deviates from |
| 4276 : | anton | 1.1 | this standard behaviour, or the word does other unusual things at |
| 4277 : | compile time, both stack effects are shown; otherwise only the run-time | ||
| 4278 : | stack effect is shown. | ||
| 4279 : | |||
| 4280 : | @cindex pronounciation of words | ||
| 4281 : | @item pronunciation | ||
| 4282 : | How the word is pronounced. | ||
| 4283 : | |||
| 4284 : | @cindex wordset | ||
| 4285 : | anton | 1.67 | @cindex environment wordset |
| 4286 : | anton | 1.1 | @item wordset |
| 4287 : | crook | 1.21 | The ANS Forth standard is divided into several word sets. A standard |
| 4288 : | system need not support all of them. Therefore, in theory, the fewer | ||
| 4289 : | word sets your program uses the more portable it will be. However, we | ||
| 4290 : | suspect that most ANS Forth systems on personal machines will feature | ||
| 4291 : | crook | 1.26 | all word sets. Words that are not defined in ANS Forth have |
| 4292 : | crook | 1.21 | @code{gforth} or @code{gforth-internal} as word set. @code{gforth} |
| 4293 : | anton | 1.1 | describes words that will work in future releases of Gforth; |
| 4294 : | @code{gforth-internal} words are more volatile. Environmental query | ||
| 4295 : | strings are also displayed like words; you can recognize them by the | ||
| 4296 : | crook | 1.21 | @code{environment} in the word set field. |
| 4297 : | anton | 1.1 | |
| 4298 : | @item Description | ||
| 4299 : | A description of the behaviour of the word. | ||
| 4300 : | @end table | ||
| 4301 : | |||
| 4302 : | @cindex types of stack items | ||
| 4303 : | @cindex stack item types | ||
| 4304 : | The type of a stack item is specified by the character(s) the name | ||
| 4305 : | starts with: | ||
| 4306 : | |||
| 4307 : | @table @code | ||
| 4308 : | @item f | ||
| 4309 : | @cindex @code{f}, stack item type | ||
| 4310 : | Boolean flags, i.e. @code{false} or @code{true}. | ||
| 4311 : | @item c | ||
| 4312 : | @cindex @code{c}, stack item type | ||
| 4313 : | Char | ||
| 4314 : | @item w | ||
| 4315 : | @cindex @code{w}, stack item type | ||
| 4316 : | Cell, can contain an integer or an address | ||
| 4317 : | @item n | ||
| 4318 : | @cindex @code{n}, stack item type | ||
| 4319 : | signed integer | ||
| 4320 : | @item u | ||
| 4321 : | @cindex @code{u}, stack item type | ||
| 4322 : | unsigned integer | ||
| 4323 : | @item d | ||
| 4324 : | @cindex @code{d}, stack item type | ||
| 4325 : | double sized signed integer | ||
| 4326 : | @item ud | ||
| 4327 : | @cindex @code{ud}, stack item type | ||
| 4328 : | double sized unsigned integer | ||
| 4329 : | @item r | ||
| 4330 : | @cindex @code{r}, stack item type | ||
| 4331 : | Float (on the FP stack) | ||
| 4332 : | crook | 1.21 | @item a- |
| 4333 : | anton | 1.1 | @cindex @code{a_}, stack item type |
| 4334 : | Cell-aligned address | ||
| 4335 : | crook | 1.21 | @item c- |
| 4336 : | anton | 1.1 | @cindex @code{c_}, stack item type |
| 4337 : | Char-aligned address (note that a Char may have two bytes in Windows NT) | ||
| 4338 : | crook | 1.21 | @item f- |
| 4339 : | anton | 1.1 | @cindex @code{f_}, stack item type |
| 4340 : | Float-aligned address | ||
| 4341 : | crook | 1.21 | @item df- |
| 4342 : | anton | 1.1 | @cindex @code{df_}, stack item type |
| 4343 : | Address aligned for IEEE double precision float | ||
| 4344 : | crook | 1.21 | @item sf- |
| 4345 : | anton | 1.1 | @cindex @code{sf_}, stack item type |
| 4346 : | Address aligned for IEEE single precision float | ||
| 4347 : | @item xt | ||
| 4348 : | @cindex @code{xt}, stack item type | ||
| 4349 : | Execution token, same size as Cell | ||
| 4350 : | @item wid | ||
| 4351 : | @cindex @code{wid}, stack item type | ||
| 4352 : | crook | 1.21 | Word list ID, same size as Cell |
| 4353 : | anton | 1.68 | @item ior, wior |
| 4354 : | @cindex ior type description | ||
| 4355 : | @cindex wior type description | ||
| 4356 : | I/O result code, cell-sized. In Gforth, you can @code{throw} iors. | ||
| 4357 : | anton | 1.1 | @item f83name |
| 4358 : | @cindex @code{f83name}, stack item type | ||
| 4359 : | Pointer to a name structure | ||
| 4360 : | @item " | ||
| 4361 : | @cindex @code{"}, stack item type | ||
| 4362 : | anton | 1.12 | string in the input stream (not on the stack). The terminating character |
| 4363 : | is a blank by default. If it is not a blank, it is shown in @code{<>} | ||
| 4364 : | anton | 1.1 | quotes. |
| 4365 : | @end table | ||
| 4366 : | |||
| 4367 : | anton | 1.65 | @comment ---------------------------------------------- |
| 4368 : | @node Case insensitivity, Comments, Notation, Words | ||
| 4369 : | @section Case insensitivity | ||
| 4370 : |