r/haskell • u/Patzer26 • 7d ago
A B compiler written in Haskell. Looking for code reviews.
This is my first ever compiler (Maybe second if I wanna consider that semi-serious assignment in Uni on a C compiler) and also my first major project with Haskell. Before this, I mainly did Hackerrank and a couple of advent of code with Haskell.
The Lexer, Parser and semantic analyser/IRGen is written in Haskell, and it targets the QBE backend.
Repo - https://github.com/SBaori/B
Looking for feedback and code reviews on good practices missed, some idiomatic patterns in compiler development and general Haskell coding standards advice.
Comment on LLMs - Every single character in the repo is thought, reasoned and written by me. LLMs were only used for summarising the original B specification, learning some compiler theory and trying to extract/query some built-in functions from Haskell.
8
u/AustinVelonaut 7d ago
Fun project! One thing I noticed is that the parsing is more imperative (using do-notation everywhere), rather than applicative. You might look into adding some combinators to your Parser implementation to make parsing a bit cleaner. For example:
could be rewritten using parser combinators as:
where the combinators would look like:
Good luck on your project!
2
u/dwaynecrooks 4d ago
If you need examples of parser combinators in use you can explore:
In all cases, you're going to see how closely the parser matches the grammar.
1
u/64ue76tjd6 6d ago
for parsing, people will usually use one of the following:
1
u/whosdafter 1d ago
No way!
I also made a B compiler in Haskell
Mine is derivative, kinda incomplete, targets arm64 and the parser combinators are rather slow.
Check it out: https://github.com/ameyakakade/baskell