提交 3f377668 authored 作者: Thomas Mueller's avatar Thomas Mueller

A Java parser / Java to C converter.

上级 cf69bd38
......@@ -56,7 +56,7 @@ public class ClassObj {
ArrayList<Statement> nativeCode = new ArrayList<Statement>();
String id;
int id;
/**
* Add a method.
......@@ -87,9 +87,9 @@ public class ClassObj {
public String toString() {
if (isPrimitive) {
return name;
return "j" + name;
}
return "struct " + name;
return name;
}
}
......@@ -127,7 +127,7 @@ class MethodObj {
/**
* The parameter list.
*/
ArrayList<FieldObj> parameters = new ArrayList<FieldObj>();
LinkedHashMap<String, FieldObj> parameters = new LinkedHashMap<String, FieldObj>();
/**
* Whether this method is final.
......@@ -143,6 +143,11 @@ class MethodObj {
* Whether this method is native.
*/
boolean isNative;
/**
* Whether this is a constructor.
*/
boolean isConstructor;
}
/**
......@@ -209,7 +214,7 @@ class Type {
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toC(type.name));
buff.append(JavaParser.toC(type.toString()));
if (!type.isPrimitive) {
buff.append("*");
}
......
......@@ -41,7 +41,7 @@ class CallExpr implements Expr {
if (className != null) {
buff.append(JavaParser.toC(className + "." + name)).append("(");
} else {
buff.append(JavaParser.toC(expr.getType().type.name + "." + name)).append("(");
buff.append(JavaParser.toC(expr.getType().type + "." + name)).append("(");
}
int i = 0;
if (expr != null) {
......@@ -198,7 +198,7 @@ class StringExpr implements Expr {
String text;
public String toString() {
return "\"" + javaEncode(text) + "\"";
return "STRING(\"" + javaEncode(text) + "\")";
}
public Type getType() {
......@@ -282,7 +282,8 @@ class VariableExpr implements Expr {
}
if (field != null) {
if (field.isStatic) {
buff.append(JavaParser.toC(field.type.type.name + "." + field.name));
// buff.append(JavaParser.toC(field.type.type.name + "." + field.name));
buff.append(JavaParser.toC(name));
} else {
buff.append(field.name);
}
......
......@@ -77,7 +77,7 @@ class SwitchStatement implements Statement {
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("case " + cases.get(i) + ":\n");
buff.append(blocks.get(i).toString());
}
if (defaultBlock != null) {
......@@ -148,7 +148,7 @@ class ForStatement implements Statement {
Type it = iterable.getType();
if (it != null && it.arrayLevel > 0) {
String idx = "i_" + iterableVariable;
buff.append("int " + idx + " = 0; " + idx + " < LEN(" + iterable + "); " + idx + "++");
buff.append("int " + idx + " = 0; " + idx + " < LENGTH(" + iterable + "); " + idx + "++");
buff.append(") {\n");
buff.append(JavaParser.indent(iterableType + " " + iterableVariable + " = " + iterable + "["+ idx +"];\n"));
buff.append(block.toString()).append("}");
......
......@@ -26,6 +26,9 @@ public class Test extends TestBase {
}
public void test() throws IOException {
// gcc --std=c99 test.c
// (for "mixed declarations and code")
// not supported yet:
// HexadecimalFloatingPointLiteral
// int x()[] { return null; }
......
......@@ -9,10 +9,15 @@ package org.h2.java;
/**
* A test application.
*/
/// #include <stdio.h>
public class TestApp {
// private static final int FINAL_VALUE = 10;
/* c:
int main(char** args) {
org_h2_java_TestApp_main(null);
}
*/
/**
* Run this application.
......
......@@ -2,17 +2,33 @@ package org.h2.java.lang;
/* c:
#define boolean int
#define LENGTH(a) (*(((int*)(a))-1))
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define jvoid void
#define jboolean int8_t
#define jbyte int8_t
#define jchar int16_t
#define jint int32_t
#define jlong int64_t
#define jfloat float
#define jdouble double
#define true 1
#define false 0
#define null 0
#define LENGTH(a) (*(((jint*)(a))-1))
#define NEW_ARRAY(size, length) new_array(0, size, length)
#define NEW_OBJ_ARRAY(length) new_array(1, sizeof(void*), length)
#define NEW_OBJ(type, size) new_object(type, sizeof(struct type))
#define NEW_OBJ(typeId, typeName) new_object(typeId, sizeof(struct typeName))
#define SET(variable, p) set_object(variable, p)
#define STRING(s) ((java_lang_String*) string(s))
void* new_array(int object, int size, int length);
void* new_object(int type, int size);
void* new_array(jint object, jint size, jint length);
void* new_object(jint type, jint size);
void* set_object(void** target, void* o);
void* string(char* s);
*/
......@@ -23,38 +39,45 @@ public class String {
/* c:
#include <stdlib.h>
void* new_array(int object, int size, int length) {
int count = sizeof(int) * 2 + size * length;
int* m = (int*) calloc(1, count);
void* new_array(jint object, jint size, jint length) {
int count = sizeof(jint) * 2 + size * length;
int* m = (jint*) calloc(1, count);
*m = object << 31 + length;
*(m+1) = 1;
return m + 2;
}
void* new_object(int type, int size) {
int count = sizeof(int) * 2 + size;
int* m = (int*) calloc(1, count);
void* new_object(jint type, jint size) {
int count = sizeof(jint) * 2 + size;
int* m = (jint*) calloc(1, count);
*m = type;
*(m+1) = 1;
return m + 2;
}
void* set_object(void** target, void* o) {
int* m = (int*) target;
int* m = (jint*) target;
if (*(m - 2) == 1) {
free(m - 1);
} else {
(*(m - 2))--;
}
*target = o;
m = (int*) target;
m = (jint*) target;
if (o != 0) {
(*(m - 2))++;
}
}
void* string(char* s) {
int len = strlen(s);
jchar* chars = NEW_ARRAY(sizeof(char), 1 * LENGTH(chars));
for (int i = 0; i < len; i++) {
chars[i] = s[i];
}
return java_lang_String_init_obj(chars);
}
*/
private char[] chars;
......
......@@ -7,8 +7,6 @@ import java.io.PrintStream;
*/
public class System {
// c: #include <stdio>
/**
* The stdout stream.
*/
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论