+ return (BinOp op a b)
+ parseCall = do
+ func <- lift (munch1 isAlpha)
+ params <- lift $ between (char '(') (char ')') $
+ sepBy (readS_to_P reads)
+ (skipSpaces >> char ',' >> skipSpaces)
+ return (Call func params)
+ parseIf = do
+ lift $ skipSpaces >> string "if" >> skipSpaces
+ cond <- readPrec
+ lift $ skipSpaces >> string "then" >> skipSpaces
+ thenE <- readPrec
+ lift $ skipSpaces >> string "else" >> skipSpaces
+ elseE <- readPrec
+ return (If cond thenE elseE)
+
+data Prototype = Prototype String [String]
+ deriving Show
+
+instance Read Prototype where
+ readPrec = lift $ do
+ name <- munch1 isAlpha
+ params <- between (char '(') (char ')') $
+ sepBy (munch1 isAlpha) skipSpaces
+ return (Prototype name params)
+
+data AST = Function Prototype Expr
+ | Extern Prototype
+ | TopLevelExpr Expr
+ deriving Show
+
+instance Read AST where
+ readPrec = parseFunction +++ parseExtern +++ parseTopLevel
+ where parseFunction = do
+ lift $ string "def" >> skipSpaces
+ Function <$> readPrec <*> readPrec
+ parseExtern = do
+ lift $ string "extern" >> skipSpaces
+ Extern <$> readPrec
+ parseTopLevel = TopLevelExpr <$> readPrec