Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED!nerds-end From: BartC Newsgroups: comp.compilers Subject: Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block Date: Sat, 21 Feb 2015 20:52:20 +0000 Organization: virginmedia.com Lines: 115 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <15-02-031@comp.compilers> References: <15-02-027@comp.compilers> <15-02-029@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: miucha.iecc.com 1424576213 8716 2001:470:1f07:1126:0:676f:7373:6970 (22 Feb 2015 03:36:53 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Sun, 22 Feb 2015 03:36:53 +0000 (UTC) Keywords: interpreter Posted-Date: 21 Feb 2015 22:36:53 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:1533 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)