Custom interpreter

I'm trying to write an interpreter in Gforth , but it doesn't work. All I get is an endless list of num num num num ...

: ?refill
  source nip >in @ =
  if
    refill drop
  then
  ;

: inter
  begin
    ?refill
    bl word find dup
    if
      state @ =
      if
        ." comp "
        ,
      else
        ." exec "
        execute
      then
    else
      dup rot count >number
      if
        abort
      then
      drop drop state @
      if
        ." lit "
        ['] lit , ,
      else
        ." num "
      then
    then
  again
  ;

inter

: test 10 20 ;

      

+3


source to share


1 answer


Your interpreter does work, it just doesn't block, see the first couple of words from the output:

num exec lit lit exec num num num ...

      



However, you are leaving 0

on the stack, so you create a stack overflow, you can use ~~

in your code to check the stack and track the unoccupied 0

.

Bernd Paysan introduced Recognizers at GForth, I suggest you take a look at them as they will make your task of writing a translator easier.

+4


source







All Articles