diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 2b4728e..fbbf880 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1835,72 +1835,24 @@ namespace chaiscript /// Reads a unary prefixed expression from input bool Prefix() { - bool retval = false; - const auto prev_stack_top = m_match_stack.size(); +// const std::array prefix_opers; - if (Symbol("++", true)) { - retval = true; + for (const auto &oper : std::initializer_list{"++", "--", "-", "+", "!", "~", "&"}) + { + bool is_char = oper.size() == 1; + if ((is_char && Char(oper[0], true)) || (!is_char && Symbol(oper.c_str(), true))) + { + if (!Operator(m_operators.size()-1)) { + throw exception::eval_error("Incomplete prefix '" + oper + "' expression", File_Position(m_line, m_col), *m_filename); + } - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '++' expression", File_Position(m_line, m_col), *m_filename); + build_match(std::make_shared(Operators::to_operator(oper, true)), prev_stack_top); + return true; } - - build_match(std::make_shared(Operators::to_operator("++")), prev_stack_top); - } else if (Symbol("--", true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '--' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("--")), prev_stack_top); - } else if (Char('-', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete unary '-' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("-", true)), prev_stack_top); - } else if (Char('+', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete unary '+' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("+", true)), prev_stack_top); - } - else if (Char('!', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '!' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("!")), prev_stack_top); - } - else if (Char('~', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("~")), prev_stack_top); - } - else if (Char('&', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '&' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("&")), prev_stack_top); } - return retval; + return false; } /// Parses any of a group of 'value' style ast_node groups from input