From ffa5bc43fe9b081dda060abf1277fd8243f6d1e9 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 6 Aug 2011 00:59:14 -0400 Subject: [PATCH] E9 - Working towards arg date/duration eval. --- src/A3.cpp | 5 ++ src/DOM.cpp | 2 - src/Duration.cpp | 2 - src/E9.cpp | 160 ++++++++++----------------------------- src/E9.h | 8 ++ src/TDB2.cpp | 6 +- src/commands/Command.cpp | 8 +- test/duration.t.cpp | 3 +- test/text.t.cpp | 9 ++- 9 files changed, 74 insertions(+), 129 deletions(-) diff --git a/src/A3.cpp b/src/A3.cpp index 52460cbe5..14328d12b 100644 --- a/src/A3.cpp +++ b/src/A3.cpp @@ -830,6 +830,7 @@ const A3 A3::tokenize (const A3& input) const n.skipWS (); } + output.dump ("A3::tokenize"); return output; } @@ -863,6 +864,7 @@ const A3 A3::infix (const A3& input) const previous = *arg; } + modified.dump ("A3::infix"); return modified; } @@ -1036,6 +1038,7 @@ const A3 A3::expand (const A3& input) const previous = arg; } + expanded.dump ("A3::expand"); return expanded; } @@ -1109,6 +1112,7 @@ const A3 A3::sequence (const A3& input) const sequenced.push_back (*arg); } + sequenced.dump ("A3::sequence"); return sequenced; } @@ -1207,6 +1211,7 @@ const A3 A3::postfix (const A3& input) const op_stack.pop_back (); } + converted.dump ("A3::postfix"); return converted; } diff --git a/src/DOM.cpp b/src/DOM.cpp index d6c3d4260..654a1fa7c 100644 --- a/src/DOM.cpp +++ b/src/DOM.cpp @@ -30,8 +30,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/src/Duration.cpp b/src/Duration.cpp index 18c77c06c..ae6b40687 100644 --- a/src/Duration.cpp +++ b/src/Duration.cpp @@ -57,7 +57,6 @@ static const char* durations[] = "minutes", "mins", "min", - "mnths", "monthly", "months", @@ -66,7 +65,6 @@ static const char* durations[] = "mo", "mths", "m", - "quarterly", "quarters", "qrtrs", diff --git a/src/E9.cpp b/src/E9.cpp index e46d1db22..eb2be8cc7 100644 --- a/src/E9.cpp +++ b/src/E9.cpp @@ -27,12 +27,24 @@ #include // TODO Remove. #include +#include +#include #include #include #include extern Context context; +std::ostream& operator<< (std::ostream& out, const Term& term) +{ + out << term._raw << "/" + << term._value << "/" + << term._type << "/" + << term._category; + + return out; +} + //////////////////////////////////////////////////////////////////////////////// // Perform all the necessary steps prior to an eval call. E9::E9 (const A3& args) @@ -79,6 +91,7 @@ void E9::eval (const Task& task, std::vector & value_stack) { // Case sensitivity is configurable. bool case_sensitive = context.config.getBoolean ("search.case.sensitive"); + std::string dateformat = context.config.get ("dateformat"); std::vector ::iterator arg; for (arg = _terms.begin (); arg != _terms.end (); ++arg) @@ -167,6 +180,15 @@ void E9::eval (const Task& task, std::vector & value_stack) // Operand. else { + // Expand the value if possible. + if (arg->_category == "dom") + arg->_value = context.dom.get (arg->_raw, task); + else if (arg->_category == "date") + arg->_value = Date (arg->_raw, dateformat).toEpochString (); + else if (arg->_category == "duration") + arg->_value = (std::string)Duration (arg->_raw); + + //std::cout << "# E9::eval operand " << *arg << "\n"; value_stack.push_back (*arg); } } @@ -206,12 +228,9 @@ bool E9::eval_match (Term& left, Term& right, bool case_sensitive) void E9::operator_not (Term& result, Term& right) { // TODO This is not right. - result = Term (right._raw, coerce (right, "bool")._value, "bool"); + result = Term (right._raw, coerce (right, "bool")._value, "bool", right._category); - std::cout << "# not " << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; +// std::cout << "# not " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -228,14 +247,7 @@ void E9::operator_and (Term& result, Term& left, Term& right) result._category = "bool"; } -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " and " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " and " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -252,14 +264,7 @@ void E9::operator_or (Term& result, Term& left, Term& right) result._category = "bool"; } -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " or " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " or " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -279,14 +284,7 @@ void E9::operator_xor (Term& result, Term& left, Term& right) result._category = "bool"; } -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " xor " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " xor " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -309,14 +307,7 @@ void E9::operator_lt (Term& result, Term& left, Term& right) result._category = "bool"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " < " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " < " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -340,14 +331,7 @@ void E9::operator_lte (Term& result, Term& left, Term& right) result._category = "bool"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " <= " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " <= " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -371,14 +355,7 @@ void E9::operator_gte (Term& result, Term& left, Term& right) result._category = "bool"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " >= " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " >= " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -401,14 +378,7 @@ void E9::operator_gt (Term& result, Term& left, Term& right) result._category = "bool"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " > " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " > " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -424,14 +394,7 @@ void E9::operator_inequal ( else result._raw = result._value = "false"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " != " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " != " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -478,14 +441,7 @@ void E9::operator_equal ( } } -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " = " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " = " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -502,14 +458,7 @@ void E9::operator_match ( else result._raw = result._value = "false"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " ~ " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " ~ " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// @@ -536,62 +485,31 @@ void E9::operator_nomatch ( else result._raw = result._value = "false"; -/* - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " !~ " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; -*/ +// std::cout << "# " << left << " !~ " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// void E9::operator_multiply (Term& result, Term& left, Term& right) { - - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " * " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; + std::cout << "# " << left << " * " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// void E9::operator_divide (Term& result, Term& left, Term& right) { - - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " / " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; + std::cout << "# " << left << " / " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// void E9::operator_add (Term& result, Term& left, Term& right) { - - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " + " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; + std::cout << "# " << left << " + " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// void E9::operator_subtract (Term& result, Term& left, Term& right) { - - std::cout << "# " << left._raw << "/" << left._value << "/" << left._category - << " - " - << right._raw << "/" << right._value << "/" << right._category - << " --> " - << result._raw << "/" << result._value << "/" << result._category - << "\n"; + std::cout << "# " << left << " - " << right << " --> " << result << "\n"; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/E9.h b/src/E9.h index 6ee56180e..196dee95c 100644 --- a/src/E9.h +++ b/src/E9.h @@ -39,6 +39,7 @@ public: Term () : _raw ("") , _value ("") + , _type ("") , _category ("") { } @@ -46,6 +47,7 @@ public: Term (const Arg& arg) : _raw (arg._raw) , _value (arg._raw) + , _type ("string") , _category (arg._category) { } @@ -53,9 +55,11 @@ public: Term ( const std::string& raw, const std::string& value, + const std::string& type, const std::string& category) : _raw (raw) , _value (value) + , _type (type) , _category (category) { } @@ -64,6 +68,7 @@ public: { _raw = other._raw; _value = other._value; + _type = other._type; _category = other._category; } @@ -73,6 +78,7 @@ public: { _raw = other._raw; _value = other._value; + _type = other._type; _category = other._category; } @@ -83,12 +89,14 @@ public: { return _raw == other._raw && _value == other._value && + _type == other._type && _category == other._category; } public: std::string _raw; // Raw input token, never modified std::string _value; // Evaluated raw token, sometimes identical + std::string _type; // Inferred type std::string _category; // Categorized argument }; diff --git a/src/TDB2.cpp b/src/TDB2.cpp index c45a8a8ca..9d104de20 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -25,7 +25,6 @@ // //////////////////////////////////////////////////////////////////////////////// -#include // TODO Remove. #include #include #include @@ -281,6 +280,11 @@ void TF2::load_lines () split (_lines, _contents, '\n'); _loaded_lines = true; + +/* + if (_lines.back () == "") + _lines.pop_back (); +*/ } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 536ae013e..e986d1f01 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -409,13 +409,19 @@ void Command::modify_task ( A3::extract_attr (arg->_raw, name, value); if (A3::is_attribute (name, name)) // Canonicalize { + std::cout << "# Command::modify_task name='" << name << "' value='" << value << "'\n"; + // All values must be eval'd first. A3 fragment; - fragment.push_back (Arg (value, "attr")); + fragment.capture (value); + fragment = fragment.tokenize (fragment); E9 e (fragment); std::string result = e.evalExpression (task); context.debug (std::string ("Eval '") + value + "' --> '" + result + "'"); +fragment.dump ("pre modify_task attr"); +std::cout << "# modify_task result='" << result << "'\n"; + // Dependencies must be resolved to UUIDs. if (name == "depends") { diff --git a/test/duration.t.cpp b/test/duration.t.cpp index d76ab4f86..bd1f977cc 100644 --- a/test/duration.t.cpp +++ b/test/duration.t.cpp @@ -48,7 +48,7 @@ int convertDuration (const std::string& input) int main (int argc, char** argv) { - UnitTest t (628); + UnitTest t (629); Duration d; @@ -570,6 +570,7 @@ int main (int argc, char** argv) t.ok (d.valid ("0w"), "valid duration 0w"); t.ok (d.valid ("1 wks"), "valid duration 1 wks"); t.ok (d.valid ("1 wk"), "valid duration 1 wk"); + t.ok (d.valid ("1wk"), "valid duration 1wk"); t.ok (d.valid ("1w"), "valid duration 1w"); t.ok (d.valid ("10 wks"), "valid duration 10 wks"); t.ok (d.valid ("10 wk"), "valid duration 10 wk"); diff --git a/test/text.t.cpp b/test/text.t.cpp index f904638a6..de13fb395 100644 --- a/test/text.t.cpp +++ b/test/text.t.cpp @@ -35,7 +35,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (258); + UnitTest t (262); // void wrapText (std::vector & lines, const std::string& text, const int width) std::string text = "This is a test of the line wrapping code."; @@ -155,6 +155,13 @@ int main (int argc, char** argv) t.is (items[2], "bc", "split '-a-bc-def' '--' -> [2] 'bc'"); t.is (items[3], "def", "split '-a-bc-def' '--' -> [3] 'def'"); + unsplit = "one\ntwo\nthree"; + split (items, unsplit, "\n"); + t.is (items.size (), (size_t) 3, "split 'one\\ntwo\\nthree' -> 'one', 'two', 'three'"); + t.is (items[0], "one", "split 'one\\ntwo\\nthree' -> [0] 'one'"); + t.is (items[1], "two", "split 'one\\ntwo\\nthree' -> [1] 'two'"); + t.is (items[2], "three", "split 'one\\ntwo\\nthree' -> [2] 'three'"); + // void splitq (std::vector&, const std::string&, const char); unsplit = "one 'two' '' 'three four' \"five six seven\" eight'nine ten'"; splitq (items, unsplit, ' ');