Changeset 877


Ignore:
Timestamp:
Jun 29, 2003, 12:45:47 AM (17 years ago)
Author:
orlov
Message:

  • Result of merge from main trunk.
Location:
to-imperative/branches/parenth_term/runtime
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/branches/parenth_term/runtime/Makefile

    r858 r877  
    2020  rf_short_int \
    2121  rf_object \
     22  rf_object_ref \
    2223  rf_box \
    2324  rf_vector \
  • to-imperative/branches/parenth_term/runtime/rf_core.cc

    r858 r877  
    9393
    9494TypeRegister rftype::Parenth::reg(&Parenth::funcs, type_parenth) ;
    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/branches/parenth_term/runtime/rf_expr.hh

    r858 r877  
    195195  inline bool operator == (Expr const& _expr) const ;
    196196  inline bool operator != (Expr const& _expr) const ;
     197  ///
     198  /// Compare two expressions.  Return 1 if first expression is larger then
     199  /// second one, 0 if they are equal, and -1 if first one is smaller then
     200  /// second one.
     201  static inline int compare (Expr const& _expr1, Expr const& _expr2) ;
    197202  ///
    198203  /// Get pointer to a memory block containing an expression
  • to-imperative/branches/parenth_term/runtime/rf_expr.ih

    r858 r877  
    1515#include "rf_term.ih"
    1616#include "rf_parenth.ih"
    17 #include "rf_object.ih"
     17#include "rf_object_ref.ih"
    1818#include "rf_short_int.ih"
    1919#include "pxx_heap_allocator.ih"
     
    6969{
    7070  init(1, 0);
    71   new(first) Term(_obj);
     71  new(first) ObjectRef(_obj);
    7272}
    7373
     
    424424{
    425425  return !(self == _expr);
     426}
     427
     428inline int Expr::compare (Expr const& _expr1, Expr const& _expr2)
     429{
     430  return Term::compare(_expr1.get_first(), _expr1.get_last(),
     431                       _expr2.get_first(), _expr2.get_last());
    426432}
    427433
  • to-imperative/branches/parenth_term/runtime/rf_object.cc

    r858 r877  
    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/branches/parenth_term/runtime/rf_object.hh

    r858 r877  
    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/branches/parenth_term/runtime/rf_short_int.cc

    r858 r877  
    1  // $Source$
     1// $Source$
    22// $Revision$
    33// $Date$
     
    3131}
    3232
     33
    3334Expr ShortInt::create_expr (pxx::WString& _str) {
    3435  wchar_t const* s = _str.get_data();
    35   size_t s_len = wcslen(s);
    36   wchar_t last = s[s_len - 1];
    37   wchar_t* last_ptr = &last;
    38   wchar_t** last_ptr_ptr = &last_ptr;
    39   intptr_t n = wcstol(s, last_ptr_ptr, 10);
    40   return ShortInt::create_expr(n);
    41 
     36  return ShortInt::create_expr( wcstol(s, null, 10));
    4237}
    4338
  • to-imperative/branches/parenth_term/runtime/rf_term.hh

    r858 r877  
    1616#include "rf_types.hh"
    1717#include "pxx_string.hh"
    18 
    19 namespace rftype {
    20   class Object ;
    21 }
    2218
    2319///
     
    7167/// We can access all term fields from expression class and some methods
    7268friend class Expr ;
    73 friend class rftype::Object ;
    7469friend inline Expr operator + (Term const&, Expr const&) ;
    7570friend inline Expr operator + (Term const&, Term const&) ;
     
    108103  /// this we should have Term (char _c) and Term (wchar_t _c).
    109104//  inline Term (uint16_t _c) ;
    110 
    111   inline Term (rftype::Object* _obj) ;
    112105
    113106  inline Term (Term const& _t) ;
     
    148141  ///
    149142  /// Get a type of "short" symbol.
     143private:
     144  ///
     145  /// Return a type of "short" symbol or type_ref or type_object.
     146  inline unsigned get_short_type () const ;
     147public:
     148  ///
     149  /// Set "short" type of the term.
     150  inline void set_type (unsigned _type) ;
     151  ///
     152  /// Get concrete type of the term.
    150153  inline unsigned get_type () const ;
    151154  ///
    152   /// Set a type of "short" symbol.
    153   inline void set_type (unsigned _type) ;
    154 
    155   inline rftype::Object& get_object () ;
     155  /// Cast to the concrete term type.  If not success return null.
     156  template <class C>
     157  inline C* cast_to (unsigned _type) ;
    156158
    157159  inline operator pxx::WString () const ;
     160
     161  static inline int compare (Term const& _t1, Term const& _t2) ;
     162  static inline int compare (
     163    Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2
     164  ) ;
    158165
    159166#if defined(RF_STORE_CHUNK_ORDER_IN_TERM)
  • to-imperative/branches/parenth_term/runtime/rf_term.ih

    r858 r877  
    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_parenth;
     77  return get_short_type() > type_parenth;
    8478}
    8579
    8680inline bool Term::is_ref () const
    8781{
    88   return get_type() == type_parenth;
     82  return get_short_type() == type_parenth;
    8983}
    9084
     
    9488}
    9589
     90inline unsigned Term::get_short_type () const
     91{
     92  return data1;
     93}
     94
     95inline void Term::set_type (unsigned _type)
     96{
     97  data1 = _type;
     98}
     99
    96100inline unsigned Term::get_type () const
    97101{
    98   return data1;
    99 }
    100 
    101 inline void Term::set_type (unsigned _type)
    102 {
    103   data1 = _type;
     102  unsigned type = get_short_type();
     103  if (type != type_object) return type;
     104  return static_cast<const ObjectRef*>(this)->get_obj_ptr()->get_type();
    104105}
    105106
     
    115116  if (data1 != _t.data1) return false;
    116117  if (uint_data2 == _t.uint_data2) return true;
    117   bool (*pf)(Term const*, Term const*) = short_type_funcs[get_type()]->eq;
     118  bool (*pf)(Term const*, Term const*) = short_type_funcs[get_short_type()]->eq;
    118119  if (pf && (*pf)(this, &_t)) return true;
    119120  return false;
     
    165166}
    166167
    167 inline Object& Term::get_object ()
    168 {
    169   if (get_type() == type_object) {
    170     Ref* r1 = (Ref*)(&uint_data2);
    171     return *(*r1);
    172   } else {
    173     throw Word::create_expr(L"Term does not contain an object");
    174   }
     168template <class C>
     169inline C* Term::cast_to (unsigned _type)
     170{
     171  unsigned t = get_short_type();
     172  if (t == _type) return reinterpret_cast<C*>(this);
     173  if (t == type_object) {
     174    Object* obj_ptr = static_cast<ObjectRef*>(this)->get_obj_ptr();
     175    if (obj_ptr->get_type() == _type)
     176      return reinterpret_cast<C*>(obj_ptr);
     177  }
     178  return null;
    175179}
    176180
    177181inline Term::operator pxx::WString () const
    178182{
    179   return (*(short_type_funcs[get_type()]->to_string))(this);
     183  return (*(short_type_funcs[get_short_type()]->to_string))(this);
     184}
     185
     186inline int Term::compare (Term const& _t1, Term const& _t2)
     187{
     188  unsigned type1 = _t1.get_short_type();
     189  unsigned type2 = _t2.get_short_type();
     190  if (type1 == type_parenth && type2 != type_parenth) return 1;
     191  if (type1 != type_parenth && type2 == type_parenth) return -1;
     192  if (type1 > type2) return 1;
     193  if (type1 < type2) return -1;
     194  int (*cmp)(Term const*, Term const*) = short_type_funcs[type1]->compare;
     195  if (cmp) return (*cmp)(&_t1, &_t2);
     196  if (_t1.uint_data2 > _t2.uint_data2) return 1;
     197  if (_t1.uint_data2 < _t2.uint_data2) return -1;
     198  return 0;
     199}
     200
     201inline int Term::compare (
     202  Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2
     203)
     204{
     205  uintptr_t len1 = _l1 - _f1;
     206  uintptr_t len2 = _l2 - _f2;
     207  int compare_len = -1;
     208  uintptr_t min_len = len1;
     209  if (len1 > len2)
     210  {
     211    compare_len = 1;
     212    min_len = len2;
     213  }
     214  else if (len1 == len2)
     215  {
     216    compare_len = 0;
     217  }
     218  for (; min_len; min_len--)
     219  {
     220    int res = Term::compare(*_f1++, *_f2++);
     221    if (res) return res;
     222  }
     223  return compare_len;
    180224}
    181225
  • to-imperative/branches/parenth_term/runtime/rf_vector.ih

    r807 r877  
    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.