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