Add operator < to calc

This commit is contained in:
Robin Jadoul 2017-01-29 12:14:17 +01:00
parent 0ebb56eb7c
commit 6a41ea1f36
4 changed files with 8 additions and 0 deletions

View File

@ -69,6 +69,8 @@ double Binop::eval(const Variables& vars, const Functions& funs) const {
return left / right;
else if (m_op == "^")
return std::pow(left, right);
else if (m_op == "<")
return left < right ? 1 : 0;
return 0;
}

View File

@ -9,6 +9,7 @@ MINUS = -
TIMES = \*
DIVIDE = /
EXPONENT = ^
LT = <
ASSIGN = =
NUM = (0|[1-9][0-9]*)(\.[0-9]+)?
IDENT = [a-zA-Z_][a-zA-Z0-9_]*

View File

@ -4,6 +4,7 @@ precedence:
right "EXPONENT"
left "TIMES" "DIVIDE"
left "PLUS" "MINUS"
nonassoc "LT"
start: <start>
grammar:
@ -27,6 +28,7 @@ grammar:
| <expr> "TIMES" <expr> [binop]
| <expr> "DIVIDE" <expr> [binop]
| <expr> "EXPONENT" <expr> [binop]
| <expr> "LT" <expr> [binop]
| "IDENT" [expr_simple]
| "NUM" [expr_simple]
| "MINUS" <expr> [umin, right 1]

View File

@ -72,6 +72,9 @@ Parser::Value Parser::reduce_binop(std::deque<Token> subparts) {
case calcParser_Symbol::T_EXPONENT:
op = "^";
break;
case calcParser_Symbol::T_LT:
op = "<";
break;
default:
break;
}