import Text.ParserCombinators.ReadP hiding ((+++), choice)
data Expr = Num Float
+ | Var String
| BinOp BinOp Expr Expr
deriving Show
instance Read Expr where
readPrec = choice [ parseNum
+ , parseVar
, parseBinOp "<" 10 (Cmp LT)
, parseBinOp "+" 20 Add
, parseBinOp "-" 20 Sub
, parseBinOp "*" 40 Mul
]
where parseNum = Num <$> readPrec
+ parseVar = Var <$> lift (munch1 isAlpha)
-- use 'prec 1' and 'step' so that parsing 'a'
-- can only go one step deep, to prevent ininfite
-- recursion