a perl5 to Java compiler - work in progress

I've been adding support for Java in Perlito5 in the last few days.

This is what it does so far:

Declare a package and import a Java class into it:

package Sample { import => "misc.Java.Sample" }

I choose this syntax because it is valid Perl, and it gives the compiler enough information to process typed variables and indirect objects, so that you can use:

my Sample $x;    # typed variable

$x = new Sample;   # indirect object

while "normal" Perl also works:

my $x = Sample->new();

It is possible to convert a Perl variable back to the Java-land value:

$x->to_Sample;   # a Java value

The coercion methods are auto-generated from the "import" declaration.

There is already an implementation of arrays, hashes, and references. There are currently no namespaces, and only a basic implementation of closures.

The code is mostly based in the Perl5-to-Javascript translator. Because this is based on a Javascript emitter, all unimplemented features will still generate Javascript instead of Java!

Here is some working code - this should work out of the box from the git repository directory (the file misc/Java/Sample.java is there already):

perl perlito5.pl -Isrc5/lib -Cjava -e ' package Sample { import => "misc.Java.Sample" }; my $x = Sample->new(); $x->to_Sample; print "got a <", $x, ">\n" ' > Test.java

javac -d . misc/Java/Sample.java
javac Test.java
java Test

# output:  got a <pSample@1906bcf8>

Don't try anything more complicated yet - it will parse without problems, but the generated code will likely be nonsense!

The code is at https://github.com/fglock/Perlito

  • tip: to see a syntax tree, replace -Cjava with -Cast-perl5

Leave a comment

About Flávio S. Glock

user-pic I blog about Perl.