git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1146, update psci for 0.7 module changes, and include :foreign directive
commit806763a607205f4f8328de515787ade743c25563 authorPhil Freeman <paf31@cantab.net> dateSun 7 Jun 2015, 12:33 (11 years ago) parent58acfae
5 files changed+63−38
psci/Completion.hs+9−8 psci/Directive.hs+11−9 psci/PSCi.hs+23−13 psci/Parser.hs+9−8 psci/Types.hs+11
psci/Completion.hs+9−8
@@ -70,14 +70,15 @@ completeDirective ws w =
7070 _ -> []
7171
7272directiveArg :: String -> Directive -> [CompletionContext]
73−directiveArg _ Browse = [CtxModule]
74−directiveArg w Load = [CtxFilePath w]
75−directiveArg _ Quit = []
76−directiveArg _ Reset = []
77−directiveArg _ Help = []
78−directiveArg _ Show = map CtxFixed replQueryStrings
79−directiveArg _ Type = [CtxIdentifier]
80−directiveArg _ Kind = [CtxType]
73+directiveArg _ Browse = [CtxModule]
74+directiveArg w Load = [CtxFilePath w]
75+directiveArg w Foreign = [CtxFilePath w]
76+directiveArg _ Quit = []
77+directiveArg _ Reset = []
78+directiveArg _ Help = []
79+directiveArg _ Show = map CtxFixed replQueryStrings
80+directiveArg _ Type = [CtxIdentifier]
81+directiveArg _ Kind = [CtxType]
8182
8283completeImport :: [String] -> String -> [CompletionContext]
8384completeImport ws w' =
psci/Directive.hs+11−9
@@ -38,6 +38,7 @@ directiveStrings =
3838 , (Reset , ["reset"])
3939 , (Browse , ["browse"])
4040 , (Load , ["load", "module"])
41+ , (Foreign, ["foreign"])
4142 , (Type , ["type"])
4243 , (Kind , ["kind"])
4344 , (Show , ["show"])
@@ -100,14 +101,15 @@ hasArgument _ = True
100101--
101102help :: [(Directive, String, String)]
102103help =
103− [ (Help, "", "Show this help menu")
104− , (Quit, "", "Quit PSCi")
105− , (Reset, "", "Discard all imported modules and declared bindings")
106− , (Browse, "<module>", "See all functions in <module>")
107− , (Load, "<file>", "Load <file> for importing")
108− , (Type, "<expr>", "Show the type of <expr>")
109− , (Kind, "<type>", "Show the kind of <type>")
110− , (Show, "import", "Show all imported modules")
111− , (Show, "loaded", "Show all loaded modules")
104+ [ (Help, "", "Show this help menu")
105+ , (Quit, "", "Quit PSCi")
106+ , (Reset, "", "Discard all imported modules and declared bindings")
107+ , (Browse, "<module>", "See all functions in <module>")
108+ , (Load, "<file>", "Load <file> for importing")
109+ , (Foreign, "<file>", "Load foreign module <file>")
110+ , (Type, "<expr>", "Show the type of <expr>")
111+ , (Kind, "<type>", "Show the kind of <type>")
112+ , (Show, "import", "Show all imported modules")
113+ , (Show, "loaded", "Show all loaded modules")
112114 ]
113115
psci/PSCi.hs+23−13
@@ -71,9 +71,9 @@ supportModule =
7171 code = unlines
7272 [ "module S where"
7373 , ""
74− , "import Console"
75− , ""
74+ , "import Prelude"
7675 , "import Control.Monad.Eff"
76+ , "import Control.Monad.Eff.Console"
7777 , "import Control.Monad.Eff.Unsafe"
7878 , ""
7979 , "class Eval a where"
@@ -431,17 +431,19 @@ handleCommand (Expression val) = handleDeclaration val
431431handleCommand ShowHelp = PSCI $ outputStrLn helpMessage
432432handleCommand (Import im) = handleImport im
433433handleCommand (Decls l) = handleDecls l
434−handleCommand (LoadFile filePath) = do
435− absPath <- psciIO $ expandTilde filePath
436− exists <- psciIO $ doesFileExist absPath
437− if exists then do
438− PSCI . lift $ modify (updateImportedFiles absPath)
439− m <- psciIO $ loadModule absPath
440− case m of
441− Left err -> PSCI $ outputStrLn err
442− Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
443− else
444− PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
434+handleCommand (LoadFile filePath) = whenFileExists filePath $ \absPath -> do
435+ PSCI . lift $ modify (updateImportedFiles absPath)
436+ m <- psciIO $ loadModule absPath
437+ case m of
438+ Left err -> PSCI $ outputStrLn err
439+ Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
440+handleCommand (LoadForeign filePath) = whenFileExists filePath $ \absPath -> do
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)
445447handleCommand ResetState = do
446448 files <- psciImportedFilenames <$> PSCI (lift get)
447449 PSCI . lift . modify $ \st -> st
@@ -457,6 +459,14 @@ handleCommand (ShowInfo QueryLoaded) = handleShowLoadedModules
457459handleCommand (ShowInfo QueryImport) = handleShowImportedModules
458460handleCommand QuitPSCi = error "`handleCommand QuitPSCi` was called. This is a bug."
459461
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+
460470loadUserConfig :: IO (Maybe [Command])
461471loadUserConfig = do
462472 configFile <- (</> ".psci") <$> getCurrentDirectory
psci/Parser.hs+9−8
@@ -76,14 +76,15 @@ parseDirective cmd =
7676 (dstr, arg) = break isSpace cmd
7777
7878 commandFor d = case d of
79− Help -> return ShowHelp
80− Quit -> return QuitPSCi
81− Reset -> return ResetState
82− Browse -> BrowseModule <$> parseRest P.moduleName arg
83− Load -> return $ LoadFile (trim arg)
84− Show -> ShowInfo <$> parseReplQuery' (trim arg)
85− Type -> TypeOf <$> parseRest P.parseValue arg
86− Kind -> KindOf <$> parseRest P.parseType arg
79+ Help -> return ShowHelp
80+ Quit -> return QuitPSCi
81+ Reset -> return ResetState
82+ Browse -> BrowseModule <$> parseRest P.moduleName arg
83+ Load -> return $ LoadFile (trim arg)
84+ Foreign -> return $ LoadForeign (trim arg)
85+ Show -> ShowInfo <$> parseReplQuery' (trim arg)
86+ Type -> TypeOf <$> parseRest P.parseValue arg
87+ Kind -> KindOf <$> parseRest P.parseType arg
8788
8889-- |
8990-- Parses expressions entered at the PSCI repl.
psci/Types.hs+11
@@ -88,6 +88,12 @@ updateModules modules st = st { psciLoadedModules = psciLoadedModules st ++ modu
8888updateLets :: [P.Declaration] -> PSCiState -> PSCiState
8989updateLets ds st = st { psciLetBindings = psciLetBindings st ++ ds }
9090
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+
9197-- |
9298-- Valid Meta-commands for PSCI
9399--
@@ -113,6 +119,10 @@ data Command
113119 --
114120 | LoadFile FilePath
115121 -- |
122+ -- Load a foreign module
123+ --
124+ | LoadForeign FilePath
125+ -- |
116126 -- Exit PSCI
117127 --
118128 | QuitPSCi
@@ -164,6 +174,7 @@ data Directive
164174 | Reset
165175 | Browse
166176 | Load
177+ | Foreign
167178 | Type
168179 | Kind
169180 | Show