implement unary - + operators

This commit is contained in:
River Tarnell 2008-08-09 09:16:04 +00:00
parent 041b24b601
commit 7bf795588e
3 changed files with 28 additions and 0 deletions

View file

@ -263,6 +263,18 @@ struct arith_visitor : boost::static_visitor<datum> {
from_string_converter<T>::convert(a),
from_string_converter<U>::convert(b));
}
/*
* Unary version.
*/
template<typename T>
datum operator() (T const &a) const {
typedef typename from_string_converter<T>::type a_type;
Operator<typename preferred_type<a_type, a_type>::type> op;
return op(from_string_converter<T>::convert(a));
}
};
/*
@ -369,6 +381,18 @@ datum::operator%=(datum const &other)
return *this;
}
datum
datum::operator+() const
{
return *this;
}
datum
datum::operator-() const
{
return boost::apply_visitor(arith_visitor<std::negate>(), value_);
}
datum
operator+(datum const &a, datum const &b) {
return datum(a) += b;

View file

@ -84,6 +84,8 @@ public:
datum &operator/=(datum const &other);
datum &operator%=(datum const &other);
bool operator!() const;
datum operator+() const;
datum operator-() const;
bool compare(datum const &other) const;
bool compare_with_type(datum const &other) const;

View file

@ -293,6 +293,8 @@ struct parser_grammar : public grammar<parser_grammar, parser_closure::context_t
basic =
( '(' >> tern_expr[basic.val = arg1] >> ')' )
| ch_p('!') >> tern_expr[basic.val = !arg1]
| ch_p('+') >> tern_expr[basic.val = arg1]
| ch_p('-') >> tern_expr[basic.val = -arg1]
| variable[basic.val = arg1]
| function[basic.val = arg1]
| value[basic.val = arg1]