I'm planning on designing and implementing my own programming language. Is it a good idea to build my language in Java and run it off of the JVM or would my language be faster if I wrote it in C and ran the code off of my own virtual machine?
Asked
Active
Viewed 182 times
1
-
2Be a man, write your own virtual machine. – Marko Topolnik Mar 07 '14 at 20:59
-
1A C implementation will probably be faster (if you do it right, of course). But writing it in Java is probably much easier, primarily since Java handles most of the memory-management for you (though, if you're really after performance you'd want to meddle in that as well). It may be easiest to start off with a Java implementation so you can iron out the details and focus on the language itself. Then, when you want to go after speed write a C version. Speaking from experience: designing a language is not much fun when you spend most of your time debugging memory-management. – Kninnug Mar 07 '14 at 21:05
-
Java is a halfway decent language for writing a compiler. You can't do some of the tricks you can with C/C++, but you also can't create goofy addressability bugs. Not sure about the VM. You probably could do a basic VM in Java, but it would be very slow and awkward. (The VM certainly doesn't need to be written in the same language as the compiler.) – Hot Licks Mar 07 '14 at 21:31
-
Depends whether you want GC. If you don't, then using JVM is pointless. If you do, JVM saves much pain. – david.pfx Mar 08 '14 at 10:51
-
@david.pfx Sorry for sounding like an idiot, but what does GC stand for? – user3318845 Mar 08 '14 at 19:58
-
@user3318845: Garbage collector.http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 – david.pfx Mar 08 '14 at 23:26
1 Answers
1
Maybe you can write your compiler in Java and let it compile to Java byte code. If you want to do this, you can have a look at the book "Compiler Construction" by Niklaus Wirth for the compiler part and use ASM(Java) to generate the bytecode. But you should think, wether you really want to build a compiler - it is not very easy.

jvh
- 341
- 3
- 15
-
Technically the OP never specified they were going to build a compiler: just the desire to design & implement a language. It's very possible they want to implement an interpreter (first), which should be easier than a full-blown compiler. – Kninnug Mar 07 '14 at 21:11
-
I heard that Java has similar performance to C, if I got my compiler to generate Java byte code, would my language perform similarly to C? Also are there any good resources that you know of that are good for learning how to create your own virtual machine? – user3318845 Mar 07 '14 at 21:15
-
You are right. But the basics are quite similar. I would recommend reading Wirth anyway if one wants to dive deep into this topic – jvh Mar 07 '14 at 21:16
-
There would be little chance that your language would perform like Java right off the bat, even if it used the Java VM. Java, javac, and the Java VM have had a lot of tuning and have evolved together to produce optimized performance, while you would be starting cold. (Not trying to discourage you, just want to set expectations.) – Hot Licks Mar 07 '14 at 21:35
-
You wont have as good compile-time optimizations like the java compiler, but you can benefit from the JIT, which will speed up the execution time of your code very much. It also is easier to use the JavaVM, because then a lot of work is done for you. – jvh Mar 07 '14 at 22:33