-
Notifications
You must be signed in to change notification settings - Fork 1
/
Options.hs
102 lines (89 loc) · 4.01 KB
/
Options.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module Options (Warnings(..), Options(..), Target(..), mkOptions) where
import Control.Monad.Instances()
import System.Console.GetOpt
import PrettyPrinter
data Warnings = Warnings
{ shadowing :: Bool
}
allWarnings :: Warnings
allWarnings = Warnings
{ shadowing = True
}
data Target = SSM | LLVM
deriving (Show, Eq)
data Options = Options
{ astPrinter :: Printer (IO ())
, showInput :: Bool
, showLexingResult :: Bool
, showParsingResult :: Bool
, showScopingResult :: Bool
, showTypingResult :: Bool
, showIR :: Bool
, showStages :: Bool
, lexOnly :: Bool
, parseOnly :: Bool
, scopeOnly :: Bool
, typeOnly :: Bool
, forceCodegen :: Bool
, enabledWarnings :: Warnings
, target :: Target
, outputFile :: String
}
defaultOptions :: Options
defaultOptions = Options
{ astPrinter = coloredPrettyPrinter
, showInput = False
, showLexingResult = False
, showParsingResult = False
, showScopingResult = False
, showTypingResult = False
, showIR = False
, showStages = False
, lexOnly = False
, parseOnly = False
, scopeOnly = False
, typeOnly = False
, forceCodegen = False
, enabledWarnings = allWarnings
, target = SSM
, outputFile = ""
}
warningsOptions :: String -> Warnings -> Warnings
warningsOptions "no-shadow" w = w { shadowing = False }
warningsOptions _ w = w
-- Apply changes on Warnings to Options
liftW :: (Warnings -> Warnings) -> (Options -> Options)
liftW f o = o { enabledWarnings = f (enabledWarnings o) }
targetOptions :: String -> Target -> Target
targetOptions "ssm" _ = SSM
targetOptions "llvm" _ = LLVM
targetOptions _ _ = error "Unrecognized target specification"
liftT :: (Target -> Target) -> (Options -> Options)
liftT f o = o { target = f (target o) }
options :: [OptDescr (Options -> Options)]
options =
[ Option [] ["colored"] (NoArg (\o -> o { astPrinter = coloredPrettyPrinter })) "prints the AST with colored text"
, Option [] ["plain"] (NoArg (\o -> o { astPrinter = plainPrettyPrinter })) "prints the AST in plain text"
, Option [] ["minimizer"] (NoArg (\o -> o { astPrinter = miniPrettyPrinter })) "prints the AST without tabs and newlines"
, Option [] ["show-input"] (NoArg (\o -> o { showInput = True })) "shows the input-file"
, Option [] ["show-lexing"] (NoArg (\o -> o { showLexingResult = True })) "shows the in-between lexing result"
, Option [] ["show-parsing"] (NoArg (\o -> o { showParsingResult = True })) "prettyprints the AST after parsing"
, Option [] ["show-scoping"] (NoArg (\o -> o { showScopingResult = True })) "prettyprints the AST after scoping"
, Option [] ["show-typing"] (NoArg (\o -> o { showTypingResult = True })) "prettyprints the AST after typing"
, Option [] ["show-ir"] (NoArg (\o -> o { showIR = True })) "prints the IR (both plain and canonical)"
, Option [] ["show-stages"] (NoArg (\o -> o { showStages = True })) "show stages during compilation"
, Option [] ["lex-only"] (NoArg (\o -> o { lexOnly = True })) "stops after the lexing pass"
, Option [] ["parse-only"] (NoArg (\o -> o { parseOnly = True })) "stops after the parsing pass"
, Option [] ["scope-only"] (NoArg (\o -> o { scopeOnly = True })) "stops after the scoping pass"
, Option [] ["type-only"] (NoArg (\o -> o { typeOnly = True })) "stops after the typing pass"
, Option [] ["force-codegen"] (NoArg (\o -> o { forceCodegen = True })) "will generate code, even when there are (non-fatal) errors in analysis"
, Option "W" [] (ReqArg (fmap liftW warningsOptions) "warning") "Controls warnings (eg: -Wno-shadow), all warnings are enable by default"
, Option [] ["target"] (ReqArg (fmap liftT targetOptions) "target") "Specifies target (eg: --target llvm), default is ssm"
, Option "o" [] (ReqArg (\s o -> o { outputFile = s }) "filename") "Specifies output file (don't specify for stdout)"
]
mkOptions :: [String] -> IO (Options, [String])
mkOptions argv =
case getOpt Permute options argv of
(o, n, [] ) -> return (foldl (flip id) defaultOptions o, n)
(_, _, errs) -> ioError (userError (concat errs ++ usageInfo header options))
where header = "splang [OPTION...] file"