Skip to content

Commit

Permalink
binary operators cannot be only chars (+, -, *, /) also strings (>=, …
Browse files Browse the repository at this point in the history
…<=, ...)
  • Loading branch information
ertsiger committed Mar 16, 2018
1 parent 7b5bd65 commit 3352efd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion parser/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Expression * createExpression( Filereader & f, TokenStruct< std::string > & ts,
Expression * right = createExpression( f, ts, d );
f.next();
f.assert_token( ")" );
return new CompositeExpression( s[0], left, right );
return new CompositeExpression( s, left, right );
}
else {
f.c -= s.size();
Expand Down
16 changes: 8 additions & 8 deletions parser/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class CompositeExpression : public Expression {

public:

char op;
std::string op;
Expression * left;
Expression * right;

CompositeExpression( char c, Expression * l, Expression * r ) : op( c ), left( l ), right( r ) {}
CompositeExpression( const std::string& c, Expression * l, Expression * r ) : op( c ), left( l ), right( r ) {}

~CompositeExpression() {
delete left;
Expand All @@ -57,12 +57,12 @@ class CompositeExpression : public Expression {

double compute( double x, double y ) {
double res = 0;
switch( op ) {
case '+': res = x + y; break;
case '-': res = x - y; break;
case '*': res = x * y; break;
case '/': res = ( y == 0 ? 0 : x / y ); break;
}

if ( op == "+" ) res = x + y;
else if ( op == "-" ) res = x - y;
else if ( op == "*" ) res = x * y;
else if ( op == "/" ) res = ( y == 0 ? 0 : x / y );

return res;
}

Expand Down

0 comments on commit 3352efd

Please sign in to comment.