2

If I wanted to create a new language for Java I should make a compiler that is able to generate the byte-code compatible with the JVM spec, right? and also for the JDK libraries?

Where can I find some info?

Thanks.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
xdevel2000
  • 20,780
  • 41
  • 129
  • 196

5 Answers5

2

I would start with a compiler which produced Java source. You may find this easier to read/understand/debug. Later you can optimise it to produce byte code.

EDIT:

If you have features which cannot be easily translated to Java code, you should be able to create a small number of byte code classes using Jasmin with all the exotic functionality which you can test to death. From the generated Java code this will look like a plain method call. The JVM can still inline the method so this might not impact performance at all.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • It depends on the type of language whether compiling to Java or to bytecode would be easier, I think. – Paŭlo Ebermann May 04 '11 at 12:43
  • 1
    @Paŭlo Ebermann, true, but there is also the ease of proving that the generated code is correct and debugging it when it doesn't work the way you expect. Debugging/validation byte code is not trivial. VerifyError can be rather more cryptic than a compiler error, so it may be worth the extra effort in some cases. ;) – Peter Lawrey May 04 '11 at 12:54
  • You are right, for most languages this would be better. Only languages which use features not directly mappable to Java need a direct compiler. (For example, `goto` would have to be implemented in Java with a loop and tricky continue/break uses, maybe additionally a switch. In bytecode there is a goto opcode, so no problem.) – Paŭlo Ebermann May 04 '11 at 17:27
  • You can map goto into Java with loops and breaks to labels. ;) – Peter Lawrey May 05 '11 at 06:22
2

Depends what you mean by "create a new language for Java" -- do you mean a language that compiles to bytecode and the code it generates can be used from any Java app (e.g. Groovy) or an interpreted language (for which you want to write a parser in Java)? If it is the former one then @Joachim is right, look at the JVM spec; for the latter look at the likes of JavaCC for creating a parser for your language grammar.

Liv
  • 6,006
  • 1
  • 22
  • 29
  • @Peter to be honest nearly wrote initially yacc then I remember the question is about Java :) – Liv May 04 '11 at 12:15
1

The Java Virtual Machine Spec should have most of what you need.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

An excellent library for bytecode generation/manipulation is ASM: http://asm.ow2.org.

It is very versatile and flexible. Note however that it's API is based on events (similar to Sax parsers) - it reads .class files and invokes a method whenever it encounters a new entity (class declaration, method declaration, statements, etc.). This may seem a bit awkward at first, but it saves a lot of memory (compared to the alternative: the library read the input, spits out a fully-evolved tree structure and then you have to iterate over it).

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
0

I don't think this will help much practically, but it has a lot of sweet theoretical stuff that I think you'll find useful.

http://www.codeproject.com/KB/recipes/B32Machine1.aspx

David
  • 180
  • 3
  • 14