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
|
module ICurry.Options
where
import Control.Monad ( when, unless )
import Data.List ( union )
import Numeric ( readNat )
import System.Console.GetOpt
import qualified Data.Map as Map
import FlatCurry.Types ( QName )
import ICurry.Types ( IArity )
import System.CurryPath ( currySubdir )
import System.Directory ( getAbsolutePath )
import System.FrontendExec ( FrontendParams, defaultParams, setQuiet )
import System.Process ( exitWith )
data ICOptions = ICOptions
{ optVerb :: Int
, optHelp :: Bool
, optLift :: Bool
, optOutput :: String
, optMain :: String
, optShowGraph :: Int
, optViewPDF :: String
, optInteractive :: Bool
, optVarDecls :: Bool
, optFrontendParams :: FrontendParams
, optModsMaps :: [String]
, optConsMap :: [(String, Map.Map String (IArity,Int))]
, optFunMap :: [(String, Map.Map String Int)]
, optFun :: QName
}
defaultICOptions :: ICOptions
defaultICOptions =
ICOptions 1 False True "" "" 0 "evince" False False
(setQuiet True defaultParams) [] [] [] ("","")
setConsFuns :: ICOptions -> [(String, [(QName,(IArity,Int))])]
-> [(String, [(QName,Int)])] -> ICOptions
setConsFuns opts modconslist modfunlist =
opts { optConsMap = foldr addIfNotPresent (optConsMap opts) modconslist
, optFunMap = foldr addIfNotPresent (optFunMap opts) modfunlist
, optModsMaps = union (map fst modconslist)
(union (map fst modfunlist) (optModsMaps opts))
}
where
addIfNotPresent (mn,nameinfos) infomap =
if mn `elem` optModsMaps opts
then infomap
else foldr addQMap infomap nameinfos
addQMap :: (QName,a) -> [(String, Map.Map String a)]
-> [(String, Map.Map String a)]
addQMap ((mn,fn),i) [] = [(mn, Map.singleton fn i)]
addQMap (qn@(mn,fn),i) ((m,mmap):mmaps) =
if mn == m then (m, Map.insert fn i mmap) : mmaps
else (m,mmap) : addQMap (qn,i) mmaps
qmapLookup :: QName -> [(String, Map.Map String a)] -> Maybe a
qmapLookup (mn,fn) mmap =
maybe Nothing
(\fm -> Map.lookup fn fm)
(lookup mn mmap)
arityPosOfCons :: ICOptions -> QName -> (IArity,Int)
arityPosOfCons opts qn =
maybe (funError opts $ "Internal error in ICurry.Compiler:\n" ++
"arity of constructor " ++ showQName qn ++ " is unknown")
id
(qmapLookup qn (optConsMap opts))
posOfCons :: ICOptions -> QName -> Int
posOfCons opts qn = snd (arityPosOfCons opts qn)
posOfFun :: ICOptions -> QName -> Int
posOfFun opts qn =
maybe (funError opts $ "Internal error in ICurry.Compiler:\n" ++
"arity of operation " ++ showQName qn ++ " is unknown")
id
(qmapLookup qn (optFunMap opts))
printStatus :: ICOptions -> String -> IO ()
printStatus opts s = when (optVerb opts > 0) $ putStrLn s
printIntermediate :: ICOptions -> String -> IO ()
printIntermediate opts s = when (optVerb opts > 1) $ putStrLn s
printDetails :: ICOptions -> String -> IO ()
printDetails opts s = when (optVerb opts > 2) $ putStrLn s
funError :: ICOptions -> String -> _
funError opts err = error $ "Function '" ++ snd (optFun opts) ++ "': " ++ err
processOptions :: String -> [String] -> IO (ICOptions,[String])
processOptions banner argv = do
let (funopts, args, opterrors) = getOpt Permute options argv
opts = foldl (flip id) defaultICOptions funopts
unless (null opterrors)
(putStr (unlines opterrors) >> printUsage >> exitWith 1)
when (optHelp opts) (printUsage >> exitWith 0)
when (not (null (optMain opts)) && not (optLift opts)) $ error
"Incompatible options: interpreter requires case/let lifting!"
let out = optOutput opts
opts1 <- if null out || out == "-" then return opts
else do aout <- getAbsolutePath out
return opts { optOutput = aout }
return (opts1, args)
where
printUsage = putStrLn (banner ++ "\n" ++ usageText)
usageText :: String
usageText = usageInfo ("Usage: icurry [options] <module name>\n") options
options :: [OptDescr (ICOptions -> ICOptions)]
options =
[ Option "h?" ["help"]
(NoArg (\opts -> opts { optHelp = True }))
"print help and exit"
, Option "q" ["quiet"]
(NoArg (\opts -> opts { optVerb = 0 }))
"run quietly (no output, only exit code)"
, Option "v" ["verbosity"]
(OptArg (maybe (checkVerb 2) (safeReadNat checkVerb)) "<n>")
"verbosity level:\n0: quiet (same as `-q')\n1: show status messages (default)\n2: show generated program (same as `-v')\n3: show all details"
, Option "o" ["output"]
(ReqArg (\s opts -> opts { optOutput = s }) "<f>")
("output file for ICurry program (or '-')\n(otherwise: store in " ++
currySubdir ++ "/MOD.icy)\nor PDF containing term graphs (with option '-g')")
, Option "m" ["main"]
(ReqArg (\s opts -> opts { optMain = s }) "<f>")
"name of the main function to be interpreted\n(otherwise the ICurry program is stored)"
, Option "g" ["graph"]
(OptArg (maybe (checkGraph 1) (safeReadNat checkGraph)) "<n>")
("level to visualize term graph during execution:\n" ++
"0: do not show term graph\n" ++
"1: show term graph (same as `-g`)\n (requires 'dot' and '" ++
viewer ++ "')\n" ++
"2: show full term graph\n3: show full graph with node IDs")
, Option "" ["viewer"]
(ReqArg (\s opts -> opts { optViewPDF = s }) "<c>")
("command to view PDF files (default: '" ++ viewer ++ "')")
, Option "i" ["interactive"]
(NoArg (\opts -> opts { optInteractive = True }))
"interactive execution (ask after each step/result)"
, Option "" ["nolifting"]
(NoArg (\opts -> opts { optLift = False }))
"do not lift nested case/let expressions"
, Option "" ["optvardecls"]
(NoArg (\opts -> opts { optVarDecls = True }))
"do not generate variable declarations when\nvariables are introduced by assignments"
]
where
viewer = optViewPDF defaultICOptions
safeReadNat opttrans s opts = case readNat s of
[(n,"")] -> opttrans n opts
_ -> error "Illegal number argument (try `-h' for help)"
checkVerb n opts = if n>=0 && n<4
then opts { optVerb = n }
else error "Illegal verbosity level (try `-h' for help)"
checkGraph n opts = if n>=0 && n<4
then opts { optShowGraph = n }
else error "Illegal graph level (try `-h' for help)"
showQName :: QName -> String
showQName (mn,fn) = mn ++ "." ++ fn
|