module Language.Fortran.Transformation.Grouping ( groupIf
, groupDo
, groupLabeledDo
, groupCase
) where
import Language.Fortran.AST
import Language.Fortran.Analysis
import Language.Fortran.Transformation.TransformMonad
import Debug.Trace
genericGroup :: ([ Block (Analysis a) ] -> [ Block (Analysis a) ]) -> Transform a ()
genericGroup groupingFunction = do
modifyProgramFile $ \ (ProgramFile pus e) -> ProgramFile (zip (map fst pus) . map (go . snd) $ pus) e
where
go pu =
case pu of
PUMain a s n bs subPUs ->
PUMain a s n (groupingFunction bs) (map go <$> subPUs)
PUModule a s n bs subPUs ->
PUModule a s n (groupingFunction bs) (map go <$> subPUs)
PUSubroutine a s r n as bs subPUs ->
PUSubroutine a s r n as (groupingFunction bs) (map go <$> subPUs)
PUFunction a s r rec n as res bs subPUs ->
PUFunction a s r rec n as res (groupingFunction bs) (map go <$> subPUs)
bd@PUBlockData{} -> bd
groupIf :: Transform a ()
groupIf = genericGroup groupIf'
groupIf' :: [ Block (Analysis a) ] -> [ Block (Analysis a) ]
groupIf' [] = []
groupIf' (b:bs) = b' : bs'
where
(b', bs') = case b of
BlStatement a s label st
| StIfThen{} <- st ->
let ( conditions, blocks, leftOverBlocks ) =
decomposeIf (b:groupedBlocks)
in ( BlIf a (getTransSpan s blocks) label conditions blocks
, leftOverBlocks)
b | containsGroups b ->
( applyGroupingToSubblocks groupIf' b, groupedBlocks )
_ -> ( b, groupedBlocks )
groupedBlocks = groupIf' bs
decomposeIf :: [ Block (Analysis a) ] -> ([ Maybe (Expression (Analysis a)) ], [ [ Block (Analysis a) ] ], [ Block (Analysis a) ])
decomposeIf blocks@(BlStatement _ _ _ (StIfThen _ _ mTargetName _):rest) =
decomposeIf' blocks
where
decomposeIf' (BlStatement _ _ _ st:rest) =
case st of
StIfThen _ _ _ condition -> go (Just condition) rest
StElsif _ _ _ condition -> go (Just condition) rest
StElse{} -> go Nothing rest
StEndif _ _ mName
| mName == mTargetName -> ([], [], rest)
| otherwise -> error $ "If statement name does not match that of " ++
"the corresponding end if statement."
_ -> error "Block with non-if related statement. Should never occur."
go maybeCondition blocks =
let (nonConditionBlocks, rest') = collectNonConditionalBlocks blocks
(conditions, listOfBlocks, rest'') = decomposeIf' rest'
in ( maybeCondition : conditions
, nonConditionBlocks : listOfBlocks
, rest'' )
collectNonConditionalBlocks :: [ Block (Analysis a) ] -> ([ Block (Analysis a) ], [ Block (Analysis a) ])
collectNonConditionalBlocks blocks =
case blocks of
BlStatement _ _ _ StElsif{}:_ -> ([], blocks)
BlStatement _ _ _ StElse{}:_ -> ([], blocks)
b@(BlStatement _ _ _ StEndif{}):_ -> ([ b ], blocks)
b:bs -> let (bs', rest) = collectNonConditionalBlocks bs in (b : bs', rest)
_ -> error "Premature file ending while parsing structured if block."
groupDo :: Transform a ()
groupDo = genericGroup groupDo'
groupDo' :: [ Block (Analysis a) ] -> [ Block (Analysis a) ]
groupDo' [ ] = [ ]
groupDo' blocks@(b:bs) = b' : bs'
where
(b', bs') = case b of
BlStatement a s label st
| StDoWhile _ _ mNameTarget _ condition <- st ->
let ( blocks, leftOverBlocks ) =
collectNonDoBlocks groupedBlocks mNameTarget
in ( BlDoWhile a (getTransSpan s blocks) label condition blocks
, leftOverBlocks)
| StDo _ _ mNameTarget Nothing doSpec <- st ->
let ( blocks, leftOverBlocks ) =
collectNonDoBlocks groupedBlocks mNameTarget
in ( BlDo a (getTransSpan s blocks) label doSpec blocks
, leftOverBlocks)
b | containsGroups b ->
( applyGroupingToSubblocks groupDo' b, groupedBlocks )
_ -> ( b, groupedBlocks )
groupedBlocks = groupDo' bs
collectNonDoBlocks :: [ Block (Analysis a) ] -> Maybe String -> ([ Block (Analysis a)], [ Block (Analysis a) ])
collectNonDoBlocks blocks mNameTarget =
case blocks of
b@(BlStatement _ _ _ (StEnddo _ _ mName)):rest
| mName == mNameTarget -> ([ b ], rest)
| otherwise ->
error "Do block name does not match that of the end statement."
b:bs ->
let (bs', rest) = collectNonDoBlocks bs mNameTarget in (b : bs', rest)
_ -> error "Premature file ending while parsing structured do block."
groupLabeledDo :: Transform a ()
groupLabeledDo = genericGroup groupLabeledDo'
groupLabeledDo' :: [ Block (Analysis a) ] -> [ Block (Analysis a) ]
groupLabeledDo' [ ] = [ ]
groupLabeledDo' blos@(b:bs) = b' : bs'
where
(b', bs') = case b of
BlStatement a s label (StDo _ _ _ (Just (ExpValue _ _ (ValInteger targetLabel))) doSpec) ->
let ( blocks, leftOverBlocks ) =
collectNonLabeledDoBlocks targetLabel groupedBlocks
in ( BlDo a (getTransSpan s blocks) label doSpec blocks
, leftOverBlocks)
b | containsGroups b ->
( applyGroupingToSubblocks groupLabeledDo' b, groupedBlocks )
_ -> (b, groupedBlocks)
groupedBlocks = groupLabeledDo' bs
collectNonLabeledDoBlocks :: String -> [ Block (Analysis a) ] -> ([ Block (Analysis a) ], [ Block (Analysis a) ])
collectNonLabeledDoBlocks targetLabel blocks =
case blocks of
b@(BlStatement _ _ (Just (ExpValue _ _ (ValInteger label))) _):rest
| label == targetLabel -> ([ b ], rest)
b:bs -> let (bs', rest) = collectNonLabeledDoBlocks targetLabel bs in (b : bs', rest)
groupCase :: Transform a ()
groupCase = genericGroup groupCase'
groupCase' :: [ Block (Analysis a) ] -> [ Block (Analysis a) ]
groupCase' [] = []
groupCase' (b:bs) = b' : bs'
where
(b', bs') = case b of
BlStatement a s label st
| StSelectCase _ _ mTargetName scrutinee <- st ->
let blocksToDecomp = dropWhile isComment groupedBlocks
( conds, blocks, leftOverBlocks ) = decomposeCase blocksToDecomp mTargetName
in ( BlCase a (getTransSpan s blocks) label scrutinee conds blocks
, leftOverBlocks)
b | containsGroups b ->
( applyGroupingToSubblocks groupCase' b, groupedBlocks )
_ -> ( b , groupedBlocks )
groupedBlocks = groupCase' bs
isComment b = case b of { BlComment{} -> True; _ -> False }
decomposeCase :: [ Block (Analysis a) ] -> Maybe String -> ([ Maybe (AList Index (Analysis a)) ], [ [ Block (Analysis a) ] ], [ Block (Analysis a) ])
decomposeCase blocks@(BlStatement _ _ _ st:rest) mTargetName =
case st of
StCase _ _ mName mCondition
| Nothing <- mName -> go mCondition rest
| mName == mTargetName -> go mCondition rest
| otherwise -> error $ "Case name does not match that of " ++
"the corresponding select case statement."
StEndcase _ _ mName
| mName == mTargetName -> ([], [], rest)
| otherwise -> error $ "End case name does not match that of " ++
"the corresponding select case statement."
_ -> error "Block with non-case related statement. Must not occur."
where
go mCondition blocks =
let (nonCaseBlocks, rest) = collectNonCaseBlocks blocks
(conditions, listOfBlocks, rest') = decomposeCase rest mTargetName
in ( mCondition : conditions
, nonCaseBlocks : listOfBlocks
, rest' )
collectNonCaseBlocks :: [ Block (Analysis a) ] -> ([ Block (Analysis a) ], [ Block (Analysis a) ])
collectNonCaseBlocks blocks =
case blocks of
b@(BlStatement _ _ _ st):_
| StCase{} <- st -> ( [], blocks )
| StEndcase{} <- st -> ( [ b ], blocks )
b:bs -> let (bs', rest) = collectNonCaseBlocks bs in (b : bs', rest)
_ -> error "Premature file ending while parsing select case block."
containsGroups :: Block (Analysis a) -> Bool
containsGroups b =
case b of
BlStatement{} -> False
BlIf{} -> True
BlCase{} -> True
BlDo{} -> True
BlDoWhile{} -> True
BlInterface{} -> False
BlComment{} -> False
applyGroupingToSubblocks :: ([ Block (Analysis a) ] -> [ Block (Analysis a) ]) -> Block (Analysis a) -> Block (Analysis a)
applyGroupingToSubblocks f b
| BlStatement{} <- b =
error "Individual statements do not have subblocks. Must not occur."
| BlIf a s l conds blocks <- b = BlIf a s l conds $ map f blocks
| BlCase a s l scrutinee conds blocks <- b =
BlCase a s l scrutinee conds $ map f blocks
| BlDo a s l doSpec blocks <- b = BlDo a s l doSpec $ f blocks
| BlDoWhile a s l doSpec blocks <- b = BlDoWhile a s l doSpec $ f blocks
| BlInterface{} <- b =
error "Interface blocks do not have groupable subblocks. Must not occur."
| BlComment{} <- b =
error "Comment statements do not have subblocks. Must not occur."