![]() |
Small compiler in Haskell
This is just something I hacked on for fun but I thought some people might find interesting.
Over the past 2 days or so I've written a small compiler for numeric expressions (i.e. "1 + 12 *42 - (4/2)") in haskell that compiles them to assembly. I just finished the initial code and it seems to be working nicely; the code generated is awful, however. The input is first lexed, and the tokens are then fed into a parser which does the shunting yard algorithm to turn things into RPN. At this point, generating code is really easy, but pretty inefficient. Either way I thought it was cool. The code can be downloaded from here; I also blogged about the code here. While I doubt this will get me a turing award, I figured a couple people here might be interested. :] * Edit, here's a trial run for those who don't want to compile, but see some output: :
[austin@continuum calc]$ ./calc -rpn |
Re: Small compiler in Haskell
Is there any reason why you didn't use Parsec?
|
Re: Small compiler in Haskell
Quote:
(Actually, now that I think of it, the original version was actually written utilizing both Alex and Happy...) The parser is pure and actually works nicely which is what I was hoping for. By any reasonable standard, there was *no* reason to use Shunting yard or my trivial implementation instead of Parsec. It would allow better code to be generated, probably be more robust and portable, etc. etc.. I suppose you can just chalk it up to wanting a somewhat different approach. In any case, my next project looks like it'll be using harpy and haskell to write a Forth, which will allow me to generate better code and get better with stack-oriented languages. :) |
Re: Small compiler in Haskell
I get what you're saying - I recently wrote a basic Lisp interpreter in Python, and I deliberately avoided all of the parsing libraries.
By the way, the way your program compiles into assembly is pretty neat :) |
Re: Small compiler in Haskell
Quote:
Luckily, harpy provides much and it is quite useful, in all honesty. Monadic x86 assembly, all for free. :) If you're interested in runtime code generation, it's really worth a look. |
| All times are GMT -5. The time now is 3:32 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC