File: generatorseq.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (266 lines) | stat: -rw-r--r-- 5,983 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
   FALCON - The Falcon Programming Language.
   FILE: generatorseq.cpp

   Virtual sequence that can be used to iterate over generators.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Thu, 06 Aug 2009 22:10:00 +020

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/


#include <falcon/generatorseq.h>
#include <falcon/error.h>
#include <falcon/eng_messages.h>
#include <falcon/iterator.h>
#include <falcon/corerange.h>
#include <falcon/mempool.h>
#include <falcon/vm.h>

namespace Falcon {

GeneratorSeq::GeneratorSeq( VMachine* runEvn, const Item& callable ):
   m_vm( runEvn ),
   m_callable( callable ),
   m_bHasCachedCur( false ),
   m_bHasCachedNext( false ),
   m_bComplete( false )
{}

GeneratorSeq::GeneratorSeq( const GeneratorSeq& other ):
   m_vm( other.m_vm ),
   m_callable(other.m_callable),
   m_cache_cur( other.m_cache_cur ),
   m_cache_next( other.m_cache_next ),
   m_bHasCachedCur( other.m_bHasCachedCur ),
   m_bHasCachedNext( other.m_bHasCachedNext ),
   m_bComplete( other.m_bComplete )
{}

GeneratorSeq::~GeneratorSeq()
{}


GeneratorSeq* GeneratorSeq::clone() const
{
   return new GeneratorSeq( *this );
}

const Item &GeneratorSeq::front() const
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
      .origin( e_orig_runtime )
      .extra( "GeneratorSeq::front" ) );
}

const Item &GeneratorSeq::back() const
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
      .origin( e_orig_runtime )
      .extra( "GeneratorSeq::back" ) );
}


void GeneratorSeq::clear()
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
      .origin( e_orig_runtime )
      .extra( "GeneratorSeq::clear" ) );
}

bool GeneratorSeq::empty() const
{
   if ( m_bComplete )
      return true;

   if ( m_bHasCachedCur )
      return false;

   return ! fillCurrentValue();
}

void GeneratorSeq::gcMark( uint32 gen )
{
   memPool->markItem( m_callable );

   if( m_bHasCachedCur )
      memPool->markItem( m_cache_cur );
   if ( m_bHasCachedNext )
      memPool->markItem( m_cache_next );

   Sequence::gcMark( gen );
}


bool GeneratorSeq::fillCurrentValue() const
{
   if( m_bComplete )
      return false;

   m_vm->callItemAtomic( m_callable, 0 );
   m_cache_cur = m_vm->regA();
   if( m_cache_cur.isOob() && m_cache_cur.isInteger() && m_cache_cur.asInteger() == 0 )
   {
     m_bComplete = true;
     m_bHasCachedCur = false;
     return false;
   }

   m_bHasCachedCur = true;
   return true;
}

bool GeneratorSeq::fillNextValue() const
{
   if( m_bComplete )
      return false;

   m_vm->callItemAtomic( m_callable, 0 );
   m_cache_next = m_vm->regA();
   if( m_cache_next.isOob() && m_cache_next.isInteger() && m_cache_next.asInteger() == 0 )
   {
      m_bHasCachedNext = false;
      m_bComplete = true;
      return false;
   }
   m_bHasCachedNext = true;
   return true;
}


void GeneratorSeq::append( const Item &data )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
      .origin( e_orig_runtime )
      .extra( "GeneratorSeq::append" ) );
}

void GeneratorSeq::prepend( const Item &data )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
      .origin( e_orig_runtime )
      .extra( "GeneratorSeq::prepend" ) );
}

void GeneratorSeq::getIterator( Iterator& tgt, bool tail ) const
{
   Sequence::getIterator( tgt, tail );
}

void GeneratorSeq::copyIterator( Iterator& tgt, const Iterator& source ) const
{
   Sequence::copyIterator( tgt, source );
}

void GeneratorSeq::insert( Iterator &, const Item & )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
         .origin( e_orig_runtime ).extra( "GeneratorSeq::insert" ) );
}

void GeneratorSeq::erase( Iterator & )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
         .origin( e_orig_runtime ).extra( "GeneratorSeq::erase" ) );
}


bool GeneratorSeq::hasNext( const Iterator &iter ) const
{
   if( ! m_bHasCachedCur )
   {
      if( ! fillCurrentValue() )
         return false;
   }

   if ( ! m_bHasCachedNext )
   {
      if( ! fillNextValue() )
         return false;
   }

   return true;
}

bool GeneratorSeq::hasPrev( const Iterator &iter ) const
{
   return true; // will eventually raise on prev.
}

bool GeneratorSeq::hasCurrent( const Iterator &iter ) const
{
   if( ! m_bHasCachedCur )
   {
      if( ! fillCurrentValue() )
         return false;
   }

   return true;
}

bool GeneratorSeq::next( Iterator &iter ) const
{
   // if we don't have a current value anymore, cache it.
   if( ! m_bHasCachedCur )
   {
      if( ! fillCurrentValue() )
         return false;

      m_bHasCachedNext = false;
      return true;
   }
   else
   {
      // we have a current value; but have we got a next value?
      if( m_bHasCachedNext )
      {
         // Yes --  demote it to current.
         m_cache_cur = m_cache_next;
         m_bHasCachedNext = false;
         return true;
      }
      else {
         // no? -- absorb a next value.
         return fillCurrentValue();
      }
   }
}

bool GeneratorSeq::prev( Iterator &iter ) const
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
      .origin( e_orig_runtime )
      .extra( "GeneratorSeq::prev" ) );
}

Item& GeneratorSeq::getCurrent( const Iterator &iter )
{
   if( ! m_bHasCachedCur )
   {
      fillCurrentValue();
      // TODO: raise if terminated? -- one should call next() or hasCurrent(), but..
   }

   return m_cache_cur;
}

Item& GeneratorSeq::getCurrentKey( const Iterator &iter )
{
   throw new CodeError( ErrorParam( e_non_dict_seq, __LINE__ )
            .origin( e_orig_runtime ).extra( "GeneratorSeq::getCurrentKey" ) );
}

bool GeneratorSeq::equalIterator( const Iterator &first, const Iterator &second ) const
{
   return true;
}


}

/* end of generatorseq.cpp */