@@ -22,7 +22,7 @@
22module PSCi where
22module PSCi where
24import Data.Foldable (traverse_)
24import Data.Foldable (traverse_)
25−import Data.List (intercalate, nub, sort)
25+import Data.List (intercalate, nub, sort, isPrefixOf)
26import Data.Traversable (traverse)
26import Data.Traversable (traverse)
27import Data.Version (showVersion)
27import Data.Version (showVersion)
28import qualified Data.Map as M
28import qualified Data.Map as M
@@ -59,6 +59,35 @@ import Parser (parseCommand)
59import Completion (completion)
59import Completion (completion)
60import Types
60import Types
62+-- | The name of the PSCI support module
63+supportModuleName :: P.ModuleName
64+supportModuleName = P.ModuleName [P.ProperName "$PSCI", P.ProperName "Support"]
66+-- | Support module, contains code to evaluate terms
67+supportModule :: P.Module
69+ case P.parseModulesFromFiles id [("", code)] of
70+ Right [(_, P.Module cs _ ds exps)] -> P.Module cs supportModuleName ds exps
71+ _ -> error "Support module could not be parsed"
79+ , "import Control.Monad.Eff"
80+ , "import Control.Monad.Eff.Unsafe"
82+ , "class Eval a where"
83+ , " eval :: a -> Eff (console :: CONSOLE) Unit"
85+ , "instance evalShow :: (Show a) => Eval a where"
88+ , "instance evalEff :: (Eval a) => Eval (Eff eff a) where"
89+ , " eval x = unsafeInterleaveEff x >>= eval"
63-- File helpers
92-- File helpers
@@ -202,7 +231,7 @@ instance P.MonadMake Make where
202 writeTextFile path text = makeIO (const (P.CannotWriteFile path)) $ do
231 writeTextFile path text = makeIO (const (P.CannotWriteFile path)) $ do
203 mkdirp path
232 mkdirp path
204 writeFile path text
233 writeFile path text
205− progress s = unless (s == "Compiling $PSCI") $ liftIO . putStrLn $ s
234+ progress s = unless ("Compiling $PSCI" `isPrefixOf` s) $ liftIO . putStrLn $ s
207mkdirp :: FilePath -> IO ()
236mkdirp :: FilePath -> IO ()
208mkdirp = createDirectoryIfMissing True . takeDirectory
237mkdirp = createDirectoryIfMissing True . takeDirectory
@@ -214,8 +243,7 @@ createTemporaryModule :: Bool -> PSCiState -> P.Expr -> P.Module
214createTemporaryModule exec PSCiState{psciImportedModules = imports, psciLetBindings = lets} val =
243createTemporaryModule exec PSCiState{psciImportedModules = imports, psciLetBindings = lets} val =
216 moduleName = P.ModuleName [P.ProperName "$PSCI"]
245 moduleName = P.ModuleName [P.ProperName "$PSCI"]
217− traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
218− trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
246+ trace = P.Var (P.Qualified (Just supportModuleName) (P.Ident "eval"))
219 mainValue = P.App trace (P.Var (P.Qualified Nothing (P.Ident "it")))
247 mainValue = P.App trace (P.Var (P.Qualified Nothing (P.Ident "it")))
220 itDecl = P.ValueDeclaration (P.Ident "it") P.Value [] $ Right val
248 itDecl = P.ValueDeclaration (P.Ident "it") P.Value [] $ Right val
221 mainDecl = P.ValueDeclaration (P.Ident "main") P.Value [] $ Right mainValue
249 mainDecl = P.ValueDeclaration (P.Ident "main") P.Value [] $ Right mainValue
@@ -262,7 +290,7 @@ handleDeclaration val = do
262 st <- PSCI $ lift get
290 st <- PSCI $ lift get
263 let m = createTemporaryModule True st val
291 let m = createTemporaryModule True st val
264 let nodeArgs = psciNodeFlags st ++ [indexFile]
292 let nodeArgs = psciNodeFlags st ++ [indexFile]
265− e <- psciIO . runMake $ P.make modulesDir (psciLoadedModules st ++ [(Left P.RebuildAlways, m)]) []
293+ e <- psciIO . runMake $ P.make modulesDir (psciLoadedModules st ++ [(Left P.RebuildAlways, supportModule), (Left P.RebuildAlways, m)]) []
266 case e of
294 case e of
267 Left errs -> printErrors errs
295 Left errs -> printErrors errs
268 Right _ -> do
296 Right _ -> do
@@ -481,6 +509,10 @@ loadUserConfig = do
482 return Nothing
510 return Nothing
512+-- | Checks if the Console module is defined
513+consoleIsDefined :: [P.Module] -> Bool
514+consoleIsDefined = any ((== P.ModuleName [P.ProperName "Console"]) . P.getModuleName)
485-- The PSCI main loop.
517-- The PSCI main loop.
@@ -496,6 +528,10 @@ loop PSCiOptions{..} = do
496 flip evalStateT (PSCiState psciInputFile defaultImports modules [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
528 flip evalStateT (PSCiState psciInputFile defaultImports modules [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
497 outputStrLn prologueMessage
529 outputStrLn prologueMessage
498 traverse_ (mapM_ (runPSCI . handleCommand)) config
530 traverse_ (mapM_ (runPSCI . handleCommand)) config
531+ unless (consoleIsDefined (map snd modules)) . outputStrLn $ unlines
532+ [ "PSCi requires the purescript-console module to be installed."
533+ , "For help getting started, visit http://wiki.purescript.org/PSCi"
501 go :: InputT (StateT PSCiState IO) ()
537 go :: InputT (StateT PSCiState IO) ()