If "a language is a mapping from syntax to semantics" then FORTH is one of the most direct languages possible. It can define new words (unlike assembly (except via macros) ) and the mapping is as clear one can imagine. Each "word" in forth (delimited only be spaces) is mapped directly to a collected sequence of other words or machine code.
Unlike machine code, where the paradigm is "data from source through operation to destination" FORTH is based on a stack, which is used to hold data for operations which then return new data to the stack. Assignments and parameters are consolidated into this one stack model.
For example, where we might say A = B + C; in C or Javascript, or MOV B, A; ADD C, A; in assembly, in FORTH we would say B@ C@ + A!. This loads the values at address B, and then A, from memory onto the stack, then + takes the top two items from the stack and adds them, replacing them on the top of the stack with the result, which we then bang into the address of A. Where we might implement a procedure called int vectorDistance(vector A, point B)which computes the distance between a line and a point, in FORTH we would A B VECTOR_DISTANCE (and be left with the distance remaining on the stack).
As a useful example, the following program converts F to C:
: F2C (f -- c) 32 - 5 * 9 / ;
The (f -- c) is a comment and is meant to show the values on the stack before and after the word is executed. We start with an expectation of a temperature in 'F on the stack, then we push a 32, subtract, push a 5, multiply, push a 9 and divide leaving the result on the stack in 'C.
There are several effects from this approach:
See
James Newton says:
Warning: Assembly code and bad memory1000 years ago, when I was 17, I wrote a FORTH for the Z80 on a Trash 80. It was unique for the time, because it used the machine language RET as NEXT for asm words. e.g. the machine stack pointed NOT at the data stack or return stack but at the list of word addresses being executed; the thread. That meant that asm words could be strung together even faster than a standard asm program because there was no asm CALL... the RET was both the return from the current asm word AND the call to the next one. The address of the data and return stacks were kept elsewhere... one of the other registers or a memory location (I don't remember, code is long lost). I think it was IX and IY. Every non-asm word had to start with an asm call to a routine that saved SP to the return stack pointer, set the SP to the value on the stack and RET. If I remember, that's called THREAD? Then at the end, the last address was to an asm word that pulled the return stack TOS back into SP and RET. I seem to remember that is called NEXT. So for asm words, NEXT is RET and for higher level words, NEXT was a few asm instructions.
Advantages: /screaming/ fast low level words, no slower high level words.
Disadvantages: low level words couldn't really use the stack, because they would overwrite a thread. Of course, you could save and restore SP... but I seem to remember doing most manipulation of the stack with other registers. e.g. LD (IX+0),SP; INC IX; INC IX; Or maybe it was DEC, I don't remember.
And the big disadvantage: NO INTERRUPTS during asm words! LOL. Can't save PC to TOS because it would over write a thread. I got around that somewhat by adding an EI (enable interrupts) in the NEXT code while the SP was pointed at the return stack, doing a NOP and then DI. LOOP also had that in it because it was asm.
A comparison of Lisp and Forth by Gordon Charlton:
Forth and Lisp are mirror images. The opposites include postfix v. prefix, static allocation v. dynamic allocation, explicit v. Implicit stack.The primary point of coincidence is that executing a Forth word and evaluating a Lisp function are both depth first tree traversal. They are both interactive and extensible.
Lisp comes from academia and the lambda calculus, a world where more abstract means more fundamental, and computing is mathematics made real with information processing technology.
Forth comes from pragmatism and electrical engineering, where more fundamental means closer to the physics, and software is hardware by other means.
In short, they are as different as Church and Turing, which is to say demonstrably equivalent.
See also: