Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
f24105c1
提交
f24105c1
authored
3月 09, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
A Java parser / Java to C converter.
上级
4ead520a
全部展开
显示空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
639 行增加
和
122 行删除
+639
-122
ClassObj.java
h2/src/tools/org/h2/java/ClassObj.java
+16
-4
Expr.java
h2/src/tools/org/h2/java/Expr.java
+118
-4
JavaParser.java
h2/src/tools/org/h2/java/JavaParser.java
+356
-95
Statement.java
h2/src/tools/org/h2/java/Statement.java
+21
-10
Test.java
h2/src/tools/org/h2/java/Test.java
+13
-7
TestApp.java
h2/src/tools/org/h2/java/TestApp.java
+9
-2
PrintStream.java
h2/src/tools/org/h2/java/io/PrintStream.java
+20
-0
Object.java
h2/src/tools/org/h2/java/lang/Object.java
+16
-0
String.java
h2/src/tools/org/h2/java/lang/String.java
+40
-0
System.java
h2/src/tools/org/h2/java/lang/System.java
+30
-0
没有找到文件。
h2/src/tools/org/h2/java/ClassObj.java
浏览文件 @
f24105c1
...
@@ -54,6 +54,8 @@ public class ClassObj {
...
@@ -54,6 +54,8 @@ public class ClassObj {
*/
*/
LinkedHashMap
<
String
,
MethodObj
>
methods
=
new
LinkedHashMap
<
String
,
MethodObj
>();
LinkedHashMap
<
String
,
MethodObj
>
methods
=
new
LinkedHashMap
<
String
,
MethodObj
>();
ArrayList
<
Statement
>
nativeInitializers
=
new
ArrayList
<
Statement
>();
/**
/**
* Add a method.
* Add a method.
*
*
...
@@ -134,6 +136,11 @@ class MethodObj {
...
@@ -134,6 +136,11 @@ class MethodObj {
* Whether this method is public.
* Whether this method is public.
*/
*/
boolean
isPublic
;
boolean
isPublic
;
/**
* Whether this method is native.
*/
boolean
isNative
;
}
}
/**
/**
...
@@ -146,6 +153,11 @@ class FieldObj {
...
@@ -146,6 +153,11 @@ class FieldObj {
*/
*/
Type
type
;
Type
type
;
/**
* Whether this is a local field.
*/
boolean
isLocal
;
/**
/**
* The field name.
* The field name.
*/
*/
...
@@ -194,13 +206,13 @@ class Type {
...
@@ -194,13 +206,13 @@ class Type {
int
arrayLevel
;
int
arrayLevel
;
public
String
toString
()
{
public
String
toString
()
{
if
(
arrayLevel
==
0
)
{
return
type
.
name
;
}
StringBuilder
buff
=
new
StringBuilder
();
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
JavaParser
.
toC
(
type
.
name
));
buff
.
append
(
JavaParser
.
toC
(
type
.
name
));
if
(!
type
.
isPrimitive
)
{
buff
.
append
(
"*"
);
}
for
(
int
i
=
0
;
i
<
arrayLevel
;
i
++)
{
for
(
int
i
=
0
;
i
<
arrayLevel
;
i
++)
{
buff
.
append
(
"
[]
"
);
buff
.
append
(
"
*
"
);
}
}
return
buff
.
toString
();
return
buff
.
toString
();
}
}
...
...
h2/src/tools/org/h2/java/Expr.java
浏览文件 @
f24105c1
...
@@ -13,17 +13,21 @@ import java.util.ArrayList;
...
@@ -13,17 +13,21 @@ import java.util.ArrayList;
*/
*/
public
interface
Expr
{
public
interface
Expr
{
// toString
// toString
Type
getType
();
}
}
/**
/**
* A method call.
* A method call.
*/
*/
class
CallExpr
implements
Expr
{
class
CallExpr
implements
Expr
{
String
object
;
Expr
expr
;
String
name
;
ArrayList
<
Expr
>
args
=
new
ArrayList
<
Expr
>();
ArrayList
<
Expr
>
args
=
new
ArrayList
<
Expr
>();
public
String
toString
()
{
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
JavaParser
.
toC
(
object
)).
append
(
"("
);
buff
.
append
(
expr
.
toString
()
+
"_"
+
JavaParser
.
toC
(
name
)).
append
(
"("
);
int
i
=
0
;
int
i
=
0
;
for
(
Expr
a
:
args
)
{
for
(
Expr
a
:
args
)
{
if
(
i
>
0
)
{
if
(
i
>
0
)
{
...
@@ -34,12 +38,17 @@ class CallExpr implements Expr {
...
@@ -34,12 +38,17 @@ class CallExpr implements Expr {
}
}
return
buff
.
append
(
")"
).
toString
();
return
buff
.
append
(
")"
).
toString
();
}
}
public
Type
getType
()
{
// TODO
return
null
;
}
}
}
/**
/**
* A assignment expression.
* A assignment expression.
*/
*/
class
AssignExpr
implements
Expr
{
class
AssignExpr
implements
Expr
{
Expr
left
;
Expr
left
;
String
op
;
String
op
;
Expr
right
;
Expr
right
;
...
@@ -47,36 +56,60 @@ class AssignExpr implements Expr {
...
@@ -47,36 +56,60 @@ class AssignExpr implements Expr {
public
String
toString
()
{
public
String
toString
()
{
return
left
+
" "
+
op
+
" "
+
right
;
return
left
+
" "
+
op
+
" "
+
right
;
}
}
public
Type
getType
()
{
// TODO
return
null
;
}
}
}
/**
/**
* A conditional expression.
* A conditional expression.
*/
*/
class
ConditionalExpr
implements
Expr
{
class
ConditionalExpr
implements
Expr
{
Expr
condition
;
Expr
condition
;
Expr
ifTrue
,
ifFalse
;
Expr
ifTrue
,
ifFalse
;
public
String
toString
()
{
public
String
toString
()
{
return
condition
+
" ? "
+
ifTrue
+
" : "
+
ifFalse
;
return
condition
+
" ? "
+
ifTrue
+
" : "
+
ifFalse
;
}
}
public
Type
getType
()
{
// TODO
return
null
;
}
}
}
/**
/**
* A literal.
* A literal.
*/
*/
class
LiteralExpr
implements
Expr
{
class
LiteralExpr
implements
Expr
{
String
literal
;
String
literal
;
public
String
toString
()
{
public
String
toString
()
{
return
literal
;
return
literal
;
}
}
public
Type
getType
()
{
// TODO
return
null
;
}
}
}
/**
/**
* An operation.
* An operation.
*/
*/
class
OpExpr
implements
Expr
{
class
OpExpr
implements
Expr
{
Expr
left
;
Expr
left
;
String
op
;
String
op
;
Expr
right
;
Expr
right
;
public
String
toString
()
{
public
String
toString
()
{
if
(
left
==
null
)
{
if
(
left
==
null
)
{
return
op
+
right
;
return
op
+
right
;
...
@@ -85,16 +118,38 @@ class OpExpr implements Expr {
...
@@ -85,16 +118,38 @@ class OpExpr implements Expr {
}
}
return
left
+
" "
+
op
+
" "
+
right
;
return
left
+
" "
+
op
+
" "
+
right
;
}
}
public
Type
getType
()
{
// TODO
return
null
;
}
}
}
/**
/**
* A "new" expression.
* A "new" expression.
*/
*/
class
NewExpr
implements
Expr
{
class
NewExpr
implements
Expr
{
String
className
;
ClassObj
type
;
ArrayList
<
Expr
>
arrayInitExpr
=
new
ArrayList
<
Expr
>();
public
String
toString
()
{
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 {
...
@@ -111,6 +166,11 @@ class StringExpr implements Expr {
return
"\""
+
javaEncode
(
text
)
+
"\""
;
return
"\""
+
javaEncode
(
text
)
+
"\""
;
}
}
public
Type
getType
()
{
// TODO
return
null
;
}
/**
/**
* Encode the String to Java syntax.
* Encode the String to Java syntax.
*
*
...
@@ -165,3 +225,57 @@ class StringExpr implements Expr {
...
@@ -165,3 +225,57 @@ class StringExpr implements Expr {
return
buff
.
toString
();
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
();
}
}
h2/src/tools/org/h2/java/JavaParser.java
浏览文件 @
f24105c1
差异被折叠。
点击展开。
h2/src/tools/org/h2/java/Statement.java
浏览文件 @
f24105c1
...
@@ -136,9 +136,18 @@ class ForStatement implements Statement {
...
@@ -136,9 +136,18 @@ class ForStatement implements Statement {
Expr
update
;
Expr
update
;
Statement
block
;
Statement
block
;
ArrayList
<
Expr
>
updates
=
new
ArrayList
<
Expr
>();
ArrayList
<
Expr
>
updates
=
new
ArrayList
<
Expr
>();
Type
iterableType
;
String
iterableVariable
;
Expr
iterable
;
public
String
toString
()
{
public
String
toString
()
{
StringBuffer
buff
=
new
StringBuffer
();
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
(
"; "
);
buff
.
append
(
" "
).
append
(
condition
.
toString
()).
append
(
"; "
);
for
(
int
i
=
0
;
i
<
updates
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
updates
.
size
();
i
++)
{
if
(
i
>
0
)
{
if
(
i
>
0
)
{
...
@@ -146,6 +155,7 @@ class ForStatement implements Statement {
...
@@ -146,6 +155,7 @@ class ForStatement implements Statement {
}
}
buff
.
append
(
updates
.
get
(
i
));
buff
.
append
(
updates
.
get
(
i
));
}
}
}
buff
.
append
(
") {\n"
);
buff
.
append
(
") {\n"
);
buff
.
append
(
block
.
toString
()).
append
(
"}"
);
buff
.
append
(
block
.
toString
()).
append
(
"}"
);
return
buff
.
toString
();
return
buff
.
toString
();
...
@@ -192,11 +202,12 @@ class VarDecStatement implements Statement {
...
@@ -192,11 +202,12 @@ class VarDecStatement implements Statement {
}
}
/**
/**
* A
variable
.
* A
native statement
.
*/
*/
class
VariableExpr
implements
Expr
{
class
StatementNative
implements
Statement
{
public
String
nam
e
;
String
cod
e
;
public
String
toString
()
{
public
String
toString
()
{
return
nam
e
;
return
cod
e
;
}
}
}
}
h2/src/tools/org/h2/java/Test.java
浏览文件 @
f24105c1
...
@@ -27,17 +27,18 @@ public class Test extends TestBase {
...
@@ -27,17 +27,18 @@ public class Test extends TestBase {
public
void
test
()
throws
IOException
{
public
void
test
()
throws
IOException
{
// not supported yet:
// not supported yet:
// annotations
// HexadecimalFloatingPointLiteral
// HexadecimalFloatingPointLiteral
// int x()[] { return null; }
// annotations
// import static
// import static
// import *
// import *
// only public or default level classes
// initializer blocks
// initializer blocks
// access to static fields with instance variable
// method call with "this": this.toString()
// final variables (within blocks, parameter list)
// final variables (within blocks, parameter list)
// Identifier : (labels)
// Identifier : (labels)
// ClassOrInterfaceDeclaration within blocks (or any other nested classes)
// ClassOrInterfaceDeclaration within blocks (or any other nested classes)
// assert
// assert
// array declaration at weird places: int x() [] { return null; }
assertEquals
(
"\\\\"
+
"u0000"
,
JavaParser
.
replaceUnicode
(
"\\\\"
+
"u0000"
));
assertEquals
(
"\\\\"
+
"u0000"
,
JavaParser
.
replaceUnicode
(
"\\\\"
+
"u0000"
));
assertEquals
(
"\u0000"
,
JavaParser
.
replaceUnicode
(
"\\"
+
"u0000"
));
assertEquals
(
"\u0000"
,
JavaParser
.
replaceUnicode
(
"\\"
+
"u0000"
));
...
@@ -54,14 +55,19 @@ public class Test extends TestBase {
...
@@ -54,14 +55,19 @@ public class Test extends TestBase {
assertEquals
(
".3d"
,
JavaParser
.
readNumber
(
".3dx"
));
assertEquals
(
".3d"
,
JavaParser
.
readNumber
(
".3dx"
));
assertEquals
(
"6.022137e+23f"
,
JavaParser
.
readNumber
(
"6.022137e+23f+1"
));
assertEquals
(
"6.022137e+23f"
,
JavaParser
.
readNumber
(
"6.022137e+23f+1"
));
JavaParser
parser
=
new
JavaParser
(
"src/tools"
,
"org.h2.java.TestApp"
);
JavaParser
parser
=
new
JavaParser
();
parser
.
parse
();
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
);
PrintWriter
w
=
new
PrintWriter
(
System
.
out
);
parser
.
writeC
(
w
);
parser
.
writeHeader
(
w
);
parser
.
writeSource
(
w
);
w
.
flush
();
w
.
flush
();
w
=
new
PrintWriter
(
new
FileWriter
(
"bin/test.c"
));
w
=
new
PrintWriter
(
new
FileWriter
(
"bin/test.c"
));
parser
.
writeC
(
w
);
parser
.
writeHeader
(
w
);
parser
.
writeSource
(
w
);
w
.
close
();
w
.
close
();
}
}
...
...
h2/src/tools/org/h2/java/TestApp.java
浏览文件 @
f24105c1
...
@@ -9,11 +9,18 @@ package org.h2.java;
...
@@ -9,11 +9,18 @@ package org.h2.java;
/**
/**
* A test application.
* A test application.
*/
*/
/// #include <stdio.h>
public
class
TestApp
{
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
)
{
public
static
void
main
(
String
...
args
)
{
// c:printf("Hello\n");
System
.
out
.
println
(
"Hello World"
);
System
.
out
.
println
(
"Hello World"
);
}
}
...
@@ -27,7 +34,7 @@ public class TestApp {
...
@@ -27,7 +34,7 @@ public class TestApp {
public
int
getName
(
int
name
,
int
x
)
{
public
int
getName
(
int
name
,
int
x
)
{
System
.
out
.
println
(
"Hello"
);
System
.
out
.
println
(
"Hello"
);
int
m
=
x
;
int
m
=
x
;
m
=
FINAL_VALUE
;
//
m = FINAL_VALUE;
switch
(
x
)
{
switch
(
x
)
{
case
1
:
case
1
:
m
=
3
;
m
=
3
;
...
...
h2/src/tools/org/h2/java/io/PrintStream.java
0 → 100644
浏览文件 @
f24105c1
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");
}
}
h2/src/tools/org/h2/java/lang/Object.java
0 → 100644
浏览文件 @
f24105c1
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
;
}
}
h2/src/tools/org/h2/java/lang/String.java
0 → 100644
浏览文件 @
f24105c1
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
;
}
}
h2/src/tools/org/h2/java/lang/System.java
0 → 100644
浏览文件 @
f24105c1
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论