1

I am testing the idea of making my dsl Jvm compatible and I wanted to test the possibility of extending Xbase and using the interpreter. I have tried to make a minimal test project to use with the interpreter but I am getting a runtime error. I think I understand the general concepts of adapting Xbase, but am unsure about how the setup/entrypoints for the interpreter and could not find any information regarding the error I am getting or how to resolve. Here are the relevant files for my situation:

Text.xtext:

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types

Program returns Program:
    {Program}
    'program' name=ID '{'
    variables=Var_Section?
    run=XExpression?
    '}'
;

Var_Section returns VarSection:
    {VarSection}
    'variables' '{'
        decls+=XVariableDeclaration+
    '}'
;


@Override // Change syntax
XVariableDeclaration returns xbase::XVariableDeclaration:
    type=JvmTypeReference name=ID '=' right=XLiteral ';'
;
@Override // Do not allow declarations outside of variable region
XExpressionOrVarDeclaration returns xbase::XExpression:
    XExpression;

TestJvmModelInferrer:

def dispatch void infer(Program element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(element.toClass(element.fullyQualifiedName)) [
            documentation = element.documentation
            if (element.variables !== null) {
                for (decl : element.variables.decls) {
                    members += decl.toField(decl.name, decl.type) [
                        static = true
                        initializer = decl.right
                        visibility = JvmVisibility.PUBLIC
                    ]
                }
            }

            if (element.run !== null) {
                members += element.run.toMethod('main', typeRef(Void::TYPE)) [
                    parameters += element.run.toParameter("args", typeRef(String).addArrayTypeDimension)
                    visibility = JvmVisibility.PUBLIC
                    static = true
                    body = element.run
                ]
            }
        ]
    }

Test case:

@Inject ParseHelper<Program> parseHelper
@Inject extension ValidationTestHelper
@Inject XbaseInterpreter interpreter
@Test
    def void basicInterpret() {
        val result = parseHelper.parse('''
        program program1 {
            variables {
                int var1 = 0;
                double var2 = 3.4;
            }
            var1 = 13
        }
        ''')
        result.assertNoErrors
        var interpretResult = interpreter.evaluate(result.run)
        println(interpretResult.result)

Partial stack trace:

java.lang.IllegalStateException: Could not access field: program1.var1 on instance: null
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._assignValueTo(XbaseInterpreter.java:1262)
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.assignValueTo(XbaseInterpreter.java:1221)
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1213)
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:216)
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:204)
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:190)
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:180)
Zannith
  • 429
  • 4
  • 21

2 Answers2

1

The interpreter does only support expressions, but does not work with types that are created by a JvmModelInferrer. Your code tries to work with fields of such an inferred type.

Rather than using the interpreter, I'd recommend to use an InMemoryCompiler in your test. The domainmodel example may serve as an inspiration: https://github.com/eclipse/xtext-eclipse/blob/c2b15c3ec118c4c200e2b28ea72d8c9116fb6800/org.eclipse.xtext.xtext.ui.examples/projects/domainmodel/org.eclipse.xtext.example.domainmodel.tests/xtend-gen/org/eclipse/xtext/example/domainmodel/tests/XbaseIntegrationTest.java

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • Interesting alternative. I am not sure if it would solve the use case for my full project though. Is there no way to extend the interpreters state management to work for inferred types? – Zannith Jan 31 '19 at 20:00
  • You would have to build a fully working interpreter for Java, eg supporting inheritance from Java classes is tricky, polymorphism is not trivial either. Basically the effort depends on your real world usecase. – Sebastian Zarnekow Feb 02 '19 at 12:58
  • Alright, probably not worth the effort for the reward in my case. Thank you for your answers! – Zannith Feb 04 '19 at 18:13
0

You may find this project interesting, which (among other stuff) implements an interpreter for Xtend based on the Xbase interpreter. It might be a bit outdated, though, and also will not fully support all Xtend concepts. But it could be a starting point, and your contrbutions are welcome :-)

https://github.com/kbirken/xtendency

  • Interesting project, As Sebastian stated though, It would not be trivial building on top of the Xbase interpreter for full java interop, which sadly I don't have the extra time for. I will certainly keep it in mind if this feature becomes more of a focus however. – Zannith Feb 04 '19 at 18:15