Maybe you could get come inspiration from the BASIC interpreter written in Haskell:
G'day all.In this day and age, it's not considered overkill to keep an entire
Quoting Giuliano Vilela <giulianoxt@gmail.com>:
- Keeping the whole AST in memory for the evalution phase seems
overkill. Is there a better way?
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.Interpretation of an imperative language is imperative. I wouldn't
- 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"?
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.You really need to learn about monad transformers. Try this for
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?
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
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners