package item nesting resolves across and throughout files

This commit is contained in:
Zachary Snow 2019-09-14 21:00:40 -04:00
parent c5f7f2cc5e
commit 2a51d20f11
1 changed files with 27 additions and 22 deletions

View File

@ -6,9 +6,9 @@
module Convert.NestPI (convert) where module Convert.NestPI (convert) where
import Control.Monad.State
import Control.Monad.Writer import Control.Monad.Writer
import Data.List (isPrefixOf) import Data.List (isPrefixOf)
import Data.List.Unique (complex)
import qualified Data.Set as Set import qualified Data.Set as Set
import Convert.Traverse import Convert.Traverse
@ -18,37 +18,35 @@ type PIs = [(Identifier, PackageItem)]
type Idents = Set.Set Identifier type Idents = Set.Set Identifier
convert :: [AST] -> [AST] convert :: [AST] -> [AST]
convert asts = convert =
map (filter (not . isPI) . nest) asts map (filter (not . isPI)) . nest
where where
nest :: AST -> AST nest :: [AST] -> [AST]
nest curr = nest curr =
if next == curr if next == curr
then curr then curr
else nest next else nest next
where where
next = evalState (traverseM curr) [] next = traverseFiles
traverseM = traverseDescriptionsM traverseDescriptionM (collectDescriptionsM collectDescriptionM)
(traverseDescriptions . convertDescription)
curr
isPI :: Description -> Bool isPI :: Description -> Bool
isPI (PackageItem item) = piName item /= Nothing isPI (PackageItem item) = piName item /= Nothing
isPI _ = False isPI _ = False
-- collects and nests in tasks and functions missing from modules -- collects packages items missing
traverseDescriptionM :: Description -> State PIs Description collectDescriptionM :: Description -> Writer PIs ()
traverseDescriptionM (PackageItem item) = do collectDescriptionM (PackageItem item) = do
() <- case piName item of case piName item of
Nothing -> return () Nothing -> return ()
Just ident -> modify $ flip (++) [(ident, item)] Just ident -> tell [(ident, item)]
return $ PackageItem item collectDescriptionM _ = return ()
traverseDescriptionM (orig @ (Part extern kw lifetime name ports items)) = do
tfs <- get -- nests packages items missing from modules
let neededPIs = Set.difference convertDescription :: PIs -> Description -> Description
(Set.union usedPIs $ convertDescription pis (orig @ (Part extern kw lifetime name ports items)) =
Set.filter (isPrefixOf "import ") $ Set.fromList $ map fst tfs) Part extern kw lifetime name ports (newItems ++ items)
existingPIs
let newItems = map MIPackageItem $ map snd $
filter (\(x, _) -> Set.member x neededPIs) tfs
return $ Part extern kw lifetime name ports (newItems ++ items)
where where
existingPIs = execWriter $ collectModuleItemsM collectPIsM orig existingPIs = execWriter $ collectModuleItemsM collectPIsM orig
runner f = execWriter $ collectModuleItemsM f orig runner f = execWriter $ collectModuleItemsM f orig
@ -57,7 +55,14 @@ traverseDescriptionM (orig @ (Part extern kw lifetime name ports items)) = do
, collectTypesM $ collectNestedTypesM collectTypenamesM , collectTypesM $ collectNestedTypesM collectTypenamesM
, collectExprsM $ collectNestedExprsM collectIdentsM , collectExprsM $ collectNestedExprsM collectIdentsM
] ]
traverseDescriptionM other = return other neededPIs = Set.difference
(Set.union usedPIs $
Set.filter (isPrefixOf "import ") $ Set.fromList $ map fst pis)
existingPIs
uniq l = l' where (l', _, _) = complex l
newItems = uniq $ map MIPackageItem $ map snd $
filter (\(x, _) -> Set.member x neededPIs) pis
convertDescription _ other = other
-- writes down the names of package items -- writes down the names of package items
collectPIsM :: ModuleItem -> Writer Idents () collectPIsM :: ModuleItem -> Writer Idents ()