Help Implementing (Simple) VM/Language

Started by
6 comments, last by Drigovas 15 years, 6 months ago
I'm experimenting with implementing a (simple) stack-based virtual machine in order to play with a toy language, but I'm having trouble finding just about any good information out there about how to go about doing this. Unfortunately, any search containing the strings, "vm" or "virtual machine" almost always contains hits talking about Java. [lol] Any good references or links to other's work are appreciated. I'm also looking around for just about anything to do with implementing languages, like compilers. Most likely, this will just a be a short-lived project, but I've become really interested in the subject (and there seem to be quite a few language related posts around GDNet). For the time being, I'm shooting for simply getting something that works, even if it just adds two numbers and prints them. Thanks!
Advertisement
One thing that is useful is to take a language of your choice (java/C#) and look into the intermediary stuff the compiler spits out. Or even assembly proper... You don't really need to learn much, but just doing a readthrough of the different ops and what they do is helpful for that first step.

I'd rather let others with more formal training help you out on the specifics. My toy lang's vm implementation isn't the traditional sort of beast. It works and all, but...
You could look at the VM for LUA or SMALL or one of the other interpretted scripting languages out there for reference. There are quite a few books on the working of the Java VM, if you wanted to take that route.
here's a tutorial
Don't forget AngleScript! It might prove to be a very nice case study for you. Check out the GD forum for it. WitchLord is a great guy, always active and helping people with it. It is more than a "simple" scripting language though, so it might take more time to learn the ins and out than something much smaller.
Also consider Jack Crenshaw's Series
Thanks for all of the great references, everyone. I'm sure they'll be really helpful.

I agree that studying existing examples is probably the best route if I want to get anywhere. I didn't think of looking at Lua or AngelScript; I'll have to check it out.
Something else you might want to think about is whether or not your 'toy language' actually needs it's own virtual machine, or whether or not you can just push it through a compiler pass [which would be really be little more than a parser in this case] into the code that can be chewed up by an existing vm that is likely more robust and full featured than you would be creating on your own. Many low level languages like Java's bytecode or .NET's CLI are general enough that you could likely push your program's text into that form, and just let those VM's, which are already widely used and very well optimized, worry about the low level grunt work. That way, your 'virtual machine' is actually a parsing pass sitting on top of an existing virtual machine, which is substantially easier to deal with. You could even use one of the less optimized but more easily edited VM's out there for Java [many of which are open source and pretty friendly] to enable extra little features by using some of the reserved op codes found in java if you absolutely need semantics that are not found in java's native bytecode.

This topic is closed to new replies.

Advertisement