git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Load modules into psci on the command line
cf072dcPhil Freeman
psci/Commands.hs+4−8
@@ -21,14 +21,10 @@ import Language.PureScript
21-- Valid Meta-commands for PSCI
21-- Valid Meta-commands for PSCI
22--
22--
23data Command
23data Command
24− -- |
25− -- And empty line
26− --
27− = Empty
28 -- |
24 -- |
29 -- A purescript expression
25 -- A purescript expression
30 --
26 --
31− | Expression Value
27+ = Expression Value
32 -- |
28 -- |
33 -- Show the help command
29 -- Show the help command
34 --
30 --
@@ -46,9 +42,9 @@ data Command
46 --
42 --
47 | Quit
43 | Quit
48 -- |
44 -- |
49− -- Reload the current modules
45+ -- Reset the state of the REPL
50 --
46 --
51− | Reload
47+ | Reset
52 -- |
48 -- |
53 -- Binds a value to a name
49 -- Binds a value to a name
54 --
50 --
@@ -67,6 +63,6 @@ help =
67 , [":i <module> ", "Import <module> for use in PSCI"]
63 , [":i <module> ", "Import <module> for use in PSCI"]
68 , [":m <file> ", "Load <file> for importing"]
64 , [":m <file> ", "Load <file> for importing"]
69 , [":q ", "Quit PSCi"]
65 , [":q ", "Quit PSCi"]
70− , [":r ", "Reload all modules"]
66+ , [":r ", "Reset"]
71 , [":t <expr> ", "Show the type of <expr>"]
67 , [":t <expr> ", "Show the type of <expr>"]
72 ]
68 ]
psci/Main.hs+40−19
@@ -28,6 +28,7 @@ import Control.Monad.Trans.State
28import Data.List (intercalate, isPrefixOf, nub, sortBy)
28import Data.List (intercalate, isPrefixOf, nub, sortBy)
29import Data.Maybe (mapMaybe)
29import Data.Maybe (mapMaybe)
30import Data.Traversable (traverse)
30import Data.Traversable (traverse)
31+import Data.Version (showVersion)
31
32
32import Parser
33import Parser
33
34
@@ -36,6 +37,7 @@ import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
36import System.Exit
37import System.Exit
37import System.Environment.XDG.BaseDir
38import System.Environment.XDG.BaseDir
38import System.FilePath ((</>), isPathSeparator)
39import System.FilePath ((</>), isPathSeparator)
40+import qualified System.Console.CmdTheLine as Cmd
39import System.Process
41import System.Process
40
42
41import Text.Parsec (ParseError)
43import Text.Parsec (ParseError)
@@ -256,7 +258,6 @@ getCommand = do
256-- Performs an action for each meta-command given, and also for expressions..
258-- Performs an action for each meta-command given, and also for expressions..
257--
259--
258handleCommand :: Command -> StateT PSCI (InputT IO) ()
260handleCommand :: Command -> StateT PSCI (InputT IO) ()
259−handleCommand Empty = return ()
260handleCommand (Expression val) = get >>= inputTToState . handleDeclaration val
261handleCommand (Expression val) = get >>= inputTToState . handleDeclaration val
261handleCommand (Let l) = modify (updateLets l)
262handleCommand (Let l) = modify (updateLets l)
262handleCommand Help = inputTToState $ outputStrLn helpMessage
263handleCommand Help = inputTToState $ outputStrLn helpMessage
@@ -268,7 +269,7 @@ handleCommand (LoadFile filePath) = do
268 either outputTStrLn (modify . updateModules) =<< ioToState (loadModule absPath)
269 either outputTStrLn (modify . updateModules) =<< ioToState (loadModule absPath)
269 else
270 else
270 outputTStrLn $ "Couldn't locate: " ++ filePath
271 outputTStrLn $ "Couldn't locate: " ++ filePath
271−handleCommand Reload = do
272+handleCommand Reset = do
272 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
273 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
273 put (PSCI defaultImports prelude [])
274 put (PSCI defaultImports prelude [])
274handleCommand (TypeOf val) = get >>= inputTToState . handleTypeOf val
275handleCommand (TypeOf val) = get >>= inputTToState . handleTypeOf val
@@ -288,24 +289,44 @@ outputTStrLn = inputTToState . outputStrLn
288outprintT :: Show a => a -> StateT PSCI (InputT IO) ()
289outprintT :: Show a => a -> StateT PSCI (InputT IO) ()
289outprintT = outputTStrLn . show
290outprintT = outputTStrLn . show
290
291
292+inputFiles :: Cmd.Term [FilePath]
293+inputFiles = Cmd.value $ Cmd.posAny [] $ Cmd.posInfo { Cmd.posDoc = "The input .ps files" }
294+
291-- |
295-- |
292-- The PSCI main loop.
296-- The PSCI main loop.
293--
297--
294−main :: IO ()
298+loop :: [FilePath] -> IO ()
295−main = do
299+loop files = do
296 preludeFilename <- getPreludeFilename
300 preludeFilename <- getPreludeFilename
297− (Right prelude) <- loadModule preludeFilename
301+ modulesOrFirstError <- fmap concat . sequence <$> mapM loadModule (preludeFilename : files)
302+ case modulesOrFirstError of
303+ Left err -> putStrLn err >> exitFailure
304+ Right modules -> do
298− historyFilename <- getHistoryFilename
305+ historyFilename <- getHistoryFilename
299− let settings = defaultSettings {historyFile = Just historyFilename}
306+ let settings = defaultSettings {historyFile = Just historyFilename}
300− runInputT (setComplete (completion prelude) settings) $ do
307+ runInputT (setComplete (completion modules) settings) $ do
301− outputStrLn prologueMessage
308+ outputStrLn prologueMessage
302− evalStateT go (PSCI defaultImports prelude [])
309+ evalStateT go (PSCI defaultImports modules [])
303− where
310+ where
304− go :: StateT PSCI (InputT IO) ()
311+ go :: StateT PSCI (InputT IO) ()
305− go = do
312+ go = do
306− c <- inputTToState getCommand
313+ c <- inputTToState getCommand
307− case c of
314+ case c of
308− Left err -> outputTStrLn (show err) >> go
315+ Left err -> outputTStrLn (show err) >> go
309− Right Nothing -> go
316+ Right Nothing -> go
310− Right (Just Quit) -> outputTStrLn quitMessage
317+ Right (Just Quit) -> outputTStrLn quitMessage
311− Right (Just c') -> handleCommand c' >> go
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'
83-- Parses 'Commands.Reload' command.
83-- Parses 'Commands.Reload' command.
84--
84--
85psciReload :: Parsec String P.ParseState Command
85psciReload :: Parsec String P.ParseState Command
86−psciReload = Reload <$ char 'r'
86+psciReload = Reset <$ char 'r'
87
87
88-- |
88-- |
89-- Parses 'Commands.TypeOf' command.
89-- Parses 'Commands.TypeOf' command.
purescript.cabal+1−1
@@ -87,7 +87,7 @@ executable psci
87 build-depends: base >=4 && <5, containers -any, directory -any, filepath -any,
87 build-depends: base >=4 && <5, containers -any, directory -any, filepath -any,
88 mtl -any, parsec -any, haskeline <=0.7.1.1, purescript -any,
88 mtl -any, parsec -any, haskeline <=0.7.1.1, purescript -any,
89 syb -any, transformers -any, utf8-string -any, process -any,
89 syb -any, transformers -any, utf8-string -any, process -any,
90− xdg-basedir -any
90+ xdg-basedir -any, cmdtheline -any
91 main-is: Main.hs
91 main-is: Main.hs
92 buildable: True
92 buildable: True
93 hs-source-dirs: psci
93 hs-source-dirs: psci