Victor Marzo
Victor Marzo

Reputation: 731

Custom interpreter

I'm trying to write an interpreter in Gforth, but it doesn't work. All I get is an infinite 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 ;

Upvotes: 3

Views: 230

Answers (1)

fiz
fiz

Reputation: 564

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

num exec lit lit exec num num num ...

However, you leave a 0 on the stack somewhere, thats why you create a stack overflow, you can use ~~ in the code to check the stack and track the unconsumed 0.

Bernd Paysan has introduced Recognizers to GForth, I suggest you take a look at them, as they would ease your task of writing an interpreter.

Upvotes: 4

Related Questions