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

A Java parser / Java to C converter.

上级 460425a5
/*
* 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;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/**
* A class or interface.
*/
public class ClassObj {
/**
* The fully qualified class name.
*/
String name;
/**
* Whether this is an interface.
*/
boolean isInterface;
/**
* Whether this class is public.
*/
boolean isPublic;
/**
* Whether this is a primitive class (int, char,...)
*/
boolean isPrimitive;
/**
* The imported classes.
*/
ArrayList<ClassObj> imports = new ArrayList<ClassObj>();
/**
* The per-instance fields.
*/
LinkedHashMap<String, FieldObj> instanceFields = new LinkedHashMap<String, FieldObj>();
/**
* The static fields of this class.
*/
LinkedHashMap<String, FieldObj> staticFields = new LinkedHashMap<String, FieldObj>();
/**
* The methods.
*/
LinkedHashMap<String, MethodObj> methods = new LinkedHashMap<String, MethodObj>();
/**
* Add a method.
*
* @param method the method
*/
void addMethod(MethodObj method) {
methods.put(method.name, method);
}
/**
* Add an instance field.
*
* @param field the field
*/
void addInstanceField(FieldObj field) {
instanceFields.put(field.name, field);
}
/**
* Add a static field.
*
* @param field the field
*/
void addStaticField(FieldObj field) {
staticFields.put(field.name, field);
}
public String toString() {
if (isPrimitive) {
return name;
}
return "struct " + name;
}
}
/**
* A method.
*/
class MethodObj {
/**
* Whether this method is static.
*/
boolean isStatic;
/**
* Whether this method is private.
*/
boolean isPrivate;
/**
* The name.
*/
String name;
/**
* The statement block (if any).
*/
Statement block;
/**
* The return type.
*/
Type returnType;
/**
* The parameter list.
*/
ArrayList<FieldObj> parameters = new ArrayList<FieldObj>();
/**
* Whether this method is final.
*/
boolean isFinal;
/**
* Whether this method is public.
*/
boolean isPublic;
}
/**
* A field.
*/
class FieldObj {
/**
* The type.
*/
Type type;
/**
* The field name.
*/
String name;
/**
* Whether this field is static.
*/
boolean isStatic;
/**
* Whether this field is final.
*/
boolean isFinal;
/**
* Whether this field is private.
*/
boolean isPrivate;
/**
* Whether this field is public.
*/
boolean isPublic;
/**
* The initial value expression (may be null).
*/
Expr value;
}
/**
* A type.
*/
class Type {
/**
* The class.
*/
ClassObj type;
/**
* The array nesting level. 0 if not an array.
*/
int arrayLevel;
public String toString() {
if (arrayLevel == 0) {
return type.name;
}
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(type.name));
for (int i = 0; i < arrayLevel; i++) {
buff.append("[]");
}
return buff.toString();
}
}
/*
* 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;
import java.util.ArrayList;
/**
* An expression.
*/
public interface Expr {
// toString
}
/**
* A method call.
*/
class CallExpr implements Expr {
String object;
ArrayList<Expr> args = new ArrayList<Expr>();
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(object)).append("(");
int i = 0;
for (Expr a : args) {
if (i > 0) {
buff.append(", ");
}
i++;
buff.append(a);
}
return buff.append(")").toString();
}
}
/**
* A assignment expression.
*/
class AssignExpr implements Expr {
Expr left;
String op;
Expr right;
public String toString() {
return left + " " + op + " " + right;
}
}
/**
* A conditional expression.
*/
class ConditionalExpr implements Expr {
Expr condition;
Expr ifTrue, ifFalse;
public String toString() {
return condition + " ? " + ifTrue + " : " + ifFalse;
}
}
/**
* A literal.
*/
class LiteralExpr implements Expr {
String literal;
public String toString() {
return literal;
}
}
/**
* An operation.
*/
class OpExpr implements Expr {
Expr left;
String op;
Expr right;
public String toString() {
if (left == null) {
return op + right;
} else if (right == null) {
return left + op;
}
return left + " " + op + " " + right;
}
}
/**
* A "new" expression.
*/
class NewExpr implements Expr {
String className;
public String toString() {
return "new " + className;
}
}
/**
* A String literal.
*/
class StringExpr implements Expr {
/**
* The literal.
*/
String text;
public String toString() {
return "\"" + javaEncode(text) + "\"";
}
/**
* Encode the String to Java syntax.
*
* @param s the string
* @return the encoded string
*/
static String javaEncode(String s) {
StringBuilder buff = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\t':
// HT horizontal tab
buff.append("\\t");
break;
case '\n':
// LF linefeed
buff.append("\\n");
break;
case '\f':
// FF form feed
buff.append("\\f");
break;
case '\r':
// CR carriage return
buff.append("\\r");
break;
case '"':
// double quote
buff.append("\\\"");
break;
case '\\':
// backslash
buff.append("\\\\");
break;
default:
int ch = c & 0xffff;
if (ch >= ' ' && (ch < 0x80)) {
buff.append(c);
// not supported in properties files
// } else if(ch < 0xff) {
// buff.append("\\");
// // make sure it's three characters (0x200 is octal 1000)
// buff.append(Integer.toOctalString(0x200 | ch).substring(1));
} else {
buff.append("\\u");
// make sure it's four characters
buff.append(Integer.toHexString(0x10000 | ch).substring(1));
}
}
}
return buff.toString();
}
}
差异被折叠。
/*
* 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;
import java.util.ArrayList;
/**
* A statement.
*/
public interface Statement {
// toString
}
/**
* A "return" statement.
*/
class ReturnStatement implements Statement {
Expr expr;
public String toString() {
return "return " + (expr == null ? "" : expr) + ";";
}
}
/**
* A "do .. while" statement.
*/
class DoWhileStatement implements Statement {
Expr condition;
Statement block;
public String toString() {
return "do {\n" + block + "} while (" + condition + ");";
}
}
/**
* A "continue" statement.
*/
class ContinueStatement implements Statement {
public String toString() {
return "continue;";
}
}
/**
* A "break" statement.
*/
class BreakStatement implements Statement {
public String toString() {
return "break;";
}
}
/**
* An empty statement.
*/
class EmptyStatement implements Statement {
public String toString() {
return ";";
}
}
/**
* A "switch" statement.
*/
class SwitchStatement implements Statement {
Expr expr;
StatementBlock defaultBlock;
ArrayList<Expr> cases = new ArrayList<Expr>();
ArrayList<StatementBlock> blocks = new ArrayList<StatementBlock>();
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append("switch (").append(expr).append(") {\n");
for (int i = 0; i < cases.size(); i++) {
buff.append("case: " + cases.get(i) + ":\n");
buff.append(blocks.get(i).toString());
}
if (defaultBlock != null) {
buff.append("default:\n");
buff.append(defaultBlock.toString());
}
buff.append("}");
return buff.toString();
}
}
/**
* An expression statement.
*/
class ExprStatement implements Statement {
Expr expr;
public String toString() {
return expr + ";";
}
}
/**
* A "while" statement.
*/
class WhileStatement implements Statement {
Expr condition;
Statement block;
public String toString() {
String w = "while (" + condition + ")";
String s = block.toString();
return w + "\n" + s;
}
}
/**
* An "if" statement.
*/
class IfStatement implements Statement {
Expr condition;
Statement block;
Statement elseBlock;
public String toString() {
String w = "if (" + condition + ") {\n";
String s = block.toString();
if (elseBlock != null) {
s += "} else {\n" + elseBlock.toString();
}
return w + s + "}";
}
}
/**
* A "for" statement.
*/
class ForStatement implements Statement {
Statement init;
Expr condition;
Expr update;
Statement block;
ArrayList<Expr> updates = new ArrayList<Expr>();
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append("for (").append(init.toString());
buff.append(" ").append(condition.toString()).append("; ");
for (int i = 0; i < updates.size(); i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(updates.get(i));
}
buff.append(") {\n");
buff.append(block.toString()).append("}");
return buff.toString();
}
}
/**
* A statement block.
*/
class StatementBlock implements Statement {
ArrayList<Statement> instructions = new ArrayList<Statement>();
public String toString() {
StringBuilder buff = new StringBuilder();
for (Statement s : instructions) {
buff.append(JavaParser.indent(s.toString()));
}
return buff.toString();
}
}
/**
* A variable declaration.
*/
class VarDecStatement implements Statement {
Type type;
ArrayList<String> variables = new ArrayList<String>();
ArrayList<Expr> values = new ArrayList<Expr>();
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(type).append(' ');
for (int i = 0; i < variables.size(); i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(variables.get(i));
Expr value = values.get(i);
if (value != null) {
buff.append(" = ").append(value);
}
}
buff.append(";");
return buff.toString();
}
}
/**
* A variable.
*/
class VariableExpr implements Expr {
public String name;
public String toString() {
return name;
}
}
/*
* 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;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.h2.test.TestBase;
/**
* A test for the Java parser.
*/
public class Test extends TestBase {
/**
* Start the task with the given arguments.
*
* @param args the arguments, or null
*/
public static void main(String... args) throws IOException {
new Test().test();
}
public void test() throws IOException {
// not supported yet:
// annotations
// HexadecimalFloatingPointLiteral
// import static
// import *
// only public or default level classes
// initializer blocks
// 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"));
assertEquals("\u0000", JavaParser.replaceUnicode("\\" + "uu0000"));
assertEquals("\\\\" + "\u0000", JavaParser.replaceUnicode("\\\\\\" + "u0000"));
assertEquals("0", JavaParser.readNumber("0a"));
assertEquals("0l", JavaParser.readNumber("0l"));
assertEquals("0xFFL", JavaParser.readNumber("0xFFLx"));
assertEquals("0xDadaCafe", JavaParser.readNumber("0xDadaCafex"));
assertEquals("1.40e-45f", JavaParser.readNumber("1.40e-45fx"));
assertEquals("1e1f", JavaParser.readNumber("1e1fx"));
assertEquals("2.f", JavaParser.readNumber("2.fx"));
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();
PrintWriter w = new PrintWriter(System.out);
parser.writeC(w);
w.flush();
w = new PrintWriter(new FileWriter("bin/test.c"));
parser.writeC(w);
w.close();
}
}
/*
* 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;
/**
* A test application.
*/
public class TestApp {
private static final int FINAL_VALUE = 10;
public static void main(String... args) {
System.out.println("Hello World");
}
/**
* 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);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论