-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvc.java
187 lines (166 loc) · 6.73 KB
/
vc.java
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
/*
* vc.java
*
* Jingling Xue, CSE, UNSW, Sydney NSW 2052, Australia.
*/
package VC;
import VC.Scanner.Scanner;
import VC.Scanner.SourceFile;
import VC.Parser.Parser;
import VC.ASTs.AST;
import VC.TreeDrawer.Drawer;
import VC.TreePrinter.Printer;
import VC.UnParser.UnParser;
import VC.Checker.Checker;
import VC.CodeGen.Emitter;
public class vc {
private static Scanner scanner;
private static ErrorReporter reporter;
private static Parser parser;
private static Drawer drawer;
private static Printer printer;
private static UnParser unparser;
private static Checker checker;
private static Emitter emitter;
private static int drawingAST = 0;
private static boolean printingAST = false;
private static boolean unparsingAST = false;
private static String inputFilename;
private static String astFilename = "";
private static String unparsingFilename = "";
private static AST theAST;
private static void cmdLineOptions() {
System.out.println("\nUsage: java VC.vc [-options] filename");
System.out.println();
System.out.println("where options include:");
System.out.println(" -d [1234] display the AST (without SourcePosition)");
System.out.println(" 1: the AST from the parser (without SourcePosition)");
System.out.println(" 2: the AST from the parser (with SourcePosition)");
System.out.println(" 3: the AST from the checker (without SourcePosition)");
System.out.println(" 4: the AST from the checker (with SourcePosition)");
System.out.println(" -t [file] print the (non-annotated) AST into <file>");
System.out.println(" (or filename + \"t\" if <file> is unspecified)");
System.out.println(" -u [file] unparse the (non-annotated) AST into <file>");
System.out.println(" (or filename + \"u\" if <file> is unspecified)");
System.exit(1);
}
public static void main(String[] args) {
int i = 0;
String arg;
System.out.println("======= The VC compiler =======\n");
while (i < args.length && args[i].startsWith("-")) {
arg = args[i++];
if (arg.startsWith("-d") && !arg.equals("-d")) {
int n = 0;
try {
n = Integer.parseInt(arg.substring(2));
} catch (NumberFormatException e) {
System.out.println("[# vc #]: invalid option " + arg);
cmdLineOptions();
}
if (1 <= n && n <= 4)
drawingAST = n;
else {
System.out.println("[# vc #]: invalid option " + arg);
cmdLineOptions();
}
} else if (arg.equals("-d")) {
if (i < args.length) {
if (args[i].equals("1")) {
drawingAST = 1;
i++;
} else if (args[i].equals("2")) {
drawingAST = 2;
i++;
} else if (args[i].equals("3")) {
drawingAST = 3;
i++;
} else if (args[i].equals("4")) {
drawingAST = 4;
i++;
} else {
System.out.println("[# vc #]: invalid option -d " + args[i]);
cmdLineOptions();
}
}
} else if (arg.startsWith("-t")) {
printingAST = true;
if (! arg.equals("-t"))
astFilename = arg.substring(2);
else if (i < args.length && !args[i].startsWith("-"))
astFilename = args[i++];
// the default is inputFilename + "p"
} else if (arg.startsWith("-u")) {
unparsingAST = true;
if (! arg.equals("-u"))
astFilename = arg.substring(2);
if (i < args.length && !args[i].startsWith("-"))
unparsingFilename = args[i++];
// the default is inputFilename + "u"
} else {
System.out.println("[# vc #]: invalid option " + arg);
cmdLineOptions();
}
}
if (i == args.length) {
System.out.println("[# vc #]: no input file");
cmdLineOptions();
} else
inputFilename = args[i];
SourceFile source = new SourceFile(inputFilename);
reporter = new ErrorReporter();
System.out.println("Pass 1: Lexical and syntactic Analysis");
scanner = new Scanner(source, reporter);
parser = new Parser(scanner, reporter);
theAST = parser.parseProgram();
if (reporter.numErrors == 0) {
// We unparse and print the AST created by the parser. Both
// are useful for debugging the construction of the AST
// (Assignment 3).
// We can also unparse and print the annotated AST from the
// checker but this is not as useful.
if (unparsingAST) {
if (unparsingFilename.equals(""))
unparsingFilename = inputFilename + "u";
unparser = new UnParser(unparsingFilename);
unparser.unparse(theAST);
System.out.println("[# vc #]: The unparsed VC program printed to " + unparsingFilename);
}
if (printingAST) {
if (astFilename.equals(""))
astFilename = inputFilename + "p";
printer = new Printer(astFilename);
printer.print(theAST);
System.out.println("[# vc #]: The linearised AST printed to " + astFilename);
}
if (1 <= drawingAST && drawingAST <= 2) {
drawer = new Drawer();
if (drawingAST == 2)
drawer.enableDebugging(); // show SourcePosition
drawer.draw(theAST); // draw the AST
}
System.out.println("Pass 2: Semantic Analysis");
checker = new Checker(reporter);
checker.check(theAST);
if (reporter.numErrors == 0) {
System.out.println("Pass 3: Code Generation");
System.out.println();
emitter = new Emitter(inputFilename, reporter);
emitter.gen(theAST);
if (reporter.numErrors == 0)
System.out.println ("Compilation was successful.");
else
System.out.println ("Compilation was unsuccessful.");
} else
System.out.println ("Compilation was unsuccessful.");
if (drawingAST >= 3) {
drawer = new Drawer();
if (drawingAST == 4)
drawer.enableDebugging(); // show SourcePosition
drawer.draw(theAST); // draw the AST
}
}
else
System.out.println ("Compilation was unsuccessful.");
}
}