Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New definer syntax #16

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open

New definer syntax #16

wants to merge 51 commits into from

Conversation

nickd4
Copy link

@nickd4 nickd4 commented Apr 30, 2022

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:

  • 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

Both should emit BA.

…to bss rather than text section which avoids the need to call mprotect(), rename things
… be wrapped with PROGRAM / END, also removes automatic bye token that was generated by END
…time.seedsource, so that we can run textual forth code without the tests or the banner
… writes to stderr, fix self-hosted tokenizer termination issue (was debugged with eemit)
…ng the nonstandard words on top of gForth's standard words, produces redefinition warnings
…seedForth-i386.pre containing some compiler words so we can do special handling for DTC
…sticated tracing and symbolic debug (via annotated trace) system used to debug the 65C02 port, the trace is also available for the Z80 port and can be used for comparison
…e caller's responsibility to do so if they need to, in the case of "new" this is done by replacing "new drop" with "new h," in the seedForth kernel, whereas in the case of "create" it is done by having the tokenizer prefix the body of the user's Definer-definition with "here h," -- or could make the latter a user responsibility
@nickd4 nickd4 mentioned this pull request May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants