git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Load modules into psci on the command line
commitcf072dc57de321b05cb816ff59437429338dd4ea authorPhil Freeman <paf31@cantab.net> dateFri 21 Feb 2014, 16:52 (13 years ago) parentaea20d4
4 files changed+46−29
psci/Commands.hs+4−8 psci/Main.hs+40−19 psci/Parser.hs+1−1 purescript.cabal+1−1
psci/Commands.hs+4−8
@@ -21,14 +21,10 @@ import Language.PureScript
2121-- Valid Meta-commands for PSCI
2222--
2323data Command
24− -- |
25− -- And empty line
26− --
27− = Empty
2824 -- |
2925 -- A purescript expression
3026 --
31− | Expression Value
27+ = Expression Value
3228 -- |
3329 -- Show the help command
3430 --
@@ -46,9 +42,9 @@ data Command
4642 --
4743 | Quit
4844 -- |
49− -- Reload the current modules
45+ -- Reset the state of the REPL
5046 --
51− | Reload
47+ | Reset
5248 -- |
5349 -- Binds a value to a name
5450 --
@@ -67,6 +63,6 @@ help =
6763 , [":i <module> ", "Import <module> for use in PSCI"]
6864 , [":m <file> ", "Load <file> for importing"]
6965 , [":q ", "Quit PSCi"]
70− , [":r ", "Reload all modules"]
66+ , [":r ", "Reset"]
7167 , [":t <expr> ", "Show the type of <expr>"]
7268 ]
psci/Main.hs+40−19
@@ -28,6 +28,7 @@ import Control.Monad.Trans.State
2828import Data.List (intercalate, isPrefixOf, nub, sortBy)
2929import Data.Maybe (mapMaybe)
3030import Data.Traversable (traverse)
31+import Data.Version (showVersion)
3132
3233import Parser
3334
@@ -36,6 +37,7 @@ import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
3637import System.Exit
3738import System.Environment.XDG.BaseDir
3839import System.FilePath ((</>), isPathSeparator)
40+import qualified System.Console.CmdTheLine as Cmd
3941import System.Process
4042
4143import Text.Parsec (ParseError)
@@ -256,7 +258,6 @@ getCommand = do
256258-- Performs an action for each meta-command given, and also for expressions..
257259--
258260handleCommand :: Command -> StateT PSCI (InputT IO) ()
259−handleCommand Empty = return ()
260261handleCommand (Expression val) = get >>= inputTToState . handleDeclaration val
261262handleCommand (Let l) = modify (updateLets l)
262263handleCommand Help = inputTToState $ outputStrLn helpMessage
@@ -268,7 +269,7 @@ handleCommand (LoadFile filePath) = do
268269 either outputTStrLn (modify . updateModules) =<< ioToState (loadModule absPath)
269270 else
270271 outputTStrLn $ "Couldn't locate: " ++ filePath
271−handleCommand Reload = do
272+handleCommand Reset = do
272273 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
273274 put (PSCI defaultImports prelude [])
274275handleCommand (TypeOf val) = get >>= inputTToState . handleTypeOf val
@@ -288,24 +289,44 @@ outputTStrLn = inputTToState . outputStrLn
288289outprintT :: Show a => a -> StateT PSCI (InputT IO) ()
289290outprintT = outputTStrLn . show
290291
292+inputFiles :: Cmd.Term [FilePath]
293+inputFiles = Cmd.value $ Cmd.posAny [] $ Cmd.posInfo { Cmd.posDoc = "The input .ps files" }
294+
291295-- |
292296-- The PSCI main loop.
293297--
294−main :: IO ()
295−main = do
298+loop :: [FilePath] -> IO ()
299+loop files = do
296300 preludeFilename <- getPreludeFilename
297− (Right prelude) <- loadModule preludeFilename
298− historyFilename <- getHistoryFilename
299− let settings = defaultSettings {historyFile = Just historyFilename}
300− runInputT (setComplete (completion prelude) settings) $ do
301− outputStrLn prologueMessage
302− evalStateT go (PSCI defaultImports prelude [])
303− where
304− go :: StateT PSCI (InputT IO) ()
305− go = do
306− c <- inputTToState getCommand
307− case c of
308− Left err -> outputTStrLn (show err) >> go
309− Right Nothing -> go
310− Right (Just Quit) -> outputTStrLn quitMessage
311− Right (Just c') -> handleCommand c' >> go
301+ modulesOrFirstError <- fmap concat . sequence <$> mapM loadModule (preludeFilename : files)
302+ case modulesOrFirstError of
303+ Left err -> putStrLn err >> exitFailure
304+ Right modules -> do
305+ historyFilename <- getHistoryFilename
306+ let settings = defaultSettings {historyFile = Just historyFilename}
307+ runInputT (setComplete (completion modules) settings) $ do
308+ outputStrLn prologueMessage
309+ evalStateT go (PSCI defaultImports modules [])
310+ where
311+ go :: StateT PSCI (InputT IO) ()
312+ go = do
313+ c <- inputTToState getCommand
314+ case c of
315+ Left err -> outputTStrLn (show err) >> go
316+ Right Nothing -> go
317+ Right (Just Quit) -> outputTStrLn quitMessage
318+ Right (Just c') -> handleCommand c' >> go
319+
320+term :: Cmd.Term (IO ())
321+term = loop <$> inputFiles
322+
323+termInfo :: Cmd.TermInfo
324+termInfo = Cmd.defTI
325+ { Cmd.termName = "psci"
326+ , Cmd.version = showVersion $ Paths.version
327+ , Cmd.termDoc = "Interactive mode for PureScript"
328+ }
329+
330+main :: IO ()
331+main = Cmd.run (term, termInfo)
332+
psci/Parser.hs+1−1
@@ -83,7 +83,7 @@ psciQuit = Quit <$ char 'q'
8383-- Parses 'Commands.Reload' command.
8484--
8585psciReload :: Parsec String P.ParseState Command
86−psciReload = Reload <$ char 'r'
86+psciReload = Reset <$ char 'r'
8787
8888-- |
8989-- Parses 'Commands.TypeOf' command.
purescript.cabal+1−1
@@ -87,7 +87,7 @@ executable psci
8787 build-depends: base >=4 && <5, containers -any, directory -any, filepath -any,
8888 mtl -any, parsec -any, haskeline <=0.7.1.1, purescript -any,
8989 syb -any, transformers -any, utf8-string -any, process -any,
90− xdg-basedir -any
90+ xdg-basedir -any, cmdtheline -any
9191 main-is: Main.hs
9292 buildable: True
9393 hs-source-dirs: psci