提交 24ed565b authored 作者: Thomas Mueller's avatar Thomas Mueller

Java to C converter.

上级 92a8cfac
......@@ -28,7 +28,7 @@ public class ClassObj {
/**
* The fully qualified class name.
*/
String name;
String className;
/**
* Whether this is an interface.
......@@ -126,9 +126,9 @@ public class ClassObj {
public String toString() {
if (isPrimitive) {
return "j" + name;
return "j" + className;
}
return name;
return className;
}
/**
......@@ -141,7 +141,7 @@ public class ClassObj {
MethodObj getMethod(String find, ArrayList<Expr> args) {
ArrayList<MethodObj> list = methods.get(find);
if (list == null) {
throw new RuntimeException("Method not found: " + name);
throw new RuntimeException("Method not found: " + className + " " + find);
}
if (list.size() == 1) {
return list.get(0);
......@@ -155,9 +155,6 @@ public class ClassObj {
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;
......@@ -167,7 +164,17 @@ public class ClassObj {
return m;
}
}
throw new RuntimeException("Method not found: " + name);
throw new RuntimeException("Method not found: " + className);
}
/**
* Get the field with the given name.
*
* @param name the field name
* @return the field
*/
FieldObj getField(String name) {
return instanceFields.get(name);
}
}
......
......@@ -27,7 +27,8 @@ class CallExpr implements Expr {
final boolean isStatic;
final String className;
final String name;
ClassObj classObj;
private ClassObj classObj;
private MethodObj method;
CallExpr(JavaParser context, Expr expr, String className, String name, boolean isStatic) {
this.context = context;
......@@ -37,19 +38,26 @@ class CallExpr implements Expr {
this.isStatic = isStatic;
}
public String toString() {
StringBuilder buff = new StringBuilder();
private void initMethod() {
if (method != null) {
return;
}
if (className != null) {
classObj = context.getClassObj(className);
} else {
classObj = expr.getType().classObj;
}
MethodObj m = classObj.getMethod(name, args);
method = classObj.getMethod(name, args);
}
public String toString() {
StringBuilder buff = new StringBuilder();
String methodName;
if (m.isVirtual) {
methodName = "virtual_" + m.name + "[CLASS_ID("+expr.toString()+")]";
initMethod();
if (method.isVirtual) {
methodName = "virtual_" + method.name + "[CLASS_ID("+expr.toString()+")]";
} else {
methodName = JavaParser.toC(classObj.toString() + "." + m.name);
methodName = JavaParser.toC(classObj.toString() + "." + method.name);
}
buff.append(methodName).append("(");
int i = 0;
......@@ -68,8 +76,8 @@ class CallExpr implements Expr {
}
public Type getType() {
// TODO
return null;
initMethod();
return method.returnType;
}
}
......@@ -168,12 +176,34 @@ class OpExpr implements Expr {
} else if (op.equals("+")) {
if (left.getType().isObject() || right.getType().isObject()) {
// TODO convert primitive to to String, call toString
return "STRING_CONCAT(" + left + ", " + right + ")";
StringBuilder buff = new StringBuilder();
buff.append("java_lang_StringBuilder_toString(");
buff.append("java_lang_StringBuilder_append(");
buff.append("java_lang_StringBuilder_init_obj(");
buff.append(convertToString(left));
buff.append("), ");
buff.append(convertToString(right));
buff.append("))");
return buff.toString();
}
}
return "(" + left + " " + op + " " + right + ")";
}
private String convertToString(Expr e) {
Type t = e.getType();
if (t.arrayLevel > 0) {
return e.toString() + ".toString()";
}
if (t.classObj.isPrimitive) {
ClassObj wrapper = context.getWrapper(t.classObj);
return JavaParser.toC(wrapper + ".toString") + "(" + e.toString() + ")";
} else if (e.getType().toString().equals("java_lang_String*")) {
return e.toString();
}
return e.toString() + ".toString()";
}
public Type getType() {
if (left == null) {
return right.getType();
......@@ -203,14 +233,20 @@ class OpExpr implements Expr {
*/
class NewExpr implements Expr {
ClassObj type;
ClassObj classObj;
ArrayList<Expr> arrayInitExpr = new ArrayList<Expr>();
ArrayList<Expr> args = new ArrayList<Expr>();
final JavaParser context;
NewExpr(JavaParser context) {
this.context = context;
}
public String toString() {
StringBuilder buff = new StringBuilder();
if (arrayInitExpr.size() > 0) {
if (type.isPrimitive) {
buff.append("NEW_ARRAY(sizeof(" + type + ")");
if (classObj.isPrimitive) {
buff.append("NEW_ARRAY(sizeof(" + classObj + ")");
buff.append(", 1 ");
for (Expr e : arrayInitExpr) {
buff.append("* ").append(e);
......@@ -224,14 +260,23 @@ class NewExpr implements Expr {
buff.append(")");
}
} else {
buff.append("NEW_OBJ(" + type.id + ", " + JavaParser.toC(type.toString()) + ")");
MethodObj m = classObj.getMethod("init_obj", args);
buff.append(JavaParser.toC(classObj.toString() + "." + m.name)).append("(");
int i = 0;
for (Expr a : args) {
if (i++ > 0) {
buff.append(", ");
}
buff.append(a);
}
buff.append(")");
}
return buff.toString();
}
public Type getType() {
Type t = new Type();
t.classObj = type;
t.classObj = classObj;
t.arrayLevel = arrayInitExpr.size();
return t;
}
......@@ -330,10 +375,16 @@ class VariableExpr implements Expr {
Expr base;
FieldObj field;
String name;
private final JavaParser context;
VariableExpr(JavaParser context) {
this.context = context;
}
public String toString() {
init();
StringBuilder buff = new StringBuilder();
if (field != null && "length".equals(field.name) && base != null && base.getType() != null && base.getType().arrayLevel > 0) {
if ("length".equals(name) && base.getType().arrayLevel > 0) {
buff.append("LENGTH(");
buff.append(base.toString());
buff.append(")");
......@@ -354,10 +405,24 @@ class VariableExpr implements Expr {
return buff.toString();
}
public Type getType() {
private void init() {
if (field == null) {
return null;
Type t = base.getType();
if (t.arrayLevel > 0) {
if ("length".equals(name)) {
field = new FieldObj();
field.type = context.getClassObj("int").baseType;
} else {
throw new RuntimeException("Unknown array method: " + name);
}
} else {
field = t.classObj.getField(name);
}
}
}
public Type getType() {
init();
return field.type;
}
......
......@@ -60,6 +60,9 @@ public class Test extends TestBase {
JavaParser parser = new JavaParser();
parser.parse("src/tools/org/h2", "java.lang.Object");
parser.parse("src/tools/org/h2", "java.lang.String");
parser.parse("src/tools/org/h2", "java.lang.Math");
parser.parse("src/tools/org/h2", "java.lang.Integer");
parser.parse("src/tools/org/h2", "java.lang.StringBuilder");
parser.parse("src/tools/org/h2", "java.io.PrintStream");
parser.parse("src/tools/org/h2", "java.lang.System");
parser.parse("src/tools/org/h2", "java.util.Arrays");
......
......@@ -28,42 +28,4 @@ int main(int argc, char** argv) {
System.out.println("Hello " + "World" + 1);
}
public int hashCode() {
return 1;
}
/**
* A test method.
*
* @param name ignored
* @param x ignored
* @return ignored
*/
public int getName(int name, int x) {
System.out.println("Hello");
int m = x;
// m = FINAL_VALUE;
switch (x) {
case 1:
m = 3;
m = 4;
break;
default:
m = 4;
m = 5;
}
for (int i = 0; i < 10; i++, i--) {
getName(0, 0);
}
if (m > 0) {
getName(2, 3);
} else {
getName(1, 12);
}
do {
getName(0, 0);
return name;
} while (true);
}
}
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.java.lang;
/**
* A java.lang.Integer implementation.
*/
public class Integer {
/**
* Convert a value to a String.
*
* @param x the value
* @return the String
*/
public static String toString(int x) {
// c: char ch[20];
// c: snprintf(ch, 20, "%d", x);
// c: return string(ch);
return null;
}
}
......@@ -115,6 +115,11 @@ void* string(char* s) {
System.arraycopy(chars, 0, this.chars, 0, chars.length);
}
public String(char[] chars, int offset, int count) {
this.chars = new char[count];
System.arraycopy(chars, offset, this.chars, 0, count);
}
public int hashCode() {
if (hashCode == 0) {
if (chars.length == 0) {
......
......@@ -14,24 +14,47 @@ public class StringBuilder {
private int length;
private char[] buffer;
public StringBuilder(String s) {
char[] chars = s.chars;
int len = chars.length;
buffer = new char[len];
System.arraycopy(chars, 0, buffer, 0, len);
this.length = len;
}
public StringBuilder() {
buffer = new char[10];
}
/**
* Append the given string.
* Append the given value.
*
* @param s the string
* @param x the value
* @return this
*/
public StringBuilder append(String s) {
int l = s.length();
public StringBuilder append(String x) {
int l = x.length();
ensureCapacity(l);
System.arraycopy(s.chars, 0, buffer, length, l);
System.arraycopy(x.chars, 0, buffer, length, l);
length += l;
return this;
}
/**
* Append the given value.
*
* @param x the value
* @return this
*/
public StringBuilder append(int x) {
append(Integer.toString(x));
return this;
}
public java.lang.String toString() {
return new java.lang.String(buffer, 0, length);
}
private void ensureCapacity(int plus) {
if (buffer.length < length + plus) {
char[] b = new char[Math.max(length + plus, buffer.length * 2)];
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论