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

BartC <bc@freeuk.com>
Sat, 21 Feb 2015 20:52:20 +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: Sat, 21 Feb 2015 20:52:20 +0000
Organization: virginmedia.com
References: 15-02-027 15-02-029
Keywords: interpreter
Posted-Date: 21 Feb 2015 22:36:53 EST

On 19/02/2015 21:51, BartC wrote:
> 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.


> 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.


I had a go at taking an existing byte-code compiler and adapting the
byte-code generator to execute the code instead of producing the normal
output.


It was a lot harder than I expected! Certainly, to be able to randomly
jump from one part of the code to another is difficult. But to execute
your example, that can be done with almost the same AST-scanning method
I'd been using for the byte-code.


I started with this version of your example, but in the language this
compiler processes (which happens to be the same language the compiler
is written in):


proc start=
count:=0


while count<10 do
          println count
          if count=5 then
                  println "High Five"
          fi
          count:=count+1
od
end


Then I only modified the small number of lines needed to get this to
execute. The While handler was:


proc eval_while (p,cond,body,c,d)=
while eval_expr(cond) do
          eval_block(body)
od
end


The If handler:


proc eval_if (p,cond,b,c,d) =
if eval_expr(cond) then
          eval_block(b)
elsif notnull(c) then # else part is optional
          eval_block(c)
fi
end


So the while handler involves a while-loop, and the if-then-else handler
involves an if-then-else statement, which sort of makes sense.


For handling code-blocks and statements, I use:


proc eval_block (p)=
forall s in p.a do
          eval_stmt(s)
od
end


But this starts to depend on the details of how your AST is organised.
(I haven't shown assignment, since I haven't implemented variables
properly and it's just a kludge.)


The few operations I needed (<, =, +) are inside this function:


function eval_expr(p)=


switch p.tag
when j_const then
return p.a


when j_name then # variable name
return p.a.code


when j_add then
return eval_expr(p.a)+eval_expr(p.b)


when j_lt then
return eval_expr(p.a)<eval_expr(p.b)


when j_eq then
return eval_expr(p.a)=eval_expr(p.b)
......
end


But you seem to have that part sorted. The output from this is:


    0
    1
    2
    3
    4
    5
    High Five
    6
    7
    8
    9


So it seems to work. (To fully convert my project to execute ASTs is
quite a lot of work, and the simple approach above won't work. There is
also the problem of how to deal with programs consisting of multiple
modules - it would be necessary to store the AST in a file, a whole lot
of problems just to deal with that. For experimenting however it's fine.)


--
Bartc


Post a followup to this message

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