source: to-imperative/trunk/runtime/rf_term.ih @ 781

Last change on this file since 781 was 781, checked in by pooh, 18 years ago
  • Added ability to create flat expressions. Fixed Char::create_expr().
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1//-----------------------------------------------------------------------------
2/// @file rf_term.ih
3///
4/// Refal+ term inline method implementation
5//
6//  $Source$
7//  $Revision: 781 $
8//  $Date: 2003-05-28 17:46:35 +0000 (Wed, 28 May 2003) $
9//-----------------------------------------------------------------------------
10
11#ifndef __rf_term_ih__
12#define __rf_term_ih__
13
14#include "rf_term.hh"
15#include "rf_types.ih"
16#include "rf_object.ih"
17#include "rf_expr.hh"
18
19namespace rfrt
20{
21
22using namespace rftype ;
23
24inline Term::Term (term_class_t _class, unsigned _type)
25{
26  set_class(_class);
27  set_type(_type);
28}
29
30inline Term::Term (uint16_t _c) :
31  uint_data2 (_c)
32{
33  set_class(term_sym);
34  set_type(type_char);
35}
36
37inline Term::Term (Object* _obj)
38{
39  set_class(term_obj);
40  set_type(_obj->get_type());
41  new(&uint_data2) Ref(_obj);
42}
43
44inline Term::~Term ()
45{
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
68}
69
70inline Term::Term (Term const& _t)
71{
72  (this->*ctor_funcs[get_class()])(_t);
73}
74
75inline Term& Term::operator = (Term const& _t)
76{
77  if (_t != self) {
78    this->~Term();
79    new(this) Term(_t);
80  }
81  return self;
82}
83
84inline Expr Term::create_expr (size_t _len, int _align /* = 0 */)
85{
86  return Expr(_len, _align);
87}
88
89inline Expr Term::create_flat_expr (size_t _len, int _align /* = 0 */)
90{
91  Expr e(_len, _align);
92  e.flags |= FLAT_BIT;
93  return e;
94}
95
96inline bool Term::is_ref () const
97{
98  return get_class() == term_ref;
99}
100
101inline bool Term::is_sym () const
102{
103  return get_class() == term_sym;
104}
105
106inline bool Term::is_border () const
107{
108  return data1 == SYM_BORDER;
109}
110
111inline Term* Term::get_first () const
112{
113  return (Term*)(data1 & PTR_MASK);
114}
115
116inline Term* Term::get_last () const
117{
118//  return (Term*)(data2 & PTR_MASK);
119  return (Term*)uint_data2;
120}
121
122inline unsigned Term::get_type () const
123{
124  return data1 >> TYPE_SHIFT;
125}
126
127inline void Term::set_type (unsigned _type)
128{
129  data1 = (_type << TYPE_SHIFT) | get_class();
130}
131
132inline term_class_t Term::get_class () const
133{
134  return (term_class_t)(data1 & CLASS_MASK);
135}
136
137inline void Term::set_class (term_class_t _class)
138{
139  data1 &= ~(CLASS_MASK);
140  data1 |= _class;
141}
142
143inline MemoryChunk* Term::get_mem_chunk() const
144{
145  return static_cast<MemoryChunk*>(allocator.get_block(get_first()));
146}
147
148#if defined(RF_STORE_CHUNK_ORDER_IN_TERM)
149inline unsigned get_order () const
150{
151  return Term::ORDER(data1 & ORDER1_MASK, data2 & ORDER2_MASK);
152}
153#endif
154
155inline bool Term::operator == (Term const& _t) const
156{
157  term_class_t cl = get_class();
158  //
159  // If both terms have the same type...
160  if (cl == _t.get_class()){
161    //
162    // ...compare them
163    return (this->*eq_funcs[cl])(_t);
164  } else {
165    return false;
166  }
167}
168
169inline bool Term::operator != (Term const& _t) const
170{
171  return !(*this == _t);
172}
173
174inline bool Term::eq (
175  Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2
176)
177{
178  //
179  // First compare term sequences lengths
180  if ((_l1 - _f1) == (_l2 - _f2)) {
181    //
182    // If lengths are equal do per-term comparison
183    for (; _f1 < _l1; _f1++, _f2++) {
184      if (*_f1 != *_f2) return false;
185    }
186    return true;
187  }
188  return false;
189}
190
191inline Term::Term (Expr const& _expr) :
192  data1 ((uintptr_t)_expr.get_first()),
193  uint_data2 ((uintptr_t)_expr.get_last())
194{
195  set_class(term_ref);
196  D(printf("+ term %p from expression %p(%p,%p,%p)\n",
197           this, &_expr, _expr.first, _expr.last, _expr.mem_chunk);)
198  data1 |= (_expr.get_flags() & FLAT_BIT);
199  _expr.get_mem_chunk()->inc_ref_count();
200}
201
202inline Object& Term::get_object ()
203{
204  if (get_class() == term_obj) {
205    Ref* r1 = (Ref*)(&uint_data2);
206    return *(*r1);
207  } else {
208    FATAL("Term does not contain an object");
209  }
210}
211
212
213inline Term::operator pxx::WString () const
214{
215  return (this->*to_string_funcs[get_class()])();
216}
217
218}
219
220#endif // __rf_term_ih__
Note: See TracBrowser for help on using the repository browser.