Find our JIT'ed function and run it
[kaleidoscope-hs.git] / AST.hs
1 module AST where
2
3 import Text.Read 
4 import Text.ParserCombinators.ReadP hiding ((+++), choice)
5
6 data Expr = Num Float
7           | Var String
8           | BinOp BinOp Expr Expr
9   deriving Show
10
11 data BinOp = Add | Sub | Mul | Cmp Ordering
12   deriving Show
13
14 instance Read Expr where
15   readPrec = choice [ parseNum
16                     , parseVar
17                     , parseBinOp "<" 10 (Cmp LT)
18                     , parseBinOp "+" 20 Add
19                     , parseBinOp "-" 20 Sub
20                     , parseBinOp "*" 40 Mul
21                     ]
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
26           -- recursion
27           parseBinOp s prc op = prec prc $ do
28             a <- step readPrec
29             lift $ do
30               skipSpaces
31               string s
32               skipSpaces
33             b <- readPrec
34             return (BinOp op a b)
35