-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompiler.java
145 lines (115 loc) · 5.19 KB
/
Compiler.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.io.IOException;
import java.io.StringWriter;
import java.io.File;
import java.io.FileWriter;
public class Compiler {
public static void compileFunction(Parser P) {
StringWriter str = new StringWriter();
CompilerContext context = new CompilerContext();
boolean isBivariate = P.argList.containsKey("y") ? true : false;
String classname = getClassName(P);
System.out.println("classname="+classname);
if ((new File("bin/" + classname + ".class")).exists() == true) return;
System.out.println("compiling new file!");
str.write( "public class " + classname + " extends Function {\n\n");
for (String var : P.argList.keySet()) {
str.write( " double " + var + ";\n");
}
str.write( "\n");
str.write( " public " + classname + "() {\n"
+" super();\n"
+" }\n\n"
+" public boolean isBivariate() {\n"
+" return " + isBivariate + ";\n"
+" }\n\n"
+" public double value(double... point) {\n");
if (P.argList.containsKey("x")) {
str.write( " x = point[0];\n");
}
if (P.argList.containsKey("y")) {
str.write( " y = point[1];\n");
}
str.write( " return ");
P.root.codeGen(str, context);
str.write(";\n");
str.write(" }\n\n");
while (context.nodev.size() != 0) {
System.out.println("got here");
Node node = context.nodev.remove(0);
String name = context.namev.remove(0);
str.write( " public double " + name + "() {\n");
int startbit = node instanceof ProdNode? 1 : 0;
str.write( " double result = " + startbit + ";\n");
if (node instanceof RexNode) {
str.write( " double v = ");
node.getArgExpr().codeGen(str, context);
str.write( ";\n"
+" for (int rex_index_var = 0; "
+ "rex_index_var < " + node.getLimit() + "; "
+ "rex_index_var++) {\n"
+" result += Math.cos( Math.log(v) * Function.zeta_zeros[rex_index_var] );\n"
+" }\n"
+" return (1 - 2*result/Math.sqrt(v) - 1 / (v*v*v-v));\n"
+" }\n\n");
}
else {
str.write( " double prev = " + node.getVar() + ";\n");
char opbit = node instanceof SumNode ? '+' : '*';
str.write(
" for (" + node.getVar() + " = " + node.getStart() + "; "
+ node.getVar() +" <= " + node.getLimit() + "; "
+ node.getVar() + "++) {\n"
+" result " + opbit + "= (");
node.getArgExpr().codeGen(str, context);
str.write( ");\n"
+" }\n"
+" "+node.getVar() + " = prev;\n"
+" return result;\n"
+" }\n\n"
);
}
}
str.write( " public static void main(String[] args) {\n"
+" " + classname + " func = new " + classname + "();\n"
+" if (args.length == 1) System.out.println(func.value(Double.parseDouble(args[0])));\n"
+" if (args.length == 2) System.out.println(func.value(Double.parseDouble(args[0]),\n"
+" Double.parseDouble(args[1])));\n"
+" }\n\n"
+"}"
);
try {
//File compilesrc = new File("compilesrc");
//compilesrc.mkdir();
//compilesrc.deleteOnExit();
File tmpfile = new File("compilesrc/" + classname + ".java");
//tmpfile.deleteOnExit();
FileWriter fr = new FileWriter(tmpfile);
fr.write(str.toString());
fr.close();
}
catch (NullPointerException e1) {
e1.printStackTrace();
}
catch (IOException e2) {
e2.printStackTrace();
}
try {
String[] cmdArray = new String[4];
cmdArray[0] = "javac";
cmdArray[1] = "-d";
cmdArray[2] = "bin";
cmdArray[3] = "compilesrc/" + classname + ".java";
Process process = Runtime.getRuntime().exec(cmdArray, null);
process.waitFor();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String getClassName(Parser P) {
String exprStr = P.root.toString();
return exprStr.hashCode() < 0
? "C_m" + String.valueOf(Math.abs(exprStr.hashCode()))
: "C_" + String.valueOf(exprStr.hashCode());
}
}