aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/parser/parser.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/parser.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 '')
-rw-r--r--server/parser/parser.hpp12
1 files changed, 11 insertions, 1 deletions
diff --git a/server/parser/parser.hpp b/server/parser/parser.hpp
index ac87fbe..596f84f 100644
--- a/server/parser/parser.hpp
+++ b/server/parser/parser.hpp
@@ -45,7 +45,17 @@ private:
m_lexer.drop_token();
auto rhs = exec_primary();
- for (op = peek_operator(); op.has_value() && op->get_precedence() > lhs_op.get_precedence(); op = peek_operator()) {
+ for (op = peek_operator(); op.has_value(); op = peek_operator()) {
+ const auto op_prec = op->get_precedence();
+ const auto lhs_prec = lhs_op.get_precedence();
+ const auto ok_left_assoc = op->is_left_associative() && op_prec > lhs_prec;
+ const auto ok_right_assoc = op->is_right_associative() && op_prec == lhs_prec;
+ const auto ok = ok_left_assoc || ok_right_assoc;
+
+ if (!ok) {
+ break;
+ }
+
rhs = exec_expr(rhs, op->get_precedence());
}