GNU Java Training Wheels 1.1: Mapping J.T.W. to Java

Next: Piping the output of javac and java, Up: Tutorial 18 Advanced J.T.W. [Contents][Index]

#### 4.18.1 Mapping J.T.W. to Java

Here is how to map from J.T.W. to Java:

`function` | -> `static`
`var` | -> nothing
`classVar` | -> `static`
`property` | -> nothing
`method` | -> nothing
`constructor` | -> nothing
`begin` | -> {
`end` | -> }
`beginMain` | -> ``public` `static` void `main`(String[] args) {`
`endMain` | -> }
`and` | -> `&&`
`or` | -> `||`
`then` | -> nothing
`elseif` | -> `else` `if`

Here is an J.T.W. program:

    class HelloWorld
    begin
        beginMain
            System.out.println("Hello, World!")
        endMain
    end

Here is the same J.T.W. program, after conversion to the Java language:

    class HelloWorld
    {
        public static void main(String[] args)
        {
            System.out.println("Hello, World!")
        }
    }

Note that these J.T.W. keywords on the left hand side of the above diagram should not map to their Java equivalents inside strings and comments. The transformation was originally written to use the m4 language to map J.T.W. onto Java but this approach had the disadvantage that keywords like begin and end inside strings were mapped to their Java equivalents like so:

`System.out.println("function");` | `-> System.out.println("static");`
`System.out.println("classVar");` | `-> System.out.println("static");`
`System.out.println("property");` | `-> System.out.println("");`
`System.out.println("method");` | `-> System.out.println("");`
`System.out.println("constructor");` | `-> System.out.println("");`
`System.out.println("begin");` | `-> System.out.println("{");`
`System.out.println("end");` | `-> System.out.println("}");`
`System.out.println("beginMain");` | `-> System.out.println("public static void main(String[] args) {");`
`System.out.println("endMain");` | `-> System.out.println("}");`
`System.out.println("and");` | `-> System.out.println("&&");`
`System.out.println("or");` | `-> System.out.println("||");`
`System.out.println("then");` | `-> System.out.println("");`
`System.out.println("elseif");` | `-> System.out.println("else if");`

which is of course the wrong behaviour. A hack to get around this limitation is to break apart the J.T.W. keywords like so:

    System.out.println("be" + "gin");)

This problem can be fixed for good either by using Flex to compile J.T.W. into Java or to use Emacs to do the same thing, only a little slower than what Flex can do. In the end I chose GNU Emacs as the host for the preprocessor language J.T.W. because it is free, libre and open source software, is adequate for my programming needs and is more powerful than Flex or m4. To remedy this deficiency Emacs’ batch mode is used to do the transformation from J.T.W. to Java. This implies that GNU Emacs must be present on the client’s system to do the J.T.W. to Java mapping. Of course, there is no compulsion to use Emacs as an editor, although there are a couple of advantages in doing this. Number one is that J.T.W. keywords and comments have automatic `syntax highlighting`. And number two is that Emacs can do correct automatic indentation of J.T.W. code.

Next: Piping the output of javac and java, Up: Tutorial 18 Advanced J.T.W. [Contents][Index]