-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunC.java
122 lines (108 loc) · 3.81 KB
/
RunC.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
package COSC1047.EditorPlus;
import java.io.File;
import java.io.IOException;
public class RunC {
RunC(){}
// FIXME: 2017-04-04
/** connect java compiler
* only on MacOS
* */
public static void compile(File path){
Runtime runtime = Runtime.getRuntime();
String cmd1 = "gcc -o "+path.getName().substring(0,path.getName().length()-4)+" "+path.getName();
String cmd2 = "cd "+path.toString().substring(0,path.toString().length()-path.getName().length());
String cmd3 = "./"+path.getName().substring(0,path.getName().length()-4);
String cmd = cmd2+"\n"+cmd1+"\n"+cmd3;
String applescriptCommand = "tell application \"Terminal\"\n" +
"do script \""+cmd+"\"" +
"activate\n" +
"end tell";
String[] args = { "osascript", "-e", applescriptCommand };
try {
Process process = runtime.exec(args);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* both java and cpp!!
* */
public static String shortcuts(String code){
String tmp = shortcutsJava(code);
if (tmp == null){
return shortcutsCpp(code);
}
return tmp;
}
// FIXME: 2017-04-04
/**
* shortcuts : to complete code
* */
public static String shortcutsJava(String code){
String[] lexemes = code.split("[ ,\n]");
for (String tmp: lexemes){
if (tmp.equals("#java")){
code = "public class ClassName{\n"+
"\tpublic static void main(String[] args){\n"+
"\t\n"+
"\t}\n"+
"}";
return code;
}
else if (tmp.matches("(.*)#prt(.*)")){
code = code.replaceAll("#prt","System.out.printf();");
return code;
}
else if (tmp.matches("(.*)#str(.*)")){
code = code.replaceAll("#str","String");
return code;
}
else if (tmp.matches("(.*)#scn(.*)")){
code = code.replaceAll("#scn","Scanner input = new Scanner(System.in);");
return code;
}
else if (tmp.matches("(.*)#impScanner(.*)")){
code = "import java.util.Scanner;\n"+code;
code = code.replaceAll("#impScanner","");
return code;
}
else if (tmp.matches("(.*)#impUtil(.*)")){
code = "import java.util.*;\n"+code;
code = code.replaceAll("#impUtil","");
return code;
}
else if (tmp.matches("(.*)#for(.*)")){
code = code.replaceAll("#for","for(int i=0; i<n; i++){\n\t\n}");
return code;
}
}
return null;
}
// FIXME: 2017-04-04
/** shortcuts for cpp */
public static String shortcutsCpp(String code){
String[] lexemes = code.split("[ ,\n]");
for (String tmp: lexemes){
if (tmp.equals("@cpp")){
code = "#include<iostream>\nusing namespace std;\n"+
"int main(){\n"+
"\treturn 0;\n"+
"}\n";
return code;
}
else if (tmp.matches("(.*)@cout(.*)")){
code = code.replaceAll("@cout","cout<< <<endl;");
return code;
}
else if (tmp.matches("(.*)@cin(.*)")){
code = code.replaceAll("#cin","cin>> ;");
return code;
}
else if (tmp.matches("(.*)#for(.*)")){
code = code.replaceAll("#for","for(int i=0; i<n; i++){\n\t\n}");
return code;
}
}
return null;
}
}