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: Thu, 19 Feb 2015 21:51:43 +0000 Organization: virginmedia.com Lines: 44 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <15-02-029@comp.compilers> References: <15-02-027@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 1424390298 17633 2001:470:1f07:1126:0:676f:7373:6970 (19 Feb 2015 23:58:18 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Thu, 19 Feb 2015 23:58:18 +0000 (UTC) Keywords: parse, interpreter Posted-Date: 19 Feb 2015 18:58:18 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:1531 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