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
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:
there is no reason why both approaches cannot coexist, so I have left Definer in as well, keeping seedsource compatibility;
I revisited my earlier hash-table code to change most underscores to hyphens, i.e. more in keeping with the usual Forth style;
I also fixed a bug where a hash table entry was considered vacant between Macro and end-macro and might be reused.
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
Currently seedForth has a special syntax for defining words, for example
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 callh,
to create a new head, we need to move this call into the seedForth defining-words, for exampleand I had done a workaround in my last PR where the tokenizer would automatically prepend
here h,
to a definer, e.g.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.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 ofVariable
) 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:
Definer
in as well, keeping seedsource compatibility;Macro
andend-macro
and might be reused.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:
versus
Both should emit
BA
.