Commit 454f8dcb by Zachary Snow

faster package item nesting traversal

parent 82290b16
...@@ -78,13 +78,10 @@ addItems pis existingPIs (item : items) = ...@@ -78,13 +78,10 @@ addItems pis existingPIs (item : items) =
addItems pis existingPIs (head itemsToAdd : item : items) addItems pis existingPIs (head itemsToAdd : item : items)
where where
thisPI = execWriter $ collectPIsM item thisPI = execWriter $ collectPIsM item
runner f = execWriter $ collectNestedModuleItemsM f item usedPIs = execWriter $
usedPIs = Set.unions $ map runner traverseNestedModuleItemsM (traverseIdentsM writeIdent) item
[ collectStmtsM collectSubroutinesM writeIdent :: Identifier -> Writer Idents Identifier
, collectTypesM $ collectNestedTypesM collectTypenamesM writeIdent x = tell (Set.singleton x) >> return x
, collectExprsM $ collectNestedExprsM collectExprIdentsM
, collectLHSsM $ collectNestedLHSsM collectLHSIdentsM
]
neededPIs = Set.difference (Set.difference usedPIs existingPIs) thisPI neededPIs = Set.difference (Set.difference usedPIs existingPIs) thisPI
itemsToAdd = map MIPackageItem $ Map.elems $ itemsToAdd = map MIPackageItem $ Map.elems $
Map.restrictKeys pis neededPIs Map.restrictKeys pis neededPIs
...@@ -98,28 +95,66 @@ collectPIsM (MIPackageItem item) = ...@@ -98,28 +95,66 @@ collectPIsM (MIPackageItem item) =
ident -> tell $ Set.singleton ident ident -> tell $ Set.singleton ident
collectPIsM _ = return () collectPIsM _ = return ()
-- writes down the names of subroutine invocations -- visits all identifiers in a module item
collectSubroutinesM :: Stmt -> Writer Idents () traverseIdentsM :: Monad m => MapperM m Identifier -> MapperM m ModuleItem
collectSubroutinesM (Subroutine (Ident f) _) = tell $ Set.singleton f traverseIdentsM identMapper = traverseNodesM
collectSubroutinesM _ = return () (traverseExprIdentsM identMapper)
(traverseDeclIdentsM identMapper)
-- writes down the names of function calls and identifiers (traverseTypeIdentsM identMapper)
collectExprIdentsM :: Expr -> Writer Idents () (traverseLHSIdentsM identMapper)
collectExprIdentsM (Call (Ident x) _) = tell $ Set.singleton x (traverseStmtIdentsM identMapper)
collectExprIdentsM (Ident x) = tell $ Set.singleton x
collectExprIdentsM _ = return () -- visits all identifiers in an expression
traverseExprIdentsM :: Monad m => MapperM m Identifier -> MapperM m Expr
-- writes down the names of identifiers traverseExprIdentsM identMapper = fullMapper
collectLHSIdentsM :: LHS -> Writer Idents () where
collectLHSIdentsM (LHSIdent x) = tell $ Set.singleton x fullMapper = exprMapper >=> traverseSinglyNestedExprsM fullMapper
collectLHSIdentsM _ = return () exprMapper (Call (Ident x) args) =
identMapper x >>= \x' -> return $ Call (Ident x') args
-- writes down aliased typenames exprMapper (Ident x) = identMapper x >>= return . Ident
collectTypenamesM :: Type -> Writer Idents () exprMapper other = return other
collectTypenamesM (Alias x _) = tell $ Set.singleton x
collectTypenamesM (PSAlias _ x _) = tell $ Set.singleton x -- visits all identifiers in a type
collectTypenamesM (CSAlias _ _ x _) = tell $ Set.singleton x traverseTypeIdentsM :: Monad m => MapperM m Identifier -> MapperM m Type
collectTypenamesM _ = return () traverseTypeIdentsM identMapper = fullMapper
where
fullMapper = typeMapper
>=> traverseTypeExprsM (traverseExprIdentsM identMapper)
>=> traverseSinglyNestedTypesM fullMapper
typeMapper (Alias x t) = aliasHelper (Alias ) x t
typeMapper (PSAlias p x t) = aliasHelper (PSAlias p ) x t
typeMapper (CSAlias c p x t) = aliasHelper (CSAlias c p) x t
typeMapper other = return other
aliasHelper constructor x t =
identMapper x >>= \x' -> return $ constructor x' t
-- visits all identifiers in an LHS
traverseLHSIdentsM :: Monad m => MapperM m Identifier -> MapperM m LHS
traverseLHSIdentsM identMapper = fullMapper
where
fullMapper = lhsMapper
>=> traverseLHSExprsM (traverseExprIdentsM identMapper)
>=> traverseSinglyNestedLHSsM fullMapper
lhsMapper (LHSIdent x) = identMapper x >>= return . LHSIdent
lhsMapper other = return other
-- visits all identifiers in a statement
traverseStmtIdentsM :: Monad m => MapperM m Identifier -> MapperM m Stmt
traverseStmtIdentsM identMapper = fullMapper
where
fullMapper = stmtMapper
>=> traverseStmtExprsM (traverseExprIdentsM identMapper)
>=> traverseStmtLHSsM (traverseLHSIdentsM identMapper)
>=> traverseSinglyNestedStmtsM fullMapper
stmtMapper (Subroutine (Ident x) args) =
identMapper x >>= \x' -> return $ Subroutine (Ident x') args
stmtMapper other = return other
-- visits all identifiers in a declaration
traverseDeclIdentsM :: Monad m => MapperM m Identifier -> MapperM m Decl
traverseDeclIdentsM identMapper =
traverseDeclExprsM (traverseExprIdentsM identMapper) >=>
traverseDeclTypesM (traverseTypeIdentsM identMapper)
-- returns the "name" of a package item, if it has one -- returns the "name" of a package item, if it has one
piName :: PackageItem -> Identifier piName :: PackageItem -> Identifier
......
...@@ -26,6 +26,7 @@ module Convert.Traverse ...@@ -26,6 +26,7 @@ module Convert.Traverse
, traverseExprsM , traverseExprsM
, traverseExprs , traverseExprs
, collectExprsM , collectExprsM
, traverseNodesM
, traverseStmtExprsM , traverseStmtExprsM
, traverseStmtExprs , traverseStmtExprs
, collectStmtExprsM , collectStmtExprsM
...@@ -84,6 +85,9 @@ module Convert.Traverse ...@@ -84,6 +85,9 @@ module Convert.Traverse
, traverseSinglyNestedExprsM , traverseSinglyNestedExprsM
, traverseSinglyNestedExprs , traverseSinglyNestedExprs
, collectSinglyNestedExprsM , collectSinglyNestedExprsM
, traverseLHSExprsM
, traverseLHSExprs
, collectLHSExprsM
, traverseNestedLHSsM , traverseNestedLHSsM
, traverseNestedLHSs , traverseNestedLHSs
, collectNestedLHSsM , collectNestedLHSsM
...@@ -503,6 +507,11 @@ traverseLHSExprsM exprMapper = ...@@ -503,6 +507,11 @@ traverseLHSExprsM exprMapper =
return $ LHSStream o e' ls return $ LHSStream o e' ls
lhsMapper other = return other lhsMapper other = return other
traverseLHSExprs :: Mapper Expr -> Mapper LHS
traverseLHSExprs = unmonad traverseLHSExprsM
collectLHSExprsM :: Monad m => CollectorM m Expr -> CollectorM m LHS
collectLHSExprsM = collectify traverseLHSExprsM
mapBothM :: Monad m => MapperM m t -> MapperM m (t, t) mapBothM :: Monad m => MapperM m t -> MapperM m (t, t)
mapBothM mapper (a, b) = do mapBothM mapper (a, b) = do
a' <- mapper a a' <- mapper a
...@@ -510,14 +519,31 @@ mapBothM mapper (a, b) = do ...@@ -510,14 +519,31 @@ mapBothM mapper (a, b) = do
return (a', b') return (a', b')
traverseExprsM :: Monad m => MapperM m Expr -> MapperM m ModuleItem traverseExprsM :: Monad m => MapperM m Expr -> MapperM m ModuleItem
traverseExprsM exprMapper = moduleItemMapper traverseExprsM exprMapper =
traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper
where where
declMapper = traverseDeclExprsM exprMapper declMapper = traverseDeclExprsM exprMapper
typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper) typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper)
lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper) lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper)
stmtMapper = traverseNestedStmtsM (traverseStmtExprsM exprMapper) stmtMapper = traverseNestedStmtsM (traverseStmtExprsM exprMapper)
traverseExprs :: Mapper Expr -> Mapper ModuleItem
traverseExprs = unmonad traverseExprsM
collectExprsM :: Monad m => CollectorM m Expr -> CollectorM m ModuleItem
collectExprsM = collectify traverseExprsM
traverseNodesM
:: Monad m
=> MapperM m Expr
-> MapperM m Decl
-> MapperM m Type
-> MapperM m LHS
-> MapperM m Stmt
-> MapperM m ModuleItem
traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper =
moduleItemMapper
where
portBindingMapper (p, e) = portBindingMapper (p, e) =
exprMapper e >>= \e' -> return (p, e') exprMapper e >>= \e' -> return (p, e')
...@@ -600,11 +626,6 @@ traverseExprsM exprMapper = moduleItemMapper ...@@ -600,11 +626,6 @@ traverseExprsM exprMapper = moduleItemMapper
e' <- exprMapper e e' <- exprMapper e
return (dir, ident, e') return (dir, ident, e')
traverseExprs :: Mapper Expr -> Mapper ModuleItem
traverseExprs = unmonad traverseExprsM
collectExprsM :: Monad m => CollectorM m Expr -> CollectorM m ModuleItem
collectExprsM = collectify traverseExprsM
traverseStmtExprsM :: Monad m => MapperM m Expr -> MapperM m Stmt traverseStmtExprsM :: Monad m => MapperM m Expr -> MapperM m Stmt
traverseStmtExprsM exprMapper = flatStmtMapper traverseStmtExprsM exprMapper = flatStmtMapper
where where
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment