-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.java
More file actions
90 lines (79 loc) · 2.77 KB
/
Compiler.java
File metadata and controls
90 lines (79 loc) · 2.77 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
import java.io.*;
import java.util.Scanner;
public class Compiler {
public void compile(String fileName){
String s;
Process p;
try {
p = Runtime.getRuntime().exec("javac " + fileName + ".java");
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println(s);
p.waitFor();
p.destroy();
} catch (Exception e) {}
}
public String run(String fileName){
String s;
String output = "";
Process r;
try {
r = Runtime.getRuntime().exec("java " + fileName);
BufferedReader br = new BufferedReader(
new InputStreamReader(r.getInputStream()));
while ((s = br.readLine()) != null){
output += (s + "\n");
}
r.waitFor();
r.destroy();
return output;
} catch (Exception e) {
return e.toString();
}
}
public String FindFileName(String fileText){
String fileName = new String();
boolean fileSwitch = false;
int nameSwitch = 0;
for(int i = 0; i <fileText.length(); i++){
char ch = fileText.charAt(i);
if(ch == 'p' && fileText.charAt(i+1) == 'u' && fileText.charAt(i+2) == 'b' && fileText.charAt(i+3) == 'l' && fileText.charAt(i+4) == 'i' && fileText.charAt(i+5) == 'c' && fileText.charAt(i+6) == ' ' && fileText.charAt(i+7) == 'c' && fileText.charAt(i+8) == 'l' && fileText.charAt(i+9) == 'a' && fileText.charAt(i+10) == 's' && fileText.charAt(i+11) == 's'){
fileSwitch = true;
}
if(fileSwitch == true){
if(nameSwitch >= 13 && fileText.charAt(i) != '{' && fileText.charAt(i) != ' '){
fileName += ch;
}
nameSwitch++;
if(fileText.charAt(i) == '{' || (nameSwitch > 13 && fileText.charAt(i) == ' '))
break;
}
}
return fileName;
}
public void deleteExecutable(String fileName){
Process r;
try {
r = Runtime.getRuntime().exec("rm " + fileName + ".class");
r.waitFor();
r.destroy();
} catch (Exception e) {
//return e.toString();
}
}
public static void main(String args[]) {
Compiler compiler = new Compiler();
String fileName = "CompilerTest";
try{
compiler.compile(fileName);
}
catch (Exception e){
System.out.println("Compilation error");
}
System.out.print(compiler.run(fileName));
String text = "import java.util.Stack;public class Format{public String formatText(String unformatted){";
System.out.println(compiler.FindFileName(text));
compiler.deleteExecutable(fileName);
}
}