[Haskell-beginners] Design questions for a Pascal interpreter

ajb at spamcop.net ajb at spamcop.net
Mon Apr 20 20:14:01 EDT 2009


G'day all.

Quoting Giuliano Vilela <giulianoxt at gmail.com>:

> - Keeping the whole AST in memory for the evalution phase seems
> overkill. Is there a better way?

In this day and age, it's not considered overkill to keep an entire
program in memory in a tree form.  Perl 5 does that, for example.

However, Pascal is simple enough that it can be translated from
within the parser.  Quite a few influential Pascal compilers,
including the simplest ones such as Pascal-P and Pascal-S, and some
not-so-simple ones such as Turbo Pascal, did not even generate an AST,
but compiled straight to P-code or assembly code from within the parser.

> - The evalution, I think, would be a set of nice pure mutually
> recursive functions that do some pattern matching on the program AST.
> I would pass the current stack and heap for those functions to use and
> modify. Is the State monad a good fit for this task? Wouldn't the code
> become "too imperative"?

Interpretation of an imperative language is imperative.  I wouldn't
worry about it.

You will probably end up using a few monad transformers, because you
need to need at least I/O and a heap, and quite possibly a symbol
table as well.

> Obviously, to evaluate writeln I need to be in the IO monad. Here, my
> whole scheme went down. Do I really have to mix my own state (stack,
> heap) within the IO monad along my evaluation functions?

You really need to learn about monad transformers.  Try this for
starters:

     http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.17.268

Good luck, and let us know how you go.

Cheers,
Andrew Bromage


More information about the Beginners mailing list