4 import Text.ParserCombinators.ReadP hiding ((+++), choice)
8 | BinOp BinOp Expr Expr
11 data BinOp = Add | Sub | Mul | Cmp Ordering
14 instance Read Expr where
15 readPrec = choice [ parseNum
17 , parseBinOp "<" 10 (Cmp LT)
18 , parseBinOp "+" 20 Add
19 , parseBinOp "-" 20 Sub
20 , parseBinOp "*" 40 Mul
22 where parseNum = Num <$> readPrec
23 parseVar = Var <$> lift (munch1 isAlpha)
24 -- use 'prec 1' and 'step' so that parsing 'a'
25 -- can only go one step deep, to prevent ininfite
27 parseBinOp s prc op = prec prc $ do