git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add help commands
commitaf875abb367b1c6ff68da6597ada436af3cff4db authorjoneshf <jones3.hardy@gmail.com> dateWed 19 Feb 2014, 07:28 (13 years ago) parentace78ad
2 files changed+40−17
psci/Commands.hs+14−2 psci/Main.hs+26−15
psci/Commands.hs+14−2
@@ -6,20 +6,23 @@ import System.Console.Haskeline
66data Command
77 = Empty
88 | Expression [String]
9+ | Help
910 | Import String
1011 | Let String
1112 | LoadModule FilePath
12− | Reload deriving (Show, Eq)
13+ | Reload
14+ | Unknown deriving (Show, Eq)
1315
1416getCommand :: InputT IO Command
1517getCommand = do
1618 firstLine <- getInputLine "> "
1719 case firstLine of
1820 Nothing -> return Empty
21+ Just ":?" -> return Help
1922 Just (':':'i':' ':moduleName) -> return $ Import moduleName
2023 Just (':':'m':' ':modulePath) -> return $ LoadModule modulePath
2124 Just ":r" -> return Reload
22− Just (':':_) -> outputStrLn "Unknown command" >> getCommand
25+ Just (':':_) -> return Unknown
2326 Just l@('l':'e':'t':_) -> return $ Let l
2427 Just other -> Expression <$> go [other]
2528 where
@@ -28,3 +31,12 @@ getCommand = do
2831 case l of
2932 Nothing -> return $ reverse ls
3033 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
2323import Control.Monad.Trans.Class
2424import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
2525
26−import Data.List (nub, isPrefixOf, sort)
26+import Data.List (intercalate, isPrefixOf, nub, sort)
2727import Data.Maybe (mapMaybe)
2828import Data.Traversable (traverse)
2929
@@ -116,6 +116,10 @@ parseDoNotationBind = P.DoNotationBind <$> P.parseBinder <*> (P.indented *> P.re
116116parseExpression :: Parsec.Parsec String P.ParseState P.Value
117117parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
118118
119+helpMessage :: String
120+helpMessage = "The following commands are available:\n\n " ++
121+ intercalate "\n " (map (intercalate " ") help)
122+
119123main :: IO ()
120124main = do
121125 preludeFilename <- getPreludeFilename
@@ -138,29 +142,36 @@ main = do
138142 cmd <- getCommand
139143 case cmd of
140144 Empty -> go imports loadedModules binders
141− Expression ls ->
142− case P.runIndentParser "" parseDoNotationBind (unlines ls) of
145+ Expression ls -> do
146+ binders' <- case P.runIndentParser "" parseDoNotationBind (unlines ls) of
143147 Left _ ->
144148 case P.runIndentParser "" parseExpression (unlines ls) of
145− Left err -> outputStrLn (show err)
149+ Left err -> outputStrLn (show err) >> return binders
146150 Right decl -> do
147151 handleDeclaration loadedModules imports (reverse binders) decl
148− go imports loadedModules binders
149− Right binder -> go imports loadedModules (binder:binders)
150− Import moduleName -> go (imports ++ [P.ProperName moduleName]) loadedModules binders
152+ return 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 ->
159+ go (imports ++ [P.ProperName moduleName]) loadedModules binders
151160 Let line -> do
152− moreLets <- case P.runIndentParser "" parseDoNotationLet line of
161+ binders' <- case P.runIndentParser "" parseDoNotationLet line of
153162 Left err -> outputStrLn (show err) >> return binders
154− Right l -> return $ l:binders
155− go imports loadedModules moreLets
163+ Right binder -> return $ binder:binders
164+ go imports loadedModules binders'
156165 LoadModule moduleFile -> do
157166 ms <- lift $ loadModule moduleFile
158− case ms of
159− Left err -> outputStrLn err
160− Right ms' -> go imports (loadedModules ++ ms') binders
167+ loadedModules' <- case ms of
168+ Left err -> outputStrLn err >> return loadedModules
169+ Right ms' -> return $ loadedModules ++ ms'
170+ go imports loadedModules' binders
161171 Reload -> do
162172 preludeFilename <- lift getPreludeFilename
163173 (Right prelude) <- lift $ loadModule preludeFilename
164174 go defaultImports prelude binders
165−
166−
175+ Unknown -> do
176+ outputStrLn "Unknown command"
177+ go imports loadedModules binders