I am trying to create a toy programming language called Mango. I first wanted it to compile down to Java and be compiled and executed as Java. But I found out it was a very slow approach. So I decided to compile it into Java Byte Code and execute it. I have given the mango code and the equivalent java code. Can you help me compile my mango code into equivalent .class file with byte code.
In test.mango
println("Hello World!")
would equal
public class test
{
public static void main(String[] args){
System.out.println("Hello World!")
}
}
I did use javap on test.java to produce the following bytecode
Compiled from "test.java"
public class test extends java.lang.Object{
public test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String Hello World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
But when I save this as test.class and execute it, I get the following error in command prompt
java.lang.ClassFormatError: Incompatible magic value number 1131375984
I have only taken two semesters of Java Programming. My professor told me that you can write programming languages on top of JVM using Java Byte Code. So only I wanted to try it out. I have no experience in programming language design. So any help would be really useful. I did go through Java Specification for any clues but I wasn’t able to get anything.
Java class files represent bytecode in binary format rather than as ASCII text. This means the instructions aren’t really human-readable in compiled form—the class file is just a bunch of raw data. Try opening a class file that you create with
javacand check what the output looks like. For more details on the binary format of a class file you can see the relavent Wikipedia article on Java class files.Rather than directly outputting binary bytecode, maybe you can use Soot as an intermediate tool in your compiler chain to create your class files.