Child pages
  • Model Transformation with Xtend
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Warning

This tutorial is not finished. Don't start working on it yet!

This installment of our little series of tutorials will be all about Xtend, a programming language that looks very similar to Java, but which adds some very convenient features. Xtend code compiles to Java and and was developed using Xtext. In fact, once you gain experience working with Xtend you will probably appreciate the power of Xtext even more.

In this tutorial, we will focus on two particular areas where Xtend excels:

  • Model transformation. You will be using Xtend to transform a given Turing Machine into a model of a simple programming language.
  • Code generation. You will be using Xtend to generate code for a given model of the simple programming language in an arbitrary programming language (except perhaps Brainfuck or Whitespace). The generated program will implement and simulate the Turing Machine without relying on its model.

In essence, you will be following the way compilers generate code: generate an abstract model representation of the program, and turn that representation into actual code.

As in the previous tutorial, we refer you to the Xtend documentation instead of explaining everything in this tutorial.

Exciting stuff, so let's begin.

Contents

Model Transformation

The first half of this tutorial will be all about the metamodel of the simple programming language.

Preliminaries

Let's start by generating the model code and familiarizing ourselves with the metamodel:

  1. Download the metamodel (TODO: INSERT META MODEL LINK)
  2. Add a new plug-in project for the metamodel, import the ecore file, and generate a diagram for it.
  3. Familiarize yourself with the model. It basically follows the typical structure of common programming languages. In fact, if you're a bit familiar with the abstract syntax of Java, you should feel right at home. (If not, this might be an excellent opportunity to learn a bit about Java's abstract syntax – knowing about this kind of stuff will deepen your understanding of the language.)
  4. Generate code from the metamodel.

Writing the Transformation

Now that you have code-generated (yes, that's a verb) the metamodel it's time to start working on your transformation:

  1. Add a new plug-in project de.cau.cs.rtprak.login.compiler to your workspace. Be sure to uncheck the option This plug-in will make contributions to the UI. Add dependencies to the two projects containing the Turing Machine metamodel and the programming language metamodel.
  2. Add an Xtend Class to your project. The class should be placed in a subpackage where all the transformation code will go, such as de.cau.cs.rtprak.login.compiler.transform.
  3. You will notice that your new class is marked with an error marker because of a missing dependency of the new plug-in project to org.eclipse.xtext.xbase.lib. If you hover over the error with your mouse, you can have Eclipse add all libraries required by Xtend to your project.
  4. Define an entry method for the transformation that takes a TuringMachine instance as an argument and returns a Program. You can use the following definition as a starting point:

    /**
     * Transforms a given Turing Machine into an imperative program model.
     * 
     * @param machine the Turing Machine to transform into an imperative
     *                program.
     * @return a program model that implements the Turing Machine. 
     */
    def Program transformTuringToImperative(TuringMachine machine) {
        // Create the program we will transform the Turing Machine into
        val program = ImperativeFactory::eINSTANCE.createProgram()
    
        // TODO: Initialize program appropriately
    
        // TODO: Call methods that generate the Program model
    
        // Return the transformed program
        program
    } 

    There's a few points to note here:


    • Lines in Xtend code don't have to and with a semicolon.
    • We have been explicit about the method's return type, but we could have easily omitted it, letting Xtend infer the return type.
    • The keyword val declares a constant, while var declares a variable. Try to make do with constants where possible.
    • The methods you call should be declared as def private since they are implementation details and shouldn't be called by other classes.
  5. Add code to transform the Turing Machine to an imperative program model. The imperative program metamodel contains enough stuff to implement Turing Machines.

Code Generation

TODO: Write this section.

 

  • No labels