Michael Uleysky
9 years ago
12 changed files with 218 additions and 21 deletions
@ -0,0 +1,148 @@ |
|||||||
|
#include <math.h> |
||||||
|
#include "object.h" |
||||||
|
|
||||||
|
ObjectBase* Arifm_Add(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=2) return 0; |
||||||
|
std::type_index t1(typeid(*input->At(0))),t2(typeid(*input->At(1))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
if( (t1!=tr && t1!=ti) || (t2!=tr && t2!=ti) ) return 0; |
||||||
|
|
||||||
|
// Integer arifmetic
|
||||||
|
if(t1==ti && t2==ti) return new ObjectInt(dynamic_cast<const ObjectInt*>(input->At(0))->Value() + dynamic_cast<const ObjectInt*>(input->At(1))->Value()); |
||||||
|
|
||||||
|
// Real arifmetic
|
||||||
|
double r1,r2; |
||||||
|
|
||||||
|
if(t1==tr) r1=dynamic_cast<const ObjectReal*>(input->At(0))->Value(); |
||||||
|
else r1=dynamic_cast<const ObjectInt*>(input->At(0))->Value(); |
||||||
|
|
||||||
|
if(t2==tr) r2=dynamic_cast<const ObjectReal*>(input->At(1))->Value(); |
||||||
|
else r2=dynamic_cast<const ObjectInt*>(input->At(1))->Value(); |
||||||
|
|
||||||
|
return new ObjectReal(r1+r2); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
ObjectBase* Arifm_Sub(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=2) return 0; |
||||||
|
std::type_index t1(typeid(*input->At(0))),t2(typeid(*input->At(1))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
if( (t1!=tr && t1!=ti) || (t2!=tr && t2!=ti) ) return 0; |
||||||
|
|
||||||
|
// Integer arifmetic
|
||||||
|
if(t1==ti && t2==ti) return new ObjectInt(dynamic_cast<const ObjectInt*>(input->At(0))->Value() - dynamic_cast<const ObjectInt*>(input->At(1))->Value()); |
||||||
|
|
||||||
|
// Real arifmetic
|
||||||
|
double r1,r2; |
||||||
|
|
||||||
|
if(t1==tr) r1=dynamic_cast<const ObjectReal*>(input->At(0))->Value(); |
||||||
|
else r1=dynamic_cast<const ObjectInt*>(input->At(0))->Value(); |
||||||
|
|
||||||
|
if(t2==tr) r2=dynamic_cast<const ObjectReal*>(input->At(1))->Value(); |
||||||
|
else r2=dynamic_cast<const ObjectInt*>(input->At(1))->Value(); |
||||||
|
|
||||||
|
return new ObjectReal(r1-r2); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
ObjectBase* Arifm_Mul(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=2) return 0; |
||||||
|
std::type_index t1(typeid(*input->At(0))),t2(typeid(*input->At(1))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
if( (t1!=tr && t1!=ti) || (t2!=tr && t2!=ti) ) return 0; |
||||||
|
|
||||||
|
// Integer arifmetic
|
||||||
|
if(t1==ti && t2==ti) return new ObjectInt(dynamic_cast<const ObjectInt*>(input->At(0))->Value() * dynamic_cast<const ObjectInt*>(input->At(1))->Value()); |
||||||
|
|
||||||
|
// Real arifmetic
|
||||||
|
double r1,r2; |
||||||
|
|
||||||
|
if(t1==tr) r1=dynamic_cast<const ObjectReal*>(input->At(0))->Value(); |
||||||
|
else r1=dynamic_cast<const ObjectInt*>(input->At(0))->Value(); |
||||||
|
|
||||||
|
if(t2==tr) r2=dynamic_cast<const ObjectReal*>(input->At(1))->Value(); |
||||||
|
else r2=dynamic_cast<const ObjectInt*>(input->At(1))->Value(); |
||||||
|
|
||||||
|
return new ObjectReal(r1*r2); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
ObjectBase* Arifm_Div(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=2) return 0; |
||||||
|
std::type_index t1(typeid(*input->At(0))),t2(typeid(*input->At(1))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
if( (t1!=tr && t1!=ti) || (t2!=tr && t2!=ti) ) return 0; |
||||||
|
|
||||||
|
// Integer arifmetic
|
||||||
|
if(t1==ti && t2==ti) return new ObjectInt(dynamic_cast<const ObjectInt*>(input->At(0))->Value() / dynamic_cast<const ObjectInt*>(input->At(1))->Value()); |
||||||
|
|
||||||
|
// Real arifmetic
|
||||||
|
double r1,r2; |
||||||
|
|
||||||
|
if(t1==tr) r1=dynamic_cast<const ObjectReal*>(input->At(0))->Value(); |
||||||
|
else r1=dynamic_cast<const ObjectInt*>(input->At(0))->Value(); |
||||||
|
|
||||||
|
if(t2==tr) r2=dynamic_cast<const ObjectReal*>(input->At(1))->Value(); |
||||||
|
else r2=dynamic_cast<const ObjectInt*>(input->At(1))->Value(); |
||||||
|
|
||||||
|
return new ObjectReal(r1/r2); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
ObjectBase* Arifm_Pow(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=2) return 0; |
||||||
|
std::type_index t1(typeid(*input->At(0))),t2(typeid(*input->At(1))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
if( (t1!=tr && t1!=ti) || (t2!=tr && t2!=ti) ) return 0; |
||||||
|
|
||||||
|
// Only real arifmetic
|
||||||
|
double r1,r2; |
||||||
|
|
||||||
|
if(t1==tr) r1=dynamic_cast<const ObjectReal*>(input->At(0))->Value(); |
||||||
|
else r1=dynamic_cast<const ObjectInt*>(input->At(0))->Value(); |
||||||
|
|
||||||
|
if(t2==tr) r2=dynamic_cast<const ObjectReal*>(input->At(1))->Value(); |
||||||
|
else r2=dynamic_cast<const ObjectInt*>(input->At(1))->Value(); |
||||||
|
|
||||||
|
return new ObjectReal(pow(r1,r2)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
ObjectBase* Arifm_Neg(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=1) return 0; |
||||||
|
std::type_index t(typeid(*input->At(0))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
// Integer arifmetic
|
||||||
|
if(t==ti) return new ObjectInt(-dynamic_cast<const ObjectInt*>(input->At(0))->Value()); |
||||||
|
// Real arifmetic
|
||||||
|
if(t==tr) return new ObjectReal(-dynamic_cast<const ObjectReal*>(input->At(0))->Value()); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
ObjectBase* Arifm_Pos(ObjectList* input) |
||||||
|
{ |
||||||
|
if(input->Size()!=1) return 0; |
||||||
|
std::type_index t(typeid(*input->At(0))); |
||||||
|
std::type_index tr(typeid(ObjectReal)),ti(typeid(ObjectInt)); |
||||||
|
|
||||||
|
// Integer arifmetic
|
||||||
|
if(t==ti) return new ObjectInt(dynamic_cast<const ObjectInt*>(input->At(0))->Value()); |
||||||
|
// Real arifmetic
|
||||||
|
if(t==tr) return new ObjectReal(dynamic_cast<const ObjectReal*>(input->At(0))->Value()); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
#include "object.h" |
||||||
|
|
||||||
|
ObjectBase* Arifm_Add(ObjectList* input); |
||||||
|
ObjectBase* Arifm_Sub(ObjectList* input); |
||||||
|
ObjectBase* Arifm_Mul(ObjectList* input); |
||||||
|
ObjectBase* Arifm_Div(ObjectList* input); |
||||||
|
ObjectBase* Arifm_Pow(ObjectList* input); |
||||||
|
ObjectBase* Arifm_Neg(ObjectList* input); |
||||||
|
ObjectBase* Arifm_Pos(ObjectList* input); |
@ -0,0 +1,6 @@ |
|||||||
|
#ifndef COMMON_H |
||||||
|
#define COMMON_H |
||||||
|
|
||||||
|
#define EXPORT __attribute__ ((visibility ("default"))) |
||||||
|
|
||||||
|
#endif |
@ -1,6 +1,6 @@ |
|||||||
#include "object.h" |
#include "object.h" |
||||||
|
|
||||||
template<> std::string ObjectSimple<bool>::type="bool"; |
template<> EXPORT std::string ObjectSimple<bool>::type="bool"; |
||||||
template<> std::string ObjectSimple<int64_t>::type="integer"; |
template<> EXPORT std::string ObjectSimple<int64_t>::type="integer"; |
||||||
template<> std::string ObjectSimple<double>::type="real"; |
template<> EXPORT std::string ObjectSimple<double>::type="real"; |
||||||
template<> std::string ObjectSimple<std::string>::type="string"; |
template<> EXPORT std::string ObjectSimple<std::string>::type="string"; |
||||||
|
Loading…
Reference in new issue