Clean up some remaining messy code after the passes rework.

This commit is contained in:
Adam Sampson 2008-06-02 17:04:30 +00:00
parent 13668f6e32
commit f811d2cdc9
2 changed files with 42 additions and 40 deletions

View File

@ -100,21 +100,24 @@ runPassR p t
Right result -> mapM_ warnReport w >> return result Right result -> mapM_ warnReport w >> return result
runPassM :: CompState -> PassM a -> IO (Either ErrorReport a, CompState, [WarningReport]) runPassM :: CompState -> PassM a -> IO (Either ErrorReport a, CompState, [WarningReport])
runPassM cs pass = liftM flatten $ flip runStateT [] $ flip runStateT cs $ runErrorT pass runPassM cs pass
= liftM flatten $ flip runStateT [] $ flip runStateT cs $ runErrorT pass
where where
flatten :: ((a, b), c) -> (a, b, c) flatten :: ((a, b), c) -> (a, b, c)
flatten ((x, y), z) = (x, y, z) flatten ((x, y), z) = (x, y, z)
enablePassesWhen :: (CompState -> Bool) -> [Pass] -> [Pass] enablePassesWhen :: (CompState -> Bool) -> [Pass] -> [Pass]
enablePassesWhen f = map (\p -> p enablePassesWhen f
{passEnabled = \c -> f c && (passEnabled p c)}) = map (\p -> p { passEnabled = \c -> f c && (passEnabled p c) })
-- | A helper to run a pass at the top-level, or deliver an error otherwise -- | A helper to run a pass at the top-level, or deliver an error otherwise
passOnlyOnAST :: forall t. Data t => String -> (A.AST -> PassM A.AST) -> t -> PassM t passOnlyOnAST :: forall t. Data t => String -> (A.AST -> PassM A.AST) -> t -> PassM t
passOnlyOnAST name func x passOnlyOnAST name func x
= case cast x :: Maybe A.AST of = case cast x :: Maybe A.AST of
Nothing -> dieP emptyMeta $ name ++ " only operates at top-level" Nothing -> dieP emptyMeta $ name ++ " only operates at top-level"
Just x' -> func x' >>= \y -> case cast y :: Maybe t of Just x' ->
do y <- func x'
case cast y :: Maybe t of
Nothing -> dieP emptyMeta $ name ++ " crazy cast error at top-level" Nothing -> dieP emptyMeta $ name ++ " crazy cast error at top-level"
Just y' -> return y' Just y' -> return y'
@ -141,7 +144,6 @@ cOnlyPass = passMakerHelper $ (== BackendC) . csBackend
cppOnlyPass :: PassMaker cppOnlyPass :: PassMaker
cppOnlyPass = passMakerHelper $ (== BackendCPPCSP) . csBackend cppOnlyPass = passMakerHelper $ (== BackendCPPCSP) . csBackend
pass :: String -> [Property] -> [Property] -> (forall t. Data t => t -> PassM t) -> Pass pass :: String -> [Property] -> [Property] -> (forall t. Data t => t -> PassM t) -> Pass
pass name pre post code pass name pre post code
= Pass { passCode = code = Pass { passCode = code