
@node Mapping J.T.W. to Java
@subsection Mapping J.T.W. to Java

@cindex Mapping from TT(*.jtw) -> TT(*.java)
@cindex J.T.W. internal details
@cindex Internal details of the J.T.W. system

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

m4_changequote(,)

@multitable @columnfractions .1 .7
@item FUNCTION
@tab -> STATIC
@item VAR
@tab -> EM(nothing)
@item CLASSVAR
@tab -> STATIC
@item PROPERTY
@tab -> EM(nothing)
@item METHOD
@tab -> EM(nothing)
@item CONSTRUCTOR
@tab -> EM(nothing)
@item BEGIN
@tab -> @{
@item END
@tab -> @}
@item BEGINMAIN
@tab -> TT(POBLIC STATIC void MAIN()(String[] args) @{)
@item ENDMAIN
@tab -> @}
@item AND
@tab -> TT(BO(&&))
@item OR
@tab -> TT(BO(||))
@item THEN
@tab -> EM(nothing)
@item ELSEIF
@tab -> ELSE IF
@end multitable

Here is an J.T.W. program:

@example
CLASS HelloWorld
BEGIN
PD BEGINMAIN
PD PD System.out.println("Hello, World!")
PD ENDMAIN
END
@end example

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

@example
CLASS HelloWorld
@{
PD POBLIC STATIC void MAIN()(String[] args)
PD @{
PD PD System.out.println("Hello, World!")
PD @}
@}
@end example

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:

m4_dnl m4_undefine([main])

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

m4_changequote([,])

NI()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:

@example
System.out.println("be" + "gin");)
@end example

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
TT(BO(syntax) EM(highlighting)). And number two is that Emacs can do
correct automatic indentation of J.T.W. code.

@node       Piping the output of javac and java
@subsection Piping the output of javac and java
@cindex     Piping the output of javac and java

Output from the executables JAVAC and JAVA have their standard output
stream and standard error stream piped into Emacs' batch mode so that
error messages like TT(Foo.java:123) point back to the correct file
TT(Foo.jtw:123) even if file inclusion (@ref{J.T.W. Proof of concept
#2 file inclusion}) has been used.  The programs TT(grep) and TT(sed)
are also used as pipes in the transformation process so they must be
present on the client's system.

@node       Makefile for building *.jtw into *.java and running *.class files
@subsection Makefile for building *.jtw into *.java and running *.class files
@cindex     TT(Makefile) for generating GRY(*.class) files from TT(*.jtw) files

Here is the EM(GNU Makefile) for building TT(*.java) files and
TT(*.class) files and running them.

@example
.PRECIOUS:
.PRECIOUS: %.java %.class

JAVAC_FLAGS = -source 1.5 -Xlint:unchecked -Xlint:deprecation
JAVA_FLAGS  = -enableassertions
SHELL       = /bin/bash

%.java: %.jtw
PD PD @@echo "* Stage 1 : Debugging $*.jtw and building $*.java file"
PD PD emacs --batch --eval "(setq *stump* \"$*\")" --load jtw-build-jtw.el --funcall doit

%.class: %.java
PD PD @@echo "* Stage 2 : Debugging *.java and building *.class file(s)"
PD PD javac $(JAVAC_FLAGS) $$(find . -name "*.java") |& emacs --batch --eval "(setq *stump* \"$*\")" --load jtw-javac.el --funcall doit |& grep "input[0-9]:" - |& sed -e "s/input[0-9]:s//g" -

%.run: %.class
PD PD @@echo "* Stage 3 : Running $*.class File"
PD PD java $(JAVA_FLAGS) $* |& emacs --batch --load jtw-java.el --funcall doit |& grep "input[0-9]*:" - |& sed -e "s/input[0-9]*://g" -

build: clean
@end example

NI()The first line TT(.PRECIOUS) without any arguments clears the list
of precious files, the list of files not to delete during the build
process.

