简体   繁体   中英

Xbase Interpreter: Could not access field on instance: null

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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