Expressions

- Implemented E9:operator_negate, the unary -.
This commit is contained in:
Paul Beckingham 2011-09-11 00:47:11 -04:00
parent d9fa6fbac8
commit 22e9d84074
2 changed files with 33 additions and 13 deletions

View file

@ -109,12 +109,15 @@ void E9::eval (const Task& task, std::vector <Arg>& value_stack)
operator_not (result, right); operator_not (result, right);
} }
/*
// TODO Unary -. // TODO Not sure this is correct.
else if (arg->raw == "-") else if (arg->_raw == "-" && value_stack.size () < 2)
{ {
Arg right = value_stack.back ();
value_stack.pop_back ();
operator_negate (result, right);
} }
*/
// Binary operators. // Binary operators.
else else
@ -218,8 +221,7 @@ bool E9::eval_match (Arg& left, Arg& right, bool case_sensitive)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void E9::operator_not (Arg& result, Arg& right) void E9::operator_not (Arg& result, Arg& right)
{ {
result = right; result = coerce (right, Arg::type_bool);
coerce (result, Arg::type_bool);
if (result._value == "true") if (result._value == "true")
result._value = "false"; result._value = "false";
@ -229,6 +231,15 @@ void E9::operator_not (Arg& result, Arg& right)
// std::cout << "# <operator_not> " << right << " --> " << result << "\n"; // std::cout << "# <operator_not> " << right << " --> " << result << "\n";
} }
////////////////////////////////////////////////////////////////////////////////
void E9::operator_negate (Arg& result, Arg& right)
{
result = coerce (right, Arg::type_number);
result._value = format (- strtod (result._value.c_str (), NULL));
std::cout << "# <operator_negate> " << right << " --> " << result << "\n";
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void E9::operator_and (Arg& result, Arg& left, Arg& right) void E9::operator_and (Arg& result, Arg& left, Arg& right)
{ {
@ -607,18 +618,26 @@ const Arg E9::coerce (const Arg& input, const Arg::type type)
if (type == Arg::type_bool) if (type == Arg::type_bool)
{ {
result._raw = input._raw; result._raw = input._raw;
result._value = get_bool (input) ? "true" : "false"; result._value = get_bool (input) ? "true" : "false";
result._type = Arg::type_bool; result._type = Arg::type_bool;
result._category = input._category; result._category = input._category;
} }
else if (type == Arg::type_string) else if (type == Arg::type_string)
{ {
// TODO Convert date? // TODO Convert date?
result._raw = input._raw; result._raw = input._raw;
result._value = input._value; result._value = input._value;
result._type = Arg::type_string; result._type = Arg::type_string;
result._category = input._category;
}
else if (type == Arg::type_number)
{
result._raw = input._raw;
result._value = input._value;
result._type = Arg::type_number;
result._category = input._category; result._category = input._category;
} }

View file

@ -47,7 +47,8 @@ private:
bool eval_match (Arg&, Arg&, bool); bool eval_match (Arg&, Arg&, bool);
// Unary. // Unary.
void operator_not (Arg&, Arg&); void operator_not (Arg&, Arg&);
void operator_negate (Arg&, Arg&);
// Binary. // Binary.
void operator_and (Arg&, Arg&, Arg&); void operator_and (Arg&, Arg&, Arg&);