25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

initParseState :: ParseState<br />

initParseState = ParseState 0 defaultOps<br />

addOperator :: FixitySpec -> Parsec s ParseState ()<br />

addOperator fixdecl = do<br />

modifyState $ \st -> st { fixities = fixdecl : (fixities st) }<br />

e initial state will consist of the default arithmetic and list operators defined with the same specification<br />

as the <strong>Haskell</strong> specification.<br />

defaultOps :: [FixitySpec]<br />

defaultOps = [<br />

FixitySpec (Infix L 4) ”>”<br />

, FixitySpec (Infix L 4) ” Int<br />

fixityPrec (FixitySpec (Infix _ n) _) = n<br />

fixityPrec (FixitySpec _ _) = 0<br />

toParser (FixitySpec ass tok) = case ass of<br />

Infix L _ -> infixOp tok (op (Name tok)) Ex.AssocLeft<br />

Infix R _ -> infixOp tok (op (Name tok)) Ex.AssocRight<br />

Infix N _ -> infixOp tok (op (Name tok)) Ex.AssocNone<br />

mkTable ops =<br />

map (map toParser) $<br />

groupBy ((==) ‘on‘ fixityPrec) $<br />

reverse $ sortBy (compare ‘on‘ fixityPrec) $ ops<br />

Now when parsing a infix operator declarations we simply do a state operation and add the operator<br />

to the parser state so that all subsequent definitions. is differs from <strong>Haskell</strong> slightly in that operators<br />

must be defined before their usage in a module.<br />

140

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

Saved successfully!

Ooh no, something went wrong!