git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add help commands
af875abjoneshf
psci/Commands.hs+14−2
@@ -6,20 +6,23 @@ import System.Console.Haskeline
6data Command
6data Command
7 = Empty
7 = Empty
8 | Expression [String]
8 | Expression [String]
9+ | Help
9 | Import String
10 | Import String
10 | Let String
11 | Let String
11 | LoadModule FilePath
12 | LoadModule FilePath
13+ | Reload
12− | Reload deriving (Show, Eq)
14+ | Unknown deriving (Show, Eq)
13
15
14getCommand :: InputT IO Command
16getCommand :: InputT IO Command
15getCommand = do
17getCommand = do
16 firstLine <- getInputLine "> "
18 firstLine <- getInputLine "> "
17 case firstLine of
19 case firstLine of
18 Nothing -> return Empty
20 Nothing -> return Empty
21+ Just ":?" -> return Help
19 Just (':':'i':' ':moduleName) -> return $ Import moduleName
22 Just (':':'i':' ':moduleName) -> return $ Import moduleName
20 Just (':':'m':' ':modulePath) -> return $ LoadModule modulePath
23 Just (':':'m':' ':modulePath) -> return $ LoadModule modulePath
21 Just ":r" -> return Reload
24 Just ":r" -> return Reload
22− Just (':':_) -> outputStrLn "Unknown command" >> getCommand
25+ Just (':':_) -> return Unknown
23 Just l@('l':'e':'t':_) -> return $ Let l
26 Just l@('l':'e':'t':_) -> return $ Let l
24 Just other -> Expression <$> go [other]
27 Just other -> Expression <$> go [other]
25 where
28 where
@@ -28,3 +31,12 @@ getCommand = do
28 case l of
31 case l of
29 Nothing -> return $ reverse ls
32 Nothing -> return $ reverse ls
30 Just l' -> go (l' : ls)
33 Just l' -> go (l' : ls)
34+
35+help :: [[String]]
36+help =
37+ [ [":? ", "Show this help menu"]
38+ , [":i <module>", "Import <module> for use in PSCI"]
39+ , [":m <module>", "Load <module> for importing"]
40+ , [":q ", "Quit PSCI"]
41+ , [":r ", "Reload all modules."]
42+ ]
psci/Main.hs+26−15
@@ -23,7 +23,7 @@ import Control.Monad
23import Control.Monad.Trans.Class
23import Control.Monad.Trans.Class
24import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
24import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
25
25
26−import Data.List (nub, isPrefixOf, sort)
26+import Data.List (intercalate, isPrefixOf, nub, sort)
27import Data.Maybe (mapMaybe)
27import Data.Maybe (mapMaybe)
28import Data.Traversable (traverse)
28import Data.Traversable (traverse)
29
29
@@ -116,6 +116,10 @@ parseDoNotationBind = P.DoNotationBind <$> P.parseBinder <*> (P.indented *> P.re
116parseExpression :: Parsec.Parsec String P.ParseState P.Value
116parseExpression :: Parsec.Parsec String P.ParseState P.Value
117parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
117parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
118
118
119+helpMessage :: String
120+helpMessage = "The following commands are available:\n\n " ++
121+ intercalate "\n " (map (intercalate " ") help)
122+
119main :: IO ()
123main :: IO ()
120main = do
124main = do
121 preludeFilename <- getPreludeFilename
125 preludeFilename <- getPreludeFilename
@@ -138,29 +142,36 @@ main = do
138 cmd <- getCommand
142 cmd <- getCommand
139 case cmd of
143 case cmd of
140 Empty -> go imports loadedModules binders
144 Empty -> go imports loadedModules binders
141− Expression ls ->
145+ Expression ls -> do
142− case P.runIndentParser "" parseDoNotationBind (unlines ls) of
146+ binders' <- case P.runIndentParser "" parseDoNotationBind (unlines ls) of
143 Left _ ->
147 Left _ ->
144 case P.runIndentParser "" parseExpression (unlines ls) of
148 case P.runIndentParser "" parseExpression (unlines ls) of
145− Left err -> outputStrLn (show err)
149+ Left err -> outputStrLn (show err) >> return binders
146 Right decl -> do
150 Right decl -> do
147 handleDeclaration loadedModules imports (reverse binders) decl
151 handleDeclaration loadedModules imports (reverse binders) decl
148− go imports loadedModules binders
152+ return binders
149− Right binder -> go imports loadedModules (binder:binders)
153+ Right binder -> return $ binder:binders
154+ go imports loadedModules binders'
155+ Help -> do
156+ outputStrLn helpMessage
157+ go imports loadedModules binders
158+ Import moduleName ->
150− Import moduleName -> go (imports ++ [P.ProperName moduleName]) loadedModules binders
159+ go (imports ++ [P.ProperName moduleName]) loadedModules binders
151 Let line -> do
160 Let line -> do
152− moreLets <- case P.runIndentParser "" parseDoNotationLet line of
161+ binders' <- case P.runIndentParser "" parseDoNotationLet line of
153 Left err -> outputStrLn (show err) >> return binders
162 Left err -> outputStrLn (show err) >> return binders
154− Right l -> return $ l:binders
163+ Right binder -> return $ binder:binders
155− go imports loadedModules moreLets
164+ go imports loadedModules binders'
156 LoadModule moduleFile -> do
165 LoadModule moduleFile -> do
157 ms <- lift $ loadModule moduleFile
166 ms <- lift $ loadModule moduleFile
158− case ms of
167+ loadedModules' <- case ms of
159− Left err -> outputStrLn err
168+ Left err -> outputStrLn err >> return loadedModules
169+ Right ms' -> return $ loadedModules ++ ms'
160− Right ms' -> go imports (loadedModules ++ ms') binders
170+ go imports loadedModules' binders
161 Reload -> do
171 Reload -> do
162 preludeFilename <- lift getPreludeFilename
172 preludeFilename <- lift getPreludeFilename
163 (Right prelude) <- lift $ loadModule preludeFilename
173 (Right prelude) <- lift $ loadModule preludeFilename
164 go defaultImports prelude binders
174 go defaultImports prelude binders
165−
175+ Unknown -> do
166−
176+ outputStrLn "Unknown command"
177+ go imports loadedModules binders