@@ -24,17 +24,20 @@ import Foreign.Marshal.Alloc (free)
24-- IO was performed.
24-- IO was performed.
25step :: TT Name -> Idris (Maybe (TT Name))
25step :: TT Name -> Idris (Maybe (TT Name))
26step tm = step' (unApply tm)
26step tm = step' (unApply tm)
27− where step' (P _ (UN "unsafePerformIO") _, [_, arg] ) = return $ Just arg
27+ where step' (P _ (UN "unsafePerformIO") _, [_, arg] ) = step arg -- Only step if arg can be stepped
28 step' (P _ (UN "mkForeign") _, args) = stepForeign args
28 step' (P _ (UN "mkForeign") _, args) = stepForeign args
29 step' _ = return Nothing
29 step' _ = return Nothing
31-- | Perform side effects until no more can be performed, then return the
31-- | Perform side effects until no more can be performed, then return the
32-- resulting term (possibly the argument).
32-- resulting term (possibly the argument).
33execute :: TT Name -> Idris (TT Name)
33execute :: TT Name -> Idris (TT Name)
34+execute tm = case unApply tm of
35+ (P _ (UN "unsafePerformIO") _, [_, arg] ) -> execute' arg
34−execute tm = do stepped <- step tm
37+ where execute' tm = do stepped <- step tm
35− case stepped of
38+ case stepped of
36− Nothing -> return tm
39+ Nothing -> return tm
37− Just tm' -> execute tm'
40+ Just tm' -> execute' tm'
@@ -46,8 +49,7 @@ call :: Foreign -> [TT Name] -> Idris (Maybe (TT Name))
46call (FFun name argTypes retType) args = do fn <- findForeign name
49call (FFun name argTypes retType) args = do fn <- findForeign name
47 case fn of
50 case fn of
48 Nothing -> return Nothing
51 Nothing -> return Nothing
49− Just f -> do res <- call' f args retType
52+ Just f -> return . Just =<< call' f args retType
51 where call' :: ForeignFun -> [TT Name] -> FTy -> Idris (TT Name)
53 where call' :: ForeignFun -> [TT Name] -> FTy -> Idris (TT Name)
52 call' (Fun _ h) args FInt = do res <- lift $ callFFI h retCInt (prepArgs args)
54 call' (Fun _ h) args FInt = do res <- lift $ callFFI h retCInt (prepArgs args)
53 return (Constant (I (fromIntegral res)))
55 return (Constant (I (fromIntegral res)))
@@ -65,7 +67,6 @@ call (FFun name argTypes retType) args = do fn <- findForeign name
65 call' (Fun _ h) args FUnit = do res <- lift $ callFFI h retVoid (prepArgs args)
67 call' (Fun _ h) args FUnit = do res <- lift $ callFFI h retVoid (prepArgs args)
66 return (P Ref unitCon (P Ref unitTy (TType (UVal 0)))) -- FIXME check universe level
68 return (P Ref unitCon (P Ref unitTy (TType (UVal 0)))) -- FIXME check universe level
69 prepArgs = map prepArg
70 prepArgs = map prepArg
70 prepArg (Constant (I i)) = argCInt (fromIntegral i)
71 prepArg (Constant (I i)) = argCInt (fromIntegral i)
71 prepArg (Constant (Fl f)) = argCDouble (realToFrac f)
72 prepArg (Constant (Fl f)) = argCDouble (realToFrac f)