Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #1533

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

From BartC <bc@freeuk.com>
Newsgroups comp.compilers
Subject Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block
Date 2015-02-21 20:52 +0000
Organization virginmedia.com
Message-ID <15-02-031@comp.compilers> (permalink)
References <15-02-027@comp.compilers> <15-02-029@comp.compilers>

Show all headers | View raw


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

Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I have managed to parse and evaluate using an AST. I got stuck in statements and block mehmet.coskun@gmail.com - 2015-02-18 11:20 -0800
  Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block Christian Gollwitzer <auriocus@gmx.de> - 2015-02-19 10:22 +0100
  Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block BartC <bc@freeuk.com> - 2015-02-19 21:51 +0000
    Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block BartC <bc@freeuk.com> - 2015-02-21 20:52 +0000
      Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block mehmet.coskun@gmail.com - 2015-03-05 18:53 -0500
  Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block Hans Aberg <haberg-news@telia.com> - 2015-02-20 15:47 +0100

csiph-web