-
Notifications
You must be signed in to change notification settings - Fork 1
/
SedRunner.hs
86 lines (69 loc) · 2.82 KB
/
SedRunner.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module SedRunner (
useG4Types,
disableAssertions
) where
import System.IO
import System.Process
import System.Exit
data SedCommand = SedIntToG4Int
| SedFloatToG4Float
| SedDoubleToG4Double
| SedBoolToG4Bool
| SedCommentAsserts
| SedCommentIncludeCassert
| SedFixG4G4
| SedFixUnsignedG4Int
deriving (Show, Eq)
--toG4TypeRegexp t = ["\\'s/\\b\\(" ++ t ++"\\)\\b/G4\\1/g\'"]
toG4TypeRegexp :: String -> [String]
toG4TypeRegexp t = ["s/\\(\\b\\)" ++ t ++ "\\(\\b\\)/\\1G4" ++ t ++ "\\2/g"]
--let substitute x y s = subRegex (mkRegex x) s y
-- substitute "int" "G4int"
sedCommandArgs :: SedCommand -> [String]
sedCommandArgs SedIntToG4Int = toG4TypeRegexp "int"
sedCommandArgs SedFloatToG4Float = toG4TypeRegexp "float"
sedCommandArgs SedDoubleToG4Double = toG4TypeRegexp "double"
sedCommandArgs SedBoolToG4Bool = toG4TypeRegexp "bool"
sedCommandArgs SedCommentAsserts = ["s/^\\s*assert/\\/\\/ assert/g"]
sedCommandArgs SedCommentIncludeCassert = ["s/#include \\+<cassert>/\\/\\/ #include <cassert>/g"]
sedCommandArgs SedFixG4G4 = ["s/G4G4/G4/g"]
sedCommandArgs SedFixUnsignedG4Int = ["s/unsigned\\ G4int/unsigned\\ int/g"]
useG4Int :: String -> IO String
useG4Int = runSed SedIntToG4Int
useG4Float :: String -> IO String
useG4Float = runSed SedFloatToG4Float
useG4Double :: String -> IO String
useG4Double = runSed SedDoubleToG4Double
useG4Bool :: String -> IO String
useG4Bool = runSed SedBoolToG4Bool
commentAsserts :: String -> IO String
commentAsserts = runSed SedCommentAsserts
commentIncludeCassert :: String -> IO String
commentIncludeCassert = runSed SedCommentIncludeCassert
disableAssertions :: String -> IO String
disableAssertions code = (commentAsserts code) >>= commentIncludeCassert
fixG4G4 :: String -> IO String
fixG4G4 = runSed SedFixG4G4
fixUnsignedG4int :: String -> IO String
fixUnsignedG4int = runSed SedFixUnsignedG4Int
-- Chain the useG4<type> functions together.
useG4Types :: String -> IO String
--useG4Types code = (useG4Int code)
useG4Types code = (useG4Int code) >>= useG4Float >>= useG4Double >>= useG4Bool >>= fixG4G4 >>= fixUnsignedG4int
runSed :: SedCommand -> String -> IO String
runSed command inputData = do
let sedArgs = sedCommandArgs command
inPipe = CreatePipe
outPipe = CreatePipe
errPipe = CreatePipe
(Just hInput, Just hOutput, Just hError, procHandle) <- createProcess (proc "sed" sedArgs) {std_in = inPipe, std_out = outPipe, std_err = errPipe}
hPutStr hInput inputData
hClose hInput
exitCode <- waitForProcess procHandle
if exitCode /= ExitSuccess
then do msg <- hGetContents hError
hClose hOutput
error $ "Sed reported an error: " ++ msg
else do outputStr <- hGetContents hOutput
hClose hError
return outputStr