00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "IntValue.h"
00017 #include "ErrorValue.h"
00018 #include "FloatValue.h"
00019 #include "BoolValue.h"
00020 #include "StringValue.h"
00021 #include "VoidValue.h"
00022
00023
00025
00027
00028 CIntValue::CIntValue()
00029
00030
00031
00032
00033 {
00034
00035 #ifdef _DEBUG_
00036 m_textval = "Int illegal constructor";
00037 #endif
00038 m_pstrRep=NULL;
00039 }
00040
00041 CIntValue::CIntValue(int innie)
00042
00043
00044
00045
00046 {
00047 m_int = innie;
00048 m_pstrRep=NULL;
00049 }
00050 CIntValue::CIntValue(int innie,CCString name,AllocationTYPE alloctype)
00051 {
00052
00053 m_int = innie;
00054 SetName(name);
00055
00056 if (alloctype==CValue::STACKVALUE)
00057 {
00058 CValue::DisableRefCount();
00059 }
00060 m_pstrRep=NULL;
00061
00062 }
00063
00064 CIntValue::~CIntValue()
00065
00066
00067
00068
00069 {
00070 if (m_pstrRep)
00071 delete m_pstrRep;
00072
00073 }
00074
00075 CValue* CIntValue::Calc(VALUE_OPERATOR op, CValue *val)
00076
00077
00078
00079
00080
00081 {
00082
00083 switch (op) {
00084 case VALUE_NEG_OPERATOR:
00085 return new CIntValue (-m_int);
00086 break;
00087 case VALUE_NOT_OPERATOR:
00088 return new CErrorValue (op2str(op) + "only allowed on booleans");
00089 break;
00090 case VALUE_AND_OPERATOR:
00091 case VALUE_OR_OPERATOR:
00092 return new CErrorValue(val->GetText() + op2str(op) + "only allowed on booleans");
00093 break;
00094 default:
00095 return val->CalcFinal(VALUE_INT_TYPE, op, this);
00096 break;
00097 }
00098 }
00099
00100 CValue* CIntValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
00101
00102
00103
00104
00105
00106 {
00107 CValue *ret;
00108
00109 switch(dtype) {
00110 case VALUE_EMPTY_TYPE:
00111 case VALUE_INT_TYPE:
00112 {
00113 switch (op) {
00114 case VALUE_ADD_OPERATOR:
00115 ret = new CIntValue (((CIntValue *) val)->GetInt() + m_int);
00116 break;
00117 case VALUE_SUB_OPERATOR:
00118 ret = new CIntValue (((CIntValue *) val)->GetInt() - m_int);
00119 break;
00120 case VALUE_MUL_OPERATOR:
00121 ret = new CIntValue (((CIntValue *) val)->GetInt() * m_int);
00122 break;
00123 case VALUE_DIV_OPERATOR:
00124 if (m_int == 0)
00125 {
00126 if (val->GetNumber() == 0)
00127 {
00128 ret = new CErrorValue("Not a Number");
00129 } else
00130 {
00131 ret = new CErrorValue("Division by zero");
00132 }
00133 }
00134 else
00135 ret = new CIntValue (((CIntValue *) val)->GetInt() / m_int);
00136 break;
00137 case VALUE_EQL_OPERATOR:
00138 ret = new CBoolValue(((CIntValue *) val)->GetInt() == m_int);
00139 break;
00140 case VALUE_NEQ_OPERATOR:
00141 ret = new CBoolValue(((CIntValue *) val)->GetInt() != m_int);
00142 break;
00143 case VALUE_GRE_OPERATOR:
00144 ret = new CBoolValue(((CIntValue *) val)->GetInt() > m_int);
00145 break;
00146 case VALUE_LES_OPERATOR:
00147 ret = new CBoolValue(((CIntValue *) val)->GetInt() < m_int);
00148 break;
00149 case VALUE_GEQ_OPERATOR:
00150 ret = new CBoolValue(((CIntValue *) val)->GetInt() >= m_int);
00151 break;
00152 case VALUE_LEQ_OPERATOR:
00153 ret = new CBoolValue(((CIntValue *) val)->GetInt() <= m_int);
00154 break;
00155 case VALUE_NEG_OPERATOR:
00156 ret = new CIntValue (-m_int);
00157 break;
00158 default:
00159 ret = new CErrorValue("illegal operator. please send a bug report.");
00160 break;
00161 }
00162 break;
00163 }
00164 case VALUE_FLOAT_TYPE:
00165 {
00166 switch (op) {
00167 case VALUE_ADD_OPERATOR:
00168 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() + m_int);
00169 break;
00170 case VALUE_SUB_OPERATOR:
00171 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() - m_int);
00172 break;
00173 case VALUE_MUL_OPERATOR:
00174 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() * m_int);
00175 break;
00176 case VALUE_DIV_OPERATOR:
00177 if (m_int == 0)
00178 ret = new CErrorValue("Division by zero");
00179 else
00180 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() / m_int);
00181 break;
00182 case VALUE_EQL_OPERATOR:
00183 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() == m_int);
00184 break;
00185 case VALUE_NEQ_OPERATOR:
00186 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() != m_int);
00187 break;
00188 case VALUE_GRE_OPERATOR:
00189 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() > m_int);
00190 break;
00191 case VALUE_LES_OPERATOR:
00192 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() < m_int);
00193 break;
00194 case VALUE_GEQ_OPERATOR:
00195 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() >= m_int);
00196 break;
00197 case VALUE_LEQ_OPERATOR:
00198 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() <= m_int);
00199 break;
00200 default:
00201 ret = new CErrorValue("illegal operator. please send a bug report.");
00202 break;
00203 }
00204 break;
00205 }
00206 case VALUE_STRING_TYPE:
00207 {
00208 switch(op) {
00209 case VALUE_ADD_OPERATOR:
00210 ret = new CStringValue(val->GetText() + GetText(),"");
00211 break;
00212 case VALUE_EQL_OPERATOR:
00213 case VALUE_NEQ_OPERATOR:
00214 case VALUE_GRE_OPERATOR:
00215 case VALUE_LES_OPERATOR:
00216 case VALUE_GEQ_OPERATOR:
00217 case VALUE_LEQ_OPERATOR:
00218 ret = new CErrorValue("[Cannot compare string with integer]" + op2str(op) + GetText());
00219 break;
00220 default:
00221 ret = new CErrorValue("[operator not allowed on strings]" + op2str(op) + GetText());
00222 break;
00223 }
00224 break;
00225 }
00226 case VALUE_BOOL_TYPE:
00227 ret = new CErrorValue("[operator not valid on boolean and integer]" + op2str(op) + GetText());
00228 break;
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 case VALUE_ERROR_TYPE:
00249 ret = new CErrorValue(val->GetText() + op2str(op) + GetText());
00250 break;
00251 default:
00252 ret = new CErrorValue("illegal type. contact your dealer (if any)");
00253 break;
00254 }
00255 return ret;
00256 }
00257
00258
00259 int CIntValue::GetInt()
00260
00261
00262
00263
00264 {
00265 return m_int;
00266 }
00267
00268
00269
00270 float CIntValue::GetNumber()
00271 {
00272 return (float) m_int;
00273 }
00274
00275
00276 const CCString & CIntValue::GetText()
00277 {
00278 if (!m_pstrRep)
00279 m_pstrRep=new CCString();
00280 m_pstrRep->Format("%d",m_int);
00281
00282 return *m_pstrRep;
00283 }
00284
00285 CValue* CIntValue::GetReplica() {
00286 CIntValue* replica = new CIntValue(*this);
00287 CValue::AddDataToReplica(replica);
00288 replica->m_pstrRep = NULL;
00289
00290 return replica;
00291 };