提交 167230d2 authored 作者: Thomas Mueller's avatar Thomas Mueller

Java to C++ converter

上级 142c2e4b
......@@ -267,9 +267,14 @@ class FieldObj {
Type type;
/**
* Whether this is a local field.
* Whether this is a variable or parameter.
*/
boolean isLocal;
boolean isVariable;
/**
* Whether this is a local field (not separately garbage collected).
*/
boolean isLocalField;
/**
* The field name.
......
......@@ -23,21 +23,19 @@ public interface Expr {
*/
class CallExpr implements Expr {
final JavaParser context;
final Expr expr;
final ArrayList<Expr> args = new ArrayList<Expr>();
final boolean isStatic;
final String className;
final String name;
private final JavaParser context;
private final String className;
private final String name;
private Expr expr;
private ClassObj classObj;
private MethodObj method;
CallExpr(JavaParser context, Expr expr, String className, String name, boolean isStatic) {
CallExpr(JavaParser context, Expr expr, String className, String name) {
this.context = context;
this.expr = expr;
this.className = className;
this.name = name;
this.isStatic = isStatic;
}
private void initMethod() {
......@@ -50,6 +48,9 @@ class CallExpr implements Expr {
classObj = expr.getType().classObj;
}
method = classObj.getMethod(name, args);
if (method.isStatic) {
expr = null;
}
}
public String toString() {
......@@ -165,7 +166,7 @@ class LiteralExpr implements Expr {
public String toString() {
if ("null".equals(literal)) {
return JavaParser.toCType(type) + "()";
return JavaParser.toCType(type, true) + "()";
}
return literal;
}
......@@ -301,7 +302,7 @@ class NewExpr implements Expr {
}
buff.append("))");
} else {
buff.append("new " + JavaParser.toC(classObj.toString()));
buff.append("ptr<" + JavaParser.toC(classObj.toString()) + ">(new " + JavaParser.toC(classObj.toString()));
buff.append("(");
int i = 0;
for (Expr a : args) {
......@@ -310,7 +311,7 @@ class NewExpr implements Expr {
}
buff.append(a);
}
buff.append(")");
buff.append("))");
}
return buff.toString();
}
......@@ -458,9 +459,6 @@ class VariableExpr implements Expr {
private void init() {
if (field == null) {
if (base == null) {
System.out.println("??");
}
Type t = base.getType();
if (t.arrayLevel > 0) {
if ("length".equals(name)) {
......
/*
* Copyright 2004-2011 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;
/**
* This annotation marks fields that are not shared and therefore don't need to
* be garbage collected separately.
*/
public @interface Local {
// empty
}
......@@ -44,7 +44,7 @@ class ReturnStatement extends StatementBase {
if (expr.getType().isSimplePrimitive()) {
return "return " + expr + ";";
}
return "return " + JavaParser.toCType(expr.getType()) + "(" + expr + ");";
return "return " + JavaParser.toCType(expr.getType(), true) + "(" + expr + ");";
}
}
......@@ -253,7 +253,7 @@ class VarDecStatement extends StatementBase {
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toCType(type)).append(' ');
buff.append(JavaParser.toCType(type, true)).append(' ');
StringBuilder assign = new StringBuilder();
for (int i = 0; i < variables.size(); i++) {
if (i > 0) {
......
......@@ -30,13 +30,16 @@ public class Test extends TestBase {
// chmod +x test
// ./test
// (for "mixed declarations and code")
// include files:
// /usr/include/c++/4.2.1/tr1/stdio.h
// /usr/include/stdio.h
// TODO initialize fields
// not supported yet:
// exceptions
// HexadecimalFloatingPointLiteral
// int x()[] { return null; }
// annotations
// import static
// import *
// initializer blocks
......@@ -66,6 +69,7 @@ public class Test extends TestBase {
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.Long");
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");
......
/*
/*
* Copyright 2004-2011 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.Long implementation.
*/
public class Long {
public static final long MIN_VALUE = 1L << 63;
public static final long MAX_VALUE = (1L << 63) - 1;
/**
* Convert a value to a String.
*
* @param x the value
* @return the String
*/
public static String toString(long x) {
// c: wchar_t ch[30];
// c: swprintf(ch, 30, L"%ld", x);
// c: return STRING(ch);
// c: return;
if (x == MIN_VALUE) {
return String.wrap("-9223372036854775808");
}
char[] ch = new char[30];
int i = 30 - 1, count = 0;
boolean negative;
if (x < 0) {
negative = true;
x = -x;
} else {
negative = false;
}
for (; i >= 0; i--) {
ch[i] = (char) ('0' + (x % 10));
x /= 10;
count++;
if (x == 0) {
break;
}
}
if (negative) {
ch[--i] = '-';
count++;
}
return new String(ch, i, count);
}
}
......@@ -7,6 +7,7 @@
package org.h2.java.lang;
import org.h2.java.Ignore;
import org.h2.java.Local;
/* c:
......@@ -37,7 +38,7 @@ protected:
jint refCount;
public:
RefBase() {
refCount = 1;
refCount = 0;
}
void reference() {
refCount++;
......@@ -47,6 +48,8 @@ public:
delete this;
}
}
virtual ~RefBase() {
}
};
template <class T> class ptr {
T* pointer;
......@@ -137,6 +140,7 @@ public class String {
/**
* The character array.
*/
@Local
char[] chars;
private int hash;
......@@ -152,18 +156,17 @@ public class String {
}
public int hashCode() {
if (hash == 0) {
if (chars.length == 0) {
return 0;
}
int h = 0;
for (char c : chars) {
h = h * 31 + c;
int h = hash;
if (h == 0) {
int size = chars.length;
if (size != 0) {
for (int i = 0; i < size; i++) {
h = h * 31 + chars[i];
}
hash = h;
return h;
}
return hash;
}
return h;
}
/**
......
......@@ -56,4 +56,21 @@ public class System {
java.lang.System.arraycopy(src, srcPos, dest, destPos, length);
}
/**
* Get the current time in milliseconds since 1970-01-01.
*
* @return the milliseconds
*/
public static long nanoTime() {
/* c:
#if CLOCKS_PER_SEC == 1000000
return (jlong) clock() * 1000;
#else
return (jlong) clock() * 1000000 / CLOCKS_PER_SEC;
#endif
*/
// c: return;
return java.lang.System.nanoTime();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论