///////////////////////////////////////////////////////////////////////////
| 201 | |
| 202 | //////////////////////////////////////////////////////////////////////////////// |
| 203 | void Eval::evaluatePostfixStack(const std::vector<std::pair<std::string, Lexer::Type>>& tokens, |
| 204 | Variant& result) const { |
| 205 | if (tokens.size() == 0) throw std::string("No expression to evaluate."); |
| 206 | |
| 207 | // This is stack used by the postfix evaluator. |
| 208 | std::vector<Variant> values; |
| 209 | values.reserve(tokens.size()); |
| 210 | |
| 211 | for (const auto& token : tokens) { |
| 212 | // Unary operators. |
| 213 | if (token.second == Lexer::Type::op && token.first == "!") { |
| 214 | if (values.size() < 1) throw std::string("The expression could not be evaluated."); |
| 215 | |
| 216 | Variant right = values.back(); |
| 217 | values.pop_back(); |
| 218 | Variant result = !right; |
| 219 | values.push_back(result); |
| 220 | if (_debug) |
| 221 | Context::getContext().debug(format("Eval {1} ↓'{2}' → ↑'{3}'", token.first, |
| 222 | (std::string)right, (std::string)result)); |
| 223 | } else if (token.second == Lexer::Type::op && token.first == "_neg_") { |
| 224 | if (values.size() < 1) throw std::string("The expression could not be evaluated."); |
| 225 | |
| 226 | Variant right = values.back(); |
| 227 | values.pop_back(); |
| 228 | |
| 229 | Variant result(0); |
| 230 | result -= right; |
| 231 | values.push_back(result); |
| 232 | |
| 233 | if (_debug) |
| 234 | Context::getContext().debug(format("Eval {1} ↓'{2}' → ↑'{3}'", token.first, |
| 235 | (std::string)right, (std::string)result)); |
| 236 | } else if (token.second == Lexer::Type::op && token.first == "_pos_") { |
| 237 | // The _pos_ operator is a NOP. |
| 238 | if (_debug) |
| 239 | Context::getContext().debug(format("[{1}] eval op {2} NOP", values.size(), token.first)); |
| 240 | } |
| 241 | |
| 242 | // Binary operators. |
| 243 | else if (token.second == Lexer::Type::op) { |
| 244 | if (values.size() < 2) throw std::string("The expression could not be evaluated."); |
| 245 | |
| 246 | Variant right = values.back(); |
| 247 | values.pop_back(); |
| 248 | |
| 249 | Variant left = values.back(); |
| 250 | values.pop_back(); |
| 251 | |
| 252 | auto contextTask = Context::getContext().currentTask; |
| 253 | |
| 254 | // Ordering these by anticipation frequency of use is a good idea. |
| 255 | Variant result; |
| 256 | if (token.first == "and") |
| 257 | result = left && right; |
| 258 | else if (token.first == "or") |
| 259 | result = left || right; |
| 260 | else if (token.first == "&&") |
nothing calls this directly
no test coverage detected