Commit bd1c0723 by Zachary Snow

experimenting with monad helpers

parent 4026ae8f
...@@ -41,10 +41,9 @@ convert = ...@@ -41,10 +41,9 @@ convert =
-- we can only collect/map non-extern interfaces -- we can only collect/map non-extern interfaces
collectDesc :: Description -> Writer (Interfaces, Modules) () collectDesc :: Description -> Writer (Interfaces, Modules) ()
collectDesc (orig @ (Part _ False kw _ name ports items)) = do collectDesc (orig @ (Part _ False kw _ name ports items)) = do
if kw == Interface then if kw == Interface
if all fullyResolved items then when (all fullyResolved items) $
then tell (Map.singleton name (ports, items), Map.empty) tell (Map.singleton name (ports, items), Map.empty)
else return ()
else tell (Map.empty, Map.singleton name (params, decls)) else tell (Map.empty, Map.singleton name (params, decls))
where where
params = map fst $ parameters items params = map fst $ parameters items
...@@ -85,13 +84,11 @@ convertDescription interfaces modules (Part attrs extern Module lifetime name po ...@@ -85,13 +84,11 @@ convertDescription interfaces modules (Part attrs extern Module lifetime name po
collectInstanceM (MIPackageItem (Decl (Variable _ t ident _ _))) = collectInstanceM (MIPackageItem (Decl (Variable _ t ident _ _))) =
case t of case t of
InterfaceT interfaceName (Just modportName) [] -> InterfaceT interfaceName (Just modportName) [] ->
if Map.member interfaceName interfaces when (Map.member interfaceName interfaces) $
then writeModport interfaceName modportName writeModport interfaceName modportName
else return ()
Alias Nothing interfaceName [] -> Alias Nothing interfaceName [] ->
if Map.member interfaceName interfaces when (Map.member interfaceName interfaces) $
then writeModport interfaceName "" writeModport interfaceName ""
else return ()
_ -> return () _ -> return ()
where where
writeModport :: Identifier -> Identifier -> writeModport :: Identifier -> Identifier ->
...@@ -100,9 +97,8 @@ convertDescription interfaces modules (Part attrs extern Module lifetime name po ...@@ -100,9 +97,8 @@ convertDescription interfaces modules (Part attrs extern Module lifetime name po
tell (Map.empty, Map.singleton ident modport) tell (Map.empty, Map.singleton ident modport)
where modport = (interfaceName, modportName) where modport = (interfaceName, modportName)
collectInstanceM (Instance part _ ident [] _) = collectInstanceM (Instance part _ ident [] _) =
if Map.member part interfaces when (Map.member part interfaces) $
then tell (Map.singleton ident part, Map.empty) tell (Map.singleton ident part, Map.empty)
else return ()
collectInstanceM _ = return () collectInstanceM _ = return ()
expandInterface :: ModuleItem -> ModuleItem expandInterface :: ModuleItem -> ModuleItem
...@@ -440,9 +436,8 @@ inlineInterface (ports, items) (instanceName, instanceParams, instancePorts) = ...@@ -440,9 +436,8 @@ inlineInterface (ports, items) (instanceName, instanceParams, instancePorts) =
mapM (collectDeclsM collectDeclDir) itemsPrefixed mapM (collectDeclsM collectDeclDir) itemsPrefixed
collectDeclDir :: Decl -> Writer (Map.Map Identifier Direction) () collectDeclDir :: Decl -> Writer (Map.Map Identifier Direction) ()
collectDeclDir (Variable dir _ ident _ _) = collectDeclDir (Variable dir _ ident _ _) =
if dir /= Local when (dir /= Local) $
then tell $ Map.singleton ident dir tell $ Map.singleton ident dir
else return ()
collectDeclDir _ = return () collectDeclDir _ = return ()
toLHS :: Expr -> LHS toLHS :: Expr -> LHS
......
...@@ -118,13 +118,13 @@ combineRanges r1 r2 = r ...@@ -118,13 +118,13 @@ combineRanges r1 r2 = r
(BinOp Sub lower (Number "1")) (BinOp Sub lower (Number "1"))
traverseModuleItemM :: ModuleItem -> State Info ModuleItem traverseModuleItemM :: ModuleItem -> State Info ModuleItem
traverseModuleItemM item = traverseModuleItemM =
traverseLHSsM traverseLHSM item >>= traverseLHSsM traverseLHSM >=>
traverseExprsM traverseExprM traverseExprsM traverseExprM
traverseStmtM :: Stmt -> State Info Stmt traverseStmtM :: Stmt -> State Info Stmt
traverseStmtM stmt = traverseStmtM =
traverseStmtLHSsM traverseLHSM stmt >>= traverseStmtLHSsM traverseLHSM >=>
traverseStmtExprsM traverseExprM traverseStmtExprsM traverseExprM
traverseExprM :: Expr -> State Info Expr traverseExprM :: Expr -> State Info Expr
......
...@@ -123,13 +123,13 @@ prefixPackageItem packageName idents item = ...@@ -123,13 +123,13 @@ prefixPackageItem packageName idents item =
convertLHSM (LHSIdent x) = prefixM x >>= return . LHSIdent convertLHSM (LHSIdent x) = prefixM x >>= return . LHSIdent
convertLHSM other = return other convertLHSM other = return other
convertModuleItemM x = return x >>= convertModuleItemM =
(traverseTypesM convertTypeM) >>= traverseTypesM convertTypeM >=>
(traverseExprsM $ traverseNestedExprsM convertExprM) >>= traverseExprsM (traverseNestedExprsM convertExprM) >=>
(traverseLHSsM $ traverseNestedLHSsM convertLHSM ) traverseLHSsM (traverseNestedLHSsM convertLHSM )
convertStmtM stmt = return stmt >>= convertStmtM =
(traverseStmtExprsM $ traverseNestedExprsM convertExprM) >>= traverseStmtExprsM (traverseNestedExprsM convertExprM) >=>
(traverseStmtLHSsM $ traverseNestedLHSsM convertLHSM ) traverseStmtLHSsM (traverseNestedLHSsM convertLHSM )
MIPackageItem item'' = MIPackageItem item'' =
evalState evalState
......
...@@ -52,21 +52,21 @@ convertDescription (description @ Part{}) = ...@@ -52,21 +52,21 @@ convertDescription (description @ Part{}) =
let MIPackageItem (Decl decl'') = res let MIPackageItem (Decl decl'') = res
return decl'' return decl''
traverseModuleItemM :: ModuleItem -> State Types ModuleItem traverseModuleItemM :: ModuleItem -> State Types ModuleItem
traverseModuleItemM item = traverseModuleItemM =
traverseLHSsM traverseLHSM item >>= traverseLHSsM traverseLHSM >=>
traverseExprsM traverseExprM >>= traverseExprsM traverseExprM >=>
traverseAsgnsM traverseAsgnM traverseAsgnsM traverseAsgnM
traverseStmtM :: Stmt -> State Types Stmt traverseStmtM :: Stmt -> State Types Stmt
traverseStmtM (Subroutine expr args) = do traverseStmtM (Subroutine expr args) = do
stateTypes <- get stateTypes <- get
let stmt' = Subroutine expr $ convertCall let stmt' = Subroutine expr $ convertCall
structs stateTypes expr args structs stateTypes expr args
traverseStmtLHSsM traverseLHSM stmt' >>= traverseStmtM' stmt'
traverseStmtExprsM traverseExprM >>= traverseStmtM stmt = traverseStmtM' stmt
traverseStmtAsgnsM traverseAsgnM traverseStmtM' :: Stmt -> State Types Stmt
traverseStmtM stmt = traverseStmtM' =
traverseStmtLHSsM traverseLHSM stmt >>= traverseStmtLHSsM traverseLHSM >=>
traverseStmtExprsM traverseExprM >>= traverseStmtExprsM traverseExprM >=>
traverseStmtAsgnsM traverseAsgnM traverseStmtAsgnsM traverseAsgnM
traverseExprM = traverseExprM =
traverseNestedExprsM $ stately converter traverseNestedExprsM $ stately converter
......
...@@ -122,9 +122,9 @@ unmonad :: (MapperM Identity a -> MapperM Identity b) -> Mapper a -> Mapper b ...@@ -122,9 +122,9 @@ unmonad :: (MapperM Identity a -> MapperM Identity b) -> Mapper a -> Mapper b
unmonad traverser mapper = runIdentity . traverser (return . mapper) unmonad traverser mapper = runIdentity . traverser (return . mapper)
collectify :: Monad m => (MapperM m a -> MapperM m b) -> CollectorM m a -> CollectorM m b collectify :: Monad m => (MapperM m a -> MapperM m b) -> CollectorM m a -> CollectorM m b
collectify traverser collector thing = collectify traverser collector =
traverser mapper thing >>= \_ -> return () traverser mapper >=> \_ -> return ()
where mapper x = collector x >>= \() -> return x where mapper x = collector x >> return x
traverseDescriptionsM :: Monad m => MapperM m Description -> MapperM m AST traverseDescriptionsM :: Monad m => MapperM m Description -> MapperM m AST
traverseDescriptionsM = mapM traverseDescriptionsM = mapM
......
...@@ -61,11 +61,11 @@ packDecl _ other = other ...@@ -61,11 +61,11 @@ packDecl _ other = other
traverseModuleItemM :: ModuleItem -> ST ModuleItem traverseModuleItemM :: ModuleItem -> ST ModuleItem
traverseModuleItemM item = traverseModuleItemM =
traverseModuleItemM' item traverseModuleItemM'
>>= traverseLHSsM traverseLHSM >=> traverseLHSsM traverseLHSM
>>= traverseExprsM traverseExprM >=> traverseExprsM traverseExprM
>>= traverseAsgnsM traverseAsgnM >=> traverseAsgnsM traverseAsgnM
traverseModuleItemM' :: ModuleItem -> ST ModuleItem traverseModuleItemM' :: ModuleItem -> ST ModuleItem
traverseModuleItemM' (Instance a b c d bindings) = do traverseModuleItemM' (Instance a b c d bindings) = do
...@@ -80,9 +80,9 @@ traverseModuleItemM' (Instance a b c d bindings) = do ...@@ -80,9 +80,9 @@ traverseModuleItemM' (Instance a b c d bindings) = do
traverseModuleItemM' other = return other traverseModuleItemM' other = return other
traverseStmtM :: Stmt -> ST Stmt traverseStmtM :: Stmt -> ST Stmt
traverseStmtM stmt = traverseStmtM =
traverseStmtLHSsM traverseLHSM stmt >>= traverseStmtLHSsM traverseLHSM >=>
traverseStmtExprsM traverseExprM >>= traverseStmtExprsM traverseExprM >=>
traverseStmtAsgnsM traverseAsgnM traverseStmtAsgnsM traverseAsgnM
traverseExprM :: Expr -> ST Expr traverseExprM :: Expr -> ST Expr
......
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