git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Remove importedFilenames field of PSCiState
f835dcdHarry Garrood
psci/PSCi.hs+17−18
@@ -113,12 +113,12 @@ loadModule filename = do
113-- |
113-- |
114-- Load all modules.
114-- Load all modules.
115--
115--
116−loadAllModules :: [FilePath] -> IO (Either P.MultipleErrors [(Either P.RebuildPolicy FilePath, P.Module)])
116+loadAllModules :: [FilePath] -> IO (Either P.MultipleErrors [(FilePath, P.Module)])
117loadAllModules files = do
117loadAllModules files = do
118 filesAndContent <- forM files $ \filename -> do
118 filesAndContent <- forM files $ \filename -> do
119 content <- readFile filename
119 content <- readFile filename
120− return (Right filename, content)
120+ return (filename, content)
121− return $ P.parseModulesFromFiles (either (const "") id) filesAndContent
121+ return $ P.parseModulesFromFiles id filesAndContent
122
122
123-- |
123-- |
124-- Load all modules, updating the application state
124-- Load all modules, updating the application state
@@ -248,13 +248,14 @@ makeIO f io = do
248 e <- liftIO $ tryIOError io
248 e <- liftIO $ tryIOError io
249 either (throwError . P.singleError . f) return e
249 either (throwError . P.singleError . f) return e
250
250
251−make :: PSCiState -> [(Either P.RebuildPolicy FilePath, P.Module)] -> P.Make P.Environment
251+make :: PSCiState -> [P.Module] -> P.Make P.Environment
252−make st@PSCiState{..} ms = P.make actions' (map snd (loadedModules ++ ms))
252+make st@PSCiState{..} ms = P.make actions' (map snd loadedModules ++ ms)
253 where
253 where
254− filePathMap = M.fromList $ (first P.getModuleName . swap) `map` (loadedModules ++ ms)
254+ filePathMap = M.fromList $ (first P.getModuleName . swap) `map` allModules
255 actions = P.buildMakeActions modulesDir filePathMap psciForeignFiles False
255 actions = P.buildMakeActions modulesDir filePathMap psciForeignFiles False
256 actions' = actions { P.progress = const (return ()) }
256 actions' = actions { P.progress = const (return ()) }
257 loadedModules = psciLoadedModules st
257 loadedModules = psciLoadedModules st
258+ allModules = map (first Right) loadedModules ++ map (Left P.RebuildAlways,) ms
258
259
259-- |
260-- |
260-- Takes a value declaration and evaluates it with the current state.
261-- Takes a value declaration and evaluates it with the current state.
@@ -264,7 +265,7 @@ handleDeclaration val = do
264 st <- PSCI $ lift get
265 st <- PSCI $ lift get
265 let m = createTemporaryModule True st val
266 let m = createTemporaryModule True st val
266 let nodeArgs = psciNodeFlags st ++ [indexFile]
267 let nodeArgs = psciNodeFlags st ++ [indexFile]
267− e <- psciIO . runMake $ make st [(Left P.RebuildAlways, supportModule), (Left P.RebuildAlways, m)]
268+ e <- psciIO . runMake $ make st [supportModule, m]
268 case e of
269 case e of
269 Left errs -> printErrors errs
270 Left errs -> printErrors errs
270 Right _ -> do
271 Right _ -> do
@@ -285,7 +286,7 @@ handleDecls ds = do
285 st <- PSCI $ lift get
286 st <- PSCI $ lift get
286 let st' = updateLets ds st
287 let st' = updateLets ds st
287 let m = createTemporaryModule False st' (P.ObjectLiteral [])
288 let m = createTemporaryModule False st' (P.ObjectLiteral [])
288− e <- psciIO . runMake $ make st' [(Left P.RebuildAlways, m)]
289+ e <- psciIO . runMake $ make st' [m]
289 case e of
290 case e of
290 Left err -> printErrors err
291 Left err -> printErrors err
291 Right _ -> PSCI $ lift (put st')
292 Right _ -> PSCI $ lift (put st')
@@ -341,7 +342,7 @@ handleImport :: ImportedModule -> PSCI ()
341handleImport im = do
342handleImport im = do
342 st <- updateImportedModules im <$> PSCI (lift get)
343 st <- updateImportedModules im <$> PSCI (lift get)
343 let m = createTemporaryModuleForImports st
344 let m = createTemporaryModuleForImports st
344− e <- psciIO . runMake $ make st [(Left P.RebuildAlways, m)]
345+ e <- psciIO . runMake $ make st [m]
345 case e of
346 case e of
346 Left errs -> printErrors errs
347 Left errs -> printErrors errs
347 Right _ -> do
348 Right _ -> do
@@ -355,7 +356,7 @@ handleTypeOf :: P.Expr -> PSCI ()
355handleTypeOf val = do
356handleTypeOf val = do
356 st <- PSCI $ lift get
357 st <- PSCI $ lift get
357 let m = createTemporaryModule False st val
358 let m = createTemporaryModule False st val
358− e <- psciIO . runMake $ make st [(Left P.RebuildAlways, m)]
359+ e <- psciIO . runMake $ make st [m]
359 case e of
360 case e of
360 Left errs -> printErrors errs
361 Left errs -> printErrors errs
361 Right env' ->
362 Right env' ->
@@ -492,7 +493,7 @@ handleKindOf typ = do
492 st <- PSCI $ lift get
493 st <- PSCI $ lift get
493 let m = createTemporaryModuleForKind st typ
494 let m = createTemporaryModuleForKind st typ
494 mName = P.ModuleName [P.ProperName "$PSCI"]
495 mName = P.ModuleName [P.ProperName "$PSCI"]
495− e <- psciIO . runMake $ make st [(Left P.RebuildAlways, m)]
496+ e <- psciIO . runMake $ make st [m]
496 case e of
497 case e of
497 Left errs -> printErrors errs
498 Left errs -> printErrors errs
498 Right env' ->
499 Right env' ->
@@ -534,11 +535,10 @@ handleCommand ShowHelp = PSCI $ outputStrLn helpMessage
534handleCommand (Import im) = handleImport im
535handleCommand (Import im) = handleImport im
535handleCommand (Decls l) = handleDecls l
536handleCommand (Decls l) = handleDecls l
536handleCommand (LoadFile filePath) = whenFileExists filePath $ \absPath -> do
537handleCommand (LoadFile filePath) = whenFileExists filePath $ \absPath -> do
537− PSCI . lift $ modify (updateImportedFiles absPath)
538 m <- psciIO $ loadModule absPath
538 m <- psciIO $ loadModule absPath
539 case m of
539 case m of
540 Left err -> PSCI $ outputStrLn err
540 Left err -> PSCI $ outputStrLn err
541− Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
541+ Right mods -> PSCI . lift $ modify (updateModules (map (absPath,) mods))
542handleCommand (LoadForeign filePath) = whenFileExists filePath $ \absPath -> do
542handleCommand (LoadForeign filePath) = whenFileExists filePath $ \absPath -> do
543 foreignsOrError <- psciIO . runMake $ do
543 foreignsOrError <- psciIO . runMake $ do
544 foreignFile <- makeIO (const (P.ErrorMessage [] $ P.CannotReadFile absPath)) (readFile absPath)
544 foreignFile <- makeIO (const (P.ErrorMessage [] $ P.CannotReadFile absPath)) (readFile absPath)
@@ -549,10 +549,9 @@ handleCommand (LoadForeign filePath) = whenFileExists filePath $ \absPath -> do
549handleCommand ResetState = do
549handleCommand ResetState = do
550 files <- psciImportedFilenames <$> PSCI (lift get)
550 files <- psciImportedFilenames <$> PSCI (lift get)
551 PSCI . lift . modify $ \st ->
551 PSCI . lift . modify $ \st ->
552− (foldl (flip updateImportedFiles) st files)
553− { psciImportedModules = []
552+ st { psciImportedModules = []
554− , psciLetBindings = []
553+ , psciLetBindings = []
555− }
554+ }
556 loadAllImportedModules
555 loadAllImportedModules
557handleCommand (TypeOf val) = handleTypeOf val
556handleCommand (TypeOf val) = handleTypeOf val
558handleCommand (KindOf typ) = handleKindOf typ
557handleCommand (KindOf typ) = handleKindOf typ
@@ -615,7 +614,7 @@ loop PSCiOptions{..} = do
615 case foreignsOrError of
614 case foreignsOrError of
616 Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> exitFailure
615 Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> exitFailure
617 Right foreigns ->
616 Right foreigns ->
618− flip evalStateT (mkPSCiState inputFiles [] modules foreigns [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
617+ flip evalStateT (mkPSCiState [] modules foreigns [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
619 outputStrLn prologueMessage
618 outputStrLn prologueMessage
620 traverse_ (traverse_ (runPSCI . handleCommand)) config
619 traverse_ (traverse_ (runPSCI . handleCommand)) config
621 modules' <- lift $ gets psciLoadedModules
620 modules' <- lift $ gets psciLoadedModules
psci/Types.hs+31−37
@@ -15,8 +15,7 @@
15
15
16module Types where
16module Types where
17
17
18−import Data.Set (Set)
18+import Control.Arrow (second)
19−import qualified Data.Set as Set
20import Data.Map (Map)
19import Data.Map (Map)
21import qualified Data.Map as Map
20import qualified Data.Map as Map
22import qualified Language.PureScript as P
21import qualified Language.PureScript as P
@@ -35,34 +34,25 @@ data PSCiOptions = PSCiOptions
35-- because it makes more sense to apply the binding to the final evaluated expression.
34-- because it makes more sense to apply the binding to the final evaluated expression.
36--
35--
37data PSCiState = PSCiState
36data PSCiState = PSCiState
38− { _psciImportedFilenames :: Set FilePath
39− , psciImportedModules :: [ImportedModule]
37+ { psciImportedModules :: [ImportedModule]
40− , _psciLoadedModules :: Map P.ModuleName (Either P.RebuildPolicy FilePath, P.Module)
38+ , _psciLoadedModules :: Map FilePath [P.Module]
41 , psciForeignFiles :: Map P.ModuleName FilePath
39 , psciForeignFiles :: Map P.ModuleName FilePath
42 , psciLetBindings :: [P.Declaration]
40 , psciLetBindings :: [P.Declaration]
43 , psciNodeFlags :: [String]
41 , psciNodeFlags :: [String]
44 }
42 }
45
43
46−-- Public psci state accessors
44+initialPSCiState :: PSCiState
47−
45+initialPSCiState =
48−-- | Get the imported filenames as a list.
46+ PSCiState [] Map.empty Map.empty [] []
49−psciImportedFilenames :: PSCiState -> [FilePath]
47+
50−psciImportedFilenames = Set.toList . _psciImportedFilenames
51−
52−-- | Get the loaded modules as a list.
53−psciLoadedModules :: PSCiState -> [(Either P.RebuildPolicy FilePath, P.Module)]
54−psciLoadedModules = Map.elems . _psciLoadedModules
55−
56−mkPSCiState :: [FilePath]
57− -> [ImportedModule]
48+mkPSCiState :: [ImportedModule]
58− -> [(Either P.RebuildPolicy FilePath, P.Module)]
49+ -> [(FilePath, P.Module)]
59− -> Map P.ModuleName FilePath
50+ -> Map P.ModuleName FilePath
60− -> [P.Declaration]
51+ -> [P.Declaration]
61− -> [String]
52+ -> [String]
62− -> PSCiState
53+ -> PSCiState
63−mkPSCiState files imported loaded foreign lets nodeFlags =
54+mkPSCiState imported loaded foreign lets nodeFlags =
64 (initialPSCiState
55 (initialPSCiState
65− |> each files updateImportedFiles
66 |> each imported updateImportedModules
56 |> each imported updateImportedModules
67 |> updateModules loaded)
57 |> updateModules loaded)
68 { psciForeignFiles = foreign
58 { psciForeignFiles = foreign
@@ -73,9 +63,18 @@ mkPSCiState files imported loaded foreign lets nodeFlags =
73 x |> f = f x
63 x |> f = f x
74 each xs f st = foldl (flip f) st xs
64 each xs f st = foldl (flip f) st xs
75
65
76−initialPSCiState :: PSCiState
66+-- Public psci state accessors
77−initialPSCiState =
67+
78− PSCiState Set.empty [] Map.empty Map.empty [] []
68+-- | Get the imported filenames as a list.
69+psciImportedFilenames :: PSCiState -> [FilePath]
70+psciImportedFilenames = Map.keys . _psciLoadedModules
71+
72+-- | Get the loaded modules as a list.
73+psciLoadedModules :: PSCiState -> [(FilePath, P.Module)]
74+psciLoadedModules = collect . Map.toList . _psciLoadedModules
75+ where
76+ collect :: [(k, [v])] -> [(k, v)]
77+ collect vss = [ (k, v) | (k, vs) <- vss, v <- vs ]
79
78
80-- | All of the data that is contained by an ImportDeclaration in the AST.
79-- | All of the data that is contained by an ImportDeclaration in the AST.
81-- That is:
80-- That is:
@@ -101,12 +100,6 @@ allImportsOf m (PSCiState{psciImportedModules = is}) =
101
100
102-- State helpers
101-- State helpers
103
102
104−-- |
105−-- Updates the state to have more imported files.
106−--
107−updateImportedFiles :: FilePath -> PSCiState -> PSCiState
108−updateImportedFiles filename st = st { _psciImportedFilenames = Set.insert filename (_psciImportedFilenames st) }
109−
110-- |
103-- |
111-- Updates the state to have more imported modules.
104-- Updates the state to have more imported modules.
112--
105--
@@ -114,13 +107,14 @@ updateImportedModules :: ImportedModule -> PSCiState -> PSCiState
114updateImportedModules im st = st { psciImportedModules = im : psciImportedModules st }
107updateImportedModules im st = st { psciImportedModules = im : psciImportedModules st }
115
108
116-- |
109-- |
117−-- Updates the state to have more loaded files.
110+-- Updates the state to have more loaded modules (available for import, but
111+-- not necessarily imported).
118--
112--
119−updateModules :: [(Either P.RebuildPolicy FilePath, P.Module)] -> PSCiState -> PSCiState
113+updateModules :: [(FilePath, P.Module)] -> PSCiState -> PSCiState
120updateModules modules st =
114updateModules modules st =
121− st { _psciLoadedModules = foldl (\m mdl -> Map.insert (keyFor mdl) mdl m) (_psciLoadedModules st) modules }
115+ st { _psciLoadedModules = Map.union (go modules) (_psciLoadedModules st) }
122 where
116 where
123− keyFor = P.getModuleName . snd
117+ go = Map.fromListWith (++) . map (second (:[]))
124
118
125-- |
119-- |
126-- Updates the state to have more let bindings.
120-- Updates the state to have more let bindings.
psci/tests/Main.hs+1−1
@@ -142,7 +142,7 @@ getPSCiState = do
142 print err >> exitFailure
142 print err >> exitFailure
143 Right modules ->
143 Right modules ->
144 let imports = [controlMonadSTasST, (P.ModuleName [P.ProperName "Prelude"], P.Implicit True, Nothing)]
144 let imports = [controlMonadSTasST, (P.ModuleName [P.ProperName "Prelude"], P.Implicit True, Nothing)]
145− in return (PSCiState [] imports modules foreigns [] [])
145+ in return (mkPSCiState imports modules foreigns [] [])
146
146
147controlMonadSTasST :: ImportedModule
147controlMonadSTasST :: ImportedModule
148controlMonadSTasST = (s "Control.Monad.ST", P.Implicit True, Just (s "ST"))
148controlMonadSTasST = (s "Control.Monad.ST", P.Implicit True, Just (s "ST"))