From 8b7fe33bf15ce205a726e6b2244d6c770cf1d66e Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 6 Oct 2016 14:41:45 -0600 Subject: [PATCH] Fix order of operations for prefix and '*', '/' The problem is that Prefix did not properly participate in operator precedence. I've fixed this, at least for the moment, by adding a final depth of precedence that can be called when the depth gets to the bottom. closes #285 --- include/chaiscript/language/chaiscript_parser.hpp | 9 ++++++--- unittests/precedence_4.chai | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 unittests/precedence_4.chai diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 8fc9827e..7653c7f0 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -219,6 +219,10 @@ namespace chaiscript m_operators.emplace_back(AST_Node_Type::Multiplication); m_operator_matches.emplace_back(std::initializer_list({"*", "/", "%"})); + // Prefix placeholder + m_operators.emplace_back(AST_Node_Type::Prefix); + m_operator_matches.emplace_back(std::initializer_list({})); + for (auto & elem : m_alphabet) { std::fill(std::begin(elem), std::end(elem), false); } @@ -2197,7 +2201,7 @@ namespace chaiscript bool retval = false; const auto prev_stack_top = m_match_stack.size(); - if (t_precedence < m_operators.size()) { + if (m_operators[t_precedence] != AST_Node_Type::Prefix) { if (Operator(t_precedence+1)) { retval = true; if (Operator_Helper(t_precedence)) { @@ -2257,8 +2261,7 @@ namespace chaiscript } while (Operator_Helper(t_precedence)); } } - } - else { + } else { return Value(); } diff --git a/unittests/precedence_4.chai b/unittests/precedence_4.chai new file mode 100644 index 00000000..b55c83d1 --- /dev/null +++ b/unittests/precedence_4.chai @@ -0,0 +1,4 @@ + +var i = 2; + +assert_equal(++i * i, 9)