Add externs and change type to double
[kaleidoscope-hs-old.git] / AST.hs
diff --git a/AST.hs b/AST.hs
index 6b9928d5b13fa0d67c848b8c3c0e446c2babb4f4..99b6d42a23b9af254c8767ac969accc8248f03bb 100644 (file)
--- a/AST.hs
+++ b/AST.hs
@@ -18,9 +18,10 @@ instance Read Program where
     return asts
 
 data AST = Function String [String] Expr
+         | Extern String [String]
          | Eval Expr
   deriving Show
-data Expr = Num Float
+data Expr = Num Double
           | BinOp BinOpType Expr Expr
           | Var String
           | Call String [Expr]
@@ -30,17 +31,20 @@ data BinOpType = Add | Sub | Mul | Cmp Ordering
   deriving Show
 
 instance Read AST where
-  readPrec = parseFunction +++ (Eval <$> readPrec)
-    where parseFunction = lift $ do
-            skipSpaces
-            string "def"
-            skipSpaces
+  readPrec = parseFunction +++ parseExtern +++ (Eval <$> readPrec)
+    where parseFunction = do
+            lift $ string "def" >> skipSpaces
+            (name, params) <- parsePrototype
+            lift skipSpaces
+            Function name params <$> readPrec
+          parseExtern = do
+            lift $ string "extern" >> skipSpaces
+            uncurry Extern <$> parsePrototype
+          parsePrototype = lift $ do
             name <- munch1 isAlpha
             params <- between (char '(') (char ')') $
                       sepBy (munch1 isAlpha) skipSpaces
-            skipSpaces
-            body <- readS_to_P reads
-            return (Function name params body)
+            return (name, params)
 
 instance Read Expr where
   readPrec = choice [ parseParens
@@ -66,13 +70,12 @@ instance Read Expr where
                               (skipSpaces >> char ',' >> skipSpaces)
             return (Call func params)
           parseBinOp s typ = step $ do
-            a <- prec 11 readPrec
+            a <- prec 11 readPrec -- set recursion limit of 11
             lift $ do
               skipSpaces
               string s
               skipSpaces
-            b <- readPrec
-            return (BinOp typ a b)
+            BinOp typ a <$> readPrec
           parseIf = do
             lift $ do
               string "if"