git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
psci: validate imports when using :i
61392d9Bogdan Popa
psci/Main.hs+22−2
@@ -233,6 +233,14 @@ instance P.MonadMake Make where
233mkdirp :: FilePath -> IO ()
233mkdirp :: FilePath -> IO ()
234mkdirp = createDirectoryIfMissing True . takeDirectory
234mkdirp = createDirectoryIfMissing True . takeDirectory
235
235
236+-- |
237+-- Just an empty module.
238+--
239+emptyModule :: P.Module
240+emptyModule =
241+ let moduleName = P.ModuleName [P.ProperName "$PSCI"]
242+ in P.Module moduleName [] Nothing
243+
236-- |
244-- |
237-- Makes a volatile module to execute the current expression.
245-- Makes a volatile module to execute the current expression.
238--
246--
@@ -288,6 +296,19 @@ handleDeclaration value = do
288 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
296 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
289 Nothing -> PSCI $ outputStrLn "Couldn't find node.js"
297 Nothing -> PSCI $ outputStrLn "Couldn't find node.js"
290
298
299+-- |
300+-- Imports a module, preserving the initial state on failure.
301+--
302+handleImport :: P.ModuleName -> PSCI ()
303+handleImport moduleName = do
304+ s <- liftM (updateImports moduleName) $ PSCI $ lift get
305+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules s ++ [("$PSCI.purs", emptyModule)]) []
306+ case e of
307+ Left err -> PSCI $ outputStrLn err
308+ Right _ -> do
309+ PSCI $ lift $ put s
310+ return ()
311+
291-- |
312-- |
292-- Takes a value and prints its type
313-- Takes a value and prints its type
293--
314--
@@ -347,7 +368,7 @@ getCommand singleLineMode = do
347handleCommand :: Command -> PSCI ()
368handleCommand :: Command -> PSCI ()
348handleCommand (Expression val) = handleDeclaration val
369handleCommand (Expression val) = handleDeclaration val
349handleCommand Help = PSCI $ outputStrLn helpMessage
370handleCommand Help = PSCI $ outputStrLn helpMessage
350−handleCommand (Import moduleName) = PSCI $ lift $ modify (updateImports moduleName)
371+handleCommand (Import moduleName) = handleImport moduleName
351handleCommand (Let l) = PSCI $ lift $ modify (updateLets l)
372handleCommand (Let l) = PSCI $ lift $ modify (updateLets l)
352handleCommand (LoadFile filePath) = do
373handleCommand (LoadFile filePath) = do
353 absPath <- psciIO $ expandTilde filePath
374 absPath <- psciIO $ expandTilde filePath
@@ -435,4 +456,3 @@ termInfo = Cmd.defTI
435
456
436main :: IO ()
457main :: IO ()
437main = Cmd.run (term, termInfo)
458main = Cmd.run (term, termInfo)
438−