Commit 295ac649 by Zachary Snow

package item conversions preserve ordering (resolves #36)

parent dd3a7e68
......@@ -9,13 +9,12 @@ module Convert.NestPI (convert) where
import Control.Monad.State
import Control.Monad.Writer
import Data.List (isPrefixOf)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Convert.Traverse
import Language.SystemVerilog.AST
type PIs = Map.Map Identifier PackageItem
type PIs = [(Identifier, PackageItem)]
type Idents = Set.Set Identifier
convert :: [AST] -> [AST]
......@@ -28,7 +27,7 @@ convert asts =
then curr
else nest next
where
next = evalState (traverseM curr) Map.empty
next = evalState (traverseM curr) []
traverseM = traverseDescriptionsM traverseDescriptionM
isPI :: Description -> Bool
isPI (PackageItem item) = piName item /= Nothing
......@@ -39,16 +38,16 @@ traverseDescriptionM :: Description -> State PIs Description
traverseDescriptionM (PackageItem item) = do
() <- case piName item of
Nothing -> return ()
Just ident -> modify $ Map.insert ident item
Just ident -> modify $ flip (++) [(ident, item)]
return $ PackageItem item
traverseDescriptionM (orig @ (Part extern kw lifetime name ports items)) = do
tfs <- get
let neededPIs = Set.difference
(Set.union usedPIs $
Set.filter (isPrefixOf "import ") $ Map.keysSet tfs)
Set.filter (isPrefixOf "import ") $ Set.fromList $ map fst tfs)
existingPIs
let newItems = map MIPackageItem $ Map.elems $
Map.restrictKeys tfs neededPIs
let newItems = map MIPackageItem $ map snd $
filter (\(x, _) -> Set.member x neededPIs) tfs
return $ Part extern kw lifetime name ports (newItems ++ items)
where
existingPIs = execWriter $ collectModuleItemsM collectPIsM orig
......
......@@ -33,7 +33,7 @@ import Convert.Traverse
import Language.SystemVerilog.AST
type Packages = Map.Map Identifier PackageItems
type PackageItems = Map.Map Identifier PackageItem
type PackageItems = [(Identifier, PackageItem)]
type Idents = Set.Set Identifier
convert :: [AST] -> [AST]
......@@ -64,13 +64,13 @@ convertFile packages ast =
globalPackageItems :: Identifier -> PackageItems -> [PackageItem]
globalPackageItems name items =
map (prefixPackageItem name (packageItemIdents items)) (Map.elems items)
map (prefixPackageItem name (packageItemIdents items)) (map snd items)
packageItemIdents :: PackageItems -> Idents
packageItemIdents items =
Set.union
(Map.keysSet items)
(Set.unions $ map packageItemSubIdents $ Map.elems items)
(Set.fromList $ map fst items)
(Set.unions $ map (packageItemSubIdents . snd) items)
where
packageItemSubIdents :: PackageItem -> Idents
packageItemSubIdents (Typedef (Enum _ enumItems _) _) =
......@@ -114,14 +114,14 @@ collectDescriptionM :: Description -> Writer Packages ()
collectDescriptionM (Package _ name items) =
if any isImport items
then return ()
else tell $ Map.singleton name itemMap
else tell $ Map.singleton name itemList
where
itemMap = Map.unions $ map toMap items
toMap :: PackageItem -> PackageItems
toMap item =
itemList = concatMap toPackageItems items
toPackageItems :: PackageItem -> PackageItems
toPackageItems item =
case piName item of
Nothing -> Map.empty
Just x -> Map.singleton x item
Nothing -> []
Just x -> [(x, item)]
isImport :: PackageItem -> Bool
isImport (Import _ _) = True
isImport _ = False
......@@ -151,7 +151,7 @@ traverseModuleItem existingItemNames packages (MIPackageItem (Import x y)) =
filterer itemName = case y of
Nothing -> Set.notMember itemName existingItemNames
Just ident -> ident == itemName
items = map snd $ filter (filterer . fst) $ Map.toList packageItems
items = map snd $ filter (filterer . fst) $ packageItems
traverseModuleItem _ _ item =
(traverseExprs $ traverseNestedExprs traverseExpr) $
(traverseStmts traverseStmt) $
......
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