-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
62 lines (49 loc) · 1.65 KB
/
Main.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
import vtable.*;
import visitor.*;
import java.io.*;
import java.util.*;
import syntaxtree.*;
import symbol_table.*;
public class Main {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Usage: java Main file1 [file2 [file3 ...]]");
System.exit(1);
}
Goal root = null;
VTable vt = null;
SymbolTable st = null;
FileWriter writer = null;
FileInputStream stream = null;
for (String filename : args) {
try {
stream = new FileInputStream(filename);
root = new MiniJavaParser(stream).Goal();
System.err.println("Program " + filename + " parsed successfully!");
vt = new VTable();
st = new SymbolTable();
// Assumption: input files follow the format "name.java"
writer = new FileWriter(filename.substring(0, filename.lastIndexOf('.')) + ".ll");
root.accept(new STVisitor(st), null); // Populate the symbol table
root.accept(new SCVisitor(st), null); // Do static semantic checking
System.err.println("Program " + filename + " is semantically sound!");
System.err.println("Translating " + filename + " to LLVM IR");
root.accept(new VTVisitor(st, vt), null); // Configure vtable
root.accept(new CGVisitor(writer, st, vt), null); // Generate LLVM IR
System.out.println();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage() + "\n");
} finally {
try {
if (stream != null)
stream.close();
if (writer != null)
writer.close();
root = null; vt = null; st = null; writer = null; stream = null;
} catch(Exception e) {
System.err.println("Error: " + e.getMessage() + "\n");
}
}
}
}
}