Page tree

Versions Compared

Key

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

...

The slides to the presentation that accompanied this tutorial can be found here.

 

Table of Contents

Preliminaries

You should already have everything you need for this tutorial providing you finished the first part Xtend 1a - Model Transformations:

...

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.

...

  • The imperative language supports arrays, which you could implement as lists or arrays. Either way, you should make sure that the generated code does not throw exceptions for reasonable array indices. Ideally, your code makes no assumptions on array sizes.
  • You will have to generate some kind of a main method which takes care of initializing input variables, running the program, and printing the result. To initialize input variables, your generated code could ask the user for input on the console.
  • Try to ensure that the generated code is readable and properly indented.

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.

File Names

Icon
When working on your menu contribution, think about how to name the files the generated code will be stored in. If your target language is C, the file name doesn't matter much (as long as it has the .c file extension). If your target language happens to be Java, however, the file name makes quite a bit of a difference...

Testing Your Implementation

To test your implementation, you can use these sample programs. Since we haven't included comments in the textual syntax (or the model, for that matter), we'll just have to hope that the programs are self-explanatory...

...