diff --git a/BinOpValue.cpp b/BinOpValue.cpp index a057d27..f48f63f 100644 --- a/BinOpValue.cpp +++ b/BinOpValue.cpp @@ -6,15 +6,48 @@ */ #include "BinOpValue.h" +#include "Util.h" namespace ParseTree { -BinOpValue::BinOpValue(std::string op, Value *a, Value *b) { +using namespace std; + +BinOpValue::BinOpValue(string op, Value *a, Value *b) { this->op=op; this->a=a; this->b=b; } +#define TRY_NUM(OP) try{ \ + double x=str2num(a); \ + double y=str2num(b); \ + return num2str(x OP y); \ + }catch(string &tx){ \ + return "NaN"; \ + }//end try \ + + +string BinOpValue::getValue(){ + string a=this->a->getValue(); + string b=this->b->getValue(); + if(!op.compare("+")){ + try{ + double x=str2num(a); + double y=str2num(b); + return num2str(x+y); + }catch(string &tx){ + return a+b; + }//end try + }else if(!op.compare("-")){ + TRY_NUM(-); + }else if(!op.compare("*")){ + TRY_NUM(*); + }else if(!op.compare("/")){ + TRY_NUM(/); + }else return a+b; + return "MAJICS_VALUE"; +} + BinOpValue::~BinOpValue() { // TODO Auto-generated destructor stub } diff --git a/BinOpValue.h b/BinOpValue.h index 64d36e1..120e060 100644 --- a/BinOpValue.h +++ b/BinOpValue.h @@ -19,6 +19,7 @@ class BinOpValue: public ParseTree::Value { Value *b; public: BinOpValue(std::string op, Value *a, Value *b); + std::string getValue(); virtual ~BinOpValue(); }; diff --git a/Util.h b/Util.h new file mode 100644 index 0000000..7983940 --- /dev/null +++ b/Util.h @@ -0,0 +1,11 @@ +/* + * Util.h + * + * Created on: Apr 27, 2013 + * Author: jimtahu + */ + +#include + +double str2num(std::string text); +std::string num2str(double val);