pfe - Portable Forth Environment
pfe [-hlqv] [-bFILE] [-dFILE] [-eEDITOR] [-fN] [-kKB] [-rN] [-sN] [filename]
pfe is a programming environment for the programming language Forth. Forth is a highly interactive, stack oriented language well suited for technical applications. Writing a program in Forth means extending the language until a "word" in the language is the solution of the problem.
pfe is nonstandard in that it claims to conform to the draft proposed American National Standard for the programming language Forth. In all other respects it conforms to this new standard. All word sets mentioned in the standard including their extensions are built into pfe and are immediately available. The only missing words are those related to the assembler. Being portable, pfe has no built-in assembler.
Additionally pfe is compatible to the FORTH 83 Standard and has several words built in that stem from the FIG Forth model. The goal is to be familiar.
pfe is free software; you can distribute it and/or modify it under the terms of the GNU General Public License for Libraries as published by the Free Software Foundation. The author has no plans for turning pfe into a commercial or share-ware product. pfe is available over the Internet by anonymous FTP from roxi.rz.fht-mannheim.de, file: pub/unix/languages/pfe-?.?.?.tar.gz.
The main purpose of this manual is to meet the "additional documentation requirements" specified in the dpANS, i.e. to document the environmental dependencies and the implementational options chosen in the portable Forth environment.
Starting from your operating system's command interpreter pfe recognizes the following command line options:
The defaults for these options can be set at compile time and depend on your installation. The -h option shows the actual settings. Turn an option off by appending a '-' to the letter.
Forth is a stack oriented, extensible programming language. Writing a program in Forth means extending the language such that finally a word or a small set of words in the language solves the given problem. In fact "word" is the technical term in Forth for variables, constants, subroutines -- for almost everything you type in -- except numbers.
The typical approach to a programming problem using Forth is `bottom up' and interactive. Most naturally at the beginning of a development words are written to solve very small sub-problems. These words are put together to solve bigger parts of the problem and finally to form the solution of the whole problem.
At any time in the evolution of the program all parts can be tested individually by simply typing in the appropriate parameters and words.
Being "stack oriented" means: The central data structure in Forth is a stack. Words expect their parameters on top of that stack and replace them by the results. It is neccessary in such a configuration to write a word consuming the parameters following those parameters, i.e. to use postfix notation (or "reverse polish notation", rpn):
123 456 + 78 * .
calculates (123+456)*78 and prints the result. In this example `+' and `*' are the "words" doing addition and multiplication, the numbers provide the parameters and `.' prints the result. You may be acquainted to this concept if you have used an HP calculator.
Word names may contain any printable character with the exception of space. Space is used to separate words from each other. You can only use words that were defined before. These two simple rules and the postfix concept make the Forth Interpreter stunningly simple. There is no need for what makes other compilers complex: lexical, syntactic and semantic analysis.
When asking if Forth is an interpreted or compiled language you may get different answers. I believe -- at least for pfe -- the correct answer is: Interpreted. But there are two different levels of interpretation and something called a "compiler" is in between of them. Other systems may be true compilers generating machine code. pfe is not such a true compiler.
Additionally the compiler can handle all sorts of control structures to make decisions and to loop in a convenient way.
The lists generated by the compiler and interpreted by the inner interpreter are called "threaded code" and Forth is sometimes referred to as a "threaded interpretive language" (TIL).
For compatibility with other - case insignificant - Forth implementations say LOWER-CASE ON or use the option -l. This causes pfe to convert all words to upper case prior to dictionary search and thus permits input of forth words in lower case.
To make input of upper case text easier, a caps lock mode is supplied. The pfe command line exchanges lower and upper case characters by default. You can toggle this caps lock mode by pressing Ctrl-C on the command line.
The pfe command line remembers your old input lines and you can recall them using the arrow up (or ^E) and arrow down (or ^X) keys. Editing the command line is possible with the arrow left/right (^S/^D) keys. Delete characters with your terminal's delete character key or with ^G. Toggle insert mode with your terminal's insert character key or with ^V.
The pfe command line completes partially entered Forth words by comparing them to all words in the dictionary (not only those in the search order). Press the Tab-key to complete a word partially typed in. If the completion is unique, a space is appended. If no completion is possible the bell rings. This might have been disabled at compile time. Check your installation if it doesn't work.
Consequently you can't quit from pfe with the interrupt key. You can use it however to break out of endless loops and to return to the Forth command line when you lost control over your Forth program.
These keys might have been changed at compile time. Check your installation if they don't work as described.
Additionally you can handle all signals with Forth words: Given a forth word's execution token (xt) use the word SIGNAL to install the word as handler to an arbitrary signal:
: PEEP ." PEEP" 7 EMIT CR ;
' PEEP SIGINT SIGNAL
Now pressing ^U (default interrupt character inside pfe) executes PEEP. Another ^U executes the default action of SIGINT again (i.e. -28 THROW). Use
0 SIGINT SIGNAL
to reset the state of SIGINT to pfe's default.
Note that due to limitations of the termcap library not every feature can be turned on and off individually. The HIGHLIGHT mode is probably the same as either INTENSITY or REVERSE. This depends on your terminals capabilities and how accurate they are described in /etc/termcap.
Note: Since there is no way to retrieve the current cursor position from the terminal using methods of the termcap library, the cursor position is tracked inside pfe. The cursor position is initially unknown. So when using ?XY do an AT-XY, a CLS or a HOME at least once before. The cursor position is tracked by all pfe output operators but if a system call is executed, it gets lost. Same remedy.
Key | termcap | Code | CONSTANT |
F1 | "k1" | 256 | K1 |
... | ... | ... | ... |
F9 | "k9" | 264 | K9 |
F10 | "k0" | 265 | K10 |
arrow left | "kl" | 266 | |
arrow right | "kr" | 267 | |
arrow up | "ku" | 268 | |
arrow down | "kd" | 269 | |
home | "kh" | 270 | |
home down | "kH" | 271 | |
next page | "kN" | 272 | |
previous page | "kP" | 273 | |
backspace | "kb" | 274 | |
delete character | "kD" | 275 | |
exit insert mode | "kM" | 276 | |
insert character | "kI" | 277 | |
insert line | "kA" | 278 | |
clear to end of line | "kE" | 279 | |
delete line | "kL" | 280 | |
clear screen | "kC" | 281 |
Usually those function keys send strings starting with the escape character ASCII 27. Therefore if the escape-key of the keyboard is pressed EKEY consumes it silently and checks if it forms a function key string together with the next keys pressed. If so, EKEY returns the code for that function key, otherwise it returns 27 and the following characters are spooled out on subsequent calls to EKEY.
pfe uses one contiguous memory area allocated at startup. The size of that area is determined by the command line option -k. The area is divided in the following parts (from low to high memory):
pfe includes ~/.pferc at startup.
pfe looks for the following environment variables:
Doesn't recognize cursor keys when running in an xterm.
Those I didn't notice yet and those I plan to fix really soon.