Feature - Calc
- Gave the calc command access to DOM resolution. Needs a Lexer change to recognize DOM addresses.
This commit is contained in:
@@ -362,9 +362,9 @@ Enables or disables pattern support on the command line, such as /foo/.
|
|||||||
Defaults to on.
|
Defaults to on.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B expressions=on
|
.B expressions=infix|postfix
|
||||||
Enables or disables algebraic expression support on the command line, such as
|
Sets a preference for infix expressions (1 + 2) or postfix expressions (1 2 +).
|
||||||
"due<eom and (pri=H or pri=M)". Defaults to on.
|
Defaults to infix.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B dom=on
|
.B dom=on
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ std::string Config::_defaults =
|
|||||||
"burndown.bias=0.666 # Weighted mean bias toward recent data\n"
|
"burndown.bias=0.666 # Weighted mean bias toward recent data\n"
|
||||||
"regex=no # Assume all search/filter strings are regexes\n"
|
"regex=no # Assume all search/filter strings are regexes\n"
|
||||||
"xterm.title=no # Sets xterm title for some commands\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"
|
"patterns=on # Support for regex patterns\n"
|
||||||
"dom=on # Support DOM access\n"
|
"dom=on # Support DOM access\n"
|
||||||
"json.array=off # Enclose JSON output in [ ]\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", "==");
|
||||||
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", ")");
|
||||||
|
a3t.entity ("operator", "%");
|
||||||
|
a3t.entity ("operator", "^");
|
||||||
|
a3t.entity ("operator", "!");
|
||||||
|
|
||||||
Tree* tree = a3t.parse ();
|
Tree* tree = a3t.parse ();
|
||||||
if (tree)
|
if (tree)
|
||||||
|
|||||||
@@ -26,12 +26,28 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <Context.h>
|
#include <Context.h>
|
||||||
|
#include <Eval.h>
|
||||||
|
#include <Dates.h>
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
#include <i18n.h>
|
#include <i18n.h>
|
||||||
#include <CmdCalc.h>
|
#include <CmdCalc.h>
|
||||||
|
|
||||||
extern Context context;
|
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 ()
|
CmdCalc::CmdCalc ()
|
||||||
{
|
{
|
||||||
@@ -45,12 +61,35 @@ CmdCalc::CmdCalc ()
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int CmdCalc::execute (std::string& output)
|
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
|
// Create an evaluator with DOM access.
|
||||||
// TODO Configurable date/number precedence
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user