gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Trying to extend gforth's outer interpreter


From: Tristan Williams
Subject: Trying to extend gforth's outer interpreter
Date: Fri, 1 Apr 2022 09:51:09 +0100

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? All help gratefully received.

Kind regards,
Tristan
        
false value mode?

: throw-unknown ( caddr n -- )
    ." Unknown word: " type cr abort
;

: mode-number ( n -- |n )

    state @ 0= if 
        mode? if
            drop
            s" STATE 0 MODE true DIFFERENT" type cr 
        else
            s" STATE 0 MODE false USUAL" type cr 
        then
    then
    
    state @ if
        mode? if
            drop 
            s" STATE <>0 MODE true DIFFERENT" type cr 
        else
            s" STATE <>0 MODE false USUAL" type cr 
            postpone literal exit
        then
    then
;

: >number ( caddr n -- )
    2dup 2>r snumber? dup 0= if drop 2r> throw-unknown then
    2r> 2drop
    1 = if mode-number then
    mode-number
;

: parser ( caddr n -- )
    2dup
    2dup [char] [ emit type [char] ] emit space 
    sfind 
    dup 0= if drop >number exit then
    2swap 2drop 1 = state @ 0= or
    if
        s" executing..." type cr
        execute
    else
        s" compiling..." type cr 
        compile,
    then
;

: altprompt state @ invert if cr ." alt> " then ;

: interpret ( -- )
    begin ?stack name dup while parser repeat
    2drop
;

: (soloquit) ( -- ) begin .status cr query interpret altprompt again ;

' (soloquit) is 'quit

true to mode?

: test 5 ;

Where "see" test it is not what I hoped for

see test
see test [see] executing...

: test  
  5 ;
alt> 

but when I define interactively

: newtest 45 ;
: newtest 45 ; [:] executing...
[45] STATE <>0 MODE true DIFFERENT
[;] executing...

and "see" newtest is what I expect 

see newtest
see newtest [see] executing...

: newtest  ;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]