Learning Truffle DSL
Truffle defines a Domain-Specific Language (DSL) based on the Java annotation processor. The language developer has to write a lot of boilerplate code to manage the states of the specializations. To appreciate how Truffle DSL makes a programmer's life easy, let's take a quick example:
c = a + b
As we discussed earlier in this chapter, in AST, every operation and operand is represented as a node. In Truffle, it is a Java class derived from com.oracle.truffle.api.nodes.Node
. To understand the need for a DSL, let's oversimplify the implementation of AST for the preceding expression.
Since we are looking at dynamically typed languages, a
and b
can be any type. We need an expression node that should implement an execute
method, which checks for all the possible types for a
and b
. We will have to write logic something like this:
In the...