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