Welcome to the second tutorial about Xtend. This time, you will take imperative language models and generate code for them in your favourite programming language. While we personally recommend Java, there's nothing stopping you from generating C code, PHP code, or anything else the constructs of our simple language model can be transformed into. We would just like to ask you to refrain from generating code in any esoteric programming language...
The code generation you will implement here should not be optimized for generating the kinds of programs your transformation generates. Instead, we expect it to work for any valid program model. Further down, you will find an archive with example programs you can test your code generation on.
The slides to the presentation that accompanied this tutorial can be found here.
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.
We will of course need a new Xtend class that will take care of the code generation.
de.cau.cs.rtprak.login.compiler.codegen
.Add a method to your class that starts the code generation. It can look something like this:
/** * 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 ''' |
Here's a few hints as to what your implementation should support:
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.
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...
This tutorial was originally created by Christoph Daniel Schulze and Miro Spönemann for the Eclipse Project WT 12/13. |