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.

module Syntax where<br />

data Expr<br />

= Tr<br />

| Fl<br />

| Zero<br />

| IsZero Expr<br />

| Succ Expr<br />

| Pred Expr<br />

| If Expr Expr Expr<br />

deriving (Eq, Show)<br />

Parser<br />

Much like before our parser is simply written in monadic blocks, each mapping a set of patterns to a<br />

construct in our Expr type. e toplevel entry point to our parser is the expr function which we can<br />

parse with by using the Parsec function parse.<br />

prefixOp s f = Ex.Prefix (reservedOp s >> return f)<br />

-- Infix operators<br />

table :: Ex.OperatorTable String () Identity Expr<br />

table = [<br />

[<br />

prefixOp ”succ” Succ<br />

, prefixOp ”pred” Pred<br />

, prefixOp ”iszero” IsZero<br />

]<br />

]<br />

-- if/then/else<br />

ifthen :: Parser Expr<br />

ifthen = do<br />

reserved ”if”<br />

cond

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

Saved successfully!

Ooh no, something went wrong!