Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block

BartC <bc@freeuk.com>
Thu, 19 Feb 2015 21:51:43 +0000

          From comp.compilers

Related articles
I have managed to parse and evaluate using an AST. I got stuck in stat mehmet.coskun@gmail.com (2015-02-18)
Re: I have managed to parse and evaluate using an AST. I got stuck in auriocus@gmx.de (Christian Gollwitzer) (2015-02-19)
Re: I have managed to parse and evaluate using an AST. I got stuck in bc@freeuk.com (BartC) (2015-02-19)
Re: I have managed to parse and evaluate using an AST. I got stuck in haberg-news@telia.com (Hans Aberg) (2015-02-20)
Re: I have managed to parse and evaluate using an AST. I got stuck in bc@freeuk.com (BartC) (2015-02-21)
Re: I have managed to parse and evaluate using an AST. I got stuck in mehmet.coskun@gmail.com (2015-03-05)
| List of all articles for this month |

From: BartC <bc@freeuk.com>
Newsgroups: comp.compilers
Date: Thu, 19 Feb 2015 21:51:43 +0000
Organization: virginmedia.com
References: 15-02-027
Keywords: parse, interpreter
Posted-Date: 19 Feb 2015 18:58:18 EST

On 18/02/2015 19:20, mehmet.coskun@gmail.com wrote:


> Can you please help me to design if, while, statement and statement
> block functions? How can I implement this? What do I miss here in my
> implementation that caused me getting stuck without a working
> interpreter.
>
> Please note that I have to use data structures like linked list, tree
> and similars. But not object oriented or a more high level language.


What you need for execution is some sort of program counter to tell you
whereabouts in the AST you are, and what to execute next. I can't quite
see that in the code you posted.


Can you at least executive linear code such as:


    write("One")
    write("Two")
    write("Three") ?




As to how to implement While; usually I would transform to linear
byte-code which would decompose it to conditional jumps and labels, but
in AST form it might be like this:


    evaluate_while(cond, body):
        if evaluate_expr(cond) = True then
              evaluate_stmt(body)
              NextLoc := Loc1
        else
              NextLoc := Loc2
        endif


(This is not Pascal...). Loc1 and Loc2 represent places in the AST. Loc1
refers to this while statement, and Loc2 is the next statment after the
while. NextLoc is some global which does the job of the program counter.


You might also make each kind of evaluate_stmt handler return or update
the program counter (NextLoc in my example). Most statements will just
increment it to the next statement; a few will perform a different
control transfer (such as 'break' or 'goto').


--
Bartc


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.