-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
125 lines (117 loc) · 4.13 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
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
import java.io.*;
import syntaxtree.*;
import symbol.*;
import visitor.*;
import java.util.regex.*;
// import typecheck.*;
import java.util.Scanner;
public class Main{
public static void main(String args[]){
lab4(args);
}
public static void lab1(String args[]){
try{
InputStream in = new FileInputStream(args[0]);
Node root = new MiniJavaParser(in).Goal();
MType allClassList = new MClassList();
root.accept(new BuildSymbolTableVisitor(), allClassList);
root.accept(new TypeCheckVisitor((MClassList)allClassList), allClassList);
System.out.println("Program type checked successfully");
}
catch(ParseException e){
e.printStackTrace();
}
catch(TokenMgrError e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void lab2(String args[]){
try{
InputStream in = new FileInputStream(args[0]);
Node root = new MiniJavaParser(in).Goal();
MType allClassList = new MClassList();
root.accept(new BuildSymbolTableVisitor(), allClassList);
root.accept(new TypeCheckVisitor((MClassList)allClassList), allClassList);
// ---piglet---
allClassList.classComplete();
allClassList.allocTemp(25);
// avoid overlap
//allClassList.printAll();
PrintStream out = new PrintStream(args[0].replace(".java", ".pg"));
MPiglet result = root.accept(new MiniJavaToPigletVistor((MClassList)allClassList), allClassList);
out.println(result.getCode().toString());
}
catch(ParseException e){
e.printStackTrace();
}
catch(TokenMgrError e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void lab3(String args[]){
try{
InputStream in = new FileInputStream(args[0]);
Node root = new PigletParser(in).Goal();
String pigletCode ="";
in = new FileInputStream(args[0]);
int size = in.available();
for (int i = 0; i < size; i++) {
pigletCode += ((char)in.read());
}
String patternString = "\\s*(TEMP)\\s*(\\d*)";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(pigletCode);
int mx = 0;
while(matcher.find()){
int num = Integer.valueOf(matcher.group(2).toString());
if(num > mx)
mx = num;
}
PigletToSpigletVisitor lab3 = new PigletToSpigletVisitor(mx+10);
MSpiglet result = root.accept(lab3);
PrintStream out = new PrintStream(args[0].replace(".pg", ".spg"));
out.println(result.getCode().toString());
}
catch(ParseException e){
e.printStackTrace();
}
catch(TokenMgrError e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void lab4(String args[]){
try{
InputStream in = new FileInputStream(args[0]);
PrintStream out = new PrintStream(args[0].replace(".spg", ".kg"));
Scanner sc = new Scanner(in);
String spgCode = "";
while(sc.hasNext()){
spgCode +=sc.nextLine()+"\n";
}
sc.close();
in = new ByteArrayInputStream(spgCode.getBytes());
MSpgProgram program = new MSpgProgram();
Node root = new SpigletParser(in).Goal();
root.accept(new BuildSpiglitTable(), program);
program.analyzeAll();
program.alloc();
root.accept(new SpiglitToKanga(), program);
out.print(program.code);
}
catch (TokenMgrError e) {
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}