diff --git a/doc/man/taskrc.5.in b/doc/man/taskrc.5.in index edcd7997c..21d52e30a 100644 --- a/doc/man/taskrc.5.in +++ b/doc/man/taskrc.5.in @@ -362,9 +362,9 @@ Enables or disables pattern support on the command line, such as /foo/. Defaults to on. .TP -.B expressions=on -Enables or disables algebraic expression support on the command line, such as -"due="); a3t.entity ("operator", "!~"); a3t.entity ("operator", "!="); + a3t.entity ("operator", "=="); a3t.entity ("operator", "="); a3t.entity ("operator", ">"); a3t.entity ("operator", "~"); @@ -140,6 +141,9 @@ int main (int argc, const char** argv) a3t.entity ("operator", "<"); a3t.entity ("operator", "("); a3t.entity ("operator", ")"); + a3t.entity ("operator", "%"); + a3t.entity ("operator", "^"); + a3t.entity ("operator", "!"); Tree* tree = a3t.parse (); if (tree) diff --git a/src/commands/CmdCalc.cpp b/src/commands/CmdCalc.cpp index 2786a9d13..0f061a7c7 100644 --- a/src/commands/CmdCalc.cpp +++ b/src/commands/CmdCalc.cpp @@ -26,12 +26,28 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include +#include #include #include #include extern Context context; +//////////////////////////////////////////////////////////////////////////////// +static bool domSource (const std::string& name, Variant& value) +{ + Task t; + std::string resolved = context.dom.get (name, t); + if (resolved != name) + { + value = Variant (resolved); + return true; + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// CmdCalc::CmdCalc () { @@ -45,12 +61,35 @@ CmdCalc::CmdCalc () //////////////////////////////////////////////////////////////////////////////// int CmdCalc::execute (std::string& output) { - int rc = 0; + // Configurable infix/postfix + bool infix = true; + if (context.config.get ("expressions") == "infix") + infix = true; + else if (context.config.get ("expressions") == "postfix") + infix = false; - // TODO Configurable infix/postfix - // TODO Configurable date/number precedence + // Create an evaluator with DOM access. + Eval e; + e.addSource (namedDates); + e.addSource (domSource); + e.ambiguity (false); // TODO Could/should be configurable. - return rc; + // Compile all the args into one expression. + std::string expression; + std::vector words = context.a3.extract_words (); + std::vector ::iterator word; + for (word = words.begin (); word != words.end (); ++word) + expression += *word + " "; + + // Evaluate according to preference. + Variant result; + if (infix) + e.evaluateInfixExpression (expression, result); + else + e.evaluatePostfixExpression (expression, result); + + output = (std::string) result + "\n"; + return 0; } ////////////////////////////////////////////////////////////////////////////////