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