[graalvm-users] Graal for use with static languages?

Steven Stewart-Gallus sstewartgallus at sstewartgallus.com
Wed May 29 08:46:54 PDT 2019


Hi!

I am exploring using Graal to implement my own little language.

I am planning on making a strongly typed purely functional nonstrict
language like Haskell with a simple syntax and strong macro system
such as in Lisp or Forth.

I have a few questions:

1. In my language every AST node is strongly typed. Should I embed
    that info as a node child/argument or in the class/in a field
    itself?

Inside the class:

public final AddIntNode extends AlwaysIntNode {
     @Child AlwaysIntNode left;
     @Child AlwaysIntNode right;

     public int executeInt(VirtualFrame frame) {
         return left.executeInt(frame) + right.executeInt(frame);
     }

     // etc...
}

As an argument:

@NodeInfo(shortName = "+")
@NodeChild("type")
public abstract class AddNode extends ExprNode {
     @Child ExprNode left;
     @Child ExprNode right;

     AddNode(ExprNode left, ExprNode right) {
         this.left = left;
         this.right = right;
     }

     @Specialization(guards = "isInt(type)")
     int addInt(VirtualFrame frame, Object type) {
         return left.executeInt(frame) + right.executeInt(frame);
     }

     @Specialization(guards = "isLong(type)")
     long addLong(VirtualFrame frame, Object type) {
         return left.executeLong(frame) + right.executeLong(frame);
     }

     // etc...
}

THe type parameter being just something like:

public final class Type {
     public static final Type LONG_VALUE = new Type();
     public static final Type INT_VALUE = new Type();

     private Type() {
     }

     // etc...
}

and the type test methods are like:

static boolean isLong(Object t) {
     return t == Type.LONG_VALUE;
}

Specialization on the type parameters feels right to me but I am
unsure if there are better ways to do things.

2. How does CompilerDirectives.ValueType work precisely?

Is it fair to just use generics/boxing everywhere and rely on
ValueType to unbox things?

If I have a class hierarchy like IntValue > Value > Any, Object > Any
should I apply the ValueType directive to Value or will it not do
anything?

3. The DSL generates code that implicitly autoboxes to standard Java
    boxes.  But what if I want to box to a custom class?

It is already possible to unbox a custom class like so:

@TypeSystem({int.class, long.class})
public abstract class Types {
     @TypeCheck(value = int.class)
     public static boolean isInteger(Object value) {
         return value instanceof IntValue;
     }

     @TypeCast(value = int.class)
     public static int asInteger(Object value) {
         return ((IntValue) value).value;
     }

     // etc...
}

But there doesn't seem to be a way to hook into the reverse boxing
operation.

4. What is Graal's story with respect to custom static datatypes?

Graal seems to have a strong story with respect to dynamic objects but
those seem like they'd be worse than just defining anonymous Java
classes at runtime if I know type information statically.


Thank you,

Steven Stewart-Gallus




More information about the GraalVM-Users mailing list