GNU Java Training Wheels 1.1: J.T.W. Proof of concept #2 file inclusion

Next: J.T.W. Tutorials, Previous: J.T.W. Proof of concept #1 A superfor macro, Up: Top [Contents][Index]

## 3 J.T.W. Proof of concept #2 file inclusion

When your classes become large and unwieldy, it becomes useful to split a source file into several compilation units. the most natural division into compilation units is at the level of methods. with each method in a separate file you can manage methods that are excessively large. here is how to use file inclusion in the J.T.W. language. first comes the `Foo.jtw` file with all bodies of methods harvested from them:

    class Foo
    begin
        include "Foo-apple.method";
        include "Foo-banana.method";
        include "Foo-carrot.method";
    end

Here are the files that get included. the first file is `Foo-apple.method`:

    property int prop1; /* declares a property for use with the apple method. */

    method void apple(/* parameters */)
    begin
        prop1 = prop1 + 1;
        /* rest of body of apple method */
    end

the second file is `Foo-banana.method`:

    method void banana(/* parameters */)
    begin
        /* body of banana method */
    end

the third file is `Foo-carrot.method`:

    method void carrot(/* parameters */)
    begin
        /* body of carrot method */
    end

when all of the file inclusions have been carried out by the J.T.W. to `java` compiler, the code that `javac` sees will be something like this:

    /* Automatically generated file. Do not edit! */
    // #foomatic #location (Foo.jtw:1)
    class Foo
    {
        // #foomatic #location (apple.method:1)
        int prop1; /* declares a property for use with the apple method. */

        void apple(/* parameters */)
        {
            prop1 = prop1 + 1;
            /* rest of body of apple method */
        }
        // #foomatic #location (banana.method:1)
        void banana(/* parameters */)
        {
            /* body of banana method */
        }
        // #foomatic #location (carrot.method:1)
        void carrot(/* parameters */)
        {
            /* body of carrot method */
        }
        // #foomatic #location (Foo.jtw:6)
    }

Note the use of the value `#foomatic` of the string `*pp-namespace*` (where pp stands for pre-processor) that is a long arbitrarily defined string to prevent accidental aliasing with the rest of the commented code that the user of the system might write. The `#location` directives are used to keep track of the original line number in the source file. Using Emacs batch mode executing the Elisp code: `jtw-build-jtw.el`, error messages in `Foo.java` now point back to the original `Foo.jtw` file, or one of the files that get `#include`d like so: `apple.method`, `banana.method` or `carrot.method`. If you are particularly clever, you can reuse the same `method` in different classes.

Version 1.0 of J.T.W. used the C Pre-Processor (`cpp`) to manage the `#location` directives but unfortunately `cpp` destroys comments in the target file, and Java uses /** ... */ comments to document the program’s behaviour so `cpp` cannot be used.

Next: J.T.W. Tutorials, Previous: J.T.W. Proof of concept #1 A superfor macro, Up: Top [Contents][Index]