views:

917

answers:

9

Is there a java compiler flag that allows me to use goto as a valid construct? If not, are there any third-party java compilers that supports goto? If not, are there any other languages that support goto while at the same time can easily call methods written in Java?

The reason is I'm making a language that is implemented in Java. Gotos are an important part of my language; I want to be able to compile it to native or JVM bytecode, although it has to be able to easily use Java libraries (ie. C supports goto, but to use it I'd have to rewrite the libraries in C).

I want to generate C or Java, etc source files, and not bytecode or machine code. I'm using a third-party compiler to do that.

+4  A: 

The JVM bytecode contains a goto instruction (e.g. see the BCEL documentation).

Don't forget that Java itself supports the concept of jumping to labels, using:

break {labelname}

or

continue {labelname}

See this JDC tech tip for more info. If your language is compiled to JVM bytecode, then you may be able to make use of this.

Brian Agnew
syntax is without the brackets: break labelname;
Thorbjørn Ravn Andersen
Yes. I was putting {labelname} to indicate some label should be inserted there. Perhaps not very clear, I appreciate.
Brian Agnew
+2  A: 

By definition, no Java compiler allows goto. Java compilers must implement the JLS, and the JLS does not allow gotos. However, it is also clearly possible to compile a language with gotos to the JVM. AMPC is one C-to-JVM compiler that claims to support C89.

Also note that Java bytecode has a goto instruction, though it's obviously instruction-based not line-based.

Matthew Flaschen
+6  A: 

JVM support goto at bytecode level. If you are doing your own language, you should use libraries like BCEL or ASM, not generating .java file.

J-16 SDiZ
+1  A: 

You can write your code generator to target Jasmin. You can use goto in Jasmin as much as you like. :-)

Chris Jester-Young
Targeting Jasmin is about equivalent to targeting ASM/BCEL (as in J-16's answer), so meh. :-P
Chris Jester-Young
+1  A: 

The goto keyword is reserved but unused in the Java programming language. (From Section 3.9 of The Java Language Specification.)

Therefore, at least in the Java programming language, there is no way to enable the use of goto.

However, as already noted, the goto opcode in the Java virtual machine is functional, and used when the Java compiler produces bytecode from the source.

Chapter 7: Compiling for the Java Virtual Machine from The Java Virtual Machine Specification may be of interest when implementing a JVM language.

coobird
A: 

Pretty much anything you can do with goto you can do with a loop. goto is really redundant and generally discredited way to program. IMHO.

If you want to goto backwards

LABEL: do {
// code before goto

// goto LABEL
continue LABEL;

// code after goto
break;
} while(true);

If you want to goto forwards

LABEL: do {
// code before goto

// goto LABEL
continue LABEL;

// code after goto
break;
} while(false);
// Label is effectively here
// code after LABEL.
Peter Lawrey
A: 

You shouldn't, ever, use goto, as it's EVIL ;-)

More seriously, maybe you could have a look at the famous article from E. Dijkstra : http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html

zim2001
A: 

Java does not allow using goto' keyword. However, it allows using labels and usingbreak' or continue' with label instead ofgoto'. In facts, Java is not a language without goto statement but a language with incomfortable implementation of it.

Python
A: 

Apache Thrift can be used to generate source code in different programming languages from a single source. http://incubator.apache.org/thrift/

Michael Munsey