VM that understands structs

Started by
14 comments, last by Zotoaster 14 years, 7 months ago
Hello. I am in the middle of programming my scripting language, Version 1.4 million. This time round, I want a VM that can understand structs. I tried making it so that variables have a parent/child structure, but this was difficult to do with a VM that only has commands with only one operand. I am quite flexible as far as solutions go. Anyone got any recommendations? Thanks
Advertisement
You could look at how many dynamic languages like Lua do it - where a struct or object is really a map or dictionary with some syntactical sugar to make it easy to use.
Hmm. I thought about that. The problem really is that, given the tree-like nature of structs, representing them as maps or dictionaries is kinda tricky.

But, if you have any examples, links, analogies, I'd be glad to hear them?

Thanks again
Quote:Original post by Zotoaster
Hmm. I thought about that. The problem really is that, given the tree-like nature of structs, representing them as maps or dictionaries is kinda tricky.

Why? If you have nested structs, you just have a dictionary within a dictionary. It's an elegant solution, the opposite of tricky really. That's why Python, Lua, &#106avascript etc all have this sort of approach.<br><br>
Oooh, I see what you're saying. You're saying instead of structs, I use dictionaries or maps (sorry, never used Lua properly). Actually, I've used this sort of approach before, but I'm more interested in something instanciable (if that's even a word... well you know what I mean anyway).

The reason for this is because 1) it's essentially for making games, and it's easier (for me at least) to keep track of objects that way, and 2) once this is done, I have an idea for how to make it OO.
Quote:
Oooh, I see what you're saying. You're saying instead of structs, I use dictionaries or maps (sorry, never used Lua properly). Actually, I've used this sort of approach before, but I'm more interested in something instanciable (if that's even a word... well you know what I mean anyway).

I have no idea what you mean [smile]. Please elaborate.

The reason for this is because 1) it's essentially for making games, and it's easier (for me at least) to keep track of objects that way, and 2) once this is done, I have an idea for how to make it OO.[/qipte]
1) not a problem, Lua (or Python...) is used for games
2) not a problem. Lua uses "metatables" for this, but you could add another layer of syntactic sugar to hide the fact that classes are simply maps.

Yep. As I said, I prefer my way of handling data. I don't mind if internally they are treated as maps or dictionaries. I just want to make sure that I get something like good ol' C++ does it.


// Example of how I'd like my scripting languagestruct Player{    var Name;    var Age;}var Player me;


You get the jist of it :)
Lua isn't exactly like that syntax but its the same concept. You work with objects (which are Lua tables) and access their member variables using the . notation like C++.

Some code:

local me = {Name="JoeJoe",Age=20};print("my age is "..me.Age);


If you want to make classes/struct definitions similar to how C++ work you can create constructors which reside in a certain namespace like so.

local PlayerClass = {}function PlayerClass:new(name,age)   local obj={}   obj.Name=name;   obj.Age=age;   return obj;endlocal me = PlayerClass:new("JoeJoe",20);print("my name is "..me.Name)


That's basically how it works in Lua and im sure similarly in Python and other dynamic scripting languages.

Good Luck!

-ddn
Actually... that's quite cool! Couple of questions:

First, what's the relevance of the keyword "new" in there? Seems like the same kind of thing could be achieved using just any normal function.

Second, I'm wondering what kind of opcodes I would use in a VM to achieve the way you create dictionaries. Any ideas?
Quote:Original post by Zotoaster
Actually... that's quite cool! Couple of questions:

First, what's the relevance of the keyword "new" in there? Seems like the same kind of thing could be achieved using just any normal function.

There is no significance in Lua - "new" is not a keyword. Its only relevance is that it is a word that programmers already associate with creating instances of classes.
Quote:
Second, I'm wondering what kind of opcodes I would use in a VM to achieve the way you create dictionaries. Any ideas?

Most of the languages mentioned have open source virtual machines - why not take a peek? [grin]

Lua appears to have "settable" and "gettable" as primitive operations, from a brief glance at the source.

This topic is closed to new replies.

Advertisement