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

A Java parser / Java to C converter.

上级 4ead520a
......@@ -54,6 +54,8 @@ public class ClassObj {
*/
LinkedHashMap<String, MethodObj> methods = new LinkedHashMap<String, MethodObj>();
ArrayList<Statement> nativeInitializers = new ArrayList<Statement>();
/**
* Add a method.
*
......@@ -134,6 +136,11 @@ class MethodObj {
* Whether this method is public.
*/
boolean isPublic;
/**
* Whether this method is native.
*/
boolean isNative;
}
/**
......@@ -146,6 +153,11 @@ class FieldObj {
*/
Type type;
/**
* Whether this is a local field.
*/
boolean isLocal;
/**
* The field name.
*/
......@@ -194,13 +206,13 @@ class Type {
int arrayLevel;
public String toString() {
if (arrayLevel == 0) {
return type.name;
}
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(type.name));
if (!type.isPrimitive) {
buff.append("*");
}
for (int i = 0; i < arrayLevel; i++) {
buff.append("[]");
buff.append("*");
}
return buff.toString();
}
......
......@@ -13,17 +13,21 @@ import java.util.ArrayList;
*/
public interface Expr {
// toString
Type getType();
}
/**
* A method call.
*/
class CallExpr implements Expr {
String object;
Expr expr;
String name;
ArrayList<Expr> args = new ArrayList<Expr>();
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(object)).append("(");
buff.append(expr.toString() + "_" + JavaParser.toC(name)).append("(");
int i = 0;
for (Expr a : args) {
if (i > 0) {
......@@ -34,12 +38,17 @@ class CallExpr implements Expr {
}
return buff.append(")").toString();
}
public Type getType() {
// TODO
return null;
}
}
/**
* A assignment expression.
*/
class AssignExpr implements Expr {
Expr left;
String op;
Expr right;
......@@ -47,36 +56,60 @@ class AssignExpr implements Expr {
public String toString() {
return left + " " + op + " " + right;
}
public Type getType() {
// TODO
return null;
}
}
/**
* A conditional expression.
*/
class ConditionalExpr implements Expr {
Expr condition;
Expr ifTrue, ifFalse;
public String toString() {
return condition + " ? " + ifTrue + " : " + ifFalse;
}
public Type getType() {
// TODO
return null;
}
}
/**
* A literal.
*/
class LiteralExpr implements Expr {
String literal;
public String toString() {
return literal;
}
public Type getType() {
// TODO
return null;
}
}
/**
* An operation.
*/
class OpExpr implements Expr {
Expr left;
String op;
Expr right;
public String toString() {
if (left == null) {
return op + right;
......@@ -85,16 +118,38 @@ class OpExpr implements Expr {
}
return left + " " + op + " " + right;
}
public Type getType() {
// TODO
return null;
}
}
/**
* A "new" expression.
*/
class NewExpr implements Expr {
String className;
ClassObj type;
ArrayList<Expr> arrayInitExpr = new ArrayList<Expr>();
public String toString() {
return "new " + className;
StringBuilder buff = new StringBuilder();
buff.append("new " + type);
for (Expr e : arrayInitExpr) {
buff.append("[").append(e).append("]");
}
return buff.toString();
}
public Type getType() {
Type t = new Type();
t.type = type;
t.arrayLevel = arrayInitExpr.size();
return t;
}
}
/**
......@@ -111,6 +166,11 @@ class StringExpr implements Expr {
return "\"" + javaEncode(text) + "\"";
}
public Type getType() {
// TODO
return null;
}
/**
* Encode the String to Java syntax.
*
......@@ -165,3 +225,57 @@ class StringExpr implements Expr {
return buff.toString();
}
}
/**
* A variable.
*/
class VariableExpr implements Expr {
Expr base;
FieldObj field;
String name;
public String toString() {
StringBuilder buff = new StringBuilder();
if (base != null) {
buff.append(base.toString()).append("->");
}
if (field != null) {
buff.append(field.name);
} else {
buff.append(JavaParser.toC(name));
}
return buff.toString();
}
public Type getType() {
if (field == null) {
return null;
}
return field.type;
}
}
/**
* A array access expression.
*/
class ArrayExpr implements Expr {
Expr obj;
ArrayList<Expr> indexes = new ArrayList<Expr>();
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(obj.toString());
for (Expr e : indexes) {
buff.append('[').append(e.toString()).append(']');
}
return buff.toString();
}
public Type getType() {
return obj.getType();
}
}
......@@ -136,9 +136,18 @@ class ForStatement implements Statement {
Expr update;
Statement block;
ArrayList<Expr> updates = new ArrayList<Expr>();
Type iterableType;
String iterableVariable;
Expr iterable;
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append("for (").append(init.toString());
buff.append("for (");
if (iterableType != null) {
buff.append(iterableType).append(' ');
buff.append(iterableVariable).append(": ");
buff.append(iterable);
} else {
buff.append(init.toString());
buff.append(" ").append(condition.toString()).append("; ");
for (int i = 0; i < updates.size(); i++) {
if (i > 0) {
......@@ -146,6 +155,7 @@ class ForStatement implements Statement {
}
buff.append(updates.get(i));
}
}
buff.append(") {\n");
buff.append(block.toString()).append("}");
return buff.toString();
......@@ -192,11 +202,12 @@ class VarDecStatement implements Statement {
}
/**
* A variable.
* A native statement.
*/
class VariableExpr implements Expr {
public String name;
class StatementNative implements Statement {
String code;
public String toString() {
return name;
return code;
}
}
......@@ -27,17 +27,18 @@ public class Test extends TestBase {
public void test() throws IOException {
// not supported yet:
// annotations
// HexadecimalFloatingPointLiteral
// int x()[] { return null; }
// annotations
// import static
// import *
// only public or default level classes
// initializer blocks
// access to static fields with instance variable
// method call with "this": this.toString()
// final variables (within blocks, parameter list)
// Identifier : (labels)
// ClassOrInterfaceDeclaration within blocks (or any other nested classes)
// assert
// array declaration at weird places: int x() [] { return null; }
assertEquals("\\\\" + "u0000", JavaParser.replaceUnicode("\\\\" + "u0000"));
assertEquals("\u0000", JavaParser.replaceUnicode("\\" + "u0000"));
......@@ -54,14 +55,19 @@ public class Test extends TestBase {
assertEquals(".3d", JavaParser.readNumber(".3dx"));
assertEquals("6.022137e+23f", JavaParser.readNumber("6.022137e+23f+1"));
JavaParser parser = new JavaParser("src/tools", "org.h2.java.TestApp");
parser.parse();
JavaParser parser = new JavaParser();
parser.parse("src/tools/org/h2", "java.lang.String");
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.TestApp");
PrintWriter w = new PrintWriter(System.out);
parser.writeC(w);
parser.writeHeader(w);
parser.writeSource(w);
w.flush();
w = new PrintWriter(new FileWriter("bin/test.c"));
parser.writeC(w);
parser.writeHeader(w);
parser.writeSource(w);
w.close();
}
......
......@@ -9,11 +9,18 @@ package org.h2.java;
/**
* A test application.
*/
/// #include <stdio.h>
public class TestApp {
private static final int FINAL_VALUE = 10;
// private static final int FINAL_VALUE = 10;
/**
* Run this application.
*
* @param args the command line arguments
*/
public static void main(String... args) {
// c:printf("Hello\n");
System.out.println("Hello World");
}
......@@ -27,7 +34,7 @@ public class TestApp {
public int getName(int name, int x) {
System.out.println("Hello");
int m = x;
m = FINAL_VALUE;
// m = FINAL_VALUE;
switch (x) {
case 1:
m = 3;
......
package org.h2.java.io;
import org.h2.java.lang.System;
/**
* A print stream.
*/
public class PrintStream {
/**
* Print the given string.
*
* @param s the string
*/
public void println(String s) {
System.arraycopy(null, 0, null, 0, 1);
// c: printf("%s\n");
}
}
package org.h2.java.lang;
/**
* A java.lang.Object implementation.
*/
public class Object {
public int hashCode() {
return 0;
}
public boolean equals(Object other) {
return this == other;
}
}
package org.h2.java.lang;
/**
* A java.lang.String implementation.
*/
public class String {
private char[] chars;
private int hashCode;
public String(char[] chars) {
this.chars = new char[chars.length];
System.arraycopy(chars, 0, this.chars, 0, chars.length);
}
public int hashCode() {
if (hashCode == 0) {
if (chars.length == 0) {
return 0;
}
int h = 0;
for (char c : chars) {
h = h * 31 + c;
}
hashCode = h;
return h;
}
return hashCode;
}
/**
* Get the length of the string.
*
* @return the length
*/
public int length() {
return chars.length;
}
}
package org.h2.java.lang;
import java.io.PrintStream;
/**
* A simple java.lang.System implementation.
*/
public class System {
// c: #include <stdio>
/**
* The stdout stream.
*/
public static PrintStream out;
/**
* Copy data from the source to the target.
* Source and target may overlap.
*
* @param src the source array
* @param srcPos the first element in the source array
* @param dest the destination
* @param destPos the first element in the destination
* @param length the number of element to copy
*/
public static void arraycopy(java.lang.Object src, int srcPos, java.lang.Object dest, int destPos, int length) {
// c: memmove(src + srcPos, dest + destPos, length);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论