views:

246

answers:

1

I am trying to make a toy language in c++. I have used boost spirit for the grammar, and hopefully for parser/lexer. The idea is a toy language where 'everything is an object' like javascript and some implementation of prototype based inheritance. I want to know how to implement the 'object' type for the language in c++. I saw source codes of engine spidermonkey but mostly it is done using structures, also getting more complex at later stages. As structures are more or less equivalent to classes in C++, I hope I could manage with the stdlib itself. All I want is a solid idea of how the basic object has to be implemented and how properties are created/modified/destroyed. I tried to take a look at V8, but its really confusing me a lot!

+2  A: 

Have each class have pointers to parent classes and implement properties and methods in STL containers like <string,pointer_fun> so that you can add/remove dynamically methods.
Then you could just lookup a method in an obj, if there isn't then follow the ptr to parent and lookup there till you find one or fail non-existant method.

For properties you could have a template to wrap them in the STL container so that they share a common ancestor and you can store pointers like <string,property<type>* > where property makes created type inherit from common type.

With this approach and some runtime checks you can support dynamically anything, just need to have clear which are the lookup rules for a method when you call it in an object.

So essentially every obj instance in your system could be:

 class obj{
    type_class parent*;
    string type;
    std::map<string,pointer_fun> methods;
    std::map<string,property_parent_class> properties;
 }

And have constructors/destructor be normal methods with special names.
Then in obj creation you could just lookup for type_name in type_objs and copy the member and properties from the type to the impl obj.

EDIT: About function objects, you can use functors inheriting from a common one to use the container_of_pointers approach.
For lists I'd create a simple class object that implements metods like __add__() or __len__() or __get__() like in python for example, then when you parse the language you'd substitute list_obj[3] for your_list_obj.method['__get__'] after checking that it exists of course.

Arkaitz Jimenez
how to implement if a function itself is an object? can u give me the bare backbone of the objects? i have 4 types of primitive objects (number,string,list,function) where list can be recursive of all four types. list somewhat behaves like a class for the toy script. thats the idea!
Sriram
i just put this on the editor, dunno whether it will work..! its just a backbone..haven't tried it!//type class -> holds typename for dataclass type{public:std::string type_name;std::string get_type(void);void set_type(std::string }//data class -> holds data for objecttemplate<typename T>class data{public:type __type;T __data;}//object -> hold data and their methodstemplate<typename T>class object{public:std::map<std::string,data<T>> property;}any ideas?
Sriram