Tuesday, August 30, 2022

P0cForth

What is P0cForth?

P0cForth is NOT another (full) Forth implementation. It is a only minimalistic Forth like system and my idea about P0cForth, I respect other ideas about minimalistic Forth and even opinion that this is so minimal that it is not a real Forth system. That is why it has a 0 in it name.

Why P0cForth?

For a long time I was thinking what Forth words are minimal needed to code to create a Forth (like) system. There are guides what Forth primitives need to be created to bootstrap a full Forth system. However I wanted a minimalist set of Forth words. Not to bootstrap a Forth system, only as a proof of concept for a Forth system that can be extended. A search on internet gives information about several interesting minimal Forth systems. Even systems minimal systems including @ ! however that is not what i wanted.
Also wanted to give it a name. I decided for P0cForth , "Proof of concept Forth" , i changed the o to 0.
It is to have bare minimal system that is relative easy to build (and can be extended) to have a proof of concept Forth system. 

What words/code need to be in P0cForth?

In the Forth2020 Zoom meeting on 9 januari 2021 Dr Ting talked about  Jeforth 2.01.
He mentioned a minimal system with 9 Forth words
: ; * . , dup dolit ret here
He also presented simple demos:
: square dup * ;
: quad square square ;
; octet quad quad ;
8 octet . 4 octet .

I used this 9 words as a start for P0cForth., However, I want to

reduce it even more.

So I ended up with this 5 words:
: ; * . dup 
I decided to reduce the system even more by omitting the interpretation of numbers defining (some) numbers direct as words. This would decrease the programming code and logic. And yes when extending the proof of concept Forth real handling of numbers would be useful and literals and dolit can be very useful. However I wanted to reduce it as much as possible. Literals are a very useful method of implementation, however i did not want to force this as a solution for handling numbers in my definition of P0cForth.
The code created for a number as a Forth words for a target system can be easy copied and adapted for another number. So it could be easy extended to include a lot of other numbers. However i wanted it to  as minimal as possible.
What numbers would be needed  minimal to have a proof of concept Forth system? At first did think at a minimum i would be needing the numbers 0 1 2 3 and (perhaps) -1 With this numbers you can play in the minimal PocForth for testing. And these numbers could be included in the dictionary.
With * and -1 it can be  and easy extend it to numbers in the range -4 to 4 (However if you continue reading to the end you will see this idea was wrong. Only one very special number is sufficient to create all numbers!)
: 4 2 2 * ;
: -2 -1 2 * ;
: -3 -1 3 * ;
: -4 -1 4 * ;
Also the demo mentioned above can be tested (after creating 8)
: 8 4 2 * ;
: square dup * ;
: quad square square ;
; octet quad quad ;
8 octet . 4 octet .

Adding code for a + function would make it possible to extend the number range even further without having gaps.

My first idea of a minimal P0cForth that could be a first start for a minimal Forth as
5 Forth words                 : ; * . dup
5 ‘number words’           -1 0 1 2 3

Creating e.g. a text input stream processor with this 6 different type of words would give a proof of concept system that could be used as a basis to create a full Forth system. 

Two facts made me not complete happy with this set of words.
- The possibility to build a complete Forth with these words is missing. it is only possible to extend this Forth by changing the 'external' code and add more words. However this minimal words can be a good basis of a proof of concept when creating a new Forth.
- And most important I did think it would be possible to reduce this word set even more!
So i started to reducing
1 is not needed as it could be defined by the other words
: 1 -1 dup * ;
If i would add the Forth word + 2 and 3 can be removed 
: 2 1 dup + ;
: 3 1 dup dup + + ;
Also 0 is not needed as it can be created
: 0 -1 1 + ; 
: 0 -1 dup dup * + ;
I would never have imagined that it was only needed to have one number to create other numbers, and that that number would be -1  

P0cForth minimal wordset 
No interpretation of numbers needed and only 7 words 
:  ;  .  *  dup  +  -1

Here some demo code to demonstrate what can be done with this 7 word system
: 0 -1 dup dup * + ;
: 1 -1 dup * ;
: 2 1 dup + ;
: 3 1 dup dup + + ;
: 4 2 dup * ;
: 8 4 2 * ;
: - -1 * + ;
: square dup * ;
: quad square square ;
: octet quad quad ;
3 square .
8 octet . 
4 octet .
8 3 - .
I think it is possible to reduce this 7 word set even more or add more capabilities adapting the selection of this words.
A simple option would be to reduce the word set would be omitting ; and have a the new definition, that in Forth starts with the colon :  close always at the end of the line.

No comments: