Forth is a ProgrammingLanguage developed by ChuckMoore in the 1960s (see the history of Forth). Forth is a Stack based language with a ReversePolish syntax. A function call is very fast as the arguments are those on the Stack when the program enters and the return value is left on the Stack when it exits. A Forth program consists of many small functions, with just the essentials written in AssemblyLanguage.
Forth is used in embedded systems. It produces very compact code. A whole Forth interpreter and development system will fit into 8 kilobytes, easily, and leave plenty of room for code. Back when the computer with 8 kilobytes of RAM that you were to write programs for was also the the computer you had to write programs on Forth was very popular.
GNU's Forth. It's unusual for a Forth in that it comes with a manual. Forth programmers aren't big on manuals :-(
It has a VirtualMachine and kernel (see below) written in C. It might be a good one to study.
A native code compiler for x86 which includes a GUI library and form editor.
Forth is also the basis for the RPL language used in HewlettPackard calculators and OpenFirmware.
Sentences long extremely and notation Polish reverse in writing about wrong is what?
— Jarkko Hietaniemi
This is a general description of how Forth works. It's different. If you like Brainf*ck you will like Forth.
Forth functions are called words. which are organised in the user dictionary.
A Forth machine has two stacks. When subroutines (called words in Forth lingo) are called they pop arguments off the parameter stack and push back return values. The return stack holds the return addresses of word calls.
Finally, the input buffer for the Forth parser forms an integral part of a Forth system.
Forth programs are written in ReversePolish notation. Forth words access the parameter stack directly to fetch arguments and leave return values. The stack is also used for temporary storage, as traditionally there are no local variables in Forth. There are words for flipping around values on the top of the stacks to help you with this. As you program in Forth you must keep careful track of what is supposed to be on the stack at every point, though this is much easier than it first seems (beginners tend to make this more complicated than necessary, thus giving Forth a bad reputation as a "write-only" language). This is all part of ChuckMoore's philosophy of brutal simplicity in software engineering.
The dictionary is structured also like a stack; there is a here pointer, where everything behind it is considered allocated and part of the runtime system, and everything in front is considered temporary storage at best. As words are defined, here is advanced. When you wish to recycle memory, you forget a word (usually by executing a word defined by 'MARKER'; see the ANSI Forth specification for details), which resets here to that word's definition point.
Some Forth's permit multiple vocabularies to exist concurrently, analogous to C++ namespaces. The first vocabulary is a tiny kernel of word definitions written in MachineCode, then there is a standard library vocabulary defined in Forth, and then optional vocabularies that define a simple text editor, assembler and disk OperatingSystem. (A modern Forth system might have a TCP/IP stack on there somewhere too.)
There is no syntax in Forth, just words for manipulating other words, the input buffer and the user dictionary.
The word : tells Forth to begin compiling the subsequent words found on the input buffer.
Some words have flags that tell Forth to execute them immediately while compiling: these create control structures in the word. Ordinary words may be compiled into call statements, or they may be expanded to native machine language instructions. Words that look like integers may be compiled into the equivalent of push statements, or may be embedded into immediate operands of machine instructions. Note that the definition is usually built piece-meal in the dictionary, so that when ; is executed, the definition is already finished. Highly optimizing compilers, however, will wait to put anything at all in the dictionary until ; is encountered, where ; pushes the new defintion onto the dictionary after proper lexical and parsed analysis. To help generate efficient code, some highly optimizing compilers will even infer types based on words you call in the definition!
You use : to add new words to the Forth system until you've created a high-level Forth vocabulary that you can easily express your problem in. You just keep going until you've defined a single word that, when called, executes your entire program. Forth is very much a BottomUp language. :-) However, this is absolutely no different from programming in any other language; in C, all programs ultimately function from a single main() function. Likewise, in Haskell as well.
MachineCode statements can be used as Forth ByteCodes so the machine can execute them directly. One form of this is called subroutine threading. It is one of a number of approaches to reduce the overhead of interpretation (which spends a lot of time executing call statements).
Real Forth geeks write their own Forth kernels from the metal up. (I wrote my own, on paper, when I was 14. (I don't think I can do that anymore.) I was working from a Forth book and a Z80 opcode table; I didn't have a computer. I still have pieces of it. God knows if it would have worked.) ChuckMoore creates his own Forth machines from silicon. It's not very hard once you've absorbed the Forth philosophy. There is shockingly little holding Forth up.
This right here is a lovely example of Forth: LifeInForth.
This more sober example comes from the IRC bot (in ISForth):
\ receive up to 512 bytes from bot socket : bot-read off> #inbuff inbuff \ where to receive into begin 1 over bot-fd recv \ read one char from bot socket 1- \ result should be 0 now if \ if its not then we didnt get any input drop exit \ so discard buffer address and exit then incr> #inbuff \ we got a character dup c@ \ get the character we just read $0a <> \ while its not an eol #inbuff 512 <> and while 1+ \ advance buffer address repeat drop \ discard buffer address ;
Notice the style of code is predominantly vertical, leading to a program structure that is intrinsically very hard to read. The while loop's condition check, for example, consists of the overwhelming bulk of the code. This, generally, is considered very poor programming style in Forth.
Yet another example, a simple text editor (from Retro Native Forth), is much easier to read, and demonstrates the preferred programming method that is more horizontal. Just as words in a college dictionary are written in only a few lines, and are predominantly horizontal, so too is the code below. Another way of thinking about this is in terms of functional programming, where most definitions are, despite being defined at the global scope, really private to the main word:
0 variable lastBlock asmbase $1024 + constant blockBase : block 512 * blockBase + ; : select dup lastBlock ! ; : vl dup 40 type cr 40 + ; : view lastBlock @ block vl vl vl vl vl vl vl vl vl vl vl vl ; : edit select block s drop drop view ; : delete select block 512 0 fill view ; : edit-line 40 * lastBlock @ block + s drop drop view ; : delete-line 40 * lastBlock @ block + 37 0 fill view ;
For example, the definition of "block" can be likened to the more traditional functional style:
let block b = blockBase + (512*b)
In fact, there are many similarities and dualities between Forth and functional programming languages.
As you can see, Forth shares many traits with functional languages, and therefore doesn't always apply to the AssemblyLanguage programmer. In fact, besides embedded devices, music is one of Forth's application domains, as it is used in some of UCSD's music courses. Forth has successfully been used in artificial intelligence research, and is the language of choice for NASA on many satellites and planetary probes. In these environments, the functional correctness and compactness of Forth weighs more heavily than the use of assembly language.
CategoryProgrammingLanguages, CategoryMachineOrientedProgrammingLanguages, CategorySystemsProgrammingLanguages