Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED!nerds-end From: Aleksey Demakov Newsgroups: comp.compilers Subject: Re: interpreter: how is it possible to interpret nested IF ELSE ENDIF ???? Date: Fri, 31 May 2013 16:07:38 +0400 Organization: Compilers Central Lines: 32 Sender: johnl@iecc.com Approved: comp.compilers@iecc.com Message-ID: <13-05-024@comp.compilers> References: <13-05-009@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: leila.iecc.com 1370027809 10797 64.57.183.58 (31 May 2013 19:16:49 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Fri, 31 May 2013 19:16:49 +0000 (UTC) Keywords: interpreter, design Posted-Date: 31 May 2013 15:16:49 EDT 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:910 Hi, Just have in your interpreter a structure that has all the information required to execute one statement. To support nested statements every such structure should have a pointer to the parent structure. You will have a global pointer to the currently executing statement. When interpreter meets a nested statement it creates a new structure, saves the current statement in the parent pointer and makes the new structure current. When the interpreter reaches the end of the current statement it takes its parent pointer and makes it current. For if-else-endif statement you will need to remember within the structure if the if-condition was met. Depending on it you set a global skip-flag when entering either of the two if branches. When the flag is set the statements like assignments and procedure calls are just ignored. But for nested if-else-endif statements the interpreter still creates nested structures and observes the described above order of entering and leaving them. But on entering a branch before setting the skip flag it is required to save its previous value and on leaving a branch the previous value has to be restored. Such implementation technique works well for such simple cases as #ifdefs of c preprocessor. But when it is necessary to handle something more complicated like loop statements or gotos then it becomes more practical to have some sort of intermediate representation like ASTs or bytecodes. Regards, Aleksey