提交 dcbd0115 authored 作者: Thomas Mueller's avatar Thomas Mueller

A Java parser / Java to C converter.

上级 49953a3d
...@@ -34,6 +34,11 @@ public class ClassObj { ...@@ -34,6 +34,11 @@ public class ClassObj {
*/ */
boolean isPrimitive; boolean isPrimitive;
/**
* The primitive type (higher types are more complex)
*/
int primitiveType;
/** /**
* The imported classes. * The imported classes.
*/ */
...@@ -52,7 +57,7 @@ public class ClassObj { ...@@ -52,7 +57,7 @@ public class ClassObj {
/** /**
* The methods. * The methods.
*/ */
LinkedHashMap<String, MethodObj> methods = new LinkedHashMap<String, MethodObj>(); LinkedHashMap<String, ArrayList<MethodObj>> methods = new LinkedHashMap<String, ArrayList<MethodObj>>();
/** /**
* The list of native statements. * The list of native statements.
...@@ -64,16 +69,27 @@ public class ClassObj { ...@@ -64,16 +69,27 @@ public class ClassObj {
*/ */
int id; int id;
Type baseType;
ClassObj() {
baseType = new Type();
baseType.classObj = this;
}
/** /**
* Add a method. * Add a method.
* *
* @param method the method * @param method the method
*/ */
void addMethod(MethodObj method) { void addMethod(MethodObj method) {
if (methods.containsKey(method.name)) { ArrayList<MethodObj> list = methods.get(method.name);
throw new RuntimeException("Method overloading is not supported: " + method.name); if (list == null) {
list = new ArrayList<MethodObj>();
methods.put(method.name, list);
} else {
method.name = method.name + "_" + (list.size() + 1);
} }
methods.put(method.name, method); list.add(method);
} }
/** /**
...@@ -101,6 +117,38 @@ public class ClassObj { ...@@ -101,6 +117,38 @@ public class ClassObj {
return name; return name;
} }
String getMethodName(String find, ArrayList<Expr> args) {
ArrayList<MethodObj> list = methods.get(find);
if (list == null) {
return name;
}
if (list.size() == 1) {
return list.get(0).name;
}
for (MethodObj m : list) {
if (!m.isVarArgs && m.parameters.size() != args.size()) {
continue;
}
boolean match = true;
int i = 0;
for (FieldObj f : m.parameters.values()) {
Expr a = args.get(i++);
Type t = a.getType();
if (t == null) {
System.out.println(a.getType());
}
if (!t.equals(f.type)) {
match = false;
break;
}
}
if (match) {
return m.name;
}
}
return name;
}
} }
/** /**
...@@ -108,6 +156,8 @@ public class ClassObj { ...@@ -108,6 +156,8 @@ public class ClassObj {
*/ */
class MethodObj { class MethodObj {
boolean isVarArgs;
/** /**
* Whether this method is static. * Whether this method is static.
*/ */
...@@ -219,17 +269,19 @@ class Type { ...@@ -219,17 +269,19 @@ class Type {
/** /**
* The class. * The class.
*/ */
ClassObj type; ClassObj classObj;
/** /**
* The array nesting level. 0 if not an array. * The array nesting level. 0 if not an array.
*/ */
int arrayLevel; int arrayLevel;
boolean isVarArgs;
public String toString() { public String toString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(type.toString())); buff.append(JavaParser.toC(classObj.toString()));
if (!type.isPrimitive) { if (!classObj.isPrimitive) {
buff.append("*"); buff.append("*");
} }
for (int i = 0; i < arrayLevel; i++) { for (int i = 0; i < arrayLevel; i++) {
...@@ -237,5 +289,21 @@ class Type { ...@@ -237,5 +289,21 @@ class Type {
} }
return buff.toString(); return buff.toString();
} }
boolean isObject() {
return arrayLevel > 0 || !classObj.isPrimitive;
}
public int hashCode() {
return toString().hashCode();
}
public boolean equals(Object other) {
if (other instanceof Type) {
return this.toString().equals(other.toString());
}
return false;
}
} }
...@@ -27,6 +27,7 @@ class CallExpr implements Expr { ...@@ -27,6 +27,7 @@ class CallExpr implements Expr {
final boolean isStatic; final boolean isStatic;
final String className; final String className;
final String name; final String name;
ClassObj classObj;
CallExpr(JavaParser context, Expr expr, String className, String name, boolean isStatic) { CallExpr(JavaParser context, Expr expr, String className, String name, boolean isStatic) {
this.context = context; this.context = context;
...@@ -39,10 +40,12 @@ class CallExpr implements Expr { ...@@ -39,10 +40,12 @@ class CallExpr implements Expr {
public String toString() { public String toString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
if (className != null) { if (className != null) {
buff.append(JavaParser.toC(className + "." + name)).append("("); classObj = context.getClassObj(className);
} else { } else {
buff.append(JavaParser.toC(expr.getType().type + "." + name)).append("("); classObj = expr.getType().classObj;
} }
String methodName = classObj.getMethodName(name, args);
buff.append(JavaParser.toC(classObj.toString() + "." + methodName)).append("(");
int i = 0; int i = 0;
if (expr != null) { if (expr != null) {
buff.append(expr.toString()); buff.append(expr.toString());
...@@ -110,14 +113,25 @@ class ConditionalExpr implements Expr { ...@@ -110,14 +113,25 @@ class ConditionalExpr implements Expr {
class LiteralExpr implements Expr { class LiteralExpr implements Expr {
String literal; String literal;
private final JavaParser context;
private final String className;
private Type type;
public LiteralExpr(JavaParser context, String className) {
this.context = context;
this.className = className;
}
public String toString() { public String toString() {
return literal; return literal;
} }
public Type getType() { public Type getType() {
// TODO if (type == null) {
return null; type = new Type();
type.classObj = context.getClassObj(className);
}
return type;
} }
} }
...@@ -130,6 +144,11 @@ class OpExpr implements Expr { ...@@ -130,6 +144,11 @@ class OpExpr implements Expr {
Expr left; Expr left;
String op; String op;
Expr right; Expr right;
private final JavaParser context;
OpExpr(JavaParser context) {
this.context = context;
}
public String toString() { public String toString() {
if (left == null) { if (left == null) {
...@@ -140,13 +159,35 @@ class OpExpr implements Expr { ...@@ -140,13 +159,35 @@ class OpExpr implements Expr {
if (op.equals(">>>")) { if (op.equals(">>>")) {
// ujint / ujlong // ujint / ujlong
return "(((u" + left.getType() + ") " + left + ") >> " + right + ")"; return "(((u" + left.getType() + ") " + left + ") >> " + right + ")";
} else if (op.equals("+")) {
if (left.getType().isObject() || right.getType().isObject()) {
// TODO convert primitive to to String, call toString
return "STRING_CONCAT(" + left + ", " + right + ")";
}
} }
return "(" + left + " " + op + " " + right + ")"; return "(" + left + " " + op + " " + right + ")";
} }
public Type getType() { public Type getType() {
// TODO if (left == null) {
return null; return right.getType();
}
if (right == null) {
return left.getType();
}
if (op.equals("+")) {
if (left.getType().isObject() || right.getType().isObject()) {
Type t = new Type();
t.classObj = context.getClassObj("java.lang.String");
return t;
}
}
Type lt = left.getType();
Type rt = right.getType();
if (lt.classObj.primitiveType < rt.classObj.primitiveType) {
return rt;
}
return lt;
} }
} }
...@@ -184,7 +225,7 @@ class NewExpr implements Expr { ...@@ -184,7 +225,7 @@ class NewExpr implements Expr {
public Type getType() { public Type getType() {
Type t = new Type(); Type t = new Type();
t.type = type; t.classObj = type;
t.arrayLevel = arrayInitExpr.size(); t.arrayLevel = arrayInitExpr.size();
return t; return t;
} }
...@@ -201,13 +242,23 @@ class StringExpr implements Expr { ...@@ -201,13 +242,23 @@ class StringExpr implements Expr {
*/ */
String text; String text;
private final JavaParser context;
private Type type;
StringExpr(JavaParser context) {
this.context = context;
}
public String toString() { public String toString() {
return "STRING(\"" + javaEncode(text) + "\")"; return "STRING(\"" + javaEncode(text) + "\")";
} }
public Type getType() { public Type getType() {
// TODO if (type == null) {
return null; type = new Type();
type.classObj = context.getClassObj("java.lang.String");
}
return type;
} }
/** /**
...@@ -384,7 +435,7 @@ class ArrayAccessExpr implements Expr { ...@@ -384,7 +435,7 @@ class ArrayAccessExpr implements Expr {
public Type getType() { public Type getType() {
Type t = new Type(); Type t = new Type();
t.type = base.getType().type; t.classObj = base.getType().classObj;
t.arrayLevel = base.getType().arrayLevel - 1; t.arrayLevel = base.getType().arrayLevel - 1;
return t; return t;
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.java; package org.h2.java;
/** /**
* A test application. * A test application.
*/ */
......
...@@ -23,6 +23,31 @@ public class Arrays { ...@@ -23,6 +23,31 @@ public class Arrays {
} }
} }
/**
* Fill an array with the given value.
*
* @param array the array
* @param x the value
*/
public static void fill(byte[] array, byte x) {
for (int i = 0; i < array.length; i++) {
array[i] = x;
}
}
/**
* Fill an array with the given value.
*
* @param array the array
* @param x the value
*/
public static void fill(int[] array, int x) {
for (int i = 0; i < array.length; i++) {
array[i] = x;
}
}
/** /**
* Fill an array with the given value. * Fill an array with the given value.
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论