# VM that understands structs

## 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 on other sites
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 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 on other sites
Quote:
 Original post by ZotoasterHmm. 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 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 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.
[quote]
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 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 languagestruct Player{    var Name;    var Age;}var Player me;

You get the jist of it :)

##### 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;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

##### 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 on other sites
Quote:
 Original post by ZotoasterActually... 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 on other sites
Quote:
 Original post by ZotoasterActually... 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?

Yeah the reason it is blue there is because this forum doesnt have a ["source"] tag that supports lua syntax... so ignore the blue new.. that could be knew or create or any other kind of word, in his example he uses C++ as the source tag

##### Share on other sites
Ok, I have tried to look through the Lua source, but it's all too complicated. I don't have a basic outline of what's happening, which makes it kind of difficult to find out what everything's supposed to be doing.

I'm sure there must be someone here who's achieved this before, right?

##### Share on other sites
Well, it really is up to you. It depends on your bytecode.

An example might be:
type Person{    var Name    var Address}var Person person = { "Bill", "Bill's house" }// !print(person.Name)

The marked line might be converted to bytecode like this:
getvar   "print"getvar   "person"push     "name"gettable // gets stack[-1], accesses field stack.top(), pops 2 items from stack and pushes resultcall     1

##### Share on other sites
Gah! I keep trying to figure out some sort of implementation, but it never works!

I'm trying some sort of three structure for the variables, where every variable has a parent, with the root being __main__, which you won't be able to see from the programming language.

Now I have no idea how to actually load sub variables, assign values, etc, using something so linear as opcodes.

Anyone done this before? What's the best way? As I said, bytecode is flexible.

##### Share on other sites
I wouldn't do that. Assuming your "structs" are reference types, there could be many parents. In fact, your system might allow for circular references. This changes the tree to a graph.

I don't know why you want to have each variable store its parent.

I'll try to flesh out my example a little:
type Person{    var Name    var Address}// 1 object creatingvar person = Person.new( "Bill", "123 Fake St." )// 2 set fieldperson.Name = "Will"// 3 get fieldprint(person.Name)

global data:"Person" -> ( Type { Members -> { "Name", "Address" } } )code:// 1push     "Bill"push     "123 Fake St."getvar   "Person"       // Person type info, itself a mappush     "new"          // constructor function is auto generatedgetfieldcall 2setvar   "person"// 2getvar   "person"push     "Name"push     "Will"setfield// 3getvar   "person"push     "Name"getfieldgetvar   "print"call 1

How you manage the translation is up to you. I am basing this partially off lua and a scripting toy language I was writing a while ago.

Is there any reason why the above (or a similar system) would not suffice? Give us more info! [smile]

##### Share on other sites
That seems understandable enough! I'll reply when I am stuck, which is inevitable :p Thanks mate.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627716
• Total Posts
2978783

• 9
• 21
• 14
• 12
• 42