git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1148 from purescript/psci-0.7
cb17eb9Phil Freeman
psci/Completion.hs+9−8
@@ -70,14 +70,15 @@ completeDirective ws w =
70 _ -> []
70 _ -> []
71
71
72directiveArg :: String -> Directive -> [CompletionContext]
72directiveArg :: String -> Directive -> [CompletionContext]
73−directiveArg _ Browse = [CtxModule]
73+directiveArg _ Browse = [CtxModule]
74−directiveArg w Load = [CtxFilePath w]
74+directiveArg w Load = [CtxFilePath w]
75+directiveArg w Foreign = [CtxFilePath w]
75−directiveArg _ Quit = []
76+directiveArg _ Quit = []
76−directiveArg _ Reset = []
77+directiveArg _ Reset = []
77−directiveArg _ Help = []
78+directiveArg _ Help = []
78−directiveArg _ Show = map CtxFixed replQueryStrings
79+directiveArg _ Show = map CtxFixed replQueryStrings
79−directiveArg _ Type = [CtxIdentifier]
80+directiveArg _ Type = [CtxIdentifier]
80−directiveArg _ Kind = [CtxType]
81+directiveArg _ Kind = [CtxType]
81
82
82completeImport :: [String] -> String -> [CompletionContext]
83completeImport :: [String] -> String -> [CompletionContext]
83completeImport ws w' =
84completeImport ws w' =
psci/Directive.hs+11−9
@@ -38,6 +38,7 @@ directiveStrings =
38 , (Reset , ["reset"])
38 , (Reset , ["reset"])
39 , (Browse , ["browse"])
39 , (Browse , ["browse"])
40 , (Load , ["load", "module"])
40 , (Load , ["load", "module"])
41+ , (Foreign, ["foreign"])
41 , (Type , ["type"])
42 , (Type , ["type"])
42 , (Kind , ["kind"])
43 , (Kind , ["kind"])
43 , (Show , ["show"])
44 , (Show , ["show"])
@@ -100,14 +101,15 @@ hasArgument _ = True
100--
101--
101help :: [(Directive, String, String)]
102help :: [(Directive, String, String)]
102help =
103help =
103− [ (Help, "", "Show this help menu")
104+ [ (Help, "", "Show this help menu")
104− , (Quit, "", "Quit PSCi")
105+ , (Quit, "", "Quit PSCi")
105− , (Reset, "", "Discard all imported modules and declared bindings")
106+ , (Reset, "", "Discard all imported modules and declared bindings")
106− , (Browse, "<module>", "See all functions in <module>")
107+ , (Browse, "<module>", "See all functions in <module>")
107− , (Load, "<file>", "Load <file> for importing")
108+ , (Load, "<file>", "Load <file> for importing")
109+ , (Foreign, "<file>", "Load foreign module <file>")
108− , (Type, "<expr>", "Show the type of <expr>")
110+ , (Type, "<expr>", "Show the type of <expr>")
109− , (Kind, "<type>", "Show the kind of <type>")
111+ , (Kind, "<type>", "Show the kind of <type>")
110− , (Show, "import", "Show all imported modules")
112+ , (Show, "import", "Show all imported modules")
111− , (Show, "loaded", "Show all loaded modules")
113+ , (Show, "loaded", "Show all loaded modules")
112 ]
114 ]
113
115
psci/PSCi.hs+23−13
@@ -71,9 +71,9 @@ supportModule =
71 code = unlines
71 code = unlines
72 [ "module S where"
72 [ "module S where"
73 , ""
73 , ""
74− , "import Console"
74+ , "import Prelude"
75− , ""
76 , "import Control.Monad.Eff"
75 , "import Control.Monad.Eff"
76+ , "import Control.Monad.Eff.Console"
77 , "import Control.Monad.Eff.Unsafe"
77 , "import Control.Monad.Eff.Unsafe"
78 , ""
78 , ""
79 , "class Eval a where"
79 , "class Eval a where"
@@ -431,17 +431,19 @@ handleCommand (Expression val) = handleDeclaration val
431handleCommand ShowHelp = PSCI $ outputStrLn helpMessage
431handleCommand ShowHelp = PSCI $ outputStrLn helpMessage
432handleCommand (Import im) = handleImport im
432handleCommand (Import im) = handleImport im
433handleCommand (Decls l) = handleDecls l
433handleCommand (Decls l) = handleDecls l
434−handleCommand (LoadFile filePath) = do
434+handleCommand (LoadFile filePath) = whenFileExists filePath $ \absPath -> do
435− absPath <- psciIO $ expandTilde filePath
436− exists <- psciIO $ doesFileExist absPath
437− if exists then do
438− PSCI . lift $ modify (updateImportedFiles absPath)
435+ PSCI . lift $ modify (updateImportedFiles absPath)
439− m <- psciIO $ loadModule absPath
436+ m <- psciIO $ loadModule absPath
440− case m of
437+ case m of
441− Left err -> PSCI $ outputStrLn err
438+ Left err -> PSCI $ outputStrLn err
442− Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
439+ Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
443− else
440+handleCommand (LoadForeign filePath) = whenFileExists filePath $ \absPath -> do
444− PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
441+ foreignsOrError <- psciIO . runMake $ do
442+ foreignFile <- makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile absPath)) (readFile absPath)
443+ P.parseForeignModulesFromFiles [(absPath, foreignFile)]
444+ case foreignsOrError of
445+ Left err -> PSCI $ outputStrLn $ P.prettyPrintMultipleErrors False err
446+ Right foreigns -> PSCI . lift $ modify (updateForeignFiles foreigns)
445handleCommand ResetState = do
447handleCommand ResetState = do
446 files <- psciImportedFilenames <$> PSCI (lift get)
448 files <- psciImportedFilenames <$> PSCI (lift get)
447 PSCI . lift . modify $ \st -> st
449 PSCI . lift . modify $ \st -> st
@@ -457,6 +459,14 @@ handleCommand (ShowInfo QueryLoaded) = handleShowLoadedModules
457handleCommand (ShowInfo QueryImport) = handleShowImportedModules
459handleCommand (ShowInfo QueryImport) = handleShowImportedModules
458handleCommand QuitPSCi = error "`handleCommand QuitPSCi` was called. This is a bug."
460handleCommand QuitPSCi = error "`handleCommand QuitPSCi` was called. This is a bug."
459
461
462+whenFileExists :: FilePath -> (FilePath -> PSCI ()) -> PSCI ()
463+whenFileExists filePath f = do
464+ absPath <- psciIO $ expandTilde filePath
465+ exists <- psciIO $ doesFileExist absPath
466+ if exists
467+ then f absPath
468+ else PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
469+
460loadUserConfig :: IO (Maybe [Command])
470loadUserConfig :: IO (Maybe [Command])
461loadUserConfig = do
471loadUserConfig = do
462 configFile <- (</> ".psci") <$> getCurrentDirectory
472 configFile <- (</> ".psci") <$> getCurrentDirectory
psci/Parser.hs+9−8
@@ -76,14 +76,15 @@ parseDirective cmd =
76 (dstr, arg) = break isSpace cmd
76 (dstr, arg) = break isSpace cmd
77
77
78 commandFor d = case d of
78 commandFor d = case d of
79− Help -> return ShowHelp
79+ Help -> return ShowHelp
80− Quit -> return QuitPSCi
80+ Quit -> return QuitPSCi
81− Reset -> return ResetState
81+ Reset -> return ResetState
82− Browse -> BrowseModule <$> parseRest P.moduleName arg
82+ Browse -> BrowseModule <$> parseRest P.moduleName arg
83− Load -> return $ LoadFile (trim arg)
83+ Load -> return $ LoadFile (trim arg)
84+ Foreign -> return $ LoadForeign (trim arg)
84− Show -> ShowInfo <$> parseReplQuery' (trim arg)
85+ Show -> ShowInfo <$> parseReplQuery' (trim arg)
85− Type -> TypeOf <$> parseRest P.parseValue arg
86+ Type -> TypeOf <$> parseRest P.parseValue arg
86− Kind -> KindOf <$> parseRest P.parseType arg
87+ Kind -> KindOf <$> parseRest P.parseType arg
87
88
88-- |
89-- |
89-- Parses expressions entered at the PSCI repl.
90-- Parses expressions entered at the PSCI repl.
psci/Types.hs+11
@@ -88,6 +88,12 @@ updateModules modules st = st { psciLoadedModules = psciLoadedModules st ++ modu
88updateLets :: [P.Declaration] -> PSCiState -> PSCiState
88updateLets :: [P.Declaration] -> PSCiState -> PSCiState
89updateLets ds st = st { psciLetBindings = psciLetBindings st ++ ds }
89updateLets ds st = st { psciLetBindings = psciLetBindings st ++ ds }
90
90
91+-- |
92+-- Updates the state to have more let bindings.
93+--
94+updateForeignFiles :: M.Map P.ModuleName (FilePath, P.ForeignJS) -> PSCiState -> PSCiState
95+updateForeignFiles fs st = st { psciForeignFiles = psciForeignFiles st `M.union` fs }
96+
91-- |
97-- |
92-- Valid Meta-commands for PSCI
98-- Valid Meta-commands for PSCI
93--
99--
@@ -113,6 +119,10 @@ data Command
113 --
119 --
114 | LoadFile FilePath
120 | LoadFile FilePath
115 -- |
121 -- |
122+ -- Load a foreign module
123+ --
124+ | LoadForeign FilePath
125+ -- |
116 -- Exit PSCI
126 -- Exit PSCI
117 --
127 --
118 | QuitPSCi
128 | QuitPSCi
@@ -164,6 +174,7 @@ data Directive
164 | Reset
174 | Reset
165 | Browse
175 | Browse
166 | Load
176 | Load
177+ | Foreign
167 | Type
178 | Type
168 | Kind
179 | Kind
169 | Show
180 | Show