Search code examples
trufflegraalvm

How do I implement a language compiler using the Truffle Language Implementation Framework


So I'm investigating how to create a language compiler using Truffle. Let's just say for the purpose of this question that the language is called Emerald.

Emerald is a statically compiled language, and it runs on the JVM, just like Java.

The compiler for Emerald would be a program called emeraldc. The compiler emeraldc will compile source files like Hello.emerald to Hello.class.

I've not found any examples of using Truffle to create such a language. All language examples I've found are interpreted languages. None seem to compile to class files for example.


Solution

  • With GraalVM's Truffle framework languages are specifically implemented as interpreters but you can still get a compiler.

    Languages are usually not intrinsically compiled or interpreted (you can interpret C and compile Javascript for example). There are even cases using a mix of both: for example your Emerald compiler compiles from emerald to Java bytecodes which can in turn be interpreted in a Java Virtual Machine and compiled Just-In-Time.

    With the GraalVM's Truffle framework the typical setup is that you implement an interpreter for your language and GraalVM will give you a JIT compiler through partial evaluation of your interpreter. You might want to check this introduction.

    If you want compilation Ahead-Of-Time, Truffle also has support for that.

    However there is currently no configuration in which the output AOT or JIT compilation would be Java bytecodes.