Changeset 858


Ignore:
Timestamp:
Jun 22, 2003, 12:52:52 AM (18 years ago)
Author:
orlov
Message:
  • Result of merge from main trunk.
Location:
to-imperative/branches/parenth_term/runtime
Files:
24 edited

Legend:

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

    r807 r858  
    2121  rf_object \
    2222  rf_box \
    23   rf_vector
     23  rf_vector \
     24  rf_channel
    2425
    2526RFRT_SOURCES = $(RFRT_MODULES:=.cc)
  • to-imperative/branches/parenth_term/runtime/rf_char.cc

    r807 r858  
     1#include "rf_char.ih"
    12#include "rf_expr.ih"
    23#include "rf_types.ih"
     
    121122}
    122123
    123 uintptr_t Char::ctor (uintptr_t _data)
     124int Char::compare (Char const* _char1, Char const* _char2)
    124125{
    125   return _data;
    126 }
    127 
    128 void Char::dtor (uintptr_t)
    129 {}
    130 
    131 int Char::compare (uintptr_t _data1, uintptr_t _data2)
    132 {
    133   if (_data1 == _data2) return 0;
    134   else if (_data1 < _data2) return -1;
     126  if (_char1->uint_data2 == _char2->uint_data2) return 0;
     127  else if (_char1->uint_data2 < _char2->uint_data2) return -1;
    135128  else return 1;
    136129}
    137130
    138 uint32_t Char::hash (uintptr_t _data)
     131uint32_t Char::hash (Char const* _char)
    139132{
    140   return _data;
     133  return _char->uint_data2;
    141134}
    142135
    143 pxx::WString Char::to_string (uintptr_t _data)
     136pxx::WString Char::to_string (Char const* _char)
    144137{
    145   wchar_t wc = _data;
     138  wchar_t wc = _char->uint_data2;
    146139  return pxx::WString(&wc, 1);
    147140}
    148141
    149142short_type_funcs_t Char::funcs = {
    150   Char::ctor,
    151   Char::dtor,
     143  null, // Char::ctor,
     144  null, // Char::dtor,
    152145  null, // Char::eq,
    153   Char::compare,
    154   Char::hash,
    155   Char::to_string
     146  (int (*)(Term const*, Term const*))(Char::compare),
     147  (uint32_t (*)(Term const*))(Char::hash),
     148  (pxx::WString (*)(Term const*))(Char::to_string)
    156149};
    157150
  • to-imperative/branches/parenth_term/runtime/rf_char.hh

    r790 r858  
    6767#endif
    6868
    69   static uintptr_t ctor (uintptr_t _data) ;
    70   static void dtor (uintptr_t _data) ;
    71   static bool eq (uintptr_t _data1, uintptr_t _data2) ;
    72   static int compare (uintptr_t _data1, uintptr_t _data2) ;
    73   static uint32_t hash (uintptr_t _data) ;
    74   static pxx::WString to_string (uintptr_t _data) ;
     69  static int compare (Char const* _char1, Char const* _char2) ;
     70  static uint32_t hash (Char const* _char) ;
     71  static pxx::WString to_string (Char const* _char) ;
    7572  static short_type_funcs_t funcs ;
    7673  static TypeRegister reg ;
  • to-imperative/branches/parenth_term/runtime/rf_char.ih

    r807 r858  
    33// $Date$
    44// $Author$
     5
     6#ifndef __rf_char_ih__
     7#define __rf_char_ih__
    58
    69#include "rf_char.hh"
     
    1821
    1922inline Char::Char(wchar_t _wch) :
    20   Term (term_val, type_char)
     23  Term (type_char)
    2124{
    2225  uint_data2 = _wch;
     
    6467
    6568}
     69
     70#endif // __rf_char_ih__
  • to-imperative/branches/parenth_term/runtime/rf_core.cc

    r807 r858  
    3030#include <locale.h>
    3131
     32namespace pxx {
     33  extern void terminate_handler () ;
     34}
     35
    3236namespace rfrt
    3337{
    3438
    3539using namespace rftype;
     40
     41void terminate_handler ()
     42{
     43  try {
     44    throw;
     45  } catch (Expr& _expr) {
     46    _expr.writeln(stderr);
     47  } catch (...) {
     48    pxx::terminate_handler();
     49  }
     50}
    3651
    3752class Init
     
    5267    s = getenv("LC_ALL");
    5368    if (s != null) setlocale(LC_ALL, s);
     69    std::set_terminate(terminate_handler);
    5470  }
    5571
     
    7692pxx::HeapAllocator& allocator = init.allocator ;
    7793
     94TypeRegister rftype::Parenth::reg(&Parenth::funcs, type_parenth) ;
     95TypeRegister rftype::Object::reg(&Object::funcs, type_object) ;
    7896TypeRegister rftype::Char::reg(&Char::funcs, type_char) ;
    7997TypeRegister rftype::ShortInt::reg(&ShortInt::funcs, type_short_int) ;
    8098TypeRegister rftype::Word::reg(&Word::funcs, type_word) ;
    8199
    82 TypeRegister rftype::Parenth::reg(&Parenth::funcs, type_parenth) ;
    83 
    84100ObjectRegister rftype::Box::reg(type_box) ;
    85101ObjectRegister rftype::Vector::reg(type_vector) ;
     102ObjectRegister rftype::Channel::reg(type_channel) ;
    86103
    87104const Expr empty;
  • to-imperative/branches/parenth_term/runtime/rf_core.hh

    r731 r858  
    3737#include "rf_box.ih"
    3838#include "rf_vector.ih"
     39#include "rf_channel.ih"
    3940#include "pxx_heap_allocator.ih"
    4041#include "pxx_common.ih"
  • to-imperative/branches/parenth_term/runtime/rf_expr.hh

    r815 r858  
    8080public:
    8181
     82#if 0
    8283  ///
    8384  /// Constructor from character string
    8485  inline Expr (char const* _string = null) ;
     86#endif
     87  ///
     88  /// Create an empty expression
     89  inline Expr () ;
    8590  ///
    8691  /// Constructor from expression holder. This constructor does dereference
     
    154159  /// Write an expression to buffered file stream. This function is incomplete
    155160  /// and suboptimal.
    156   INLINE void write (FILE* _fp) const ;
     161  INLINE bool write (FILE* _fp) const ;
    157162  ///
    158163  /// The same as write(), but adds new line at the end of output.
    159   inline void writeln (FILE* _fp) const ;
    160   INLINE void print (FILE* _fp) const ;
    161   inline void println (FILE* _fp) const ;
     164  inline bool writeln (FILE* _fp) const ;
     165  INLINE bool print (FILE* _fp) const ;
     166  inline bool println (FILE* _fp) const ;
     167  INLINE operator WString () const ;
    162168  ///
    163169  /// Compute expression hash
     
    188194  /// Same methods for const expressions.
    189195  inline bool operator == (Expr const& _expr) const ;
    190   ///
    191   /// Inequality check operator
    192196  inline bool operator != (Expr const& _expr) const ;
    193197  ///
  • to-imperative/branches/parenth_term/runtime/rf_expr.ih

    r819 r858  
    1616#include "rf_parenth.ih"
    1717#include "rf_object.ih"
     18#include "rf_short_int.ih"
    1819#include "pxx_heap_allocator.ih"
    1920#include "pxx_common.ih"
    2021
    2122#include <wchar.h>
     23#include <wctype.h>
    2224
    2325namespace rfrt
     
    7072}
    7173
     74#if 0
    7275inline Expr::Expr (char const* _string /* = null */)
    7376{
     
    8689  flags = FLAT_BIT;
    8790  D(printf("+ %p(%p,%p,%p) from %s\n", this, first, last, mem_chunk, _string);)
     91}
     92#endif
     93
     94inline Expr::Expr ()
     95{
     96  init(0, 0);
    8897}
    8998
     
    250259#endif
    251260
    252 inline void Expr::writeln (FILE* _fp) const
    253 {
    254   write (_fp);
    255   fputc('\n', _fp);
    256 }
    257 
    258 inline void Expr::println (FILE* _fp) const
    259 {
    260   print (_fp);
    261   fputc('\n', _fp);
     261inline bool Expr::writeln (FILE* _fp) const
     262{
     263  if (!write (_fp)) return false;
     264  if (fputc('\n', _fp) == EOF) return false;
     265  return true;
     266}
     267
     268inline bool Expr::println (FILE* _fp) const
     269{
     270  if (!print (_fp)) return false;
     271  if (fputc('\n', _fp) == EOF) return false;
     272  return true;
    262273}
    263274
     
    408419    return res;
    409420  }
    410 //  return Term::eq(first, last, _expr.first, _expr.last);
    411421}
    412422
     
    732742}
    733743
    734 INLINE void Expr::write (FILE* _fp) const
     744static INLINE bool is_good_wstr (WString const& _ws)
     745{
     746  wchar_t const* p = _ws.get_data();
     747  wchar_t wc;
     748  bool first = true;
     749  if (*p == null) return false;
     750  while ((wc = *p++) != null) {
     751    if (!iswalnum(wc) && wc != L'!' && wc != L'?' && wc != L'-') return false;
     752    if (first && !iswupper(wc)) return false;
     753    first = false;
     754  }
     755  return true;
     756}
     757
     758static INLINE bool write_wstr (FILE* _fp, WString const& _ws)
     759{
     760  wchar_t const* p = _ws.get_data();
     761  wchar_t wc;
     762  char buf[MB_CUR_MAX + 1];
     763  while ((wc = *p++) != null) {
     764    switch (wc) {
     765    case L'\t':
     766      if (fputs("\\t", _fp) == EOF) return false; break;
     767    case L'\r':
     768      if (fputs("\\r", _fp) == EOF) return false; break;
     769    case L'\n':
     770      if (fputs("\\n", _fp) == EOF) return false; break;
     771    case L'\v':
     772      if (fputs("\\v", _fp) == EOF) return false; break;
     773    case L'\\':
     774      if (fputs("\\\\", _fp) == EOF) return false; break;
     775    case L'\'':
     776      if (fputs("\\\'", _fp) == EOF) return false; break;
     777    case L'\"':
     778      if (fputs("\\\"", _fp) == EOF) return false; break;
     779    default:
     780      if (iswprint(wc)) {
     781        size_t n = wctomb(buf, wc);
     782        if (n != (size_t)(-1)) {
     783          buf[n] = 0;
     784          if (fputs(buf, _fp) == EOF) return false;
     785        } else {
     786          if (fprintf(_fp, "\\%04x", wc) == -1) return false;
     787        }
     788      } else {
     789        if (fprintf(_fp, "\\%04x", wc) == -1) return false;
     790      }
     791    }
     792  }
     793  return true;
     794}
     795
     796INLINE bool Expr::write (FILE* _fp) const
    735797{
    736798  bool chars_flag = false;
    737799  for (Term* p = first; p < last; p++) {
    738     if (p->is_sym()) {
    739       if (p->get_type() == type_char) {
    740         if (!chars_flag) {
    741           if (p != first) fputc(' ', _fp);
    742           fputc('\'', _fp);
    743           chars_flag = true;
     800    if (p->get_type() == type_char) {
     801      if (!chars_flag) {
     802        if (p != first) {
     803          if (fputc(' ', _fp) == EOF) return false;
    744804        }
    745         fputc(p->uint_data2, _fp);
     805        if (fputc('\'', _fp) == EOF) return false;
     806        chars_flag = true;
     807      }
     808      if (!write_wstr(_fp, (WString)(*p))) return false;
     809    } else {
     810      if (chars_flag) {
     811        if (fputc('\'', _fp) == EOF) return false;
     812        chars_flag = false;
     813      }
     814      if (p != first) fputc(' ', _fp);
     815      if (p->get_type() == type_parenth) {
     816        if (fputs("( ", _fp) == EOF) return false;
     817        Expr e(p);
     818        if (!e.write(_fp)) return false;
     819        if (fputs(" )", _fp) == EOF) return false;
     820      } else if (p->get_type() == type_word) {
     821        WString ws = (WString)(*p);
     822        bool f = is_good_wstr(ws);
     823        if (!f) fputc('\"', _fp);
     824        if (!write_wstr(_fp, ws)) return false;
     825        if (!f) fputc('\"', _fp);
     826      } else if (p->get_type() == type_short_int) {
     827        if (fprintf(_fp, "%" PRIdPTR, ((ShortInt const&)(*p)).to_int()) == -1)
     828          return false;
    746829      } else {
    747830        FATAL("Not supported yet");
    748831      }
    749     } else {
    750       if (chars_flag) {
    751         fputc('\'', _fp);
    752         chars_flag = false;
    753       }
    754       if (p != first) fputc(' ', _fp);
    755       fputc('(', _fp);
    756       Expr e(p);
    757       e.write(_fp);
    758       fputc(')', _fp);
    759832    }
    760833  }
    761834  if (chars_flag) {
    762     fputc('\'', _fp);
    763   }
    764 }
    765 
    766 INLINE void Expr::print (FILE* _fp) const
     835    if (fputc('\'', _fp) == EOF) return false;
     836  }
     837  return true;
     838}
     839
     840INLINE bool Expr::print (FILE* _fp) const
    767841{
    768842  for (Term* p = first; p < last; p++) {
     
    775849        if (n != (size_t)(-1)) {
    776850          buf[n] = 0;
    777           fputs(buf, _fp);
     851          if (fputs(buf, _fp) == EOF) return false;
    778852        } else {
    779           fputc('?', _fp);
     853          if (fputc('?', _fp) == EOF) return false;
    780854        }
    781855      }
    782856    } else if (p->is_ref()) {
    783       fputc('(', _fp);
     857      if (fputc('(', _fp) == EOF) return false;
    784858      Expr e(p);
    785       e.print(_fp);
    786       fputc(')', _fp);
     859      if (!e.print(_fp)) return false;
     860      if (fputc(')', _fp) == EOF) return false;
    787861    } else {
    788862      FATAL("Not supported yet");
    789863    }
    790864  }
     865  return true;
     866}
     867
     868INLINE Expr::operator WString () const
     869{
     870  WString res;
     871  for (Term* p = first; p < last; p++) {
     872    if (p->is_sym()) {
     873      res = res + (WString)(*p);
     874    } else if (p->is_ref()) {
     875      res = res + WString(L"(") + (WString)(Expr(p)) + WString(L")");
     876    } else {
     877      FATAL("Not supported yet");
     878    }
     879  }
     880  return res;
    791881}
    792882
  • to-imperative/branches/parenth_term/runtime/rf_macros.hh

    r561 r858  
    141141#define cleanup stack.destroy_results(__trap_top)
    142142
    143 #define error(x) throw Expr(x)
     143#define error(x) throw Char::create_expr(x)
    144144
    145145//#define RF_LIB_ERROR(msg) RF_LIB_ERROR_IN_FUNC(__func__, msg)
  • to-imperative/branches/parenth_term/runtime/rf_object.cc

    r482 r858  
    11#include "rf_object.ih"
     2#include "rf_term.ih"
     3#include "pxx_string.ih"
     4
     5namespace rftype
     6{
     7
     8using namespace rfrt ;
     9
     10void 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
     18void Object::dtor (Term* _obj)
     19{
     20  Ref* r = (Ref*)(&_obj->uint_data2);
     21  r->~Ref();
     22}
     23
     24bool Object::eq (Term const* _obj1, Term const* _obj2)
     25{
     26  return _obj1->uint_data2 == _obj2->uint_data2;
     27}
     28
     29//
     30// FIXME: implement this
     31pxx::WString Object::to_string (Term const* _obj)
     32{
     33  return pxx::WString(L"");
     34}
     35
     36short_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

    r502 r858  
    33
    44#include "rf_common.hh"
     5#include "rf_term.hh"
     6#include "pxx_string.hh"
    57
    68namespace rftype
    79{
     10
     11using namespace rfrt ;
    812
    913class Object
     
    7377#endif
    7478
     79
     80private:
     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
    7590};
    7691
     
    99114  /// Dereference operator
    100115  inline Object& operator * () ;
    101 
    102116};
    103117
  • to-imperative/branches/parenth_term/runtime/rf_parenth.cc

    r815 r858  
    77using namespace rfrt ;
    88
    9 uintptr_t Parenth::ctor (uintptr_t _data)
     9void Parenth::ctor (Parenth* _to, Parenth const* _from)
    1010{
    11   reinterpret_cast<Content*>(_data)->ref_count++;
    12   return _data;
     11  _to->data1 = _from->data1;
     12  _to->ptr_data2 = _from->ptr_data2;
     13  static_cast<Content*>(_to->ptr_data2)->ref_count++;
    1314}
    1415
    15 void Parenth::dtor (uintptr_t _data)
     16void Parenth::dtor (Parenth* _parenth)
    1617{
    17   Content* p = reinterpret_cast<Content*>(_data);
     18  Content* p = static_cast<Content*>(_parenth->ptr_data2);
    1819  p->ref_count--;
    1920  if (!p->ref_count)
     
    2425}
    2526
    26 bool Parenth::eq (uintptr_t _data1, uintptr_t _data2)
     27bool Parenth::eq (Parenth const* _parenth1, Parenth const* _parenth2)
    2728{
    28   Content* p1 = reinterpret_cast<Content*>(_data1);
    29   Content* p2 = reinterpret_cast<Content*>(_data2);
     29  Content* p1 = static_cast<Content*>(_parenth1->ptr_data2);
     30  Content* p2 = static_cast<Content*>(_parenth2->ptr_data2);
    3031  return p1->expr == p2->expr;
    3132}
    3233
    33 pxx::WString Parenth::to_string (uintptr_t)
     34pxx::WString Parenth::to_string (Parenth const*)
    3435{
    3536  return pxx::WString(L"");
     
    3738
    3839short_type_funcs_t Parenth::funcs = {
    39   Parenth::ctor,
    40   Parenth::dtor,
    41   Parenth::eq,
    42   null, //Parenth::compare,
    43   null, //Parenth::hash,
    44   Parenth::to_string
     40  (void (*)(Term*, Term const*))(Parenth::ctor),
     41  (void (*)(Term*))(Parenth::dtor),
     42  (bool (*)(Term const*, Term const*))(Parenth::eq),
     43  null, //(int (*)(Term const*, Term const*))(Parenth::compare),
     44  null, //(uint32_t (*)(Term const*))(Parenth::hash),
     45  (pxx::WString (*)(Term const*))(Parenth::to_string)
    4546};
    4647
  • to-imperative/branches/parenth_term/runtime/rf_parenth.hh

    r815 r858  
    3131  inline const Expr& get_expr () const ;
    3232
    33   static uintptr_t ctor (uintptr_t _data) ;
    34   static void dtor (uintptr_t _data) ;
    35   static bool eq (uintptr_t _data1, uintptr_t _data2) ;
    36   static int compare (uintptr_t _data1, uintptr_t _data2) ;
    37   static uint32_t hash (uintptr_t _data) ;
    38   static pxx::WString to_string (uintptr_t _data) ;
     33  static void ctor (Parenth* _to, Parenth const* _from) ;
     34  static void dtor (Parenth* _parenth) ;
     35  static bool eq (Parenth const* _parenth1, Parenth const* _parenth2) ;
     36  static int compare (Parenth const* _parenth1, Parenth const* _parenth2) ;
     37  static uint32_t hash (Parenth const* _parenth) ;
     38  static pxx::WString to_string (Parenth const* _parenth) ;
    3939  static short_type_funcs_t funcs ;
    4040  static TypeRegister reg ;
    41 
     41 
    4242};
    4343
  • to-imperative/branches/parenth_term/runtime/rf_parenth.ih

    r815 r858  
    1111
    1212inline Parenth::Parenth (const Expr& _expr) :
    13   Term (term_val, type_parenth)
     13  Term (type_parenth)
    1414{
    1515  Content* p = static_cast<Content*>(allocator.allocate(sizeof(Content)));
  • to-imperative/branches/parenth_term/runtime/rf_short_int.cc

    r790 r858  
    99#include "rf_char.ih"
    1010#include "rf_word.ih"
     11#include "wchar.h"
    1112
    1213namespace rftype
     
    2324const unsigned ShortInt::max_len = compute_max_len();
    2425
    25 Expr ShortInt::create_expr (intptr_t _n) 
     26Expr ShortInt::create_expr (intptr_t _n)
    2627{
    2728  Expr e = Term::create_expr(1);
     
    3031}
    3132
     33Expr ShortInt::create_expr (pxx::WString& _str) {
     34  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);
    3241
     42}
     43
     44#if 0
    3345Expr ShortInt::create_expr (Expr& _expr, int _flag)
    3446{
     
    6173      intptr_t n_temp = ((ShortInt*)p)->to_int() / 10;
    6274      intptr_t k      = 10;
    63      
     75
    6476      while (n_temp) {
    6577        n_temp /= 10;
     
    6880
    6981      n = n * k + ((ShortInt*)p)->to_int();
    70      
     82
    7183    }
    7284    if (p->get_type() == type_char) {
    73       if (((Char*)p)->is_digit()) 
     85      if (((Char*)p)->is_digit())
    7486        n = n * 10 + (((Char*)p)->to_wchar_t() - 48);
    7587    }
     
    8799      Word::NumIterator j = *((Word*)p);
    88100
    89       for (; j.in_bounds(); j++){       
     101      for (; j.in_bounds(); j++){
    90102        n_word += (*j) * k_word;
    91103        k_word /= 10;
    92       } 
    93       n += size_t (n_word);                 
     104      }
     105      n += size_t (n_word);
    94106    }
    95107  }
     
    100112  return e;
    101113}
     114#endif
    102115
    103 uintptr_t ShortInt::ctor (uintptr_t _data)
     116void ShortInt::ctor (ShortInt* _to, ShortInt const* _from)
    104117{
    105   return _data;
     118  _to->data1 = _from->data1;
     119  _to->uint_data2 = _from->uint_data2;
    106120}
    107121
    108 void ShortInt::dtor (uintptr_t)
    109 { }
    110 
    111 int ShortInt::compare (uintptr_t _data1, uintptr_t _data2)
     122int ShortInt::compare (ShortInt const* _shint1, ShortInt const* _shint2)
    112123{
    113   if (_data1 == _data2) return 0;
    114   else if (_data1 < _data2) return -1;
     124  if (_shint1->int_data2 == _shint2->int_data2) return 0;
     125  else if (_shint1->int_data2 < _shint2->int_data2) return -1;
    115126  else return 1;
    116127}
    117128
    118 uint32_t ShortInt::hash (uintptr_t _data)
     129uint32_t ShortInt::hash (ShortInt const* _shint)
    119130{
    120   return _data;
     131  return _shint->uint_data2;
    121132}
    122133
    123 pxx::WString ShortInt::to_string (uintptr_t _data) {
    124   intptr_t n = _data;
     134pxx::WString ShortInt::to_string (ShortInt const* _shint) {
     135  intptr_t n = _shint->int_data2;
    125136  int flag = 0;
    126    
     137
    127138  if (n < 0) {
    128139    n = -n;
     
    140151      str[i] = n % 10 + 48;
    141152      n /= 10;
    142       i--; 
     153      i--;
    143154    };
    144155
    145156    if (flag) {
    146       str[i] = '-'; 
     157      str[i] = '-';
    147158    }
    148159    else i++;
    149160  }
    150161  return pxx::WString(&str[i], max_len + 1 - i);
    151  
     162
    152163}
    153164
    154165short_type_funcs_t ShortInt::funcs = {
    155   ShortInt::ctor,
    156   ShortInt::dtor,
     166  (void (*)(Term*, Term const*))(ShortInt::ctor),
     167  null, // ShortInt::dtor,
    157168  null, // ShortInt::eq,
    158   ShortInt::compare,
    159   ShortInt::hash,
    160   ShortInt::to_string
     169  (int (*)(Term const*, Term const*))(ShortInt::compare),
     170  (uint32_t (*)(Term const*))(ShortInt::hash),
     171  (pxx::WString (*)(Term const*))(ShortInt::to_string)
    161172};
    162173
  • to-imperative/branches/parenth_term/runtime/rf_short_int.hh

    r723 r858  
    1111#include "rf_term.hh"
    1212
    13 namespace rftype 
     13namespace rftype
    1414{
    1515
     
    2323
    2424public:
    25  
     25
    2626  static const unsigned max_len;
    2727
    2828  static Expr create_expr (intptr_t _n) ;
    29   static Expr create_expr (Expr& _expr, int _flag);
     29
     30  static Expr create_expr (pxx::WString& _str);
    3031
    3132  inline ShortInt (intptr_t n) ;
     
    3435  inline size_t get_char_len () const ;
    3536
    36   static uintptr_t ctor (uintptr_t _data) ;
    37   static void dtor (uintptr_t _data) ;
    38   static bool eq (uintptr_t _data1, uintptr_t _data2) ;
    39   static int compare (uintptr_t _data1, uintptr_t _data2) ;
    40   static uint32_t hash (uintptr_t _data) ;
    41   static pxx::WString to_string (uintptr_t _data) ;
     37  static void ctor (ShortInt* _to, ShortInt const* _from) ;
     38  static int compare (ShortInt const* _shint1, ShortInt const* _shint2) ;
     39  static uint32_t hash (ShortInt const* _shint) ;
     40  static pxx::WString to_string (ShortInt const* _shint) ;
    4241  static short_type_funcs_t funcs ;
    4342  static TypeRegister reg ;
    44  
     43
    4544};
    4645
    4746}
     47//  static Expr create_expr (Expr& _expr, int _flag);
    4848
    4949#endif // __rf_short_int_hh__
  • to-imperative/branches/parenth_term/runtime/rf_short_int.ih

    r807 r858  
    44// $Author$
    55
     6#ifndef __rf_short_int_ih__
     7#define __rf_short_int_ih__
     8
    69#include "rf_short_int.hh"
    710#include "rf_term.ih"
    811
    9 namespace rftype 
     12namespace rftype
    1013{
    1114
     
    1316
    1417inline ShortInt::ShortInt(intptr_t _n) :
    15   Term(term_val, type_short_int)
     18  Term(type_short_int)
    1619{
    1720  int_data2 = _n;
    1821}
    1922
    20 inline intptr_t ShortInt::to_int() const 
     23inline intptr_t ShortInt::to_int() const
    2124{
    2225  return int_data2;
     
    3538    };
    3639  };
    37   return k; 
     40  return k;
    3841};
    3942
    4043}
     44
     45#endif // __rf_short_int_ih__
  • to-imperative/branches/parenth_term/runtime/rf_term.cc

    r815 r858  
    1212#include "rf_term.ih"
    1313
    14 #include <new>
    15 
    16 namespace rfrt {
    17 
    18 using namespace rftype ;
    19 
    20 Term::ctor_func_t Term::ctor_funcs[term_class_num] = {
    21   &Term::ctor_val,
    22   &Term::ctor_obj
    23 };
    24 
    25 Term::dtor_func_t Term::dtor_funcs[term_class_num] = {
    26   &Term::dtor_val,
    27   &Term::dtor_obj
    28 };
    29 
    30 Term::eq_func_t Term::eq_funcs[term_class_num] = {
    31   &Term::eq_val,
    32   &Term::eq_obj
    33 };
    34 
    35 Term::to_string_func_t Term::to_string_funcs[term_class_num] = {
    36   &Term::to_string_val,
    37   &Term::to_string_obj
    38 };
    39 
    40 void Term::ctor_val (Term const& _t)
    41 {
    42   data1 = _t.data1;
    43   uintptr_t (*pf)(uintptr_t) = short_type_funcs[get_type()]->ctor;
    44   if (pf != null) uint_data2 = (*pf)(_t.uint_data2);
    45   else uint_data2 = _t.uint_data2;
    46 }
    47 
    48 void Term::ctor_obj (Term const& _t)
    49 {
    50   set_class(term_obj);
    51   Ref* r1 = (Ref*)(uint_data2);
    52   Ref* r2 = (Ref*)(&(_t.uint_data2));
    53   new(r1) Ref(*r2);
    54 }
    55 
    56 void Term::dtor_val ()
    57 {
    58   void (*pf)(uintptr_t) = short_type_funcs[get_type()]->dtor;
    59   if (pf != null) (*pf)(uint_data2);
    60 }
    61 
    62 void Term::dtor_obj ()
    63 {
    64   Ref* r = (Ref*)(&uint_data2);
    65   r->~Ref();
    66 }
    67 
    68 bool Term::eq_val (Term const& _t) const
    69 {
    70   unsigned type = get_type();
    71   if (type != _t.get_type())
    72     return false;
    73   if (uint_data2 == _t.uint_data2)
    74     return true;
    75   bool (*pf)(uintptr_t, uintptr_t) = short_type_funcs[type]->eq;
    76   if (pf)
    77     return (*pf)(uint_data2, _t.uint_data2);
    78   return false;
    79 }
    80 
    81 bool Term::eq_obj (Term const& _t) const
    82 {
    83   Ref* r1 = (Ref*)(&uint_data2);
    84   Ref* r2 = (Ref*)(&(_t.uint_data2));
    85   return *(*r1) == *(*r2);
    86 }
    87 
    88 pxx::WString Term::to_string_val () const
    89 {
    90   return (*(short_type_funcs[get_type()]->to_string))(uint_data2);
    91 }
    92 
    93 pxx::WString Term::to_string_obj () const
    94 {
    95   return WString(L"");
    96 }
    97 
    98 }
    99 
  • to-imperative/branches/parenth_term/runtime/rf_term.hh

    r819 r858  
    1515#include "rf_common.hh"
    1616#include "rf_types.hh"
    17 #include "rf_object.hh"
    1817#include "pxx_string.hh"
     18
     19namespace rftype {
     20  class Object ;
     21}
    1922
    2023///
     
    2225namespace rfrt
    2326{
    24 
    25 using namespace rftype ;
    2627
    2728//
     
    3132// stuff related to this. However, we still keep old code, because in a future
    3233// we may return to the old scheme if it can lead to faster program execution.
    33 #if !defined(RF_STORE_CHUNK_ORDER_IN_TERM)
     34#if defined(RF_STORE_CHUNK_ORDER_IN_TERM)
    3435
    35 typedef enum {
    36   term_val,
    37   term_obj,
    38   term_class_num
    39 } term_class_t ;
    40 
    41 ///
    42 /// Type mask. Used to distinguish between reference term and other terms.
    43 /// This is the maximum number of internal "short" types (i.e. types that can
    44 /// be placed in place in term data.
    45 #define TYPE_MASK (uintptr_t(0xFFFF0))
    46 #define CLASS_MASK (uintptr_t(0x03))
    47 #define TYPE_SHIFT 4
    48 
    49 #else
    5036#if UINTPTR_MAX == UINT32_MAX
    5137#define ORDER_SHIFT 3
     
    8571/// We can access all term fields from expression class and some methods
    8672friend class Expr ;
     73friend class rftype::Object ;
    8774friend inline Expr operator + (Term const&, Expr const&) ;
    8875friend inline Expr operator + (Term const&, Term const&) ;
     
    10491  ///
    10592  /// Constructor for empty term with given class and type
    106   inline Term (term_class_t _class, unsigned _type) ;
     93  inline Term (unsigned _type) ;
    10794  ///
    10895  /// Create expression with specified length
     
    120107  /// was decided later to have both character types, so probably instead of
    121108  /// this we should have Term (char _c) and Term (wchar_t _c).
    122   inline Term (uint16_t _c) ;
     109//  inline Term (uint16_t _c) ;
    123110
    124   inline Term (Object* _obj) ;
     111  inline Term (rftype::Object* _obj) ;
    125112
    126113  inline Term (Term const& _t) ;
    127114  inline Term& operator = (Term const& _t) ;
    128 
    129   typedef void (Term::*ctor_func_t)(Term const& _t) ;
    130   typedef void (Term::*dtor_func_t)() ;
    131   typedef bool (Term::*eq_func_t)(Term const& _t) const ;
    132   typedef pxx::WString (Term::*to_string_func_t)() const ;
    133   typedef size_t (Term::*get_char_len_func_t)() const ;
    134   typedef Term* (Term::*to_chars_func_t)(Term* _p) const ;
    135 
    136   static ctor_func_t ctor_funcs[term_class_num] ;
    137   static dtor_func_t dtor_funcs[term_class_num] ;
    138   static eq_func_t eq_funcs[term_class_num] ;
    139   static to_string_func_t to_string_funcs[term_class_num] ;
    140 
    141   void ctor_val (Term const& _t) ;
    142   void ctor_obj (Term const& _t) ;
    143   void dtor_val () ;
    144   void dtor_obj () ;
    145   bool eq_val (Term const& _t) const ;
    146   bool eq_obj (Term const& _t) const ;
    147   pxx::WString to_string_val () const ;
    148   pxx::WString to_string_obj () const ;
    149115
    150116public:
     
    172138  ) ;
    173139  ///
     140  /// Check whether a term is a symbol
     141  inline bool is_sym () const ;
     142  ///
    174143  /// Check whether a term is a reference term
    175144  inline bool is_ref () const ;
    176   ///
    177   /// Check whether a term is a symbol
    178   inline bool is_sym () const ;
    179145  ///
    180146  /// Check whether a term is a border symbol
     
    186152  /// Set a type of "short" symbol.
    187153  inline void set_type (unsigned _type) ;
    188   inline term_class_t get_class () const ;
    189   inline void set_class (term_class_t _class) ;
    190   inline Object& get_object () ;
     154
     155  inline rftype::Object& get_object () ;
    191156
    192157  inline operator pxx::WString () const ;
  • to-imperative/branches/parenth_term/runtime/rf_term.ih

    r819 r858  
    1616#include "rf_object.ih"
    1717#include "rf_expr.hh"
     18#include "rf_word.ih"
    1819
    1920namespace rfrt
     
    2223using namespace rftype ;
    2324
    24 inline Term::Term (term_class_t _class, unsigned _type)
     25inline Term::Term (unsigned _type)
    2526{
    26   set_class(_class);
    2727  set_type(_type);
    2828}
    2929
     30#if 0
    3031inline Term::Term (uint16_t _c) :
    3132  uint_data2 (_c)
    3233{
    33   set_class(term_val);
    3434  set_type(type_char);
    3535}
     36#endif
    3637
    3738inline Term::Term (Object* _obj)
    3839{
    39   set_class(term_obj);
    40   set_type(_obj->get_type());
     40  set_type(type_object);
    4141  new(&uint_data2) Ref(_obj);
    4242}
     
    4444inline Term::~Term ()
    4545{
    46 #if 0
    47   if (is_ref()) {
    48 //    Expr e(this);
    49 //    e.get_mem_chunk()->dec_ref_count();
    50     MemoryChunk* m = get_mem_chunk();
    51     //
    52     // Decrement reference counter and destroy an object if it is zero
    53     if (m->dec_ref_count() == 0) {
    54       //
    55       // Walk through and decrement reference counters on childs
    56 //      bool f = (data1 & REF_BIT) == 0 ? true : false;
    57       Expr::deref_childs(
    58         get_first(), (data1 & FLAT_BIT) != 0 ? get_last() : get_first(), m
    59       );
    60       //
    61       // Deallocate expression holder in memory
    62       MemoryChunk::destroy_instance(m);
    63     }
    64   }
    65 #else
    66   (this->*dtor_funcs[get_class()])();
    67 #endif
     46  void (*pf)(Term*) = short_type_funcs[get_type()]->dtor;
     47  if (pf != null) (*pf)(this);
    6848}
    6949
    7050inline Term::Term (Term const& _t)
    7151{
    72   (this->*ctor_funcs[_t.get_class()])(_t);
     52  void (*pf)(Term*, Term const*) = short_type_funcs[_t.get_type()]->ctor;
     53  if (pf != null) (*pf)(this, &_t);
     54  else {
     55    data1 = _t.data1;
     56    uint_data2 = _t.uint_data2;
     57  }
    7358}
    7459
     
    9479}
    9580
     81inline bool Term::is_sym () const
     82{
     83  return get_type() > type_parenth;
     84}
     85
    9686inline bool Term::is_ref () const
    9787{
    9888  return get_type() == type_parenth;
    99 }
    100 
    101 inline bool Term::is_sym () const
    102 {
    103   return get_type() != type_parenth;
    10489}
    10590
     
    11196inline unsigned Term::get_type () const
    11297{
    113   return data1 >> TYPE_SHIFT;
     98  return data1;
    11499}
    115100
    116101inline void Term::set_type (unsigned _type)
    117102{
    118   data1 = (_type << TYPE_SHIFT) | get_class();
    119 }
    120 
    121 inline term_class_t Term::get_class () const
    122 {
    123   return (term_class_t)(data1 & CLASS_MASK);
    124 }
    125 
    126 inline void Term::set_class (term_class_t _class)
    127 {
    128   data1 &= ~(CLASS_MASK);
    129   data1 |= _class;
     103  data1 = _type;
    130104}
    131105
     
    141115  if (data1 != _t.data1) return false;
    142116  if (uint_data2 == _t.uint_data2) return true;
    143   if (get_class() != term_val) return false;
    144   bool (*pf)(uintptr_t, uintptr_t) = short_type_funcs[get_type()]->eq;
    145   if (!pf || !(*pf)(uint_data2, _t.uint_data2)) return false;
    146   return true;
     117  bool (*pf)(Term const*, Term const*) = short_type_funcs[get_type()]->eq;
     118  if (pf && (*pf)(this, &_t)) return true;
     119  return false;
    147120}
    148121
     
    194167inline Object& Term::get_object ()
    195168{
    196   if (get_class() == term_obj) {
     169  if (get_type() == type_object) {
    197170    Ref* r1 = (Ref*)(&uint_data2);
    198171    return *(*r1);
    199172  } else {
    200     FATAL("Term does not contain an object");
     173    throw Word::create_expr(L"Term does not contain an object");
    201174  }
    202175}
     
    204177inline Term::operator pxx::WString () const
    205178{
    206   return (this->*to_string_funcs[get_class()])();
     179  return (*(short_type_funcs[get_type()]->to_string))(this);
    207180}
    208181
  • to-imperative/branches/parenth_term/runtime/rf_types.hh

    r807 r858  
    44#include "rf_common.hh"
    55#include "pxx_string.hh"
    6 #include "rf_term.hh"
    76
    87namespace rfrt
    98{
     9
     10class Term ;
    1011
    1112static const unsigned short_types_count = 0x1000 ;
     
    1516{
    1617  SYM_BORDER = 0,
    17   type_char = 1,
     18  type_parenth = 1,
     19  type_object,
     20  type_char,
    1821  type_word,
    1922  type_int,
    2023  type_short_int,
    21   type_parenth,
    2224  type_user,
    23   type_object = short_types_count,
     25  type_object_begin = short_types_count,
    2426  type_box,
    2527  type_vector,
    2628  type_string,
    2729  type_table,
     30  type_channel,
    2831  type_user_object,
    2932  type_expr = max_type
     
    3740  ///
    3841  /// Copy constructor for short symbol
    39   uintptr_t (*ctor)(uintptr_t) ;
     42  void (*ctor)(Term*, Term const*) ;
    4043  ///
    4144  /// Destructor for short symbol
    42   void (*dtor)(uintptr_t) ;
     45  void (*dtor)(Term*) ;
    4346  ///
    4447  /// Equality check for short symbol (may be faster than compare)
    45   bool (*eq)(uintptr_t, uintptr_t) ;
     48  bool (*eq)(Term const*, Term const*) ;
    4649  ///
    4750  /// Compare function for short symbol
    48   int (*compare)(uintptr_t, uintptr_t) ;
     51  int (*compare)(Term const*, Term const*) ;
    4952  ///
    5053  /// Hash function for short symbol.
    51   uint32_t (*hash)(uintptr_t) ;
     54  uint32_t (*hash)(Term const*) ;
    5255  ///
    5356  /// Conversion to string.
    54   pxx::WString (*to_string)(uintptr_t) ;
     57  pxx::WString (*to_string)(Term const*) ;
    5558} short_type_funcs_t ;
    5659
  • to-imperative/branches/parenth_term/runtime/rf_word.cc

    r786 r858  
    88using namespace rfrt ;
    99
    10 uintptr_t Word::ctor (uintptr_t _data)
     10void Word::ctor (Word* _to, Word const* _from)
    1111{
    12   Header* h = reinterpret_cast<Header*>(_data);
    13   h->ref_count++;
    14   return _data;
     12  _to->data1 = _from->data1;
     13  _to->ptr_data2 = _from->ptr_data2;
     14  (static_cast<Header*>(_to->ptr_data2))->ref_count++;
    1515}
    1616
    17 void Word::dtor (uintptr_t _data)
     17void Word::dtor (Word* _word)
    1818{
    19   Header* h = reinterpret_cast<Header*>(_data);
     19  Header* h = static_cast<Header*>(_word->ptr_data2);
    2020  if (--(h->ref_count) == 0) allocator.deallocate(h);
    2121}
    2222
    23 bool Word::eq (uintptr_t _data1, uintptr_t _data2)
     23bool Word::eq (Word const* _word1, Word const* _word2)
    2424{
    25   Header* h1 = reinterpret_cast<Header*>(_data1);
    26   Header* h2 = reinterpret_cast<Header*>(_data2);
     25  Header* h1 = static_cast<Header*>(_word1->ptr_data2);
     26  Header* h2 = static_cast<Header*>(_word2->ptr_data2);
    2727  if (h1 == h2) return true;
    2828  else {
    29     if (hash(_data1) != hash(_data2)) return false;
     29    if (hash(_word1) != hash(_word2)) return false;
    3030    else if (h1->length != h2->length) return false;
    3131    else if (wmemcmp(h1->content, h2->content, h1->length) == 0) return true;
     
    3434}
    3535
    36 pxx::WString Word::to_string (uintptr_t _data)
     36pxx::WString Word::to_string (Word const* _word)
    3737{
    38   Header* h = reinterpret_cast<Header*>(_data);
     38  Header* h = static_cast<Header*>(_word->ptr_data2);
    3939  return pxx::WString(h->content, h->length);
    4040}
     
    5656}
    5757
    58 int Word::compare (uintptr_t _data1, uintptr_t _data2)
     58Expr Word::create_expr (Expr const& _expr) {
     59  Term* p = _expr.get_first();
     60
     61  pxx::WString str;
     62
     63  for (; p < _expr.get_last(); p++){
     64    str = str + pxx::WString(*p);
     65  }
     66
     67  Expr e = Term::create_expr(1);
     68  Term* t = e.get_first();
     69  new(t) Word(str.get_data());
     70  return e; 
     71}
     72
     73
     74int Word::compare (Word const* _word1, Word const* _word2)
    5975{
    60   Header* h1 = reinterpret_cast<Header*>(_data1);
    61   Header* h2 = reinterpret_cast<Header*>(_data2);
     76  Header* h1 = static_cast<Header*>(_word1->ptr_data2);
     77  Header* h2 = static_cast<Header*>(_word2->ptr_data2);
    6278  if (h1 == h2) return 0;
    6379  else {
     
    7187}
    7288
    73 uint32_t Word::hash (uintptr_t _data)
     89uint32_t Word::hash (Word const* _word)
    7490{
    75   Header* h = reinterpret_cast<Header*>(_data);
     91  Header* h = static_cast<Header*>(_word->ptr_data2);
    7692  if (h->hash == 0xffffffff) {
    7793    // Should never occur, because we always compute hash for words
     
    8298
    8399short_type_funcs_t Word::funcs = {
    84   Word::ctor,
    85   Word::dtor,
    86   Word::eq,
    87   Word::compare,
    88   Word::hash,
    89   Word::to_string
     100  (void (*)(Term*, Term const*))(Word::ctor),
     101  (void (*)(Term*))(Word::dtor),
     102  (bool (*)(Term const*, Term const*))(Word::eq),
     103  (int (*)(Term const*, Term const*))(Word::compare),
     104  (uint32_t (*)(Term const*))(Word::hash),
     105  (pxx::WString (*)(Term const*))(Word::to_string)
    90106};
    91107
  • to-imperative/branches/parenth_term/runtime/rf_word.hh

    r789 r858  
    4343  inline size_t get_len () const;
    4444
     45  static Expr create_expr (wchar_t const* _wstr) ;
     46
     47  static Expr create_expr (char const* _str, char const* _locale = null) ;
     48
     49  static Expr create_expr (Expr const& _expr) ;
     50
     51  static void ctor (Word* _to, Word const* _from) ;
     52  static void dtor (Word* _word) ;
     53  static bool eq (Word const* _word1, Word const* _word2) ;
     54  static int compare (Word const* _word1, Word const* _word2) ;
     55  static uint32_t hash (Word const* _word) ;
     56  static pxx::WString to_string (Word const* _word) ;
     57  static short_type_funcs_t funcs ;
     58  static TypeRegister reg ;
     59
     60 
     61 
     62};
     63
     64
     65
     66}
     67#if 0
    4568  typedef enum {plus, minus, positive_number,
    4669                not_a_number, space_number} is_number_res;
     
    4972
    5073  inline int has_spaces () const ;
    51 
    52   static Expr create_expr (wchar_t const* _wstr) ;
    53 
    54   static Expr create_expr (char const* _str, char const* _locale = null) ;
    55 
    56   static Expr create_expr (Expr const& _expr) ;
    57 
    58 
    59   static uintptr_t ctor (uintptr_t _data) ;
    60   static void dtor (uintptr_t _data) ;
    61   static bool eq (uintptr_t _data1, uintptr_t _data2) ;
    62   static int compare (uintptr_t _data1, uintptr_t _data2) ;
    63   static uint32_t hash (uintptr_t _data) ;
    64   static pxx::WString to_string (uintptr_t _data) ;
    65   static short_type_funcs_t funcs ;
    66   static TypeRegister reg ;
    67 
    68  
    6974  class NumIterator
    7075  {
     
    7984    inline const char operator*() const;
    8085  };
    81  
    82 };
    8386
    84 
    85 
    86 }
     87#endif
    8788
    8889#endif // __rf_word_hh__
  • to-imperative/branches/parenth_term/runtime/rf_word.ih

    r807 r858  
    1515
    1616inline Word::Word (wchar_t const* _wstr) :
    17   Term (term_val, type_word)
     17  Term (type_word)
    1818{
    1919  size_t len = wcslen(_wstr);
     
    3232
    3333inline Word::Word (char const* _str, char const* _locale /* = null */) :
    34   Term (term_val, type_word)
     34  Term (type_word)
    3535{
    3636  char const* saved_locale = null;
     
    6060 
    6161inline Word::Word (Word const& _word) :
    62   Term (term_val, type_word)
     62  Term (type_word)
    6363{
    6464  ptr_data2 = _word.ptr_data2;
     
    9090}
    9191
    92 
     92#if 0
    9393//first symbol in word is '-' res == -1; '+' res == 1,
    9494//'digit' res == 2; othewise res == 0
     
    224224    return *iter - 48;
    225225}
    226 
    227 #if 0
    228226Expr Word::create_expr (wchar_t const* _wstr)
    229227 {
Note: See TracChangeset for help on using the changeset viewer.