Add support for more binary ops
[kaleidoscope-hs-old.git] / AST.hs
diff --git a/AST.hs b/AST.hs
index 6528215422482fbfa3111faece64c3ff80205983..424cfbe7b13010e0960e4b88a1675cdcfa3b3f0f 100644 (file)
--- a/AST.hs
+++ b/AST.hs
@@ -1,20 +1,26 @@
 module AST where
 
 import Text.Read
-import Text.ParserCombinators.ReadP hiding ((+++))
+import Text.ParserCombinators.ReadP hiding ((+++), choice)
 
+data BinOpType = Add | Sub | Mul
+  deriving Show
 data Expr = Num Float
-          | Add Expr Expr
+          | BinOp BinOpType Expr Expr
   deriving Show
 
 instance Read Expr where
-  readPrec = parseNum +++ parseAdd
+  readPrec = choice [ parseNum
+                    , parseBinOp '+' Add
+                    , parseBinOp '-' Sub
+                    , parseBinOp '*' Mul
+                    ]
     where parseNum = Num <$> readPrec
-          parseAdd = step $ do
+          parseBinOp c typ = step $ do
             a <- prec 11 readPrec
             lift $ do
               skipSpaces
-              char '+'
+              char c
               skipSpaces
             b <- readPrec
-            return (Add a b)
+            return (BinOp typ a b)