[Haskell-beginners] Design questions for a Pascal interpreter
giulianoxt at gmail.com
Mon Apr 20 19:28:44 EDT 2009
I've been doing some research for a upcoming homework project, a
Pascal interpreter written in Haskell. As a beginner to the language,
I have a few design questions I'd like to share with you.
I started playing with Parsec, getting a feel for parsing with monadic
combinators. My assumption here is that i'm going to take the source,
transform it in an AST (built with normal data constructors present in
Haskell), maybe build a symbol table while doing that (using the
implicity state in Parsec), do some checking (mainly type checking I
think), and the evaluation itself.
Here are a few questions I had so far:
- Keeping the whole AST in memory for the evalution phase seems
overkill. Is there a better way?
- 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"?
- And finally, the big question. Consider the following pascal source:
To evaluate the AST, I would eventually arrive at something like:
eval (FunctionCall func_name func_args) = ...
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?
PS: I could keep a list of things to print, that I would eventually do
after I traversed the whole tree, but that wouldn't be "realistic".
More information about the Beginners