Fern   davin.50webs.com
New Zealanders making it harder to hate computers

       Main Menu        Research Projects       Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address     Computer Games        Web Design           Java Programming       The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      


S.J.S. tutorial 18: Mapping S.J.S. to Java

SECTION(18, Mapping S.J.S. to Java) Here is how to map from S.J.S. to Java:
NU()function ARROW static EOL classVar ARROW static EOL property ARROW nothing EOL method ARROW nothing EOL constructor ARROW nothing EOL begin ARROW SQU EOL end ARROW GLY EOL beginMain ARROW PUBLIC STATIC VOID main(STRING[] args) SQU EOL endMain ARROW GLY EOL and ARROW AMP()AMP() EOL or ARROW BAR()BAR() EOL
NU()Here is an S.J.S. program:
NU()CLASS HelloWorld EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD SYSTEM_OUT_PRINTLN("Hello, World!") EOL PD END_MAIN EOL END EOL
NU()Here is the same S.J.S. program, after conversion to the Java language:
NU()CLASS HelloWorld EOL SQU EOL PD PUBLIC STATIC VOID main(STRING[] args) EOL PD SQU EOL PD PD SYSTEM_OUT_PRINTLN("Hello, World!") EOL PD GLY EOL GLY EOL
NU()Note that these S.J.S. 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 S.J.S. onto Java but this approach had the disadvantage that keywords like BEGIN and END inside strings were mapped to their Java equivalents like so:
NU()SYSTEM_OUT_PRINTLN("function"); ARROW SYSTEM_OUT_PRINTLN("static"); EOL SYSTEM_OUT_PRINTLN("classVar"); ARROW SYSTEM_OUT_PRINTLN("static"); EOL SYSTEM_OUT_PRINTLN("property"); ARROW SYSTEM_OUT_PRINTLN(""); EOL SYSTEM_OUT_PRINTLN("method"); ARROW SYSTEM_OUT_PRINTLN(""); EOL SYSTEM_OUT_PRINTLN("constructor"); ARROW SYSTEM_OUT_PRINTLN(""); EOL SYSTEM_OUT_PRINTLN("begin"); ARROW SYSTEM_OUT_PRINTLN("SQU"); EOL SYSTEM_OUT_PRINTLN("end"); ARROW SYSTEM_OUT_PRINTLN("GLY"); EOL SYSTEM_OUT_PRINTLN("beginMain"); ARROW SYSTEM_OUT_PRINTLN("public static void main(String[] args) {"); EOL SYSTEM_OUT_PRINTLN("endMain"); ARROW SYSTEM_OUT_PRINTLN("}"); EOL SYSTEM_OUT_PRINTLN("and"); ARROW SYSTEM_OUT_PRINTLN("AMP()AMP()"); EOL SYSTEM_OUT_PRINTLN("or"); ARROW SYSTEM_OUT_PRINTLN("BAR()BAR()"); EOL
NU()which is of course the wrong behaviour. A hack to get around this limitation is to break apart the S.J.S. keywords like so:
NU()SYSTEM_OUT_PRINTLN("be" + "gin"); EOL
NU()This problem can be fixed for good either by using Flex to compile S.J.S. 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 S.J.S. 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 S.J.S. to Java. This implies that GNU Emacs must be present on the client's system to do the S.J.S. 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 S.J.S. keywords, comments and strings have syntax highlighting. And number two is that Emacs can do correct automatic indentation of S.J.S. code. SUBSECTION(18.2, Using sed to transform the output) Output from the executables javac and java have their standard error stream piped into the UNIX program sed so that references to Java files in error messages like (Foo.java:123) actually point to the original S.J.S. file like it should (Foo.sjs:123). This implies that sed must be present in the client's system to build *.class files using javac and to run *.class files with java. SUBSECTION(18.3, Makefile for building *.sjs into *.java and running *.class files) Here is the GNU Makefile for building *.java files and *.class files: .PRECIOUS: %.java %.class JAVAC_FLAGS = -source 1.5 -Xlint:unchecked -Xlint:deprecation JAVA_FLAGS = -enableassertions SHELL = /usr/bin/bash %.java: %.sjs @echo "* Stage 1 : Debugging *.sjs and building *.java file" emacs --batch --eval "(setq target \"\$*.sjs\")" --load sjs2java.el %.class: %.java @echo "* Stage 2 : Debugging *.java and building *.class file" (javac $(JAVAC_FLAGS) $*.java >compilation 2>&1) \ || !(sed -e "s/\\(^[a-zA-Z0-9_-]*\\)\\.java:/\\1.sjs:/g" compilation) %.run: %.class @echo "* Stage 3 : Running *.class File" java $(JAVA_FLAGS) $* 2>&1 \ | sed -e "s/(\\([a-zA-Z0-9]*\\)\\.java:/(\\1.sjs:/g"
Back to Main Menu
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Programming | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |

Please report any broken links to the Web-person
Last modified: Tue Mar 25 12:27:00 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.