-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.java
More file actions
102 lines (87 loc) · 3.48 KB
/
Compiler.java
File metadata and controls
102 lines (87 loc) · 3.48 KB
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
import java.io.*;
import java.nio.file.*;
import ast.PPVisitor;
import ast.Program;
import ast.TypeCheckVisitor;
import ast.BaseULException;
import ast.IRGenVisitor;
import ast.IRProgram;
import types.*;
import types.BoolType;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import jvmgen.ULJVMGenerator;
public class Compiler {
public static void main (String[] args) throws Exception {
ANTLRInputStream input;
FileInputStream fp;
if (args.length == 0 ) {
System.out.println("Usage: Test filename.ul");
return;
} else {
fp = new FileInputStream(args[0]);
input = new ANTLRInputStream(fp);
}
ULLexer lexer = new ULLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ULParser parser = new ULParser(tokens);
Program prog = null;
try {
prog = (Program)parser.program();
} catch (RecognitionException e) {
// This exception is silenced for reasonable debugging Output
// when run with the enclosed runtests.py script. see the README.
// System.out.println("Error: could not 'compile' grammar. Aborting.");
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
//PPVisitor visitor = new PPVisitor();
//visitor.visit(prog);
TypeCheckVisitor tcvisitor = new TypeCheckVisitor(
parser._float, parser._int, parser._bool, parser._char, parser._str, parser._void);
try {
tcvisitor.verify(prog);
} catch (BaseULException e) {
String line = getFailureLine(e.lineCrashed, args[0]);
String msg = String.format("%s:%s:Error:%s: %s",
args[0], String.valueOf(e.lineCrashed), e.getClass().getSimpleName(), e.msg);
System.out.println(msg);
System.out.println(String.format("Line %s:%s", String.valueOf(e.lineCrashed), line));
return;
}
Path p = Paths.get(args[0]);
String className = p.getFileName().toString();
IRGenVisitor irvisitor = new IRGenVisitor(
parser._float, parser._int, parser._bool, parser._char, parser._str, parser._void, className);
try {
irvisitor.gen(prog);
} catch(BaseULException e) {
System.out.println("An error occured when generating the IR");
e.printStackTrace();
}
String iRRepresentation = irvisitor.getIRRepresenation(className);
ultype type_package = new ultype(
parser._float, parser._int, parser._bool, parser._char, parser._str, parser._void);
IRProgram irP = irvisitor.getIRProgram();
ULJVMGenerator uljvmgen = new ULJVMGenerator(irP, args[0], className, type_package);
uljvmgen.generate();
if (args.length > 1 && "--compare".equals(args[1])) {
System.out.println(iRRepresentation);
System.out.println(" -- converted to -- ");
}
System.out.println(uljvmgen.toString());
}
private static String getFailureLine(int linenum, String fname) {
String line = null;
try (BufferedReader br = new BufferedReader(new FileReader(fname))) {
for (int i = 0; i < linenum - 1; i++)
br.readLine();
line = br.readLine();
} catch (IOException e) {
// quality engineering
}
return line;
}
}