[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Trying to extend gforth's outer interpreter
From: |
Anton Ertl |
Subject: |
Re: Trying to extend gforth's outer interpreter |
Date: |
Fri, 1 Apr 2022 13:18:21 +0200 |
On Fri, Apr 01, 2022 at 09:51:09AM +0100, Tristan Williams wrote:
> Hello,
>
> I am trying to extend gforth's outer interpreter so that how numbers
> are treated is dependent on which mode (indicated by the value of
> mode?) my program is in. In the code below, if mode? is true the
> number is just dropped, otherwise it should be treated as usual and
> placed on the host data stack. This seems to work if I type
> definitions in interactively at the terminal, but if I include them at
> the end of the source file or interactively using include "filename"
> it does not. What am I doing wrong?
You hook into 'quit, which is only used for text-interpretation of
terminal input.
If you use a recent development version of Gforth, the way to go is to
define a new number recognizer, and replace the default number
recognizer REC-NUM with the new one (or just insert the new one in
front of REC-NUM in the recognizer stack). The documentation is not
finished yet, but look at the source of REC-NUM, and use
GET-RECOGNIZERS and SET-RECOGNIZERS to change the recognizer stack.
If you use Gforth-0.7, things are more complex: You need to replace
INTERPRETER1 and COMPILER1. These are not hooks, so you need to patch
the existing colon definitions. To do that, use:
: >colon-body ( xt -- addr )
dup @ docol: <> -12 and throw >body ;
: >prim-code ( xt -- x )
\ converts xt of a primitive into a form usable in the code of
\ colon definitions on the current engine
threading-method 0= IF @ THEN ;
: replace-word ( xt1 xt2 -- ) \ gforth
\G make xt2 do xt1, both need to be colon definitions
swap >colon-body ['] branch >prim-code rot >colon-body 2! ;
- anton