00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef CBZ_CONSTANT_H
00039 #define CBZ_CONSTANT_H
00040
00041 #include "basic_type.h"
00042 #include "operators.h"
00043
00044 class constant {
00045
00046 private:
00047
00048 union {
00049 signed char SChar;
00050 unsigned char UChar;
00051 signed short int SShort;
00052 unsigned short int UShort;
00053 signed int SInt;
00054 unsigned int UInt;
00055 signed long int SLong;
00056 unsigned long int ULong;
00057 float Float;
00058 double Double;
00059
00060 char * Str;
00061 void * Ptr;
00062 } _v;
00063
00064 basic_type _bt;
00065 bool _is_ptr;
00066 bool _is_str;
00067 bool _no_val;
00068
00069 public:
00070
00071 inline const basic_type & basic() const { return _bt; }
00072
00073 constant(signed char SChar)
00074 : _bt(basic_type::SChar), _is_ptr(false), _is_str(false), _no_val(false)
00075 { _v.SChar = SChar; }
00076
00077 inline signed char SChar() const { return _v.SChar; }
00078 inline void SChar(signed char val) { *this = constant(val); }
00079
00080 constant(unsigned char UChar)
00081 : _bt(basic_type::UChar), _is_ptr(false), _is_str(false), _no_val(false)
00082 { _v.UChar = UChar; }
00083
00084 inline unsigned char UChar() const { return _v.UChar; }
00085 inline void UChar(unsigned char val) { *this = constant(val); }
00086
00087 constant(signed short int SShort)
00088 : _bt(basic_type::SShort), _is_ptr(false), _is_str(false), _no_val(false)
00089 { _v.SShort = SShort; }
00090
00091 inline signed short int SShort() const { return _v.SShort; }
00092 inline void SShort(signed short int val) { *this = constant(val); }
00093
00094 constant(unsigned short int UShort)
00095 : _bt(basic_type::UShort), _is_ptr(false), _is_str(false), _no_val(false)
00096 { _v.UShort = UShort; }
00097
00098 inline unsigned short int UShort() const { return _v.UShort; }
00099 inline void UShort(unsigned short int val) { *this = constant(val); }
00100
00101 constant(signed int SInt)
00102 : _bt(basic_type::SInt), _is_ptr(false), _is_str(false), _no_val(false)
00103 { _v.SInt = SInt; }
00104
00105 inline signed int SInt() const { return _v.SInt; }
00106 inline void SInt(signed int val) { *this = constant(val); }
00107
00108 constant(unsigned int UInt)
00109 : _bt(basic_type::UInt), _is_ptr(false), _is_str(false), _no_val(false)
00110 { _v.UInt = UInt; }
00111
00112 inline unsigned int UInt() const { return _v.UInt; }
00113 inline void UInt(unsigned int val) { *this = constant(val); }
00114
00115 constant(signed long int SLong)
00116 : _bt(basic_type::SLong), _is_ptr(false), _is_str(false), _no_val(false)
00117 { _v.SLong = SLong; }
00118
00119 inline signed long int SLong() const { return _v.SLong; }
00120 inline void SLong(signed long int val) { *this = constant(val); }
00121
00122 constant(unsigned long int ULong)
00123 : _bt(basic_type::ULong), _is_ptr(false), _is_str(false), _no_val(false)
00124 { _v.ULong = ULong; }
00125
00126 inline unsigned long int ULong() const { return _v.ULong; }
00127 inline void ULong(unsigned long int val) { *this = constant(val); }
00128
00129 constant(float Float)
00130 : _bt(basic_type::Float), _is_ptr(false), _is_str(false), _no_val(false)
00131 { _v.Float = Float; }
00132
00133 inline float Float() const { return _v.Float; }
00134 inline void Float(float val) { *this = constant(val); }
00135
00136 constant(double Double)
00137 : _bt(basic_type::Double), _is_ptr(false), _is_str(false), _no_val(false)
00138 { _v.Double = Double; }
00139
00140 inline double Double() const { return _v.Double; }
00141 inline void Double(double val) { *this = constant(val); }
00142
00143 constant(const char * Str)
00144 : _bt(), _is_ptr(false), _is_str(true), _no_val(false)
00145 { _v.Str = strdup(Str); }
00146
00147 inline char * Str() const { return _v.Str; }
00148 inline bool is_str() const { return _is_str; }
00149
00150 constant(void * Ptr)
00151 : _bt(), _is_ptr(true), _is_str(false), _no_val(false)
00152 { _v.Ptr = Ptr; }
00153
00154 inline void * Ptr() const { return _v.Ptr; }
00155 inline bool is_ptr() const { return _is_ptr; }
00156
00157 constant()
00158 : _bt(), _is_ptr(false), _is_str(false), _no_val(true)
00159 {}
00160
00161 inline bool no_val() const { return _no_val; }
00162 inline void set_no_val() { _no_val = true; }
00163
00164
00165
00166 constant(const constant & other);
00167
00168
00169
00170 ~constant();
00171
00172
00173
00174 constant & operator=(const constant & rhs);
00175
00176
00177
00178 unsigned long Integer() const;
00179 bool Boolean() const;
00180 bool is_zero() const;
00181 void make_zero();
00182
00183
00184
00185 bool is_equal_to(const constant & other) const;
00186
00187
00188
00189 bool operator<(const constant & other) const;
00190
00191
00192
00193 static constant eval(const Operator * op,
00194 const constant & operand1,
00195 const constant & operand2);
00196
00197 static constant eval(const Operator * op,
00198 const constant & operand);
00199
00200 static constant cast(const basic_type & new_bt, const constant & con);
00201
00202
00203
00204
00205
00206 string to_string(bool wantQuoteChar = true) const;
00207
00208 private:
00209
00210 static void print_char(int value, ostringstream & ost);
00211
00212 };
00213
00214
00215
00216 #endif // CBZ_CONSTANT_H