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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
module PevalOpts
( Options (..), defaultOptions, Verbosity (..), Abstraction (..)
, Semantics (..), ProceedMode (..), ColorMode (..), getOpts
) where
import IO (hIsTerminalDevice, hPutStrLn, stderr, stdout)
import ReadShowTerm (readsQTerm)
import System (exitWith, getArgs, getProgName)
import GetOpt2 ( OptDescr (..), ArgDescr (..), ArgOrder (Permute), OptErr
, OptTable, addErr, getOpt, onOpts, option, usageInfo)
import Utils (rpad)
import Configuration
version :: String
version = unlines
[ "Curry Partial Evaluator"
, "Version 0.1 of 12/09/2016"
, "CAU Kiel"
]
data Options = Options
{ optHelp :: Bool
, optVersion :: Bool
, optDebug :: Bool
, optAssert :: Bool
, optClosed :: Bool
, optFunPats :: Bool
, optVerbosity :: Verbosity
, optColorMode :: ColorMode
, optSemantics :: Semantics
, optAbstract :: Abstraction
, optProceedMode :: ProceedMode
, optSuffix :: String
}
defaultOptions :: Options
defaultOptions = Options
{ optHelp = False
, optVersion = False
, optDebug = False
, optAssert = False
, optClosed = False
, optFunPats = True
, optVerbosity = Status
, optColorMode = CMAuto
, optSemantics = Natural
, optAbstract = None
, optProceedMode = PMOne
, optSuffix = "_pe"
}
data Verbosity = Quiet | Status | Info | Detail | Debug
verbosities :: [(Verbosity, String, String)]
verbosities = [ (Quiet , "0", "quiet" )
, (Status, "1", "show status" )
, (Info , "2", "show evaluation and abstraction")
, (Detail, "3", "show details of evaluation")
, (Debug , "4", "show information useful for debugging")
]
options :: [OptDescr (OptErr Options -> OptErr Options)]
options =
[ Option ['h', '?'] ["help" ]
(NoArg (onOpts $ \opts -> opts { optHelp = True }))
"show usage information"
, Option ['V'] ["version" ]
(NoArg (onOpts $ \opts -> opts { optVersion = True }))
"print the version information and exit"
, Option ['d'] ["debug" ]
(NoArg (onOpts $ \opts -> opts { optDebug = True }))
"print resulting program to standard output"
, Option [] ["assert" ]
(NoArg (onOpts $ \opts -> opts { optAssert = True }))
"check additional assertions during abstraction (slower)"
, Option [] ["closed" ]
(NoArg (onOpts $ \opts -> opts { optClosed = True }))
"preserve closedness by not removing copy functions"
, Option [] ["no-funpats" ]
(NoArg (onOpts $ \opts -> opts { optFunPats = False }))
"do not automatically optimize functions defined using functional patterns"
, option ['v'] ["verbosity" ] verbDescriptions "n"
"verbosity level"
, option [] ["color", "colour"] colorDescriptions "mode"
"coloring mode"
, option ['S'] ["semantics" ] semanticsDescriptions "semantics"
"semantics for evaluation"
, option ['A'] ["abstract" ] abstractDescriptions "mode"
"abstraction mode"
, option ['P'] ["proceed" ] proceedDescriptions "mode"
"proceed mode"
, Option [] ["suffix" ]
(ReqArg (\s -> onOpts $ \ opts -> opts { optSuffix = s }) "SUFFIX")
"specify the suffix for the specialised program"
]
verbDescriptions :: OptTable Options
verbDescriptions = map toDescr verbosities
where
toDescr (flag, name, desc) = (name, desc, set flag)
set f opts = opts { optVerbosity = f }
semanticsDescriptions :: OptTable Options
semanticsDescriptions = map toDescr semantics
where
toDescr (flag, name, desc) = (name, desc, set flag)
set s opts = opts { optSemantics = s }
abstractDescriptions :: OptTable Options
abstractDescriptions = map toDescr abstractions
where
toDescr (flag, name, desc) = (name, desc, set flag)
set a opts = opts { optAbstract = a }
proceedDescriptions :: OptTable Options
proceedDescriptions = map toDescr proceedModes
where
toDescr (flag, name, desc) = (name, desc, set flag)
set p opts = opts { optProceedMode = p }
colorDescriptions :: OptTable Options
colorDescriptions = map toDescr colorModes
where
toDescr (flag, name, desc) = (name, desc, set flag)
set c opts = opts { optColorMode = c }
getOpts :: IO (Options, [String])
getOpts = do
args <- getArgs
prog <- getProgName
parsed@(opts, _) <- processOpts prog $ parseOpts args
when (optVerbosity opts >= Status) (putStrLn version)
return parsed
parseOpts :: [String] -> (Options, [String], [String])
parseOpts args = (opts, files, errs ++ argErrs)
where
(opts, argErrs) = foldl (flip ($)) (defaultOptions, []) optErrs
(optErrs, files, errs) = getOpt Permute options args
processOpts :: String -> (Options, [String], [String])
-> IO (Options, [String])
processOpts prog (opts, files, errs)
| optHelp opts = printUsage prog
| optVersion opts = printVersion
| not (null errs') = badUsage prog errs'
| otherwise = do
opts' <- setColoring opts
return (opts', files)
where errs' = errs ++ checkOpts opts files
setColoring :: Options -> IO Options
setColoring opts
| optColorMode opts == CMAuto = do
isTerminal <- hIsTerminalDevice stdout
return opts { optColorMode = if isTerminal then CMAlways else CMNever }
| otherwise = return opts
checkOpts :: Options -> [String] -> [String]
checkOpts _ [] = ["no files"]
checkOpts _ (_:_) = []
printUsage :: String -> IO a
printUsage prog = do
putStrLn $ usageInfo header options
exitWith 0
where header = "usage: curry " ++ prog ++ " [OPTION] ... MODULE ..."
badUsage :: String -> [String] -> IO a
badUsage prog errs = do
mapIO_ (hPutStrLn stderr) errs
hPutStrLn stderr $ "Try '" ++ prog ++ " --help' for more information"
exitWith 1
printVersion :: IO a
printVersion = do
putStrLn version
exitWith 0
|