Find a file
Koichi Nakamura 410a5c2ed0 minor fix
2021-01-05 08:17:54 +09:00
others Add build target c and python 2021-01-04 22:16:12 +09:00
.gitignore Add .gitignore 2020-12-28 22:11:45 +09:00
bootstrap.fs minor fix 2021-01-05 08:17:54 +09:00
helloworld.fs change helloworld.fs 2021-01-03 12:46:17 +09:00
LICENSE fix copyright year 2021-01-02 13:29:46 +09:00
Makefile Fix '\n' 2021-01-04 22:23:54 +09:00
planck.xxd BREAKING CHANGE: Changed Pascal string to C-string 2021-01-04 21:57:52 +09:00
README.md change title 2021-01-04 22:17:05 +09:00

PlanckForth: Bootstrapping Forth from Handwritten Binary

This project aims to bootstrap a Forth interpreter from hand-written tiny ELF binary.

How to build

You can create a binary for x86 linux environment by following steps. Only xxd is needed to build PlanckForth.

$ git clone https://github.com/nineties/planckforth.git
$ cd planckforth
$ make
xxd -r -c 8 planck.xxd > planck
chmod +x planck

There are implementations in C and Python 3 as a reference in others. Try make c or make python

Hello World

The hello world program at the beginning looks like this.

$ ./planck
kHtketkltkltkotk tkWtkotkrtkltkdtk!tk:k0-tQ

After bootstrapping by bootstrap.fs, it looks like this.

$ cat bootstrap.fs - | ./planck
." Hello World!" cr

Other examples.

$ cat bootstrap.fs - | ./planck
123 456 + . cr
: fib dup 2 < unless 1- dup recurse swap 1- recurse + then ;
30 fib . cr

Builtin Words

code name stack effect semantics
Q quit ( -- ) Exit the process
C cell ( -- n ) The size of Cells
h &here ( -- a-addr ) The address of 'here' cell
l &latest ( -- a-addr ) The address of 'latest' cell
k key ( -- c ) Read character
t type ( c -- ) Print character
j jump ( -- ) Unconditional branch
J 0jump ( n -- ) Jump if a == 0
f find ( c -- xt ) Get execution token of c
x execute ( xt -- ... ) Run the execution token
@ fetch ( a-addr -- w ) Load value from addr
! store ( w a-addr -- ) Store value to addr
? cfetch ( c-addr -- c ) Load byte from addr
$ cstore ( c c-addr -- ) Store byte to addr
d dfetch ( -- a-addr ) Get data stack pointer
D dstore ( a-addr -- ) Set data stack pointer
r rfetch ( -- a-addr ) Get return stack pointer
R rstore ( a-addr -- ) Set return stack pointer
i docol ( -- a-addr ) Get the interpreter function
e exit ( -- ) Exit current function
L lit ( -- n ) Load immediate
S litstring ( -- c-addr ) Load string literal
+ add ( a b -- c ) c = (a + b)
- sub ( a b -- c ) c = (a - b)
* mul ( a b -- c ) c = (a * b)
/ div ( a b -- c ) c = (a / b)
% mod ( a b -- c ) c = (a % b)
& and ( a b -- c ) c = (a & b)
| or ( a b -- c ) c = (a | b)
^ xor ( a b -- c ) c = (a ^ b)
< less ( a b -- c ) c = (a < b)
= equal ( a b -- c ) c = (a == b)