Rain: fixed the constant folding pass to work with constant sub-expressions

This commit is contained in:
Neil Brown 2007-09-15 10:26:52 +00:00
parent e35873f01c
commit 989f12b051
2 changed files with 12 additions and 4 deletions

View File

@ -48,13 +48,19 @@ recordInfNameTypes = everywhereM (mkM recordInfNameTypes')
return input
recordInfNameTypes' r = return r
-- | Folds all constants.
constantFoldPass :: Data t => t -> PassM t
constantFoldPass = doGeneric `extM` doExpression
everywhereASTM :: (Data s, Data t) => (s -> PassM s) -> t -> PassM t
everywhereASTM f = doGeneric `extM` (doSpecific f)
where
doGeneric :: Data t => t -> PassM t
doGeneric = makeGeneric constantFoldPass
doGeneric = makeGeneric (everywhereASTM f)
doSpecific :: Data t => (t -> PassM t) -> t -> PassM t
doSpecific f x = (doGeneric x >>= f)
-- | Folds all constants.
constantFoldPass :: Data t => t -> PassM t
constantFoldPass = everywhereASTM doExpression
where
doExpression :: A.Expression -> PassM A.Expression
doExpression = (liftM (\(x,_,_) -> x)) . constantFold

View File

@ -35,6 +35,8 @@ constantFoldTest = TestList
,foldCon 100 (lit 2) (Dy (lit 1) A.Plus (lit 1))
,foldCon 101 (lit 65537) (Dy (lit 2) A.Plus (lit 65535))
,foldCon 102 (lit (- two63)) (Dy (lit $ two63 - 1) A.Plus (lit 1))
,foldCon 110 (Dy (Var "x") A.Plus (lit 2)) (Dy (Var "x") A.Plus (Dy (lit 1) A.Plus (lit 1)))
]
where
two63 :: Integer