--- parser3/src/include/Attic/pa_vclass.h 2001/02/24 15:26:02 1.14 +++ parser3/src/include/Attic/pa_vclass.h 2001/03/10 16:34:36 1.29 @@ -1,90 +1,97 @@ /* - $Id: pa_vclass.h,v 1.14 2001/02/24 15:26:02 paf Exp $ + Parser + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan + + $Id: pa_vclass.h,v 1.29 2001/03/10 16:34:36 paf Exp $ */ #ifndef PA_VCLASS_H #define PA_VCLASS_H -#include "pa_value.h" +#include "pa_valiased.h" #include "pa_vhash.h" -#include "pa_vstring.h" #include "pa_vjunction.h" -class VClass : public Value { +#define CLASS_NAME "CLASS" +#define BASE_NAME "BASE" + +class VClass : public VAliased { public: // Value // all: for error reporting after fail(), etc - const char *type() const { return "Class"; } + const char *type() const { return "class"; } // object_class: (field)=STATIC.value;(STATIC)=hash;(method)=method_ref with self=object_class - Value *get_element(const String& name) { - // $NAME=name() - if(name==NAME_NAME) - return NEW VString(VClass::name()); - // $PARENTS=parents table - if(name==PARENTS_NAME) - return 0;// TODO: table of parents - // $STATIC=STATIC hash - if(name==STATICS_NAME) - return &STATICS; - - // $method=junction(this+method) - if(Method *method=static_cast(methods.get(name))) { - Junction& j=*NEW Junction(pool(), - *this, - method,0,0,0,0); - - return NEW VJunction(j); - } - - // $field=STATIC.field - return STATICS.get_element(name); - } + Value *get_element(const String& aname); // object_class, operator_class: (field)=value - static values only - void put_element(const String& name, Value *value) { - STATICS.put_element(name, value); - } + void put_element(const String& name, Value *value); // object_class, object_instance: object_class - VClass *get_class() { return this; /*TODO: think when?*/ } - - // object_class: true when this class is derived from 'ancestor' - bool is_or_derived_from(VClass& ancestor) { - if(this==&ancestor) - return true; // it's me - - return parents_hash.get(*ancestor.name())!=0; - } + VClass *get_class() { return this; } public: // usage - VClass(Pool& apool) : - Value(apool), - STATICS(apool), - methods(apool), - parents(apool), - parents_hash(apool) { + VClass(Pool& apool) : VAliased(apool, *this), + fbase(0), + ffields(apool), + fmethods(apool) { } void add_method(const String& name, Method& method) { - methods.put(name, &method); + fmethods.put(name, &method); } - void add_parent(VClass& parent) { - parents+=&parent; - parents_hash.put(*parent.name(), &parent); - // TODO: monkey immediate_parent - // fill parents & parents_hash +// Hash& methods() { return fmethods; } + + void set_base(VClass& abase) { + // remember the guy + fbase=&abase; } + VClass *base() { return fbase; } -public: //usage + bool is_or_derived_from(VClass& vclass) { + return + this==&vclass || + fbase && fbase->is_or_derived_from(vclass); + } + + Junction *get_junction(VAliased& self, const String& name) { + if(Method *method=static_cast(fmethods.get(name))) + return NEW Junction(pool(), self, this, method, 0,0,0,0); + if(fbase) + return fbase->get_junction(self, name); + return 0; + } - VHash STATICS; + void set_field(const String& name, Value *value) { + value->set_name(name); + if(fbase && fbase->replace_field(name, value)) + return; + + ffields.put(name, value); + } private: - Hash methods; - Array parents; Hash parents_hash; + Value *get_field(const String& name) { + Value *result=static_cast(ffields.get(name)); + if(!result && fbase) + result=fbase->get_field(name); + return result; + } + + bool replace_field(const String& name, Value *value) { + return + (fbase && fbase->replace_field(name, value)) || + ffields.put_replace(name, value); + } + +private: + + VClass *fbase; + Hash ffields; + Hash fmethods; }; #endif