424cfbe7b13010e0960e4b88a1675cdcfa3b3f0f
[kaleidoscope-hs-old.git] / AST.hs
1 module AST where
2
3 import Text.Read
4 import Text.ParserCombinators.ReadP hiding ((+++), choice)
5
6 data BinOpType = Add | Sub | Mul
7   deriving Show
8 data Expr = Num Float
9           | BinOp BinOpType Expr Expr
10   deriving Show
11
12 instance Read Expr where
13   readPrec = choice [ parseNum
14                     , parseBinOp '+' Add
15                     , parseBinOp '-' Sub
16                     , parseBinOp '*' Mul
17                     ]
18     where parseNum = Num <$> readPrec
19           parseBinOp c typ = step $ do
20             a <- prec 11 readPrec
21             lift $ do
22               skipSpaces
23               char c
24               skipSpaces
25             b <- readPrec
26             return (BinOp typ a b)