root/src/Parser/variable.cpp

Revision 196, 35.9 kB (checked in by hak, 2 months ago)

09/14/08 Ver.0.91.0.137 checked in.
- Added OSX/GCC support!!
- Can compile in XCode 3.1/GCC 4.0.1 & Visual Studio 2005.
- Runs in OSX 10.5.4 & Windows XP/Vista
- Number::toString support a radix.
- BRFALSE bug fix
- Minor code clean up

Line 
1 /****************************************************************************
2  *
3  * CRI Middleware SDK
4  *
5  * Copyright (c) 2008 CRI Middleware, Inc. All rights reserved.
6  *
7  * Use, modification and distribution are subject to the CRI Script Software
8  * License, Version 1.0(see accompanying file "CriScript_License_1_0.txt" or
9  * copy at www.criscript.com/trac/wiki/CRI%20Script%20Software%20License%201.0 ).
10  *
11  *
12  * Library  : CRIScript
13  * Module   : cil Virtual Machine/CodeGen
14  * File     : Variable.cpp
15  * Date     :
16  * Version  :
17  *
18  ****************************************************************************/
19
20 /***************************************************************************
21  *      Include file
22  ***************************************************************************/
23 #include "stdafx.h"
24 #include "criScript.h"
25 namespace cri {
26 /***************************************************************************
27  *      Variables
28  ***************************************************************************/
29
30 /***************************************************************************
31  *      Methods
32  ***************************************************************************/
33
34 /***************************************************************************
35  *      CVariable::ctor
36  ***************************************************************************/
37 CVariable::CVariable() : iOperandType(OPERAND_UNDEFINED), iOperandFlag( OPERAND_FLAG_NONE ), i64Value( 0 ),
38                                                                 ridConstraintType( 0 )
39
40 {
41 };
42
43 CVariable::CVariable( const int32_t i )// : i64Value(0)
44 {
45 #ifdef WIN32
46         //Assuming bitfield layout is contiguous in memory
47         iPrameters = OPERAND_INT;
48 #else
49         iOperandType = OPERAND_INT;
50         iOperandFlag = OPERAND_FLAG_NONE;
51         ridConstraintType = 0;
52 #endif
53         iValue = i;
54 };
55
56 CVariable::CVariable( const int64_t i )
57 {
58 #ifdef WIN32
59         //Assuming bitfield layout is contiguous in memory
60         iPrameters = OPERAND_INT64;
61 #else
62         iOperandType = OPERAND_INT64;
63         iOperandFlag = OPERAND_FLAG_NONE;
64         ridConstraintType = 0;
65 #endif
66         i64Value = i;
67 };
68
69 CVariable::CVariable( const float f )// : i64Value(0)
70 {
71 #ifdef WIN32
72         //Assuming bitfield layout is contiguous in memory
73         iPrameters = OPERAND_FLOAT;
74 #else
75         iOperandType = OPERAND_FLOAT;
76         iOperandFlag = OPERAND_FLAG_NONE;
77         ridConstraintType = 0;
78 #endif
79         fValue = f;
80 };
81        
82 CVariable::CVariable( const double d )
83 {
84 #ifdef WIN32
85         //Assuming bitfield layout is contiguous in memory
86         iPrameters = OPERAND_DOUBLE;
87 #else
88         iOperandType = OPERAND_DOUBLE;
89         iOperandFlag = OPERAND_FLAG_NONE;
90         ridConstraintType = 0;
91 #endif
92         dValue = d;
93 };
94
95 CVariable::CVariable( const VMOBJECTREF ref )// : i64Value(0)
96 {
97 #ifdef WIN32
98         //Assuming bitfield layout is contiguous in memory
99         iPrameters = OPERAND_OBJECTREF;
100 #else
101         iOperandType = OPERAND_OBJECTREF;
102         iOperandFlag = OPERAND_FLAG_NONE;
103         ridConstraintType = 0;
104 #endif
105         refObject = ref;
106         if( refObject )
107                 refObject->addRef();
108 };
109
110 CVariable::CVariable( wstring* const p )// : i64Value(0)
111 {
112 #ifdef WIN32
113         //Assuming bitfield layout is contiguous in memory
114         iPrameters = OPERAND_STRING;
115 #else
116         iOperandType = OPERAND_STRING;
117         iOperandFlag = OPERAND_FLAG_NONE;
118         ridConstraintType = 0;
119 #endif
120         pString = new wstring( p->data(), p->length() );
121 };
122
123 CVariable::CVariable( const bool b )// : i64Value(0)
124 {
125 #ifdef WIN32
126         //Assuming bitfield layout is contiguous in memory
127         iPrameters = OPERAND_BOOLEAN;
128 #else
129         iOperandType = OPERAND_BOOLEAN;
130         iOperandFlag = OPERAND_FLAG_NONE;
131         ridConstraintType = 0;
132 #endif
133         iValue = int32_t(b);
134 };
135
136 CVariable::CVariable( const wchar_t * const p )// : i64Value(0)
137 {
138 #ifdef WIN32
139         //Assuming bitfield layout is contiguous in memory
140         iPrameters = OPERAND_STRING;
141 #else
142         iOperandType = OPERAND_STRING;
143         iOperandFlag = OPERAND_FLAG_NONE;
144         ridConstraintType = 0;
145 #endif
146         pString = new wstring( p );
147 };
148
149 CVariable::CVariable( const int32_t i, const OPERAND_FLAG flag )// : i64Value(0)
150 {
151 #ifdef WIN32
152         //Assuming bitfield layout is contiguous in memory
153         iPrameters = OPERAND_INT;
154 #else
155         iOperandType = OPERAND_INT;
156         ridConstraintType = 0;
157 #endif
158         iOperandFlag = flag;
159         iValue = i;
160 };
161
162 CVariable::CVariable( const float f, const OPERAND_FLAG flag )// : i64Value(0)
163 {
164 #ifdef WIN32
165         //Assuming bitfield layout is contiguous in memory
166         iPrameters = OPERAND_FLOAT;
167 #else
168         iOperandType = OPERAND_FLOAT;
169         ridConstraintType = 0;
170 #endif
171         iOperandFlag = flag;
172         fValue = f;
173 };
174
175 CVariable::CVariable( const double d, const OPERAND_FLAG flag )
176 {
177 #ifdef WIN32
178         //Assuming bitfield layout is contiguous in memory
179         iPrameters = OPERAND_DOUBLE;
180 #else
181         iOperandType = OPERAND_DOUBLE ;
182         ridConstraintType = 0;
183 #endif
184         iOperandFlag = flag;
185         dValue = d;
186 };
187
188 CVariable::CVariable( const VMOBJECTREF ref, const OPERAND_FLAG flag )// : i64Value(0)
189 {
190 #ifdef WIN32
191         //Assuming bitfield layout is contiguous in memory
192         iPrameters = OPERAND_OBJECTREF;
193 #else
194         iOperandType = OPERAND_OBJECTREF;
195         ridConstraintType = 0;
196 #endif
197         iOperandFlag = flag;
198         refObject = ref;
199         if( refObject )
200                 refObject->addRef();
201 };
202
203 CVariable::CVariable( wstring* const p, const OPERAND_FLAG flag )// : i64Value(0)
204 {
205 #ifdef WIN32
206         //Assuming bitfield layout is contiguous in memory
207         iPrameters = OPERAND_STRING;
208 #else
209         iOperandType = OPERAND_STRING;
210         ridConstraintType = 0;
211 #endif
212         iOperandFlag = flag;
213         pString = new wstring( p->data(), p->length() );
214 };
215
216 CVariable::CVariable( const bool b, const OPERAND_FLAG flag )// : i64Value(0)
217 {
218 #ifdef WIN32
219         //Assuming bitfield layout is contiguous in memory
220         iPrameters = OPERAND_BOOLEAN;
221 #else
222         iOperandType = OPERAND_BOOLEAN;
223         ridConstraintType = 0;
224 #endif
225         iOperandFlag = flag;
226         bValue = b;
227 };
228
229 CVariable::CVariable( const wchar_t * const p, const OPERAND_FLAG flag )// : i64Value(0)
230 {
231 #ifdef WIN32
232         //Assuming bitfield layout is contiguous in memory
233         iPrameters = OPERAND_STRING;
234 #else
235         iOperandType = OPERAND_STRING;
236         ridConstraintType = 0;
237 #endif
238         iOperandFlag = flag;
239         pString = new wstring( p );
240 };
241
242 CVariable::CVariable( const OPERAND_FLAG flag )// : i64Value(0)
243 {
244 #ifdef WIN32
245         //Assuming bitfield layout is contiguous in memory
246         iPrameters = OPERAND_UNDEFINED;
247 #else
248         iOperandType = OPERAND_UNDEFINED;
249         ridConstraintType = 0;
250 #endif
251         iOperandFlag = flag ;
252 };
253        
254 /***************************************************************************
255  *      CVariable::setFlag
256  ***************************************************************************/
257 void CVariable::setFlag( const OPERAND_FLAG flag )
258 {
259         iOperandFlag = flag;
260 }
261
262 /***************************************************************************
263  *      CVariable::Copy ctor
264  ***************************************************************************/
265 CVariable::CVariable( const CVariable& right )
266 {
267         //iOperandType = right.iOperandType;
268         //iOperandFlag = right.iOperandFlag;
269         //ridConstraintType = right.ridConstraintType;
270         iPrameters = right.iPrameters;
271
272         //If already keeping a object ref...
273         switch( OperandType( iOperandType ) )
274         {
275         case OPERAND_OBJECTREF:
276                 //i64Value = 0;
277                 refObject = right.refObject;
278                 if( refObject )
279                 {
280                         assert( refObject->getRefCount() );
281                         refObject->addRef();
282                 }break;
283         case OPERAND_STRING:
284                 //i64Value = 0;
285                 pString = new wstring( *right.pString );
286                 break;
287         case OPERAND_INT64:
288         case OPERAND_UNSIGNEDINT64:
289         case OPERAND_DOUBLE:
290         default:
291                 i64Value = right.i64Value;
292                 break;
293         }
294 };
295
296 /***************************************************************************
297  *      CVariable::Operator=
298  ***************************************************************************/
299 CVariable& CVariable::operator=( const CVariable& right )
300 {
301         //Check if the variable is READONLY
302         if( iOperandFlag & OPERAND_FLAG_READONLY )
303                 return *this;
304
305         if( OperandType( right.iOperandType ) == OPERAND_SYSTEM_DELETION )
306         {
307                 if( iOperandFlag & OPERAND_FLAG_DONTDELETE )
308                         return *this;
309                 else
310                 {
311                         //If already keeping a object ref...
312                         switch( OperandType( iOperandType ) )
313                         {
314                         case OPERAND_OBJECTREF:
315                                 if( refObject )
316                                         refObject->release();
317                                 break;
318                         case OPERAND_STRING:
319                                 delete pString; //Delete old string
320                                 break;
321                         default:
322                                 break;
323                         }
324                         iOperandType = OPERAND_UNDEFINED;
325                         iOperandFlag = OPERAND_FLAG_DONTENUM;
326                         return *this;
327                 }
328         }
329
330         //If this already keeps an object ref...
331         switch( OperandType( iOperandType ) )
332         {
333         case OPERAND_OBJECTREF:
334                 if( refObject )
335                         refObject->release();
336                 break;
337         case OPERAND_STRING:
338                 delete pString; //Delete old string
339                 break;
340         default:
341                 break;
342         }
343
344         //iOperandType = (OPERAND_TYPE)right.iOperandType;
345         //iOperandFlag = right.iOperandFlag;
346         iPrameters = right.iPrameters;
347
348         switch( OperandType( right.iOperandType ) )
349         {
350         case OPERAND_OBJECTREF:
351                 refObject = right.refObject;
352                 //ridConstraintType = right.ridConstraintType;
353                 if( refObject )
354                 {
355                         refObject->addRef();
356                 }
357                 break;
358         case OPERAND_STRING:
359                 pString = new wstring( *right.pString );
360                 break;
361         case OPERAND_DOUBLE:
362         case OPERAND_INT64:
363         case OPERAND_UNSIGNEDINT64:
364                 i64Value = right.i64Value;
365                 break;
366         default:
367                 iValue = right.iValue;
368                 break;
369         }
370
371         return *this;
372 }
373
374 /***************************************************************************
375  *      CVariable::moveTo
376  *              Copy a pointer/ref without tweaking ref count,
377  ***************************************************************************/
378 void CVariable::moveTo( CVariable& dest )
379 {
380         //Check if the variable is READONLY
381         if( dest.iOperandFlag & OPERAND_FLAG_READONLY )
382                 return;
383
384         uint32_t iOperand = OperandType( iOperandType );
385         switch( iOperand )
386         {
387         case OPERAND_SYSTEM_DELETION:
388                 if( dest.iOperandFlag & OPERAND_FLAG_DONTDELETE )
389                         return;
390                 else
391                 {
392                         dest.iOperandType = OPERAND_UNDEFINED;
393                         dest.iOperandFlag = OPERAND_FLAG_DONTENUM;
394                 }
395                 break;
396         case OPERAND_STRING:
397                 dest.iOperandType = (OPERAND_TYPE)iOperand;
398                 dest.pString = pString;
399                 pString = NULL; //Clear original pointer not to be deleted
400                 break;
401         case OPERAND_OBJECTREF:
402                 dest.iOperandType = (OPERAND_TYPE)iOperand;
403                
404                 //Should not override restrict type
405                 //dest.ridConstraintType = ridConstraintType;
406                 
407                 //Does not want to invoke Copy ctor     
408                 dest.iValue = iValue;
409                 assert( dest.refObject->getRefCount() );
410                 refObject = NULL;       //Clear original pointer not to be deleted
411                 break;
412         default:
413                 dest.iOperandType = (OPERAND_TYPE)iOperand;
414                 dest.i64Value = i64Value;
415                 break;
416         }
417 }
418
419 /***************************************************************************
420  *      CVariable::moveToWithFlags
421  *              Copy a pointer/ref without tweaking ref count,
422  ***************************************************************************/
423 void CVariable::moveToWithFlags( CVariable& dest )
424 {
425         //Check if the variable is READONLY
426         if( dest.iOperandFlag & OPERAND_FLAG_READONLY )
427                 return;
428
429         uint32_t iOperand = OperandType( iOperandType );
430         switch( iOperand )
431         {
432         case OPERAND_SYSTEM_DELETION:
433                 if( dest.iOperandFlag & OPERAND_FLAG_DONTDELETE )
434                         return;
435                 else
436                 {
437                         dest.iOperandType = OPERAND_UNDEFINED;
438                         dest.iOperandFlag = OPERAND_FLAG_DONTENUM;
439                 }
440                 break;
441         case OPERAND_STRING:
442                 dest.iPrameters = iPrameters;
443                 dest.pString = pString;
444                 pString = NULL; //Clear original pointer not to be deleted
445                 break;
446         case OPERAND_OBJECTREF:
447                 dest.iOperandType = (OPERAND_TYPE)iOperand;
448                 dest.iOperandFlag = iOperandFlag;
449                
450                 //Should not override restrict type
451                 //dest.ridConstraintType = ridConstraintType;
452                 
453                 //Does not want to invoke Copy ctor     
454                 dest.iValue = iValue;
455                 assert( dest.refObject->getRefCount() );
456                 refObject = NULL;       //Clear original pointer not to be deleted
457                 break;
458         case OPERAND_DOUBLE:
459         case OPERAND_INT64:
460         case OPERAND_UNSIGNEDINT64:
461                 dest.iPrameters = iPrameters;
462                 dest.i64Value = i64Value;
463                 break;
464         default:
465                 dest.iPrameters = iPrameters;
466                 dest.iValue = iValue;
467                 break;
468         }
469 }
470
471 /***************************************************************************
472  *      CVariable::SetObject reference and increment ref count
473  ***************************************************************************/
474 void CVariable::setObjectRef( const VMOBJECTREF obj )
475 {
476         //Check if the variable is READONLY
477         if( iOperandFlag & OPERAND_FLAG_READONLY )
478                 return;
479
480         //If already keeping a object ref...
481         switch( OperandType( iOperandType ) )
482         {
483         case OPERAND_OBJECTREF:
484                 if( refObject )
485                         refObject->release();
486                 break;
487         case OPERAND_STRING:
488                 delete pString; //Delete old string
489                 break;
490         default:
491                 break;
492         }
493
494 #ifdef WIN32
495         //Assuming bitfield layout is contiguous in memory
496         iPrameters = OPERAND_OBJECTREF;
497 #else
498         iOperandType = OPERAND_OBJECTREF;
499         iOperandFlag = OPERAND_FLAG_NONE;
500         ridConstraintType = 0;
501 #endif
502         refObject = obj;
503         refObject->addRef();
504 }
505
506 /***************************************************************************
507  *      CVariable::setObjectRefWithFlags reference and increment ref count
508  ***************************************************************************/
509 void CVariable::setObjectRefWithFlags( const VMOBJECTREF obj, const OPERAND_FLAG flag, const RID ridConstraint )
510 {
511         //Check if the variable is READONLY
512         if( iOperandFlag & OPERAND_FLAG_READONLY )
513                 return;
514
515         //If already keeping a object ref...
516         switch( OperandType( iOperandType ) )
517         {
518         case OPERAND_OBJECTREF:
519                 if( refObject )
520                         refObject->release();
521                 break;
522         case OPERAND_STRING:
523                 delete pString; //Delete old string
524                 break;
525         default:
526                 break;
527         }
528
529         iOperandType = OPERAND_OBJECTREF;
530         iOperandFlag = flag;
531         ridConstraintType = ridConstraint;
532         refObject = obj;
533         refObject->addRef();
534 }
535
536 /***************************************************************************
537  *      CVariable::setString set string value to the variable
538  ***************************************************************************/
539 void CVariable::setString( const wstring& str )
540 {
541         //Check if the variable is READONLY
542         if( iOperandFlag & OPERAND_FLAG_READONLY )
543                 return;
544
545         switch( OperandType( iOperandType ) )
546         {
547         case OPERAND_OBJECTREF:
548                 //If already keeping a object ref...
549                 if( refObject )
550                         refObject->release();
551                 break;
552         case OPERAND_STRING:
553                 delete pString;
554                 break;
555         default:
556                 break;
557         }
558 #ifdef WIN32
559         //Assuming bitfield layout is contiguous in memory
560         iPrameters = OPERAND_STRING;
561 #else
562         iOperandType = OPERAND_STRING;
563         iOperandFlag = OPERAND_FLAG_NONE;
564 #endif
565         pString = new wstring( str );
566
567 }
568
569 /***************************************************************************
570  *      CVariable::setInt set integer value to the variable
571  ***************************************************************************/
572 void CVariable::setInt( const int32_t i )
573 {
574         //Check if the variable is READONLY
575         if( iOperandFlag & OPERAND_FLAG_READONLY )
576                 return;
577
578         switch( OperandType( iOperandType ) )
579         {
580         case OPERAND_OBJECTREF:
581                 //If already keeping a object ref...
582                 if( refObject )
583                         refObject->release();
584                 break;
585         case OPERAND_STRING:
586                 delete pString;
587                 break;
588         default:
589                 break;
590         }
591 #ifdef WIN32
592         //Assuming bitfield layout is contiguous in memory
593         iPrameters = OPERAND_INT;
594 #else
595         iOperandType = OPERAND_INT;
596         iOperandFlag = OPERAND_FLAG_NONE;
597 #endif
598         iValue = i