/minijava

Implementation of a subset of Java for the GEMOC Studio

Primary LanguageJavaEclipse Public License 1.0EPL-1.0

MiniJava: a subset of Java as an executable DSL

This repository contains the complete definition of MiniJava, a "re-implementation" of a small subset of Java as an executable DSL. MiniJava was created in the Eclipse GEMOC Studio Language Workbench, and can be deployed in a Eclipse GEMOC Studio Modeling Workbench for creating, executing and debugging MiniJava programs.

Example of a valid MiniJava program:

class BubbleSorter {

    public void bubbleSort(int[] arr, int n) {
        if (n != 1) {
            for (int i = 0; i<n-1; i = i+1) {
                if (arr[i]> arr[i+1]) {
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                }
            }
            this.bubbleSort(arr, n-1);
        }
    }

    public void display(int[] arr) {
        for (int i = 0;
        i<arr.length; i = i +1) {
            System.out.println(arr[i]);
        }
    }
}

class Main {
    public static void main(String[] args) {
        BubbleSorter bs = new BubbleSorter();
        int[] arr = new int[6];
        arr[0] = 6;
        arr[1] = 4;
        arr[2] = 5;
        arr[3] = 12;
        arr[4] = 11;
        arr[5] = 9;
        bs.bubbleSort(arr, arr.length);
        bs.display(arr);
    }
}

1. Description

1.1. Scope

MiniJava is an interpreted object-oriented imperative language that shares the same syntax as a small subset of older versions of Java (near 1.4−1.5). It was designed only for research purposes, to have an easily accessible language that shares some of the complexity of a "real-world" programming language, while built with the same tools and technologies as Domain-Specific Languages (DSLs).

In principle, every MiniJava program (using the .minijava extension) is a valid Java program with the same semantics.

Some features of MiniJava (not exhaustive):

  • Support interfaces, classes, methods (which can be public or private) ;

  • Support loops (while, for), and simple conditionals (if) ;

  • Support integers (int), strings (String), booleans (boolean), arrays (eg. int[]) ;

  • Support imports (import) ;

  • Support all common arithmetic and boolean operators.

Some differences with Java (not exhaustive):

  • It is possible to have multiple type declarations in the same MiniJava file, and the name of a MiniJava file does not have to match the name of a type declaration that is found in the file ;

  • The package declaration at the beginning of a file will apply to all type declarations that are found in the file, and the folder structure does not have to match the package declaration ;

  • Some typical Java attributes and methods are hard-coded as MiniJava keywords, such as System.out.println or myArray.length ;

  • The static modifier on methods is available in the syntax, but not properly handled (except for finding the main method) ;

  • When using attributes and methods of an enclosing class, the keyword this must always be used explicitly ;

  • Many operators missing, such as increment/decrement operators (eg`i++`) ;

  • No switch ;

  • No empty return statement ;

  • No direct creation of arrays directly values (eg. int[] x = {1,4,5,6}) ;

  • No generic types (eg. List<Integer>).

1.2. Repository structure

The repository comes with two folders:

  • language_workbench contains the language definition in the form of a set of Eclipse plugins that can be loaded in the Eclipse GEMOC Studio (see next sections).

  • modeling_workbench contains one Eclipse project with a couple of MiniJava programs that can be executed.

1.3. Implementation

MiniJava was developped using the Eclipse GEMOC Studio Language Workbench:

  • Ecore is used to define the abstract syntax and to define the runtime state of the interpreter (org.tetrabox.minijava.model plugin),

  • Xtext is used to define the concrete textual syntax (org.tetrabox.minijava.xtext plugin),

  • Kermeta is used to define the operational semantics, ie. the interpreter (org.tetrabox.minijava.semantics plugin).

2. Compilation and use

  1. To compile MiniJava, start the Eclipse GEMOC Studio, import all plugin projects from the language_workbench folder, and wait until the compilation is done. If there are no errors, then the compilation is done.

  2. To use MiniJava, first do the compilation (previous point), then start a new development Eclipse instance (Run Configurations

    • To write a MiniJava program, create a file with the .minijava extension, and start coding.

    • To run a MiniJava program, create a new Run Configuration of type Executable model with GEMOC Java engine, where you select the model and the language, and then press Run.

    • To debug a MiniJava program, use the Run Configuration from the previous point, but this time check the Break at start box, and press the Debug button. The execution will then start in debug mode from the beginning of the execution (note: breakpoints are not available).

    • If you want you can import the Eclipse project available in modeling_workbench, and have a look at (and/or run) the provided examples.