git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Ensure no duplicates appear in the PSCiState
e66c773Harry Garrood
psci/PSCi.hs+11−10
@@ -129,7 +129,7 @@ loadAllImportedModules = do
129 modulesOrFirstError <- psciIO $ loadAllModules files
129 modulesOrFirstError <- psciIO $ loadAllModules files
130 case modulesOrFirstError of
130 case modulesOrFirstError of
131 Left errs -> printErrors errs
131 Left errs -> printErrors errs
132− Right modules -> PSCI . lift . modify $ \st -> st { psciLoadedModules = modules }
132+ Right modules -> PSCI . lift . modify $ updateModules modules
133
133
134-- |
134-- |
135-- Expands tilde in path.
135-- Expands tilde in path.
@@ -249,11 +249,12 @@ makeIO f io = do
249 either (throwError . P.singleError . f) return e
249 either (throwError . P.singleError . f) return e
250
250
251make :: PSCiState -> [(Either P.RebuildPolicy FilePath, P.Module)] -> P.Make P.Environment
251make :: PSCiState -> [(Either P.RebuildPolicy FilePath, P.Module)] -> P.Make P.Environment
252−make PSCiState{..} ms = P.make actions' (map snd (psciLoadedModules ++ 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` (psciLoadedModules ++ ms)
254+ filePathMap = M.fromList $ (first P.getModuleName . swap) `map` (loadedModules ++ ms)
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
258
258-- |
259-- |
259-- Takes a value declaration and evaluates it with the current state.
260-- Takes a value declaration and evaluates it with the current state.
@@ -294,7 +295,7 @@ handleDecls ds = do
294--
295--
295handleShowLoadedModules :: PSCI ()
296handleShowLoadedModules :: PSCI ()
296handleShowLoadedModules = do
297handleShowLoadedModules = do
297− PSCiState { psciLoadedModules = loadedModules } <- PSCI $ lift get
298+ loadedModules <- PSCI $ lift $ gets psciLoadedModules
298 psciIO $ readModules loadedModules >>= putStrLn
299 psciIO $ readModules loadedModules >>= putStrLn
299 return ()
300 return ()
300 where readModules = return . unlines . sort . nub . map toModuleName
301 where readModules = return . unlines . sort . nub . map toModuleName
@@ -547,11 +548,11 @@ handleCommand (LoadForeign filePath) = whenFileExists filePath $ \absPath -> do
547 Right foreigns -> PSCI . lift $ modify (updateForeignFiles foreigns)
548 Right foreigns -> PSCI . lift $ modify (updateForeignFiles foreigns)
548handleCommand ResetState = do
549handleCommand ResetState = do
549 files <- psciImportedFilenames <$> PSCI (lift get)
550 files <- psciImportedFilenames <$> PSCI (lift get)
550− PSCI . lift . modify $ \st -> st
551+ PSCI . lift . modify $ \st ->
551− { psciImportedFilenames = files
552+ (foldl (flip updateImportedFiles) st files)
552− , psciImportedModules = []
553+ { psciImportedModules = []
553− , psciLetBindings = []
554+ , psciLetBindings = []
554− }
555+ }
555 loadAllImportedModules
556 loadAllImportedModules
556handleCommand (TypeOf val) = handleTypeOf val
557handleCommand (TypeOf val) = handleTypeOf val
557handleCommand (KindOf typ) = handleKindOf typ
558handleCommand (KindOf typ) = handleKindOf typ
@@ -614,7 +615,7 @@ loop PSCiOptions{..} = do
614 case foreignsOrError of
615 case foreignsOrError of
615 Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> exitFailure
616 Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> exitFailure
616 Right foreigns ->
617 Right foreigns ->
617− flip evalStateT (PSCiState inputFiles [] modules foreigns [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
618+ flip evalStateT (mkPSCiState inputFiles [] modules foreigns [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
618 outputStrLn prologueMessage
619 outputStrLn prologueMessage
619 traverse_ (traverse_ (runPSCI . handleCommand)) config
620 traverse_ (traverse_ (runPSCI . handleCommand)) config
620 modules' <- lift $ gets psciLoadedModules
621 modules' <- lift $ gets psciLoadedModules
psci/Types.hs+49−9
@@ -15,7 +15,10 @@
15
15
16module Types where
16module Types where
17
17
18+import Data.Set (Set)
19+import qualified Data.Set as Set
20+import Data.Map (Map)
18−import qualified Data.Map as M
21+import qualified Data.Map as Map
19import qualified Language.PureScript as P
22import qualified Language.PureScript as P
20
23
21data PSCiOptions = PSCiOptions
24data PSCiOptions = PSCiOptions
@@ -32,14 +35,48 @@ data PSCiOptions = PSCiOptions
32-- because it makes more sense to apply the binding to the final evaluated expression.
35-- because it makes more sense to apply the binding to the final evaluated expression.
33--
36--
34data PSCiState = PSCiState
37data PSCiState = PSCiState
35− { psciImportedFilenames :: [FilePath]
38+ { _psciImportedFilenames :: Set FilePath
36 , psciImportedModules :: [ImportedModule]
39 , psciImportedModules :: [ImportedModule]
37− , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
40+ , _psciLoadedModules :: Map P.ModuleName (Either P.RebuildPolicy FilePath, P.Module)
38− , psciForeignFiles :: M.Map P.ModuleName FilePath
41+ , psciForeignFiles :: Map P.ModuleName FilePath
39 , psciLetBindings :: [P.Declaration]
42 , psciLetBindings :: [P.Declaration]
40 , psciNodeFlags :: [String]
43 , psciNodeFlags :: [String]
41 }
44 }
42
45
46+-- Public psci state accessors
47+
48+-- | Get the imported filenames as a list.
49+psciImportedFilenames :: PSCiState -> [FilePath]
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]
58+ -> [(Either P.RebuildPolicy FilePath, P.Module)]
59+ -> Map P.ModuleName FilePath
60+ -> [P.Declaration]
61+ -> [String]
62+ -> PSCiState
63+mkPSCiState files imported loaded foreign lets nodeFlags =
64+ (initialPSCiState
65+ |> each files updateImportedFiles
66+ |> each imported updateImportedModules
67+ |> updateModules loaded)
68+ { psciForeignFiles = foreign
69+ , psciLetBindings = lets
70+ , psciNodeFlags = nodeFlags
71+ }
72+ where
73+ x |> f = f x
74+ each xs f st = foldl (flip f) st xs
75+
76+initialPSCiState :: PSCiState
77+initialPSCiState =
78+ PSCiState Set.empty [] Map.empty Map.empty [] []
79+
43-- | All of the data that is contained by an ImportDeclaration in the AST.
80-- | All of the data that is contained by an ImportDeclaration in the AST.
44-- That is:
81-- That is:
45--
82--
@@ -65,10 +102,10 @@ allImportsOf m (PSCiState{psciImportedModules = is}) =
65-- State helpers
102-- State helpers
66
103
67-- |
104-- |
68−-- Updates the state to have more imported modules.
105+-- Updates the state to have more imported files.
69--
106--
70updateImportedFiles :: FilePath -> PSCiState -> PSCiState
107updateImportedFiles :: FilePath -> PSCiState -> PSCiState
71−updateImportedFiles filename st = st { psciImportedFilenames = filename : psciImportedFilenames st }
108+updateImportedFiles filename st = st { _psciImportedFilenames = Set.insert filename (_psciImportedFilenames st) }
72
109
73-- |
110-- |
74-- Updates the state to have more imported modules.
111-- Updates the state to have more imported modules.
@@ -80,7 +117,10 @@ updateImportedModules im st = st { psciImportedModules = im : psciImportedModule
80-- Updates the state to have more loaded files.
117-- Updates the state to have more loaded files.
81--
118--
82updateModules :: [(Either P.RebuildPolicy FilePath, P.Module)] -> PSCiState -> PSCiState
119updateModules :: [(Either P.RebuildPolicy FilePath, P.Module)] -> PSCiState -> PSCiState
83−updateModules modules st = st { psciLoadedModules = psciLoadedModules st ++ modules }
120+updateModules modules st =
121+ st { _psciLoadedModules = foldl (\m mdl -> Map.insert (keyFor mdl) mdl m) (_psciLoadedModules st) modules }
122+ where
123+ keyFor = P.getModuleName . snd
84
124
85-- |
125-- |
86-- Updates the state to have more let bindings.
126-- Updates the state to have more let bindings.
@@ -91,8 +131,8 @@ updateLets ds st = st { psciLetBindings = psciLetBindings st ++ ds }
91-- |
131-- |
92-- Updates the state to have more let bindings.
132-- Updates the state to have more let bindings.
93--
133--
94−updateForeignFiles :: M.Map P.ModuleName FilePath -> PSCiState -> PSCiState
134+updateForeignFiles :: Map P.ModuleName FilePath -> PSCiState -> PSCiState
95−updateForeignFiles fs st = st { psciForeignFiles = psciForeignFiles st `M.union` fs }
135+updateForeignFiles fs st = st { psciForeignFiles = psciForeignFiles st `Map.union` fs }
96
136
97-- |
137-- |
98-- Valid Meta-commands for PSCI
138-- Valid Meta-commands for PSCI