uho / preForth

a minimalistic Forth kernel that can bootstrap
GNU General Public License v3.0
74 stars 8 forks source link

New definer syntax #16

Open nickd4 opened 2 years ago

nickd4 commented 2 years ago

Currently seedForth has a special syntax for defining words, for example

Definer Variable ( <name> -- )    create ( x ) drop 0 , ;

and this is necessary because we cannot really properly handle arguments to Forth words when working via the tokenizer, we can only sort of approximate it with workarounds for common use cases such as defining-words as seen here.

Referring to my last PR "Less use of heads", since I have changed the seedForth create primitive to no longer call h, to create a new head, we need to move this call into the seedForth defining-words, for example

Definer Variable ( <name> -- )    create ( x ) h, 0 , ;

and I had done a workaround in my last PR where the tokenizer would automatically prepend here h, to a definer, e.g.

Definer Variable ( <name> -- )    here h, create ( x ) drop 0 , ;

and this preserved seedsource compatibility. But I wasn't really all that happy with that workaround.

So in this PR I am trying a new approach which is more Forth-like, and allows us to define defining-words with a simple :, e.g.

: Variable ( <name> -- )    Create ( x ) drop 0 , ;

and the trick here is that Create (with a capital C) is now basically an immediate word which pulls some tricks in the tokenizer to mark the current colon-definition (the definition of Variable) as a definer. Apart from that, it works kind of similarly to before.

The code is well-commented so I will not go into the details of the algorithm here, except to say that:

I have not rewritten all the library code to the new syntax yet, I can do that later if the PR is approved. I used the following test:

Definer Variable create drop 0 , ;

Variable a 65 a !
Variable b 66 b !
Variable t a @ t ! b @ a ! t @ b !
a @ emit b @ emit

versus

: Variable Create drop 0 , ;

Variable a 65 a !
Variable b 66 b !
Variable t a @ t ! b @ a ! t @ b !
a @ emit b @ emit

Both should emit BA.