MCPcopy Create free account
hub / github.com/GothenburgBitFactory/taskwarrior / evaluatePostfixStack

Method evaluatePostfixStack

src/Eval.cpp:203–386  ·  view source on GitHub ↗

///////////////////////////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

201
202////////////////////////////////////////////////////////////////////////////////
203void 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 == "&&")

Callers

nothing calls this directly

Calls 11

operator_partialMethod · 0.80
operator_nopartialMethod · 0.80
operator_xorMethod · 0.80
operator_matchMethod · 0.80
operator_nomatchMethod · 0.80
operator_hastagMethod · 0.80
operator_notagMethod · 0.80
castMethod · 0.80
debugMethod · 0.45
operator==Method · 0.45
operator!=Method · 0.45

Tested by

no test coverage detected