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

Re: Tips on adding GOTO, GOSUB to MiniBasic



> 
> Hello Folks,
> 
> Would appreciate on how (if possible) would I go about adding
> unstructured constructs such as GOTO / GOSUB to AST based interpreter
> ??

One trick for GOTO to raise a GoTo exception, where objects of class GoTo
contain information about the label.
Every place in the interpreter where labels could occur (such as statement
lists) will catch this exception, decide whether it knows the label,
and either handle it or rethrow it.

It helps if the statement list handler can process the GoTos in a loop,
thather than recursively, so you son't get stack overflow from a long loop.

for(;;)
  {
    try
      {
	interpret next statement;
      }
    catch(GoTo g)
      {
	if( the label is in my statement list )
          {
            choose new next statement
            continue
          }
	else throw(g);
      }
    advance to next statement or exit if there isn't one
  }

I imagine GOSUB would be easier, because you can actually do a recursive
call in the ineterpreter.

> 
> TIA,
> 
> Arman.
> 
> ===
> 
> He, 
>   Who is capable of attempting the absurd,
>     Is capable of achieving the impossible.
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Bid and sell for free at http://auctions.yahoo.com
> 
>