|interpreter: how is it possible to interpret nested IF ELSE ENDIF ???? email@example.com (Jens Kallup) (2013-05-15)|
|Re: interpreter: how is it possible to interpret nested IF ELSE ENDIF firstname.lastname@example.org (Aleksey Demakov) (2013-05-31)|
|Re: interpreter: how is it possible to interpret nested IF ELSE ENDIF email@example.com (2013-06-19)|
|Date:||Wed, 19 Jun 2013 03:43:58 -0700 (PDT)|
|Keywords:||interpreter, design, comment|
|Posted-Date:||20 Jun 2013 21:15:28 EDT|
Am Mittwoch, 15. Mai 2013 07:42:19 UTC+2 schrieb Jens Kallup:
> like the topic says:
> a give construct should be interpret through a single pass???:
> IF 2 == 3
> print "not print"
> IF 2 == 3
> print "not"
> print "wrong1"
> print "3"
> print "this"
In my BASIC interpreter and in my make utility I use source code
interpretation (when I started to write the BASIC interpreter I
expected to encounter BASIC programs with self modifying code. :-)
Therefore I used source code interpretation).
For source code interpretation you need functions to skip some code.
- Skip to next ELSE or ENDIF
- Skip to next ENDIF
This functions must check if an IF statement is encountered.
In this case they must call "Skip to next ENDIF"
recursively and continue skipping after the ENDIF.
Then you can essentially work as follows:
- When an IF is taken you can continue executing statements.
- When an IF is not taken you use "Skip to next ELSE or ENDIF"
and continue executing after the ELSE or ENDIF.
- When you approach an ELSE you use "Skip to next ENDIF"
and continue executing after the ENDIF.
- When you approach an ENDIF you can continue executing statements.
There are also corner cases, like what to do when the end of
a file is reached during the skipping.
If you have also ELSIF statements the situation becomes
more complicated, but it is also solveable.
If you are interested in the details:
The Bas7 interpreter is described here:
There is a link to the source code in the top right area of the page.
The functions to skip code (regarding IF statements) are:
The functions will probably not fit to your interpreter, but you
might get some ideas from them.
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
[This can work, but it's a lot easier to translate the source code
into an AST and interpret that. If the source changes, translate it
Return to the
Search the comp.compilers archives again.