|
Forth is a programming language that is simple, easy to
implement, elegantly extensible and efficient. Using separate stacks for
data and calls allows for easy extensibility of both keywords and
language constructs such as branches and switches.
While various efforts have been made to make a
minimalist dialect, implementations to date have been either not
very small, just shells or have inefficiencies in the realm of a
classic Turing machine. This page shows a dialect with the
following aspects.
In Forth, memory is organized in three sections:
| Data stack* |
Call stack |
Program |
| D0 |
C0 |
P0 |
| D1 |
C1 |
P1 |
| D2 |
C2 |
P3 |
| ... |
... |
... |
* data and subroutine parameters
Primitives:
|
Symbol |
Summary |
Definition |
| + |
Add |
Replace D0 and D1 by
sum |
| N |
Nand |
Replace D0 and D1 by
nand |
| J |
Jmp not 0 |
Change IP by D0 if D1
not zero and drop D0 |
| ^ |
Promote |
Move D0-th data item
to D0. If D0 = 0 remove D0. |
| D |
Duplicate |
Push copy of D0 |
| n |
Literal |
Push to D0 a literal
numeric value (e.g. 123) |
| ! |
Store |
Put D0 at program
address D1 |
| @ |
Load |
Push to data stack
the value at program address D0 |
| : |
Subroutine |
Permits subroutines,
macros and language extension |
| ; |
Return |
End subroutine
definition and return |
| Z |
Return if zero |
If D0 is 0 return to
C0 |
Bootstrapping examples
|
Code |
Definition |
|
: NOP 0 + ; |
No
operation |
|
: DROP 0 ^ ; |
Remove
top item of data stack |
|
: SWAP 1 ^ ; |
Switch
top two items of data stack |
|
: ROLL 2 ^ 1 ^ ; |
Roll top
of data stack below the next two items |
|
: INC 1 + ; |
Increase
value at top of data stack by 1 |
|
: COM D N ; |
1's
compliment |
|
: DEC 255 + ; |
Decrease
value at top of data stack by 1 |
|
: NEG COM INC ; |
Change
sign of value at top of data stack |
|
: SUB NEG + ; |
Replace
D0 and D1 by D1 minus D0 |
|
: ABS D 128 N 4 J DROP NEG INC D DROP ; |
Replace
D0 by its absolute value |
|
Uses
* Makes it quick and easy to port to various
machines
* Appropriate for some embedded applications
* Bootstrapped BIOS usage
* Teaching Forth
* Language research
* Allows to introduce Forth to someone using a single simple web
page
* etc.
Project status
Project is currently neither complete nor debugged. I'm working on
determining what call stack primitives are needed to allow building
control statements like branch, switch, etc. and showing some of the
associated bootstraps. I welcome assistance from the Forth community to
complete and refine the dialect, staying within the mentioned goals.
|