简体   繁体   English

将Java编译器后端重用于新JVM语言的有效方法

[英]Efficient ways to reuse a Java compiler's backend for a new JVM language

I'm writing a language targeted at the JVM, and I'm currently putting the compiler together. 我正在写针对JVM的语言,目前正在将编译器放在一起。 It becomes apparent to me that, logically, my new language has many of the same needs as Java when it comes to creating the bytecode. 对我来说,很明显,从逻辑上讲,我的新语言在创建字节码时与Java具有许多相同的需求。 Just to give a few examples: 仅举几个例子:

  • Find out what a dot-separated sequence of identifiers refers to: abc could refer to local variable a, field a, class a or class ab 找出由点分隔的标识符序列是什么:abc可以引用局部变量a,字段a,类a或类ab
  • Convert primitive types for arithmetic operations: for intVar*doubleVar, intVar has to be cast to double 转换基本类型以进行算术运算:对于intVar * doubleVar,必须将intVar强制转换为double
  • Find the matching method for a set of parameters: With proper inheritance and the same primitive casting as above 查找一组参数的匹配方法:具有适当的继承和与上述相同的原始转换
  • on a related note, autoboxing 在相关说明中,自动装箱
  • generic type checking, eg whether List<X<? super Y>> l = new List<X<Z>>(); 通用类型检查,例如List<X<? super Y>> l = new List<X<Z>>(); List<X<? super Y>> l = new List<X<Z>>(); is compatible 兼容

There's probably much more, and I somehow don't want to reinvent these wheels (making a new language, of course I'm reinventing some wheels already...), so I wondered what would be the best ways to reuse (parts of) an existing Java compiler, passing it the pieces of the AST it needs to figure out the above. 可能还有更多,我不知何故不想重新发明这些轮子(制作一种新语言,当然我已经在重新发明一些轮子了……),所以我想知道什么是最好的重用方法(部分内容)现有的Java编译器,并将上面需要的AST片段传递给它。

I do already have lexer and parser (ANTLR) in place, so I'm really looking for advice on what compilers are out there that would make it relatively easy for me to work with them (for example, I have looked a little into ECJ, but if someone told me that it's not capable of what I want or another compiler would be easier to use that would be great). 我已经有了lexer和parser(ANTLR),所以我真的在寻找有关哪些编译器的建议,这将使我相对容易地使用它们(例如,我对ECJ进行了一些研究,但是如果有人告诉我它不具备我想要的功能,或者使用另一个编译器会更容易使用,那就太好了。

to sum it up with a definite question: Which Java compilers out there have an easily accessible backend that is suiteable to be used with a frontend for a non-Java JVM language? 概括地说,这是一个肯定的问题:那里有哪个Java编译器具有易于访问的后端,适合与非Java JVM语言的前端一起使用?

Here are two extensible compiler backend that I heard of while reading academic papers: 这是我在阅读学术论文时听说的两个可扩展的编译器后端:

For domain-specific language engineering, I would suggest 对于特定领域的语言工程,我建议

These projects seem mature. 这些项目似乎已经成熟。 I never had a close look to them, but I really would like to ;) 我从没近距离看过他们,但我真的很想;)

At the risk of being a little obtuse, my answer is Scala. 冒着有点钝的危险,我的答案是斯卡拉。

Scala is a JVM-based language which has, as of the latest release, a macro system . Scala是一种基于JVM的语言,自最新版本起,它具有宏系统 Macros (when they're more powerful than the ones associated with C) are a technique for building domain-specific languages on top of existing languages, without having to start from scratch. 宏(当它们比与C关联的函数更强大时)是一种在现有语言之上构建特定于域的语言的技术,而无需从头开始。 A macro system lets you write code in the existing language, enhanced by new constructs, and will compile the new constructs down into the base language. 宏系统使您可以用现有语言编写代码,并通过新的构造进行增强,并将新的构造编译为基础语言。

If you want to build straight off of Java, it might be worth considering the Java Syntax Extender , but JSE is not extensively used, and may be pretty rough. 如果您想直接构建Java,可能值得考虑使用Java Syntax Extender ,但是JSE并未得到广泛使用,并且可能相当粗糙。 (There may, in fact, be more mature Java macro systems I haven't heard of.) (实际上,我可能还没有听说过更成熟的Java宏系统。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM