Toward Terser Java  
Author Message
ram





PostPosted: 2005-7-9 23:49:00 Top

java-programmer, Toward Terser Java Roedy Green <email***@***.com> writes:
>Sometimes I just long for C preprocessor macro ability for when
>Java won't let you encapsulate patterns without a fight.

I could not resist that temptation.

I have a very early version of such an experimental
preprocessor, which I develope just for fun.

It is written in Java and called "Metava". To simplify the
implementation, it uses a package that can read extended
S-expressions in a language, which I call "Unotal".

My first goal is to have Metava to be able to pre-process
itself to Java, so that I can write Metava in Metava.

This is not yet finished; what works so far is that the
following input (which is the very start of the preprocessor
source code only) is translated to Java.

< import =
< de.dclj.ram.notation.unotal.RoomSource
de.dclj.ram.notation.unotal.StringValue
de.dclj.ram.notation.unotal.SetValue
de.dclj.ram.notation.unotal.FileRoomModule.fileRoom >

Hello =
< verbose = < from=String < &ePrint it >>
error = < from=String < &ePrint it >>
warning = < from=String < &ePrint it >>
emit = < from=String < &write it >>
emitLine = < < &line > < &emitIndentation >>
quit = < < &verbose ["quitting"] >< &System.exit 99 >>
indentation = < type=int >
indent = << &increment indentation by=2 >>
outdent = << &decrement indentation by=2 >>
emitIndentation = < < &repeat times=indentation < &write [" "] > >>
emitLine = < from=int < &repeat < &line >>< &emitIndentation >>
emitOpen = < < &emitLine >< &emit ["{ "] >< &indent >>
emitLeft = < from=boolean < &emitLine >< &emit ["("] >< &if <&emit [" "]>> <&indent> >
emitClose = < from=boolean < &if <&emit [" "]>>< &emit ["}"]>< &outdent >>
emitRight = < from=boolean < &if <&emit [" "]>>< &emit [")"]>< &outdent >>
< mainClass = < type=String value=["Hello"] >
source = < type=RoomSource >
< &verbose ["OK 555E6k: main"] >>>>>

The last three lines are supposed to become the body of the
main-method, but I see from the output, that a bug still
prevents it to be generated from them.

There are some fixed ad-hoc abbreviations for names used
often, like "ePrint" for "java.lang.System.err.printLn", but I
try to keep their number small.

The point is that I can implement whatever notation I would
like to have in that preprocessor. When I write a program with
it and miss a notation, I can add it to the preprocessor and
use it.

To explain a procedure definition in more detail as an example:

emitLine = < from=int < &repeat < &line >>< &emitIndentation >>

defines a procedure "emitLine". The name of the int-Parameter
defaults to "it". The "&repeat" keyword generates a loop to
repeat something a number of times, which - if unspecified -
also defaults to "it". So this gives "emitLine" as it can
be seen in the following Java-source-code that was generated
from the Metava-Source-code above:

import de.dclj.ram.notation.unotal.RoomSource;
import de.dclj.ram.notation.unotal.StringValue;
import de.dclj.ram.notation.unotal.SetValue;
import static de.dclj.ram.notation.unotal.FileRoomModule.fileRoom;

public class
Hello
{
public static void
emitClose
( final boolean it )
{
if
( it )
{
emit
( " " ); }
emit
( "}" );
outdent
(); }

public static void
emitIndentation
()
{
for
( int i = indentation; i-- > 0; )java.lang.System.out.print
( " " ); }

public static void
outdent
()
{
indentation -= 2;
}

public static void
emitLeft
( final boolean it )
{
emitLine
();
emit
( "(" );
if
( it )
{
emit
( " " ); }
indent
(); }

public static int indentation;

public static void
error
( final String it )
{
java.lang.System.err.println
( it ); }

public static void
quit
()
{
verbose
( "quitting" );
System.exit
( 99 ); }

public static void
emit
( final String it )
{
java.lang.System.out.print
( it ); }

public static void
main
( final java.lang.String[] it )
{ }

public static void
warning
( final String it )
{
java.lang.System.err.println
( it ); }

public static void
emitRight
( final boolean it )
{
if
( it )
{
emit
( " " ); }
emit
( ")" );
outdent
(); }

public static void
verbose
( final String it )
{
java.lang.System.err.println
( it ); }


public static void
emitLine
( final int it )
{
for
( int i = it; i-- > 0; )line
();
emitIndentation
(); }

public static void
emitLine
()
{
line
();
emitIndentation
(); }

public static void
indent
()
{
indentation += 2;
}

public static void
emitOpen
()
{
emitLine
();
emit
( "{ " );
indent
(); }}

 
ram





PostPosted: 2005-7-9 23:49:00 Top

java-programmer >> Toward Terser Java Roedy Green <email***@***.com> writes:
>Sometimes I just long for C preprocessor macro ability for when
>Java won't let you encapsulate patterns without a fight.

I could not resist that temptation.

I have a very early version of such an experimental
preprocessor, which I develope just for fun.

It is written in Java and called "Metava". To simplify the
implementation, it uses a package that can read extended
S-expressions in a language, which I call "Unotal".

My first goal is to have Metava to be able to pre-process
itself to Java, so that I can write Metava in Metava.

This is not yet finished; what works so far is that the
following input (which is the very start of the preprocessor
source code only) is translated to Java.

< import =
< de.dclj.ram.notation.unotal.RoomSource
de.dclj.ram.notation.unotal.StringValue
de.dclj.ram.notation.unotal.SetValue
de.dclj.ram.notation.unotal.FileRoomModule.fileRoom >

Hello =
< verbose = < from=String < &ePrint it >>
error = < from=String < &ePrint it >>
warning = < from=String < &ePrint it >>
emit = < from=String < &write it >>
emitLine = < < &line > < &emitIndentation >>
quit = < < &verbose ["quitting"] >< &System.exit 99 >>
indentation = < type=int >
indent = << &increment indentation by=2 >>
outdent = << &decrement indentation by=2 >>
emitIndentation = < < &repeat times=indentation < &write [" "] > >>
emitLine = < from=int < &repeat < &line >>< &emitIndentation >>
emitOpen = < < &emitLine >< &emit ["{ "] >< &indent >>
emitLeft = < from=boolean < &emitLine >< &emit ["("] >< &if <&emit [" "]>> <&indent> >
emitClose = < from=boolean < &if <&emit [" "]>>< &emit ["}"]>< &outdent >>
emitRight = < from=boolean < &if <&emit [" "]>>< &emit [")"]>< &outdent >>
< mainClass = < type=String value=["Hello"] >
source = < type=RoomSource >
< &verbose ["OK 555E6k: main"] >>>>>

The last three lines are supposed to become the body of the
main-method, but I see from the output, that a bug still
prevents it to be generated from them.

There are some fixed ad-hoc abbreviations for names used
often, like "ePrint" for "java.lang.System.err.printLn", but I
try to keep their number small.

The point is that I can implement whatever notation I would
like to have in that preprocessor. When I write a program with
it and miss a notation, I can add it to the preprocessor and
use it.

To explain a procedure definition in more detail as an example:

emitLine = < from=int < &repeat < &line >>< &emitIndentation >>

defines a procedure "emitLine". The name of the int-Parameter
defaults to "it". The "&repeat" keyword generates a loop to
repeat something a number of times, which - if unspecified -
also defaults to "it". So this gives "emitLine" as it can
be seen in the following Java-source-code that was generated
from the Metava-Source-code above:

import de.dclj.ram.notation.unotal.RoomSource;
import de.dclj.ram.notation.unotal.StringValue;
import de.dclj.ram.notation.unotal.SetValue;
import static de.dclj.ram.notation.unotal.FileRoomModule.fileRoom;

