-
Notifications
You must be signed in to change notification settings - Fork 6
/
Driver.sv
139 lines (123 loc) · 4.66 KB
/
Driver.sv
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
grammar edu:umn:cs:melt:ableC:drivers:parseAndPrint ;
imports edu:umn:cs:melt:ableC:concretesyntax as cst;
imports edu:umn:cs:melt:ableC:abstractsyntax as abs;
imports silver:langutil;
imports silver:langutil:pp;
imports core:monad;
import edu:umn:cs:melt:ableC:abstractsyntax:env ; --only env, emptyEnv;
function driver
IOVal<Integer> ::= args::[String] ioIn::IO
theParser::(ParseResult<cst:Root>::=String String)
{
local fileName :: String = head(args);
local splitFileName :: Pair<String String> = splitFileNameAndExtension(fileName);
local baseFileName :: String = splitFileName.fst;
local cppFileName :: String = baseFileName ++ ".gen_cpp";
local ppFileName :: String = baseFileName ++ ".pp_out.c";
local partitionedArgs :: Pair<[String] [String]> = partition( partitionArg, tail(args) );
local cppArgs :: [String] = partitionedArgs.snd;
local xcArgs :: [String] = partitionedArgs.fst;
local cppOptions :: String = if length(args) >= 2 then implode(" ", cppArgs) else "" ;
local cppCmd :: String = "gcc -E -x c -D _POSIX_C_SOURCE -std=gnu1x -I . " ++ cppOptions;
local fullCppCmd :: String = cppCmd ++ " \"" ++ fileName ++ "\" > " ++ cppFileName;
local result::IOMonad<Integer> = do (bindIO, returnIO) {
if null(args) then {
printM("Usage: [ableC invocation] [file name] [c preprocessor arguments]\n");
return 5;
} else {
isF::Boolean <- isFileM(fileName);
if !isF then {
printM("File \"" ++ fileName ++ "\" not found.\n");
return 1;
} else {
if containsBy(stringEq, "--show-cpp", args) then
printM("CPP command: " ++ fullCppCmd ++ "\n");
mkCppFile::Integer <-
systemM(
fullCppCmd);
if mkCppFile != 0 then {
printM("CPP call failed: " ++ fullCppCmd ++ "\n");
return 3;
} else {
text :: String <- readFileM(cppFileName);
result :: ParseResult<cst:Root> = theParser(text, cppFileName);
if !result.parseSuccess then {
printM(result.parseErrors ++ "\n");
return 2;
} else {
comp :: Decorated abs:Compilation =
decorate abs:compilation(result.parseTree.ast) with {
env = addEnv( map(xcArgDef, xcArgs) , emptyEnv() );
};
if containsBy(stringEq, "--show-ast", args) then {
printM(substitute("edu:umn:cs:melt:", "", hackUnparse(comp.abs:srcAst)) ++ "\n");
return 0;
}
else if containsBy(stringEq, "--show-host-ast", args) then {
printM(substitute("edu:umn:cs:melt:", "", hackUnparse(comp.abs:hostAst)) ++ "\n");
return 0;
}
else if containsBy(stringEq, "--show-lifted-ast", args) then {
printM(substitute("edu:umn:cs:melt:", "", hackUnparse(comp.abs:liftedAst)) ++ "\n");
return 0;
}
else if containsBy(stringEq, "--show-pp", args) then {
printM(show(100, comp.abs:srcPP) ++ "\n");
return 0;
}
else if containsBy(stringEq, "--show-host-pp", args) then {
printM(show(100, comp.abs:hostPP) ++ "\n");
return 0;
}
else if containsBy(stringEq, "--show-lifted-pp", args) then {
printM(show(100, comp.abs:liftedPP) ++ "\n");
return 0;
}
else {
if !null(comp.errors) then
printM(messagesToString(comp.errors) ++ "\n");
if containsBy(stringEq, "--force-trans", args) || !containsErrors(comp.errors, false) then
writeFileM(ppFileName, show(80, comp.abs:finalPP));
if containsErrors(comp.errors, false) then
return 4;
else
return 0;
}
}
}
}
}
};
return evalIO(result, ioIn);
}
function partitionArg
Boolean ::= arg::String
{
return
arg=="--show-ast" ||
arg=="--show-host-ast" ||
arg=="--show-lifted-ast" ||
arg=="--show-pp" ||
arg=="--show-host-pp" ||
arg=="--show-lifted-pp" ||
arg=="--show-cpp" ||
arg=="--force-trans" ||
startsWith("--xc-", arg) ;
}
-- From an arg, create the environment Def that indicates its presence
-- Note that no value is stored in the env for this.
-- ToDo: Add ability to add a value for these xc arguments.
function xcArgDef
Def ::= arg::String
{ return miscDef(arg,emptyMiscItem()); }
{-
function removeNonCppArgs
[String] ::= args::[String]
{
return case args of
[] -> []
| "--show-ast" :: rest -> removeNonCppArgs(rest)
| arg :: rest -> arg :: removeNonCppArgs(rest)
end;
}
-}