mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Feature - Calc
- Gave the calc command access to DOM resolution. Needs a Lexer change to recognize DOM addresses.
This commit is contained in:
parent
9c41610f56
commit
02116a9a93
4 changed files with 51 additions and 8 deletions
|
@ -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<eom and (pri=H or pri=M)". Defaults to on.
|
||||
.B expressions=infix|postfix
|
||||
Sets a preference for infix expressions (1 + 2) or postfix expressions (1 2 +).
|
||||
Defaults to infix.
|
||||
|
||||
.TP
|
||||
.B dom=on
|
||||
|
|
|
@ -98,7 +98,7 @@ std::string Config::_defaults =
|
|||
"burndown.bias=0.666 # Weighted mean bias toward recent data\n"
|
||||
"regex=no # Assume all search/filter strings are regexes\n"
|
||||
"xterm.title=no # Sets xterm title for some commands\n"
|
||||
"expressions=on # Support for algebraic expressions\n"
|
||||
"expressions=infix # Prefer infix over postfix expressions\n"
|
||||
"patterns=on # Support for regex patterns\n"
|
||||
"dom=on # Support DOM access\n"
|
||||
"json.array=off # Enclose JSON output in [ ]\n"
|
||||
|
|
|
@ -126,6 +126,7 @@ 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", ">");
|
||||
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)
|
||||
|
|
|
@ -26,12 +26,28 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <Context.h>
|
||||
#include <Eval.h>
|
||||
#include <Dates.h>
|
||||
#include <main.h>
|
||||
#include <i18n.h>
|
||||
#include <CmdCalc.h>
|
||||
|
||||
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 <std::string> words = context.a3.extract_words ();
|
||||
std::vector <std::string>::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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue