25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

, _flags :: Flags.Flags -- ^ Compiler flags<br />

, _venv :: CoreEval.ValEnv Core.Expr -- ^ Core interpreter environment<br />

, _denv :: DataEnv.DataEnv -- ^ Entity dictionary<br />

, _clenv :: ClassEnv.ClassHier -- ^ Typeclass hierarchy<br />

} deriving (Eq, Show)<br />

e compiler itself will have several entry points, expr for interactive evaluation that expects an expression<br />

object and joins it into accumulated interactive environment. And modl path that compile whole<br />

modules.<br />

roughout the next 10 chapters we will incrementally create a series of transformations with the following<br />

type signatures.<br />

parseP :: FilePath -> L.Text -> CompilerM Syn.Module<br />

dataP :: Syn.Module -> CompilerM Syn.Module<br />

groupP :: Syn.Module -> CompilerM Syn.Module<br />

renameP :: Syn.Module -> CompilerM Syn.Module<br />

desugarP :: Syn.Module -> CompilerM Syn.Module<br />

inferP :: Syn.Module -> CompilerM Core.Module<br />

evalP :: Core.Module -> CompilerM ()<br />

e code path for modl is then simply the passes composed with the Kleisli composition operator to<br />

form the composite pipeline for compiling modules.<br />

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c<br />

And that’s basically the entire structure of the compiler. It’s just a pipeline of monadic actions for each<br />

pass rolled up inside of CompilerM.<br />

modl :: FilePath -> L.Text -> CompilerM ()<br />

modl fname<br />

= parseP fname<br />

>=> dataP<br />

>=> groupP<br />

>=> renameP<br />

>=> desugarP<br />

>=> inferP<br />

>=> evalP<br />

Engineering Overview<br />

REPL<br />

It is extremely important to have an interactive shell to be able to interactively explore the compilation<br />

steps and intermediate forms for arbitrary expressions. GHCi does this very well, and nearly every<br />

intermediate form is inspectable. We will endeavor to recreate this experience with our toy language.<br />

108

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!