我正在为 java 的一个子集编写一个编译器,我几乎完成了。我遇到的一件事是处理命名为 jasmin-keywords 的类、字段和方法。
例如:
class Test {
public static void main(String[] args) {
int a;
a = new pop().run();
}
}
class pop {
int lcmp;
int stack;
int isub;
int iload_0;
public int run() {
int aaload;
lcmp = 1;
aaload = lcmp;
return aaload;
}
public int swap() {
return 0;
}
public int iload_0() {
int istore;
boolean ret;
return 0;
}
}
这会像这样编译成茉莉花:
.source evul_names.java
.class Test
.super java/lang/Object
.method public <init>()V
aload_0
invokespecial java/lang/Object/<init>()V
return
.end method
.method public static main([Ljava/lang/String;)V
.limit locals 2
.limit stack 2
.var 0 is a I
.line 4
new pop
dup
invokespecial pop/<init>()V
invokevirtual pop/run()I
istore_0 ; a
return
.end method
.source evul_names.java
.class pop
.super java/lang/Object
.field public lcmp I
.field public stack I
.field public isub I
.field public iload_0 I
.method public <init>()V
aload_0
invokespecial java/lang/Object/<init>()V
return
.end method
.method public run()I
.limit locals 3
.limit stack 2
.line 15
.var 0 is <this> Lpop;
.var 1 is aaload I
.line 17
iconst_1
aload_0 ; this
swap
putfield pop/lcmp I
.line 18
aload_0 ; this
getfield pop/lcmp I
istore_1 ; aaload
.line 19
iload_1 ; aaload
ireturn
.end method
.method public swap()I
.limit locals 2
.limit stack 1
.line 22
.var 0 is <this> Lpop;
.line 23
iconst_0
ireturn
.end method
.method public iload_0()I
.limit locals 4
.limit stack 1
.line 26
.var 0 is <this> Lpop;
.var 1 is istore I
.var 2 is ret Z
.line 29
iconst_0
ireturn
.end method
问题是 .class 和 .field 指令,如果名称是关键字,它们会导致编译错误。我尝试在它们周围加上单引号和双引号;使用单引号将 jasmin 组装成类文件,但随后会出现验证错误(看起来它实际上包含名称中的引号,这是次优的)。使用双引号根本无法编译。
对此有什么解决方案,还是在茉莉花中不可能做到这一点?