[gforth] / gforth / doc / gforth.ds  

gforth: gforth/doc/gforth.ds


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