-
Notifications
You must be signed in to change notification settings - Fork 1
/
Options.lhs
204 lines (189 loc) · 8.69 KB
/
Options.lhs
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
% -*- LaTeX -*-
% $Id: Options.lhs 3191 2016-01-18 20:47:45Z wlux $
%
% Copyright (c) 2001-2016, Wolfgang Lux
% See LICENSE for the full license.
%
\nwfilename{Options.lhs}
\section{Compiler options}
\begin{verbatim}
> module Options where
> import Files
> import GetOpt
\end{verbatim}
A record of type \texttt{Options} is used to gather the settings of
all compiler options.
\begin{verbatim}
> data Options =
> Options {
> importPaths :: [ImportPath], -- directories for searching imports
> output :: Maybe FilePath, -- name of output file
> link :: Bool, -- compile main function or goal
> goal :: Maybe String, -- goal to be evaluated
> typeIt :: Maybe String, -- goal to be typed
> noInterface :: Bool, -- do not create an interface file
> splitCode :: Bool, -- split object code
> debug :: Bool, -- add debugging transformation
> trusted :: Bool, -- trusted module for debugging
> caseMode :: CaseMode, -- case mode
> warn :: [Warn], -- warnings
> dump :: [Dump] -- dumps
> }
> deriving Show
> defaultOptions =
> Options{
> importPaths = [],
> output = Nothing,
> link = False,
> goal = Nothing,
> typeIt = Nothing,
> noInterface = False,
> splitCode = False,
> debug = False,
> trusted = False,
> caseMode = FreeMode,
> warn = [],
> dump = []
> }
> data CaseMode =
> FreeMode | HaskellMode | PrologMode | GoedelMode
> deriving (Eq,Show)
> data Warn =
> WarnUnusedData -- warnings for unused data constructors
> | WarnUnusedDecl -- warnings for unused declarations
> | WarnUnusedVar -- warnings for all unused variables
> | WarnShadow -- warnings for shadowing definitions
> | WarnOverlap -- warnings for overlapping patterns
> deriving (Eq,Bounded,Enum,Show)
> minUnused, maxUnused :: Warn
> minUnused = WarnUnusedData
> maxUnused = WarnUnusedVar
> data Dump =
> DumpRenamed -- dump source after renaming
> | DumpTypes -- dump types after typechecking
> | DumpDesugared -- dump source after desugaring
> | DumpUnlabeled -- dump source after removing labels
> | DumpNewtype -- dump source after removing newtypes
> | DumpUnlazy -- dump source after lifting lazy patt.
> | DumpFlatCase -- dump source after case flattening
> | DumpSimplified -- dump source after simplification
> | DumpPBU -- dump source with pattern updates
> | DumpLifted -- dump source after lambda-lifting
> | DumpIL -- dump IL code after translation
> | DumpTransformed -- dump transformed code
> | DumpNormalized -- dump IL code after normalization
> | DumpCam -- dump abstract machine code
> deriving (Eq,Bounded,Enum,Show)
\end{verbatim}
The \texttt{Option} type maps every command line switch on a data
constructor. This is needed in order to use the \texttt{GetOpt}
library.
\begin{verbatim}
> data Option =
> Help
> | ImportPath FilePath | LibPath FilePath | Output FilePath
> | Link | Eval String | Type String
> | SplitCode | NoInterface | Debug | Trusted
> | CaseMode CaseMode | Warn [Warn] | Dump [Dump]
> deriving (Eq,Show)
\end{verbatim}
The global variable \texttt{options} defines all options which are
recognized by the compiler.
\begin{verbatim}
> options = [
> Option "i" ["import-dir"] (ReqArg ImportPath "DIR")
> "search for imports in DIR",
> Option "P" ["lib-dir"] (ReqArg LibPath "DIR")
> "search for library interfaces in DIR",
> Option "" ["main"] (NoArg Link)
> "generate code for main function or goal",
> Option "e" ["eval"] (ReqArg Eval "GOAL")
> "generate code to evaluate GOAL",
> Option "t" ["type"] (ReqArg Type "GOAL")
> "compute type of GOAL",
> Option "o" ["output"] (ReqArg Output "FILE")
> "write code or type to FILE",
> Option "" ["no-icurry"] (NoArg NoInterface)
> "do not create an interface file",
> Option "" ["split-code"] (NoArg SplitCode)
> "emit one C file for each function",
> Option "g" ["debug"] (NoArg Debug)
> "transform code for debugging",
> Option "" ["trusted"] (NoArg Trusted)
> "trust this module (if compiled with -g/--debug)",
> Option "" ["haskell-mode"] (NoArg (CaseMode HaskellMode))
> "use Haskell naming conventions",
> Option "" ["prolog-mode"] (NoArg (CaseMode PrologMode))
> "use Prolog naming conventions",
> Option "" ["goedel-mode"] (NoArg (CaseMode GoedelMode))
> "use Goedel naming conventions",
> Option "" ["warn-all"] (NoArg (Warn [minBound..maxBound]))
> "enable all warnings",
> Option "" ["warn-unused"] (NoArg (Warn [minUnused..maxUnused]))
> "enable all unused warnings",
> Option "" ["warn-unused-data"] (NoArg (Warn [WarnUnusedData]))
> "enable unused data constructor warnings",
> Option "" ["warn-unused-decls"] (NoArg (Warn [WarnUnusedDecl]))
> "enable unused declaration warnings",
> Option "" ["warn-unused-vars"] (NoArg (Warn [WarnUnusedVar]))
> "enable unused variable warnings",
> Option "" ["warn-shadow"] (NoArg (Warn [WarnShadow]))
> "enable shadowing definitions warnings",
> Option "" ["warn-overlap"] (NoArg (Warn [WarnOverlap]))
> "enable overlapping patterns warnings",
> Option "" ["dump-all"] (NoArg (Dump [minBound..maxBound]))
> "dump everything",
> Option "" ["dump-renamed"] (NoArg (Dump [DumpRenamed]))
> "dump source code after renaming",
> Option "" ["dump-types"] (NoArg (Dump [DumpTypes]))
> "dump types after type-checking",
> Option "" ["dump-desugared"] (NoArg (Dump [DumpDesugared]))
> "dump source code after desugaring",
> Option "" ["dump-unlabeled"] (NoArg (Dump [DumpUnlabeled]))
> "dump source code after removing field labels",
> Option "" ["dump-newremoved"] (NoArg (Dump [DumpNewtype]))
> "dump source code after removing newtypes",
> Option "" ["dump-unlazy"] (NoArg (Dump [DumpUnlazy]))
> "dump source code after lifting lazy patterns",
> Option "" ["dump-flattened"] (NoArg (Dump [DumpFlatCase]))
> "dump source code after case flattening",
> Option "" ["dump-simplified"] (NoArg (Dump [DumpSimplified]))
> "dump source code after simplification",
> Option "" ["dump-pbu"] (NoArg (Dump [DumpPBU]))
> "dump source code with pattern binding updates",
> Option "" ["dump-lifted"] (NoArg (Dump [DumpLifted]))
> "dump source code after lambda-lifting",
> Option "" ["dump-il"] (NoArg (Dump [DumpIL]))
> "dump intermediate language before lifting",
> Option "" ["dump-transformed"] (NoArg (Dump [DumpTransformed]))
> "dump IL code after debugging transformation",
> Option "" ["dump-normalized"] (NoArg (Dump [DumpNormalized]))
> "dump IL code after normalization",
> Option "" ["dump-cam"] (NoArg (Dump [DumpCam]))
> "dump abstract machine code",
> Option "?h" ["help"] (NoArg Help)
> "display this help and exit"
> ]
\end{verbatim}
The function \texttt{selectOption} applies an \texttt{Option} to an
\texttt{Options} record. Note that there is no case for
\texttt{Help}. If the user asks for help, the compiler will simply
print its usage message and terminate.
\begin{verbatim}
> selectOption :: Option -> Options -> Options
> selectOption (ImportPath dir) opts =
> opts{ importPaths = ImpDir dir : importPaths opts }
> selectOption (LibPath dir) opts =
> opts{ importPaths = LibDir dir : importPaths opts }
> selectOption (Output file) opts = opts{ output = Just file }
> selectOption Link opts = opts{ link = True }
> selectOption (Eval goal) opts = opts{ link = True, goal = Just goal }
> selectOption (Type goal) opts = opts{ link = True, typeIt = Just goal }
> selectOption NoInterface opts = opts{ noInterface = True }
> selectOption SplitCode opts = opts{ splitCode = True }
> selectOption Debug opts = opts{ debug = True }
> selectOption Trusted opts = opts{ trusted = True }
> selectOption (CaseMode cm) opts = opts{ caseMode = cm }
> selectOption (Warn ws) opts = opts{ warn = ws ++ warn opts }
> selectOption (Dump ds) opts = opts{ dump = ds ++ dump opts }
\end{verbatim}