diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2011-12-20 09:43:36 -0700 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-20 09:43:36 -0700 |
| commit | df412b348f10ee46830e6e117b4bb0dd3b6b057b (patch) | |
| tree | 6006d25b98c28f27d98829830b75b0b67bbe484f /src | |
| parent | c33bfc4ede827d3335e353fd35815c1613257942 (diff) | |
| download | stage-df412b348f10ee46830e6e117b4bb0dd3b6b057b.tar.gz stage-df412b348f10ee46830e6e117b4bb0dd3b6b057b.tar.bz2 stage-df412b348f10ee46830e6e117b4bb0dd3b6b057b.tar.xz stage-df412b348f10ee46830e6e117b4bb0dd3b6b057b.zip | |
Most of the variable type conversion routine is done.
Diffstat (limited to 'src')
| -rw-r--r-- | src/variable.cpp | 215 | ||||
| -rw-r--r-- | src/variable.h | 10 |
2 files changed, 216 insertions, 9 deletions
diff --git a/src/variable.cpp b/src/variable.cpp index a698b1c..6487474 100644 --- a/src/variable.cpp +++ b/src/variable.cpp | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | #include "variable.h" | 1 | #include "variable.h" |
| 2 | 2 | ||
| 3 | #include <stdlib.h> | ||
| 4 | |||
| 3 | typedef Bu::ExceptionBase VariableException; | 5 | typedef Bu::ExceptionBase VariableException; |
| 4 | 6 | ||
| 5 | Variable::Variable() : | 7 | Variable::Variable() : |
| @@ -52,6 +54,114 @@ Variable::~Variable() | |||
| 52 | deinitType(); | 54 | deinitType(); |
| 53 | } | 55 | } |
| 54 | 56 | ||
| 57 | Variable Variable::to( Type e ) const | ||
| 58 | { | ||
| 59 | if( e == eType ) | ||
| 60 | return *this; | ||
| 61 | |||
| 62 | switch( eType ) | ||
| 63 | { | ||
| 64 | case tNull: | ||
| 65 | switch( e ) | ||
| 66 | { | ||
| 67 | case tNull: break; | ||
| 68 | case tBool: break; | ||
| 69 | case tInt: break; | ||
| 70 | case tFloat: break; | ||
| 71 | case tString: return Variable("(null)"); | ||
| 72 | case tList: break; | ||
| 73 | case tDictionary: break; | ||
| 74 | } | ||
| 75 | break; | ||
| 76 | |||
| 77 | case tBool: | ||
| 78 | switch( e ) | ||
| 79 | { | ||
| 80 | case tNull: break; | ||
| 81 | case tBool: break; | ||
| 82 | case tInt: break; | ||
| 83 | case tFloat: break; | ||
| 84 | case tString: | ||
| 85 | return bValue?Variable("true"):Variable("false"); | ||
| 86 | case tList: break; | ||
| 87 | case tDictionary: break; | ||
| 88 | } | ||
| 89 | break; | ||
| 90 | |||
| 91 | case tInt: | ||
| 92 | switch( e ) | ||
| 93 | { | ||
| 94 | case tNull: break; | ||
| 95 | case tBool: break; | ||
| 96 | case tInt: break; | ||
| 97 | case tFloat: return Variable( (double)iValue ); | ||
| 98 | case tString: | ||
| 99 | return Variable( Bu::String("%1").arg( iValue ) ); | ||
| 100 | case tList: break; | ||
| 101 | case tDictionary: break; | ||
| 102 | } | ||
| 103 | break; | ||
| 104 | |||
| 105 | case tFloat: | ||
| 106 | switch( e ) | ||
| 107 | { | ||
| 108 | case tNull: break; | ||
| 109 | case tBool: break; | ||
| 110 | case tInt: return Variable( (int64_t)fValue ); | ||
| 111 | case tFloat: break; | ||
| 112 | case tString: | ||
| 113 | return Variable( Bu::String("%1").arg( fValue ) ); | ||
| 114 | case tList: break; | ||
| 115 | case tDictionary: break; | ||
| 116 | } | ||
| 117 | break; | ||
| 118 | |||
| 119 | case tString: | ||
| 120 | switch( e ) | ||
| 121 | { | ||
| 122 | case tNull: break; | ||
| 123 | case tBool: break; | ||
| 124 | case tInt: | ||
| 125 | return Variable( strtoll(sValue->getStr(), NULL, 10 ) ); | ||
| 126 | case tFloat: | ||
| 127 | return Variable( strtod(sValue->getStr(), NULL ) ); | ||
| 128 | case tString: break; | ||
| 129 | case tList: break; | ||
| 130 | case tDictionary: break; | ||
| 131 | } | ||
| 132 | break; | ||
| 133 | |||
| 134 | case tList: | ||
| 135 | switch( e ) | ||
| 136 | { | ||
| 137 | case tNull: break; | ||
| 138 | case tBool: break; | ||
| 139 | case tInt: break; | ||
| 140 | case tFloat: break; | ||
| 141 | case tString: break; | ||
| 142 | case tList: break; | ||
| 143 | case tDictionary: break; | ||
| 144 | } | ||
| 145 | break; | ||
| 146 | |||
| 147 | case tDictionary: | ||
| 148 | switch( e ) | ||
| 149 | { | ||
| 150 | case tNull: break; | ||
| 151 | case tBool: break; | ||
| 152 | case tInt: break; | ||
| 153 | case tFloat: break; | ||
| 154 | case tString: break; | ||
| 155 | case tList: break; | ||
| 156 | case tDictionary: break; | ||
| 157 | } | ||
| 158 | break; | ||
| 159 | } | ||
| 160 | |||
| 161 | throw VariableException("Could not convert from %d to %d.\n", | ||
| 162 | eType, e ); | ||
| 163 | } | ||
| 164 | |||
| 55 | Variable &Variable::operator=( const Variable &rhs ) | 165 | Variable &Variable::operator=( const Variable &rhs ) |
| 56 | { | 166 | { |
| 57 | deinitType(); | 167 | deinitType(); |
| @@ -88,15 +198,110 @@ Variable &Variable::operator=( const Variable &rhs ) | |||
| 88 | break; | 198 | break; |
| 89 | } | 199 | } |
| 90 | } | 200 | } |
| 201 | |||
| 202 | Variable &Variable::operator+=( const Variable &rhs ) | ||
| 203 | { | ||
| 204 | switch( eType ) | ||
| 205 | { | ||
| 206 | case tNull: | ||
| 207 | throw VariableException("You cannot add nulls."); | ||
| 208 | |||
| 209 | case tBool: | ||
| 210 | throw VariableException("You cannot add bools."); | ||
| 211 | |||
| 212 | case tInt: | ||
| 213 | switch( rhs.eType ) | ||
| 214 | { | ||
| 215 | case tInt: | ||
| 216 | iValue += rhs.iValue; | ||
| 217 | break; | ||
| 218 | |||
| 219 | case tFloat: | ||
| 220 | { | ||
| 221 | double dTmp = iValue; | ||
| 222 | eType = tFloat; | ||
| 223 | fValue = dTmp + rhs.fValue; | ||
| 224 | } | ||
| 225 | break; | ||
| 226 | |||
| 227 | default: | ||
| 228 | throw VariableException("Int += invalid..."); | ||
| 229 | } | ||
| 230 | break; | ||
| 231 | |||
| 232 | case tFloat: | ||
| 233 | switch( rhs.eType ) | ||
| 234 | { | ||
| 235 | case tInt: | ||
| 236 | fValue += rhs.iValue; | ||
| 237 | break; | ||
| 238 | |||
| 239 | case tFloat: | ||
| 240 | fValue += rhs.fValue; | ||
| 241 | break; | ||
| 242 | |||
| 243 | default: | ||
| 244 | throw VariableException("Int += invalid..."); | ||
| 245 | } | ||
| 246 | break; | ||
| 247 | |||
| 248 | case tString: | ||
| 249 | (*sValue).append( *(rhs.to( tString ).sValue) ); | ||
| 250 | break; | ||
| 251 | } | ||
| 252 | |||
| 253 | return *this; | ||
| 254 | } | ||
| 255 | |||
| 91 | /* | 256 | /* |
| 92 | Variable &Variable::operator+=( const Variable &rhs ); | ||
| 93 | Variable &Variable::operator-=( const Variable &rhs ); | 257 | Variable &Variable::operator-=( const Variable &rhs ); |
| 94 | Variable &Variable::operator*=( const Variable &rhs ); | 258 | Variable &Variable::operator*=( const Variable &rhs ); |
| 95 | Variable &Variable::operator/=( const Variable &rhs ); | 259 | Variable &Variable::operator/=( const Variable &rhs ); |
| 96 | Variable &Variable::operator+( const Variable &rhs ) const; | 260 | */ |
| 97 | Variable &Variable::operator-( const Variable &rhs ) const; | 261 | Variable Variable::operator+( const Variable &rhs ) const |
| 98 | Variable &Variable::operator*( const Variable &rhs ) const; | 262 | { |
| 99 | Variable &Variable::operator/( const Variable &rhs ) const; | 263 | if( eType != rhs.eType ) |
| 264 | { | ||
| 265 | throw VariableException("Adding between dissimilar types is not yet supported."); | ||
| 266 | } | ||
| 267 | else | ||
| 268 | { | ||
| 269 | switch( eType ) | ||
| 270 | { | ||
| 271 | case tNull: | ||
| 272 | throw VariableException("You cannot add nulls."); | ||
| 273 | |||
| 274 | case tBool: | ||
| 275 | throw VariableException("You cannot add booleans."); | ||
| 276 | |||
| 277 | case tInt: | ||
| 278 | return Variable( iValue + rhs.iValue ); | ||
| 279 | |||
| 280 | case tFloat: | ||
| 281 | return Variable( fValue + rhs.fValue ); | ||
| 282 | |||
| 283 | case tString: | ||
| 284 | return Variable( *sValue + *rhs.sValue ); | ||
| 285 | |||
| 286 | case tList: | ||
| 287 | { | ||
| 288 | Variable vRet( tList ); | ||
| 289 | vRet.lValue->append( *lValue ); | ||
| 290 | vRet.lValue->append( *rhs.lValue ); | ||
| 291 | return vRet; | ||
| 292 | } | ||
| 293 | |||
| 294 | case tDictionary: | ||
| 295 | throw VariableException("You cannot add dictionaries."); | ||
| 296 | break; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | /* | ||
| 302 | Variable Variable::operator-( const Variable &rhs ) const; | ||
| 303 | Variable Variable::operator*( const Variable &rhs ) const; | ||
| 304 | Variable Variable::operator/( const Variable &rhs ) const; | ||
| 100 | */ | 305 | */ |
| 101 | 306 | ||
| 102 | bool Variable::operator==( const Variable &rhs ) const | 307 | bool Variable::operator==( const Variable &rhs ) const |
diff --git a/src/variable.h b/src/variable.h index 0236951..67f35c8 100644 --- a/src/variable.h +++ b/src/variable.h | |||
| @@ -33,15 +33,17 @@ public: | |||
| 33 | 33 | ||
| 34 | Type getType() const { return eType; } | 34 | Type getType() const { return eType; } |
| 35 | 35 | ||
| 36 | Variable to( Type e ) const; | ||
| 37 | |||
| 36 | Variable &operator=( const Variable &rhs ); | 38 | Variable &operator=( const Variable &rhs ); |
| 37 | Variable &operator+=( const Variable &rhs ); | 39 | Variable &operator+=( const Variable &rhs ); |
| 38 | Variable &operator-=( const Variable &rhs ); | 40 | Variable &operator-=( const Variable &rhs ); |
| 39 | Variable &operator*=( const Variable &rhs ); | 41 | Variable &operator*=( const Variable &rhs ); |
| 40 | Variable &operator/=( const Variable &rhs ); | 42 | Variable &operator/=( const Variable &rhs ); |
| 41 | Variable &operator+( const Variable &rhs ) const; | 43 | Variable operator+( const Variable &rhs ) const; |
| 42 | Variable &operator-( const Variable &rhs ) const; | 44 | Variable operator-( const Variable &rhs ) const; |
| 43 | Variable &operator*( const Variable &rhs ) const; | 45 | Variable operator*( const Variable &rhs ) const; |
| 44 | Variable &operator/( const Variable &rhs ) const; | 46 | Variable operator/( const Variable &rhs ) const; |
| 45 | bool operator==( const Variable &rhs ) const; | 47 | bool operator==( const Variable &rhs ) const; |
| 46 | bool operator!=( const Variable &rhs ) const; | 48 | bool operator!=( const Variable &rhs ) const; |
| 47 | bool operator>( const Variable &rhs ) const; | 49 | bool operator>( const Variable &rhs ) const; |
