Jump to content
  • Advertisement
Sign in to follow this  
Zotoaster

VM that understands structs

This topic is 3256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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, javascript etc all have this sort of approach.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 language
struct Player
{
var Name;
var Age;
}

var Player me;



You get the jist of it :)

Share this post


Link to post
Share on other sites
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;
end

local 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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!