Search code examples
forthgforth

How do I "extend" the outer interpreter?


In different Forths, how do I customize the interpreter?

For example, in SP-Forth the interpreter always write the stack content and always make a CR/LF before printing:

1 2 3
 Ok ( 1 2 3 )
.s
1 2 3  Ok ( 1 2 3 )

I would like to see:

1 2 3  ok
.s 1 2 3  ok

And generally, I would like to be able to define new data inputs like

4-3i
{1,2,3,4,5}

The interpreter should then store the data as I defined in the extension. Also, on errors I would like soft stacks and variables to be reset.

Any ideas?


Solution

  • In different Forth systems there are different ways for customization. In SP-Forth you can define another action for OK vector. For example, to avoid printing data stack state:

    : OK2 ( -- ) STATE @ IF EXIT THEN ."  ok" CR ;
    ' OK2 TO OK
    \ see the original definition in src/compiler/spf_translate.f
    

    Though, it is difficult to avoid CRLF before "ok" in Windows console application since this CRLF is part of the text that you input.

    Regarding a new data formats there is Recognizers RfD. It is implemented in some Forth systems, but not in SP-Forth yet.

    In SP-Forth you can use NOTFOUND mechanism to add custom word format (words interpreter).