Changeset 862


Ignore:
Timestamp:
Jun 22, 2003, 10:49:06 AM (18 years ago)
Author:
orlov
Message:
  • Objects are referenced through ObjectRef? (instead of Ref) which is derived

from Term.

  • Term::get_type() returns concrete type of the object.
  • Method Term::cast_to() can be used to check that term has a certain type and

cast it to that type simultaneously.

Location:
to-imperative/trunk/runtime
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/runtime/Makefile

    r830 r862  
    1919  rf_short_int \
    2020  rf_object \
     21  rf_object_ref \
    2122  rf_box \
    2223  rf_vector \
  • to-imperative/trunk/runtime/rf_core.cc

    r835 r862  
    9393
    9494TypeRegister rftype::Term::ref_reg(&Term::ref_funcs, type_ref) ;
    95 TypeRegister rftype::Object::reg(&Object::funcs, type_object) ;
     95TypeRegister rftype::ObjectRef::reg(&ObjectRef::funcs, type_object) ;
    9696TypeRegister rftype::Char::reg(&Char::funcs, type_char) ;
    9797TypeRegister rftype::ShortInt::reg(&ShortInt::funcs, type_short_int) ;
  • to-imperative/trunk/runtime/rf_expr.ih

    r860 r862  
    1414#include "rf_expr.hh"
    1515#include "rf_term.ih"
    16 #include "rf_object.ih"
     16#include "rf_object_ref.ih"
    1717#include "rf_short_int.ih"
    1818#include "pxx_heap_allocator.ih"
     
    6868{
    6969  init(1, 0);
    70   new(first) Term(_obj);
     70  new(first) ObjectRef(_obj);
    7171}
    7272
  • to-imperative/trunk/runtime/rf_object.cc

    r835 r862  
    11#include "rf_object.ih"
    2 #include "rf_term.ih"
    3 #include "pxx_string.ih"
    42
    5 namespace rftype
    6 {
    7 
    8 using namespace rfrt ;
    9 
    10 void Object::ctor (Term* _to, Term const* _from)
    11 {
    12   _to->set_type(type_object);
    13   Ref* r1 = (Ref*)(&_from->uint_data2);
    14   Ref* r2 = (Ref*)(&_to->uint_data2);
    15   new(r1) Ref(*r2);
    16 }
    17 
    18 void Object::dtor (Term* _obj)
    19 {
    20   Ref* r = (Ref*)(&_obj->uint_data2);
    21   r->~Ref();
    22 }
    23 
    24 bool Object::eq (Term const* _obj1, Term const* _obj2)
    25 {
    26   return _obj1->uint_data2 == _obj2->uint_data2;
    27 }
    28 
    29 //
    30 // FIXME: implement this
    31 pxx::WString Object::to_string (Term const* _obj)
    32 {
    33   return pxx::WString(L"");
    34 }
    35 
    36 short_type_funcs_t Object::funcs = {
    37   (void (*)(Term*, Term const*))(Object::ctor),
    38   (void (*)(Term*))(Object::dtor),
    39   (bool (*)(Term const*, Term const*))(Object::eq),
    40   null, // (int (*)(Term const*, Term const*))(Object::compare), FIXME:
    41   null, // (uint32_t (*)(Term const*))(Object::hash), FIXME:
    42   (pxx::WString (*)(Term const*))(Object::to_string)
    43 };
    44 
    45 }
  • to-imperative/trunk/runtime/rf_object.hh

    r835 r862  
    1515
    1616  friend class Ref ;
     17  friend class ObjectRef ;
    1718
    1819private:
     
    7778#endif
    7879
    79 
    80 private:
    81   static void ctor (Term* _to, Term const* _from) ;
    82   static void dtor (Term* _word) ;
    83   static bool eq (Term const* _word1, Term const* _word2) ;
    84   static int compare (Term const* _word1, Term const* _word2) ;
    85   static uint32_t hash (Term const* _word) ;
    86   static pxx::WString to_string (Term const* _word) ;
    87   static short_type_funcs_t funcs ;
    88   static TypeRegister reg ;
    89 
    9080};
    9181
  • to-imperative/trunk/runtime/rf_short_int.cc

    r835 r862  
    1  // $Source$
     1// $Source$
    22// $Revision$
    33// $Date$
  • to-imperative/trunk/runtime/rf_term.hh

    r860 r862  
    1616#include "rf_types.hh"
    1717#include "pxx_string.hh"
    18 
    19 namespace rftype {
    20   class Object ;
    21 }
    2218
    2319///
     
    9187/// We can access all term fields from expression class and some methods
    9288friend class Expr ;
    93 friend class rftype::Object ;
    9489friend inline Expr operator + (Term const&, Expr const&) ;
    9590friend inline Expr operator + (Term const&, Term const&) ;
     
    137132  /// be separated.
    138133  inline Term (Expr const& _expr) ;
    139   inline Term (rftype::Object* _obj) ;
    140134
    141135  inline Term (Term const& _t) ;
     
    191185  /// a reference term.
    192186  inline Term* get_last () const ;
    193   ///
    194   /// Get a type of "short" symbol.
     187private:
     188  ///
     189  /// Return a type of "short" symbol or type_ref or type_object.
     190  inline unsigned get_short_type () const ;
     191public:
     192  ///
     193  /// Set "short" type of the term.
     194  inline void set_type (unsigned _type) ;
     195  ///
     196  /// Get concrete type of the term.
    195197  inline unsigned get_type () const ;
    196198  ///
    197   /// Set a type of "short" symbol.
    198   inline void set_type (unsigned _type) ;
    199   inline rftype::Object& get_object () ;
     199  /// Cast to the concrete term type.  If not success return null.
     200  template <class C>
     201  inline C* cast_to (unsigned _type) ;
    200202  ///
    201203  /// Get a pointer to memory block containing referenced expression. Note
  • to-imperative/trunk/runtime/rf_term.ih

    r860 r862  
    1414#include "rf_term.hh"
    1515#include "rf_types.ih"
    16 #include "rf_object.ih"
    1716#include "rf_expr.hh"
     17#include "rf_object_ref.ih"
    1818#include "rf_word.ih"
    1919
     
    3636#endif
    3737
    38 inline Term::Term (Object* _obj)
    39 {
    40   set_type(type_object);
    41   new(&uint_data2) Ref(_obj);
    42 }
    43 
    4438inline Term::~Term ()
    4539{
    46   void (*pf)(Term*) = short_type_funcs[get_type()]->dtor;
     40  void (*pf)(Term*) = short_type_funcs[get_short_type()]->dtor;
    4741  if (pf != null) (*pf)(this);
    4842}
     
    5044inline Term::Term (Term const& _t)
    5145{
    52   void (*pf)(Term*, Term const*) = short_type_funcs[_t.get_type()]->ctor;
     46  void (*pf)(Term*, Term const*) = short_type_funcs[_t.get_short_type()]->ctor;
    5347  if (pf != null) (*pf)(this, &_t);
    5448  else {
     
    8175inline bool Term::is_sym () const
    8276{
    83   return get_type() > type_ref;
     77  return get_short_type() > type_ref;
    8478}
    8579
    8680inline bool Term::is_ref () const
    8781{
    88   return get_type() == type_ref;
     82  return get_short_type() == type_ref;
    8983}
    9084
     
    10599}
    106100
    107 inline unsigned Term::get_type () const
     101inline unsigned Term::get_short_type () const
    108102{
    109103  if ((data1 & 0x01) == 0) return data1 >> TYPE_SHIFT;
     
    115109  if (_type == type_ref) data1 |= 0x01;
    116110  else data1 = (_type << TYPE_SHIFT);
     111}
     112
     113inline unsigned Term::get_type () const
     114{
     115  unsigned type = get_short_type();
     116  if (type != type_object) return type;
     117  return static_cast<const ObjectRef*>(this)->get_obj_ptr()->get_type();
    117118}
    118119
     
    194195}
    195196
    196 inline Object& Term::get_object ()
    197 {
    198   if (get_type() == type_object) {
    199     Ref* r1 = (Ref*)(&uint_data2);
    200     return *(*r1);
    201   } else {
    202     throw Word::create_expr(L"Term does not contain an object");
    203   }
     197template <class C>
     198inline C* Term::cast_to (unsigned _type)
     199{
     200  unsigned t = get_short_type();
     201  if (t == _type) return reinterpret_cast<C*>(this);
     202  if (t == type_object) {
     203    Object* obj_ptr = static_cast<ObjectRef*>(this)->get_obj_ptr();
     204    if (obj_ptr->get_type() == _type)
     205      return reinterpret_cast<C*>(obj_ptr);
     206  }
     207  return null;
    204208}
    205209
  • to-imperative/trunk/runtime/rf_vector.ih

    r835 r862  
    3333  size_t index = 0;
    3434  for (; p < q; p++) {
    35     if (p->get_type() == type_ref) {
     35    if (p->is_ref()) {
    3636      self[index++] = Expr(p);
    3737    } else {
    38       try {
    39         Vector& v = dynamic_cast<Vector&>(p->get_object());
    40         for (size_t i = 0; i < v.get_length(); i++) {
    41           self[index++] = v[i];
    42         }
    43       } catch (std::bad_cast&) {
     38      Vector* v = p->cast_to<Vector>(type_vector);
     39      if (!v)
    4440        FATAL("Vector(): Argument expression contains non-vector object");
     41      for (size_t i = 0; i < v->get_length(); i++) {
     42        self[index++] = (*v)[i];
    4543      }
    4644    }
Note: See TracChangeset for help on using the changeset viewer.