2

I have a code generator, which takes a syntax tree and converts it into a source file (text).

Basically, it traverses through all nodes of the tree, maps the node to text and appends the resulting texts to a StringBuilder.

Now I want the node to text mappers to be implemented using Xtend like this:

public class NodeXMapper
{
    private XtendRunner xtendRunner = ...;

    public String map(final NodeX aNode)
    {
        return xtendRunner.runScript("def String map(NodeX aNode) {
            ''' «aNode.fieldX» - «aNode.fieldY» '''
        }", aNode);
    }
}

xtendRunner.runScript(String aScript, final Object... aParams) is a method, which passes the parameters aParams to Xtend script aScript and returns the result.

How can I implement that method?

Update 1: Here I found this piece of code, which seems to run Xtend code in Java:

// setup
XtendFacade f = XtendFacade.create("my::path::MyExtensionFile");

// use
f.call("sayHello",new Object[]{"World"});

But I can't find XtendFacade class in the Type hiearchy view of Eclipse.

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • https://www.eclipse.org/forums/index.php/t/648897/ – Konstantin V. Salikhov Oct 17 '14 at 08:21
  • @KonstantinV.Salikhov Thanks. The answer on that link says "A simple exmaple for an interpreter is included within the simple arithmetics example". I can't find the simple arithmetics example in the sample projects in Eclipse. Where is it? – Glory to Russia Oct 17 '14 at 08:28
  • I think it is here: https://github.com/eclipse/xtext/tree/master/examples/org.eclipse.xtext.xtext.ui.examples/contents/org.eclipse.xtext.example.arithmetics.ui – Gábor Bakos Oct 19 '14 at 09:02
  • Here is a test base class that also uses XTendFacade: https://github.com/eclipse/xtext/tree/master/examples/org.eclipse.xtext.xtext.ui.examples/contents/org.eclipse.xtext.example.arithmetics.ui, although it seems it it no longer in the repository elsewhere. – Gábor Bakos Oct 21 '14 at 16:50

1 Answers1

2

The interpreter you found was for the old Xtend1 language, which is not what you are looking for.

The new Xtend you are referring to is compiled, so there is no interpreter.

However, you could build an interpreted expression language using Xbase. See the documentation and Github for an example on how to do that. Then you could run the interpreter of your expression language from Java.

Stefan Oehme
  • 449
  • 2
  • 7