module Hint.Eval (
interpret, as, infer,
eval ,parens)
where
import qualified GHC.Exts ( unsafeCoerce# )
import Data.Typeable hiding ( typeOf )
import qualified Data.Typeable ( typeOf )
import Hint.Base
import Hint.Context
import Hint.Parsers
import Hint.Sandbox
import Hint.Util
import qualified Hint.Compat as Compat
as, infer :: Typeable a => a
as = undefined
infer = undefined
interpret :: (MonadInterpreter m, Typeable a) => String -> a -> m a
interpret expr wit = unsafeInterpret expr (show $ Data.Typeable.typeOf wit)
unsafeInterpret :: (MonadInterpreter m) => String -> String -> m a
unsafeInterpret expr type_str = sandboxed go expr
where go e =
do
failOnParseError parseExpr e
let expr_typesig = concat [parens e, " :: ", type_str]
expr_val <- mayFail $ runGhc1 Compat.compileExpr expr_typesig
return (GHC.Exts.unsafeCoerce# expr_val :: a)
eval :: MonadInterpreter m => String -> m String
eval expr = do in_scope_show <- support_show
in_scope_String <- support_String
let show_expr = unwords [in_scope_show, parens expr]
unsafeInterpret show_expr in_scope_String
parens :: String -> String
parens s = concat ["(let {", foo, " =\n", s, "\n",
" ;} in ", foo, ")"]
where foo = safeBndFor s