summaryrefslogtreecommitdiff
path: root/src/Interpreter.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Interpreter.hs')
-rw-r--r--src/Interpreter.hs34
1 files changed, 28 insertions, 6 deletions
diff --git a/src/Interpreter.hs b/src/Interpreter.hs
index 3fb5d7b..3d6f33d 100644
--- a/src/Interpreter.hs
+++ b/src/Interpreter.hs
@@ -3,6 +3,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
@@ -23,13 +24,14 @@ import Data.Char (isSpace)
import Data.Kind (Type)
import Data.Int (Int64)
import Data.IORef
-import GHC.Stack (HasCallStack)
+import System.IO (hPutStrLn, stderr)
import System.IO.Unsafe (unsafePerformIO)
import Debug.Trace
import Array
import AST
+import AST.Pretty
import CHAD.Types
import Data
import Interpreter.Rep
@@ -42,14 +44,25 @@ newtype AcM s a = AcM { unAcM :: IO a }
runAcM :: (forall s. AcM s a) -> a
runAcM (AcM m) = unsafePerformIO m
+acmDebugLog :: String -> AcM s ()
+acmDebugLog s = AcM (hPutStrLn stderr s)
+
interpret :: Ex '[] t -> Rep t
interpret = interpretOpen SNil
interpretOpen :: SList Value env -> Ex env t -> Rep t
-interpretOpen env e = runAcM (interpret' env e)
-
-interpret' :: forall env t s. HasCallStack => SList Value env -> Ex env t -> AcM s (Rep t)
-interpret' env = \case
+interpretOpen env e = runAcM (let ?depth = 0 in interpret' env e)
+
+interpret' :: forall env t s. (?depth :: Int) => SList Value env -> Ex env t -> AcM s (Rep t)
+interpret' env e = do
+ let dep = ?depth
+ acmDebugLog $ replicate dep ' ' ++ "ev: " ++ ppExpr env e
+ res <- let ?depth = dep + 1 in interpret'Rec env e
+ acmDebugLog $ replicate dep ' ' ++ "<- " ++ showValue 0 (typeOf e) res ""
+ return res
+
+interpret'Rec :: forall env t s. (?depth :: Int) => SList Value env -> Ex env t -> AcM s (Rep t)
+interpret'Rec env = \case
EVar _ _ i -> case slistIdx env i of Value x -> return x
ELet _ a b -> do
x <- interpret' env a
@@ -125,11 +138,20 @@ interpretOp op arg = case op of
ONeg st -> numericIsNum st $ negate arg
OLt st -> numericIsNum st $ uncurry (<) arg
OLe st -> numericIsNum st $ uncurry (<=) arg
- OEq st -> numericIsNum st $ uncurry (==) arg
+ OEq st -> styIsEq st $ uncurry (==) arg
ONot -> not arg
+ OAnd -> uncurry (&&) arg
+ OOr -> uncurry (||) arg
OIf -> if arg then Left () else Right ()
ORound64 -> round arg
OToFl64 -> fromIntegral arg
+ where
+ styIsEq :: SScalTy t -> (Eq (Rep (TScal t)) => r) -> r
+ styIsEq STI32 = id
+ styIsEq STI64 = id
+ styIsEq STF32 = id
+ styIsEq STF64 = id
+ styIsEq STBool = id
zeroD2 :: STy t -> Rep (D2 t)
zeroD2 typ = case typ of