|interpreter: how is it possible to interpret nested IF ELSE ENDIF ???? firstname.lastname@example.org (Jens Kallup) (2013-05-15)|
|Re: interpreter: how is it possible to interpret nested IF ELSE ENDIF email@example.com (Aleksey Demakov) (2013-05-31)|
|Re: interpreter: how is it possible to interpret nested IF ELSE ENDIF firstname.lastname@example.org (2013-06-19)|
|From:||Aleksey Demakov <email@example.com>|
|Date:||Fri, 31 May 2013 16:07:38 +0400|
|Posted-Date:||31 May 2013 15:16:49 EDT|
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.
Return to the
Search the comp.compilers archives again.