Skip to content

Commit

Permalink
Basic binary operators now work
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtahu committed Apr 27, 2013
1 parent e9ec23a commit 2c8ab85
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
35 changes: 34 additions & 1 deletion BinOpValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions BinOpValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down
11 changes: 11 additions & 0 deletions Util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Util.h
*
* Created on: Apr 27, 2013
* Author: jimtahu
*/

#include <string>

double str2num(std::string text);
std::string num2str(double val);

0 comments on commit 2c8ab85

Please sign in to comment.