--- parser3/src/include/Attic/pa_vclass.h 2001/02/21 16:11:49 1.1 +++ parser3/src/include/Attic/pa_vclass.h 2001/03/06 15:02:46 1.20 @@ -1,119 +1,109 @@ /* - $Id: pa_vclass.h,v 1.1 2001/02/21 16:11:49 paf Exp $ -*/ - -/* - data core + $Id: pa_vclass.h,v 1.20 2001/03/06 15:02:46 paf Exp $ */ #ifndef PA_VCLASS_H #define PA_VCLASS_H -#include "pa_value.h" -///#include "pa_vhash.h" -class VHash { -public: - VHash(Pool& apool, Hash& hash) {} -}; +#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 *get_type() const { return "Class"; } - - // object_class: [class classname] - virtual String *get_string() const { - String *result=new(pool()) String(pool()); - result->APPEND("[class ", 0, 0, 0); - result->APPEND(name().cstr(), 0, 0, 0); - result->APPEND("]", 0, 0, 0); - return result; - } + const char *type() const { return "class"; } // object_class: (field)=STATIC.value;(STATIC)=hash;(method)=method_ref with self=object_class - virtual Value *get_element(const String& name) const { - // $STATIC=STATIC hash - if(name==STATIC_NAME) - return 0;//new(pool()) VHash(pool(), STATIC); - - // $field=STATIC.field - Value *result=static_cast(STATIC.get(name)); - if(!result) { - // $method=VMethod_ref - if(Method *method=get_method(name)) - result=0;///new(pool()) VMethod_ref(this, method); - } - - return result; + Value *get_element(const String& aname) { + // $NAME=my name + if(aname==NAME_NAME) + return NEW VString(class_alias->name()); + // $CLASS=my class=myself + if(aname==CLASS_NAME) + return class_alias; + // $BASE=my parent + if(aname==BASE_NAME) + return class_alias->base(); + // $method=junction(self+class+method) + if(Junction *junction=get_junction(*this, aname)) + return NEW VJunction(*junction); + // $field=static field + return get_field(aname); } // object_class, operator_class: (field)=value - static values only - virtual void put_element(const String& name, Value *value) { - STATIC.put(name, value); - } - - // object_instance, object_class: method - Method *get_method(const String& name) const { - return static_cast(methods.get(name)); + void put_element(const String& name, Value *value) { + set_field(name, 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 - - const String& name() const { return *fname; } -public: // creation - - VClass(Pool& apool, String& aname, const Array& immediate_parents) : - Value(apool), - fname(&aname), - STATIC(apool), - methods(apool), - parents(apool), - parents_hash(apool) { - // monkey immediate_parents - // fill parents & parents_hash + VClass(Pool& apool) : VAliased(apool, *this), + fields(apool), + fmethods(apool), + fbase(0) { } - - void rename(String *aname) { fname=aname; } 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); +// Hash& methods() { return fmethods; } + + void set_base(VClass& abase) { + // remember the guy + fbase=&abase; + } + VClass *base() { return fbase; } + + 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; } private: - String *fname; - Hash STATIC; - Hash methods; - Array parents; Hash parents_hash; -}; + Value *get_field(const String& name) { + Value *result=static_cast(fields.get(name)); + if(!result && fbase) + result=fbase->get_field(name); + return result; + } + + void set_field(const String& name, Value *value) { + if(fbase && fbase->replace_field(name, value)) + return; + + fields.put(name, value); + } + bool replace_field(const String& name, Value *value) { + return + (fbase && fbase->replace_field(name, value)) || + fields.put_replace(name, value); + } + +private: -/* -descendants: - text:+ value:String - hash:+ keys&values:Hash - table:+ columns_order:Array, columns:Hash, rows:Array - object_class:+ STATIC:Hash, methods:Hash - object_instance:+ object_class, fields:Hash - method_ref:+ self:Value/object_class, method:String - method_self_n_params_n_locals:+ self:Value/object_class[1st try], params_locals&values:Hash[2nd try] - junction:+ self:Value, code:String -*/ + VClass *fbase; + Hash fields; + Hash fmethods; +}; #endif