aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/parser/operator.hpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2019-12-30 13:06:13 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2019-12-31 19:43:25 +0300
commit372064af20a456989c2a9ea2fda6e535d74dbafd (patch)
tree9cc9039586252bda5ef44a68db9f53627a87ce4b /server/parser/operator.hpp
parentadd .dockerignore (diff)
downloadmath-server-372064af20a456989c2a9ea2fda6e535d74dbafd.tar.gz
math-server-372064af20a456989c2a9ea2fda6e535d74dbafd.zip
support the power (^) operator
It doesn't work with the unary minus currently (as is reflected in the tests), it should have a higher precedence.
Diffstat (limited to 'server/parser/operator.hpp')
-rw-r--r--server/parser/operator.hpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/server/parser/operator.hpp b/server/parser/operator.hpp
index f1c24de..86b9eb1 100644
--- a/server/parser/operator.hpp
+++ b/server/parser/operator.hpp
@@ -10,6 +10,8 @@
#include "../lexer/token.hpp"
#include "../lexer/token_type.hpp"
+#include <cmath>
+
namespace math::server::parser {
class BinaryOp {
@@ -23,6 +25,7 @@ public:
case Type::MINUS:
case Type::ASTERISK:
case Type::SLASH:
+ case Type::CARET:
return true;
default:
@@ -49,25 +52,49 @@ public:
case Type::SLASH:
return min_precedence() + 1;
+ case Type::CARET:
+ return min_precedence() + 2;
+
default:
throw ParserError{"internal: undefined operator precedence"};
}
}
+ bool is_right_associative() const {
+ switch (m_type) {
+ case Type::CARET:
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
+ bool is_left_associative() const {
+ return !is_right_associative();
+ }
+
double exec(double lhs, double rhs) const {
switch (m_type) {
case Type::PLUS:
return lhs + rhs;
+
case Type::MINUS:
return lhs - rhs;
+
case Type::ASTERISK:
return lhs * rhs;
+
case Type::SLASH:
// Trapping the CPU would be better?
if (rhs == 0.) {
throw ParserError{"division by zero"};
}
return lhs / rhs;
+
+ case Type::CARET:
+ return std::pow(lhs, rhs);
+
default:
throw ParserError{"internal: unsupported operator"};
}