|I have managed to parse and evaluate using an AST. I got stuck in stat email@example.com (2015-02-18)|
|Re: I have managed to parse and evaluate using an AST. I got stuck in firstname.lastname@example.org (Christian Gollwitzer) (2015-02-19)|
|Re: I have managed to parse and evaluate using an AST. I got stuck in email@example.com (BartC) (2015-02-19)|
|Re: I have managed to parse and evaluate using an AST. I got stuck in firstname.lastname@example.org (Hans Aberg) (2015-02-20)|
|Re: I have managed to parse and evaluate using an AST. I got stuck in email@example.com (BartC) (2015-02-21)|
|Re: I have managed to parse and evaluate using an AST. I got stuck in firstname.lastname@example.org (2015-03-05)|
|Date:||Thu, 19 Feb 2015 21:51:43 +0000|
|Posted-Date:||19 Feb 2015 18:58:18 EST|
On 18/02/2015 19:20, email@example.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
> 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:
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:
if evaluate_expr(cond) = True then
NextLoc := Loc1
NextLoc := Loc2
(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').
Return to the
Search the comp.compilers archives again.