Sign in to follow this  

VM that understands structs

This topic is 3038 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
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.
[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 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
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.

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 this post


Link to post
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 this post


Link to post
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 result
call 1

Share this post


Link to post
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 this post


Link to post
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 creating
var person = Person.new( "Bill", "123 Fake St." )

// 2 set field
person.Name = "Will"

// 3 get field
print(person.Name)



Bytecode & metadata:

global data:
"Person" -> ( Type { Members -> { "Name", "Address" } } )

code:
// 1
push "Bill"
push "123 Fake St."
getvar "Person" // Person type info, itself a map
push "new" // constructor function is auto generated
getfield
call 2
setvar "person"

// 2
getvar "person"
push "Name"
push "Will"
setfield

// 3
getvar "person"
push "Name"
getfield
getvar "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 this post


Link to post
Share on other sites

This topic is 3038 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this