tools/unitCycles.hs
author unC0Rr
Thu, 18 Nov 2010 11:50:02 +0300
changeset 4376 9654205a9424
parent 4367 f4a0ec067601
child 4382 935de6cd5ea3
permissions -rw-r--r--
Move SendStats to uIO
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     1
module Main where
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     2
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     3
import PascalParser
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     4
import System
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     5
import Control.Monad
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     6
import Data.Either
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     7
import Data.List
4355
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
     8
import Data.Graph
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     9
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    10
unident :: Identificator -> String
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    11
unident (Identificator s) = s
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    12
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    13
extractUnits :: PascalUnit -> (String, [String])
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    14
extractUnits (Program (Identificator name) (Implementation (Uses idents) _ _) _) = ("program " ++ name, map unident idents)
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    15
extractUnits (Unit (Identificator name) (Interface (Uses idents1) _) (Implementation (Uses idents2) _ _) _ _) = (name, map unident $ idents1 ++ idents2)
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    16
4355
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    17
f :: [(String, [String])] -> String
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    18
f = unlines . map showSCC . stronglyConnComp . map (\(a, b) -> (a, a, b))
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    19
    where
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    20
    showSCC (AcyclicSCC v) = v
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    21
    showSCC (CyclicSCC vs) = intercalate ", " vs
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    22
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    23
main = do
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    24
    fileNames <- getArgs
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    25
    files <- mapM readFile fileNames
4355
4554c4df9f1a A program which finds a cycles in units dependencies
unC0Rr
parents: 4353
diff changeset
    26
    putStrLn . f . map extractUnits . rights . map parsePascalUnit $ files