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

Re: Tips on adding GOTO, GOSUB to MiniBasic

Etienne ,

There are several reasons why I want to implement the GOTO construct:

a. A lot of the CC tools out there support AST like constructs but I
wanted to see how one would effectively implement a GOTO in such a
framework. If one wants to implement a completely languages such as
C/C++ the GOTO has to be taken care of (in you simple C it is part of
the token list)

b. There is a lot of legacy code centered aroung GOTOs that people
would not like to port

c. etc.

But thanks for you valuable input .. I think I shall probably try the
linear approach as one with ASTs seems too convoluted.



--- Etienne Gagnon <egagnon@j-meg.com> wrote:
> 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
> constructs?
> 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)
>    {
>       next_stmt.apply(interp_switch);
>    }
> }
> 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/


  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