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

Re: Tips on adding GOTO, GOSUB to MiniBasic

Arman Anwar wrote:
> Would appreciate on how (if possible) would I go about adding
> unstructured constructs such as GOTO / GOSUB to AST based interpreter
> ??

Hi Arman.

The first question is: Do you really want to add unstructured

As you may well know, you could allow "structured" GOSUB statements, or
simply add functions/procedures to MiniBasic.  This is often enough a
viable alternative to GOSUB.

As for the "devilish" GOTO statement, do you really want to add it to
your language, with line numbers and all?  There are many "structured"
tricks that provide the same functionality, for example, you can add
"break" and "continue" statements that exit loops.  You can do as in
java and have "labeled" loops so that break and continue statements
could directly exit to "outer" loops.  You can even add an "exception"
notation to take care of the more complex type of branching.  My
experience is that these constructs cover all the functionality seeked
by GOTO users, while keeping "code readability".  Spaghetti BASIC code,
with unstructured GOTOs is a hell to debug...

If you can't do without "unstructured constructs", then the solution is
to modify the interpreter engine.  You cannot use "recursive"
interpretation anymore (as it is currently done).  You must use
"sequential" interpretation.  For optimal efficiency, and probably code
readability,  you should:
1- Do an initial traversal of the AST,  to record all the interesting
information.  Here, I mean: labels, branch destinations (like: what is
the destination of "Next I").
2- Write the interpreter main loop which should look like:

void interp_loop(...)
   while(next_stmt != null)

static ... next_stmt;
static ... interp_switch = new InterpSwitch
     public void caseAGotoStmt(AGotoStmt node)
        next_stmt = labels.get(node.getDest().getText())

As you see, I assume that you have a "Hashtable labels" that associate
labels with statements.  This hashtable could be filled in in the
initial pass over the AST.

I hope this gives you some hints as how to proceed.  But, please
consider the structured alternatives before adding any unstructured
statements.  It is much easier to teach the use of structured statements
than to debug unstructured programs!  Anyway, a good incentive for you
is that it is much easier to implement the structured alternatives;-) 
The only good reason I would see to implement GOTO and GOSUB would be to
run legacy code that cannot be rewritten easily into structured code.

Please let me know of the state of your development.  And if you get a
better Basic, you should probably share it with the rest of us.  

PS:  Please remember the GNU LGPL license on the Basic grammar: It means
that you can improve it, but you improvements should be licensed under
the GNU LGPL terms, on the other hand, an application that incorporates
the MiniBasic interpreter can be licensed under whatever terms that
comply with section 6 of the LGPL.

Etienne Gagnon, M.Sc.                        e-mail: egagnon@j-meg.com
Author of SableCC:                 http://www.sable.mcgill.ca/sablecc/