# Wiki source code of Code Generation with Xtend

Last modified by Richard Kreissig on 2025/01/30 12:03
Export
Office Formats
Export as PDF Export as ODT Export as RTF
Other Formats

Select the pages to export:

select all/ none

Legend:
  Created Page
  Modified Extension Page
  Clean Extension Page

Export as HTML

Show last authors

author | version | line-number | content
--- | --- | --- | ---
|  | 1 | 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...
|  | 2 |
|  | 3 | 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.
|  | 4 |
|  | 5 | The slides to the presentation that accompanied this tutorial [[can be found here>>attach:presentation.pdf]].
|  | 6 |
|  | 7 | **Contents**
|  | 8 |
|  | 9 |
|  | 10 |
|  | 11 | {{toc/}}
|  | 12 |
|  | 13 | = Preliminaries =
|  | 14 |
|  | 15 | You should have everything you need for this tutorial:
|  | 16 |
|  | 17 | * The Turing Machine metamodel you created in the third tutorial.
|  | 18 | * The Imperative Programming Language metamodel you downloaded in the previous tutorial.
|  | 19 | * The Turing-Machine-to-Imperative-Programming-Language transformation you developed in the previous tutorial.
|  | 20 |
|  | 21 | This tutorial will make xtendsive use of [[template expressions>>url:http://www.eclipse.org/xtend/documentation.html#templates||shape="rect"]], so be sure to read up on those.
|  | 22 |
|  | 23 | = Generating Code with Xtend =
|  | 24 |
|  | 25 | We will of course need a new Xtend class that will take care of the code generation.
|  | 26 |
|  | 27 | 1. Add a new //Xtend Class// to the compiler project, preferrably in a new package called {{code language="none"}}de.cau.cs.rtprak.login.compiler.codegen{{/code}}.
|  | 28 | 1. (((
|  | 29 | Add a method to your class that starts the code generation. It can look something like this:
|  | 30 |
|  | 31 | {{code}}
|  | 32 | /**
|  | 33 | * Generates Java code for the given imperative program.
|  | 34 | *
|  | 35 | * @param program the imperative program to generate code for.
|  | 36 | * @return code that implements the imperative program.
|  | 37 | */
|  | 38 | def String generateCode(Program program) '''
|  | 39 | YOUR CODE GENERATION
|  | 40 | '''
|  | 41 | {{/code}}
|  | 42 | )))
|  | 43 | 1. 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. Make sure that your code generation supports transformed Turing Machines as well as the sample programs we provide below.
|  | 44 |
|  | 45 | Here's a few hints as to what your implementation should support:
|  | 46 |
|  | 47 | * 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.
|  | 48 | * 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.
|  | 49 | * Try to ensure that the generated code is readable and properly indented.
|  | 50 |
|  | 51 | = Making the Code Generation Available =
|  | 52 |
|  | 53 | As in the previous tutorial, add a menu contribution to the {{code language="none"}}...compiler.ui{{/code}} 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.
|  | 54 |
|  | 55 | {{info title="File Names"}}
|  | 56 | 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...
|  | 57 | {{/info}}
|  | 58 |
|  | 59 | == Testing Your Implementation ==
|  | 60 |
|  | 61 | To test your implementation, you can use [[these sample programs>>attach:pseudo_samples.zip]]. 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...

# Quick Links

KIELER on GitHub
RTSYS Homepage

XWiki 14.4.7