public class
Hello
{
public static void
emitClose
( final boolean it )
{
if
( it )
{
emit
( " " ); }
emit
( "}" );
outdent
(); }

public static void
emitIndentation
()
{
for
( int i = indentation; i-- > 0; )java.lang.System.out.print
( " " ); }

public static void
outdent
()
{
indentation -= 2;
}

public static void
emitLeft
( final boolean it )
{
emitLine
();
emit
( "(" );
if
( it )
{
emit
( " " ); }
indent
(); }

public static int indentation;

public static void
error
( final String it )
{
java.lang.System.err.println
( it ); }

public static void
quit
()
{
verbose
( "quitting" );
System.exit
( 99 ); }

public static void
emit
( final String it )
{
java.lang.System.out.print
( it ); }

public static void
main
( final java.lang.String[] it )
{ }

public static void
warning
( final String it )
{
java.lang.System.err.println
( it ); }

public static void
emitRight
( final boolean it )
{
if
( it )
{
emit
( " " ); }
emit
( ")" );
outdent
(); }

public static void
verbose
( final String it )
{
java.lang.System.err.println
( it ); }


public static void
emitLine
( final int it )
{
for
( int i = it; i-- > 0; )line
();
emitIndentation
(); }

public static void
emitLine
()
{
line
();
emitIndentation
(); }

public static void
indent
()
{
indentation += 2;
}

public static void
emitOpen
()
{
emitLine
();
emit
( "{ " );
indent
(); }}

 
Owen Jacobson





PostPosted: 2005-7-10 6:44:00 Top

java-programmer >> Toward Terser Java On Tue, 05 Jul 2005 10:55:30 +0000, Roedy Green wrote:

> Sometimes I just long for C preprocessor macro ability for when Java won't
> let you encapsulate patterns without a fight.

Nothing's particularly stopping you from making m4 or even cpp part of
your build process. It won't play nice with javac's automatic dependency
resolution, but that's not impossible to work around (just run m4 on the
whole source tree before doing any compilation).

Make sure you really need that before implementing such a beast.

-O
 
 
Roedy Green





PostPosted: 2005-7-10 13:23:00 Top

java-programmer >> Toward Terser Java On Sat, 9 Jul 2005 13:38:40 GMT, Tim Tyler <email***@***.com> wrote or
quoted :

>That really was bad, though. At least these days machines can
>read and write source code - and do some editing tasks for us.
>
>The preprocessor was one of the things that made that *very*
>challenging to do properly in C/C++.

I don't want it back, just the ability to encapsulate any sort of
repetitive pattern. I can write "stompers" - code generators and
skeleton generators written in Java to get you started, but they are
not so hot for maintenance.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
 
Tim Tyler





PostPosted: 2005-7-10 16:05:00 Top

java-programmer >> Toward Terser Java Owen Jacobson <email***@***.com> wrote or quoted:
> On Tue, 05 Jul 2005 10:55:30 +0000, Roedy Green wrote:

> > Sometimes I just long for C preprocessor macro ability for when Java won't
> > let you encapsulate patterns without a fight.
>
> Nothing's particularly stopping you from making m4 or even cpp part of
> your build process. It won't play nice with javac's automatic dependency
> resolution, but that's not impossible to work around (just run m4 on the
> whole source tree before doing any compilation).

Stuff like that would probably stop Eclipse's check-as-you-type, from
working, stop its "quick fixes" from working, would probably prevent its
refactoring tools from working and might well play havoc with tools
like Checkstyle.

> Make sure you really need that before implementing such a beast.

Indeed.
--
__________
|im |yler http://timtyler.org/ email***@***.com Remove lock to reply.
 
 
Dale King





PostPosted: 2005-7-15 11:42:00 Top

java-programmer >> Toward Terser Java Roedy Green wrote:
> On Sat, 9 Jul 2005 13:38:40 GMT, Tim Tyler <email***@***.com> wrote or
> quoted :
>
>
>>That really was bad, though. At least these days machines can
>>read and write source code - and do some editing tasks for us.
>>
>>The preprocessor was one of the things that made that *very*
>>challenging to do properly in C/C++.
>
>
> I don't want it back, just the ability to encapsulate any sort of
> repetitive pattern. I can write "stompers" - code generators and
> skeleton generators written in Java to get you started, but they are
> not so hot for maintenance.

You might want to check out OpenJava
(http://www.csg.is.titech.ac.jp/openjava/) which would be able to do
what you want. It takes some study to wrap your head around it, but it
is pretty cool.

--
Dale King