j2d - translating Java to D with the language machine

Brad Anderson brad at dsource.dot.org
Mon Mar 27 18:44:40 PST 2006


Walter Bright wrote:
> "Peri Hankey" <mpah at thegreen.co.uk> wrote in message 
> news:e09ta6$2rco$1 at digitaldaemon.com...
>> Following an exchange of emails with Brad Anderson I have adapted my 
>> d-to-d translator to translate from java to D. It's at a very early stage 
>> (I started a couple of days ago). It seems to me that the first thing to 
>> do is to apply it to the gnu classpath sources.
> 
> What are you using for a lexer? 

Walter, I just started with The Language Machine, and am no expert by 
any means, but I'll give it a shot...  The LM is kind of a lexer and 
parser rolled into one.

http://languagemachine.sourceforge.net/ is a good intro, as well as the 
paradigm shift page.

Rules like:
[a-z_A-Z] % { { repeat [a-zA-Z_0-9] % } toSym:X } <- identifier symbol :X ;

identify that any letter or underscore, followed by zero or more 
letter/underscores is stored in a symbol X, and the right-hand side of 
the rule says these are symbols (tokens in a normal lexer).

Later on, rules like:
"instanceof" type :B <- op opnd:{ ft  :"ty"   :A :B };

tell us that this java 'instanceof' symbol becomes a 'ft' in the 
intermediate representation.

These are the Java to X frontend rules contained here:
http://www.dsource.org/projects/languagemachine/browser/trunk/languagemachine/src/j2d/j2xfe.lmn
(X being the intermediate representation).

Then an X to D backend set of rules is used to take that 'ft' and make 
it into a 'D instanceof', if there is any difference between the two 
languages, which there is in this case.

The rule in the backend turns it into D code:
ft :F :A :B  <- code - "(cast(" B ")" A ")";

The D backend can be found here:
http://www.dsource.org/projects/languagemachine/browser/trunk/languagemachine/src/j2d/x2dbe.lmn

It's a little bit like Scott Sanders' Molt project which hacked the 
Jikes compiler into emitting an XML intermediate representation of the 
Java code and XSLT was used to take the XML doc and transform it into D.

hth,
BA

P.S. Peri, how'd I do?



More information about the Digitalmars-d mailing list