r/haskell • • 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.

28 Upvotes

10 comments sorted by

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:

    getParams :: Parser [(Node String)]
    getParams = do
        (Token l k p) <- peek 0

        case k of
            CLOSE_ROUND -> pure []

            COMMA -> do
                expectKind COMMA
                getParams

            _ -> do
                expectKind IDENTIFIER
                ((Node l p):) <$> getParams

could be rewritten using parser combinators as:

getParams :: Parser [(Node String)]
getParams = inParens (manySepBy comma identifier)

where the combinators would look like:

inParens :: Parser a -> Parser a
inParens p = openRound *> p <* closeRound

someSepBy, manySepBy :: Parser s -> parser a -> Parser [a]
someSepBy ps p = liftA2 (:) p (many (ps *> p))
manySepBy ps p = someSepBy ps <|> pure []

Good luck on your project!

3

u/Patzer26 7d ago

Thats so elegant. Didn't know about the "many" function. Will try to read more about parser combinators. Thank you for the edit!

3

u/jeffstyr 7d ago

You can also use an existing parser combinator library, such as Megaparsec or Attoparsec.

2

u/Patzer26 7d ago

Yes, just wanted to handroll as much as I can by myself, given I had already allocated the codegen stuff to QBE. I also didn't just wanna use a library without actually understanding how it worked. But yes, in future I will try to incorporate this more.

2

u/jeffstyr 7d ago

Makes sense. Just wanted to make sure you knew about them.

2

u/Vigintillionn 6d ago

Shameless plug, but if you're interested I wrote a blog post on it: https://www.yarne.me/blog/parsers

1

u/pthierry 6d ago

Yeah, parsers combiantors and applicative parsers are a pretty extraordinary feature of Haskell.

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. an existing parser combinator library (e.g. parsec).
  2. alex / happy. these are parser generators for haskell, similar to lex and yacc.

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