GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      
      Tutorial 1             Tutorial 2               Tutorial 3                   Tutorial 4                 Tutorial 5      
      Tutorial 6             Tutorial 7               Tutorial 8                   Tutorial 9                 Tutorial 10      
      Tutorial 11             Tutorial 12               Tutorial 13                   Tutorial 14                 Tutorial 15      
      Tutorial 16             Tutorial 17             Tutorial 18              Using Emacs            Download Links    


J.T.W. tutorial 18: Advanced J.T.W.


§ 18 Advanced J.T.W.


§ 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("var");-> System.out.println("");
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 software and 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, comments and strings have syntax highlighting. And number two is that Emacs can do correct automatic indentation of J.T.W. code.


§ 18.2 Makefile for building *.jtw into *.java and running *.class files

Here is the GNU Makefile:

.PRECIOUS: 
.PRECIOUS: %.java %.class 

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

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

%.class: %.java 
      @echo "* Stage 2 : Debugging *.java and building *.class file(s)" 
      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]://g" - 

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

build: clean 
  

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


§ 18.3 Building a collection of classes or an entire package

When your class X uses another class Y which is in a different file from the X class, then you need to add to the build target which is initially like so:

build: clean
  

to what follows:

build: clean Y.java
  

If your class Y is in another package such as the class ~/jtw-tutorials/path/to/dir/Y.class i.e. in the package path.to.dir then you need to add to the build target like so:

build: clean path/to/dir/Y.java
  

This process should be repeated for every class that is called, directly or indirectly from your main class X. This process can be applied to build an entire package when you simply issue the command make build. To actually build and run the X class, let ~/jtw-tutorials/path2/to/dir/X.class be the location of the X class. Then you need to invoke the following Makefile target:

make build path2/to/dir/X.run
  

The build target calls the "clean" target which deletes all *.java and *.class files directly or indirectly in the folder ~/jtw-tutorials. If you don't do this then java might run an old version of *.class files despite earlier errors in the build process. This is because the use of pipes in building and executing *.class files hides the return values of the programs javac and java.


§ 18.4 Running javadoc on a package

To invoke javadoc, you need to issue the following command from the folder ~/jtw-tutorials:

make build
  

See §18.3 for more information about setting up the build target. Then you need to issue the following command from the folder ~/jtw-tutorials:

javadoc path3/to/pkg -d /path4/to/dir
  

where path3.to.pkg is the name of the package that you want to build (in the location ~/jtw-tutorials/path3/to/dir) and /path4/to/dir is the desired location for your documentation files in *.html format.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Mon Jun 19 10:16:43 NZST 2017
Best viewed at 800x600 or above resolution.
© Copyright 1999-2017 Davin Pearson.
Please report any broken links to