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

A Java parser / Java to C converter.

上级 49953a3d
......@@ -34,6 +34,11 @@ public class ClassObj {
*/
boolean isPrimitive;
/**
* The primitive type (higher types are more complex)
*/
int primitiveType;
/**
* The imported classes.
*/
......@@ -52,7 +57,7 @@ public class ClassObj {
/**
* 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.
......@@ -64,16 +69,27 @@ public class ClassObj {
*/
int id;
Type baseType;
ClassObj() {
baseType = new Type();
baseType.classObj = this;
}
/**
* Add a method.
*
* @param method the method
*/
void addMethod(MethodObj method) {
if (methods.containsKey(method.name)) {
throw new RuntimeException("Method overloading is not supported: " + method.name);
ArrayList<MethodObj> list = methods.get(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 {
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 {
*/
class MethodObj {
boolean isVarArgs;
/**
* Whether this method is static.
*/
......@@ -219,17 +269,19 @@ class Type {
/**
* The class.
*/
ClassObj type;
ClassObj classObj;
/**
* The array nesting level. 0 if not an array.
*/
int arrayLevel;
boolean isVarArgs;
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(type.toString()));
if (!type.isPrimitive) {
buff.append(JavaParser.toC(classObj.toString()));
if (!classObj.isPrimitive) {
buff.append("*");
}
for (int i = 0; i < arrayLevel; i++) {
......@@ -237,5 +289,21 @@ class Type {
}
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 {
final boolean isStatic;
final String className;
final String name;
ClassObj classObj;
CallExpr(JavaParser context, Expr expr, String className, String name, boolean isStatic) {
this.context = context;
......@@ -39,10 +40,12 @@ class CallExpr implements Expr {
public String toString() {
StringBuilder buff = new StringBuilder();
if (className != null) {
buff.append(JavaParser.toC(className + "." + name)).append("(");
classObj = context.getClassObj(className);
} 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;
if (expr != null) {
buff.append(expr.toString());
......@@ -110,14 +113,25 @@ class ConditionalExpr implements Expr {
class LiteralExpr implements Expr {
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() {
return literal;
}
public Type getType() {
// TODO
return null;
if (type == null) {
type = new Type();
type.classObj = context.getClassObj(className);
}
return type;
}
}
......@@ -130,6 +144,11 @@ class OpExpr implements Expr {
Expr left;
String op;
Expr right;
private final JavaParser context;
OpExpr(JavaParser context) {
this.context = context;
}
public String toString() {
if (left == null) {
......@@ -140,13 +159,35 @@ class OpExpr implements Expr {
if (op.equals(">>>")) {
// ujint / ujlong
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 + ")";
}
public Type getType() {
// TODO
return null;
if (left == 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 {
public Type getType() {
Type t = new Type();
t.type = type;
t.classObj = type;
t.arrayLevel = arrayInitExpr.size();
return t;
}
......@@ -201,13 +242,23 @@ class StringExpr implements Expr {
*/
String text;
private final JavaParser context;
private Type type;
StringExpr(JavaParser context) {
this.context = context;
}
public String toString() {
return "STRING(\"" + javaEncode(text) + "\")";
}
public Type getType() {
// TODO
return null;
if (type == null) {
type = new Type();
type.classObj = context.getClassObj("java.lang.String");
}
return type;
}
/**
......@@ -384,7 +435,7 @@ class ArrayAccessExpr implements Expr {
public Type getType() {
Type t = new Type();
t.type = base.getType().type;
t.classObj = base.getType().classObj;
t.arrayLevel = base.getType().arrayLevel - 1;
return t;
}
......
......@@ -6,6 +6,7 @@
*/
package org.h2.java;
/**
* A test application.
*/
......
......@@ -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.
*
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论