-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
84 lines (61 loc) · 2.63 KB
/
Copy pathMain.java
File metadata and controls
84 lines (61 loc) · 2.63 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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import syntaxtree.Goal;
public class Main {
public static void main(String[] args) throws Exception {
if(args.length < 1){
System.err.println("Usage: java Main <inputFile>");
System.exit(1);
}
for(int i = 0; i < args.length;i++){
FileInputStream fis = null;
try{
fis = new FileInputStream(args[i]);
MiniJavaParser parser = new MiniJavaParser(fis);
String filename = args[i].split("\\.java")[0];
System.out.println(filename);
PrintStream outPrintStream = new PrintStream(filename+".ll");
Goal root = parser.Goal();
DeclarationsVisitor declarations = new DeclarationsVisitor();
SymbolTable symbolTable = new SymbolTable();
root.accept(declarations, symbolTable);
// symbolTable.print();
TypesVisitor typeCheck = new TypesVisitor();
root.accept(typeCheck, symbolTable);
for(Symbol s: symbolTable.peek().values()){
ClassDeclSymbol classSym = (ClassDeclSymbol)s;
int fieldOffset = 0; // computeClassSize(classSym.parentClass, symbolTable);
int methodOffset = 0;
for(Symbol field: classSym.fields.values()){
classSym.fieldOffset.put(field.id, fieldOffset);
fieldOffset += field.type.getSize();
}
classSym.size = fieldOffset;
for(Symbol method: classSym.methods.values()){
classSym.methodOffset.put(method.id, methodOffset);
methodOffset += 1;
}
}
GeneratorVisitor generator = new GeneratorVisitor(symbolTable, outPrintStream);
root.accept(generator, false);
symbolTable.exit();
}
catch(ParseException ex){
System.out.println(ex.getMessage());
}
catch(FileNotFoundException ex){
System.err.println(ex.getMessage());
}
finally{
try{
if(fis != null) fis.close();
}
catch(IOException ex){
System.err.println(ex.getMessage());
}
}
}
}
}