Child pages
  • Code Generation with Xtend

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • The Turing Machine metamodel you created in the third tutorial.
  • The Imperative Programming Language metamodel you downloaded in the previous tutorial.
  • The Turing-Machine-to-Imperative-Programming-Language transformation you developed in the previous tutorial.

This tutorial will make xtendsive use of template expressions, so be sure to read up on those.

Generating Code with

...

Xtend

We will of course need a new Xtend class that will take care of the code generation.

  1. Add a new Xtend Class to the compiler project, preferrably in a new package called de.cau.cs.rtprak.login.compiler.codegen.
  2. Add a method to your class that starts the code generation. It can look something like this:

    Code Block
        /**
         * Generates Java code for the given imperative program.
         * 
         * @param program the imperative program to generate code for.
         * @return code that implements the imperative program.
         */
        def String generateCode(Program program) '''
            YOUR CODE GENERATION
        '''
  3. Decide, which programming language to generate code for. The easiest will probably be Java, but other languages should be fine too. Your code is supposed to generate code that is complete and compilable in your target language.

Making the Code Generation Available

As in the previous tutorial, add a menu contribution to the ...compiler.ui plug-in to make the code generation available in the interface. Since your code generation implementation is expected to work for arbitrary (valid) instances of the programming language model, your menu contribution should be available for all programming language models and their textual representations. (".imperative" and ".pseudo" files) As in the previous tutorial, your handler should create a new file in the same directory as the input file and refresh the folder afterwards to have the file show up in the project explorer.

Testing Your Implementation

...