|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r61015 - in trunk/libs/spirit/example/scheme: . test
From: joel_at_[hidden]
Date: 2010-04-03 05:28:29
Author: djowel
Date: 2010-04-03 05:28:28 EDT (Sat, 03 Apr 2010)
New Revision: 61015
URL: http://svn.boost.org/trac/boost/changeset/61015
Log:
complete scheme operators
Text files modified:
trunk/libs/spirit/example/scheme/test/utree_test.cpp | 7 +
trunk/libs/spirit/example/scheme/utree_operators.hpp | 147 +++++++++++++++++++++++++++++++++++----
2 files changed, 137 insertions(+), 17 deletions(-)
Modified: trunk/libs/spirit/example/scheme/test/utree_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/utree_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/utree_test.cpp 2010-04-03 05:28:28 EDT (Sat, 03 Apr 2010)
@@ -196,6 +196,11 @@
}
{ // operators
+
+ BOOST_ASSERT((utree(true) && utree(true)) == utree(true));
+ BOOST_ASSERT((utree(true) || utree(false)) == utree(true));
+ BOOST_ASSERT(!utree(true) == utree(false));
+
BOOST_ASSERT((utree(456) + utree(123)) == utree(456 + 123));
BOOST_ASSERT((utree(456) + utree(123.456)) == utree(456 + 123.456));
BOOST_ASSERT((utree(456) - utree(123)) == utree(456 - 123));
@@ -205,12 +210,14 @@
BOOST_ASSERT((utree(456) / utree(123)) == utree(456 / 123));
BOOST_ASSERT((utree(456) / utree(123.456)) == utree(456 / 123.456));
BOOST_ASSERT((utree(456) % utree(123)) == utree(456 % 123));
+ BOOST_ASSERT(-utree(456) == utree(-456));
BOOST_ASSERT((utree(456) & utree(123)) == utree(456 & 123));
BOOST_ASSERT((utree(456) | utree(123)) == utree(456 | 123));
BOOST_ASSERT((utree(456) ^ utree(123)) == utree(456 ^ 123));
BOOST_ASSERT((utree(456) << utree(3)) == utree(456 << 3));
BOOST_ASSERT((utree(456) >> utree(2)) == utree(456 >> 2));
+ BOOST_ASSERT(~utree(456) == utree(~456));
}
return 0;
Modified: trunk/libs/spirit/example/scheme/utree_operators.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree_operators.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree_operators.hpp 2010-04-03 05:28:28 EDT (Sat, 03 Apr 2010)
@@ -14,7 +14,7 @@
namespace scheme
{
- // Available operators
+ // Relational operators
bool operator==(utree const& a, utree const& b);
bool operator<(utree const& a, utree const& b);
bool operator!=(utree const& a, utree const& b);
@@ -23,17 +23,26 @@
bool operator>=(utree const& a, utree const& b);
std::ostream& operator<<(std::ostream& out, utree const& x);
+ // Logical operators
+ utree operator&&(utree const& a, utree const& b);
+ utree operator||(utree const& a, utree const& b);
+ utree operator!(utree const& a);
+
+ // Arithmetic operators
utree operator+(utree const& a, utree const& b);
utree operator-(utree const& a, utree const& b);
utree operator*(utree const& a, utree const& b);
utree operator/(utree const& a, utree const& b);
utree operator%(utree const& a, utree const& b);
+ utree operator-(utree const& a);
+ // Bitwise operators
utree operator&(utree const& a, utree const& b);
utree operator|(utree const& a, utree const& b);
utree operator^(utree const& a, utree const& b);
utree operator<<(utree const& a, utree const& b);
utree operator>>(utree const& a, utree const& b);
+ utree operator~(utree const& a);
// Implementation
struct utree_is_equal
@@ -198,6 +207,40 @@
};
template <typename Base>
+ struct logical_function
+ {
+ typedef utree result_type;
+
+ // binary
+ utree operator()(bool a, bool b) const
+ {
+ return Base::eval(a, b); // for boolean types
+ }
+
+ // binary
+ template <typename A, typename B>
+ utree operator()(A const& a, B const& b) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non booleans
+ }
+
+ // unary
+ utree operator()(bool a) const
+ {
+ return Base::eval(a); // for boolean types
+ }
+
+ // unary
+ template <typename A>
+ utree operator()(A const& a) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non booleans
+ }
+ };
+
+ template <typename Base>
struct arithmetic_function
{
typedef utree result_type;
@@ -206,7 +249,7 @@
utree dispatch(A&, B&, boost::mpl::false_) const
{
// $$$ Throw exception here? $$$
- return utree(); // cannot apply to different types
+ return utree(); // cannot apply to non-arithmetic types
}
template <typename A, typename B>
@@ -215,6 +258,7 @@
return Base::eval(a, b); // for arithmetic types
}
+ // binary
template <typename A, typename B>
utree operator()(A& a, B& b) const
{
@@ -223,6 +267,26 @@
boost::is_arithmetic<A>,
boost::is_arithmetic<B> >());
}
+
+ template <typename A>
+ utree dispatch(A&, boost::mpl::false_) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non-arithmetic types
+ }
+
+ template <typename A>
+ utree dispatch(A& a, boost::mpl::true_) const
+ {
+ return Base::eval(a); // for arithmetic types
+ }
+
+ // unary
+ template <typename A>
+ utree operator()(A& a) const
+ {
+ return dispatch(a, boost::is_arithmetic<A>());
+ }
};
template <typename Base>
@@ -234,7 +298,7 @@
utree dispatch(A&, B&, boost::mpl::false_) const
{
// $$$ Throw exception here? $$$
- return utree(); // cannot apply to different types
+ return utree(); // cannot apply to non-integral types
}
template <typename A, typename B>
@@ -243,6 +307,7 @@
return Base::eval(a, b); // for integral types
}
+ // binary
template <typename A, typename B>
utree operator()(A& a, B& b) const
{
@@ -251,6 +316,26 @@
boost::is_integral<A>,
boost::is_integral<B> >());
}
+
+ template <typename A>
+ utree dispatch(A&, boost::mpl::false_) const
+ {
+ // $$$ Throw exception here? $$$
+ return utree(); // cannot apply to non-integral types
+ }
+
+ template <typename A>
+ utree dispatch(A& a, boost::mpl::true_) const
+ {
+ return Base::eval(a); // for integral types
+ }
+
+ // unary
+ template <typename A>
+ utree operator()(A& a) const
+ {
+ return dispatch(a, boost::is_integral<A>());
+ }
};
#define SCHEME_CREATE_FUNCTION(name, expr, base) \
@@ -261,6 +346,11 @@
{ \
return utree(expr); \
} \
+ template <typename A> \
+ static utree eval(A& a) \
+ { \
+ return utree(expr); \
+ } \
}; \
base<BOOST_PP_CAT(function_impl_, name)> const \
BOOST_PP_CAT(base, BOOST_PP_CAT(_, name)) = {}; \
@@ -274,20 +364,8 @@
SCHEME_CREATE_FUNCTION(name, expr, integral_function) \
/***/
-#define SCHEME_CREATE_ARITHMETIC_OPERATOR(name, op) \
- SCHEME_CREATE_FUNCTION(name, a op b, arithmetic_function) \
- inline utree operator op (utree const& a, utree const& b) \
- { \
- return utree::visit(a, b, BOOST_PP_CAT(arithmetic_function, name)); \
- } \
- /***/
-
-#define SCHEME_CREATE_INTEGRAL_OPERATOR(name, op) \
- SCHEME_CREATE_FUNCTION(name, a op b, integral_function) \
- inline utree operator op (utree const& a, utree const& b) \
- { \
- return utree::visit(a, b, BOOST_PP_CAT(integral_function, name)); \
- } \
+#define SCHEME_CREATE_LOGICAL_FUNCTION(name, expr) \
+ SCHEME_CREATE_FUNCTION(name, expr, logical_function) \
/***/
inline bool operator==(utree const& a, utree const& b)
@@ -326,17 +404,38 @@
return out;
}
+ SCHEME_CREATE_LOGICAL_FUNCTION(and_, a&&b);
+ SCHEME_CREATE_LOGICAL_FUNCTION(or_, a||b);
+ SCHEME_CREATE_LOGICAL_FUNCTION(not_, !a);
+
SCHEME_CREATE_ARITHMETIC_FUNCTION(plus, a+b);
SCHEME_CREATE_ARITHMETIC_FUNCTION(minus, a-b);
SCHEME_CREATE_ARITHMETIC_FUNCTION(times, a*b);
SCHEME_CREATE_ARITHMETIC_FUNCTION(divides, a/b);
SCHEME_CREATE_INTEGRAL_FUNCTION(modulus, a%b);
+ SCHEME_CREATE_ARITHMETIC_FUNCTION(negate, -a);
SCHEME_CREATE_INTEGRAL_FUNCTION(bitand_, a&b);
SCHEME_CREATE_INTEGRAL_FUNCTION(bitor_, a|b);
SCHEME_CREATE_INTEGRAL_FUNCTION(bitxor_, a^b);
SCHEME_CREATE_INTEGRAL_FUNCTION(shift_left, a<<b);
SCHEME_CREATE_INTEGRAL_FUNCTION(shift_right, a>>b);
+ SCHEME_CREATE_INTEGRAL_FUNCTION(invert, ~a);
+
+ inline utree operator&&(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, logical_function_and_);
+ }
+
+ inline utree operator||(utree const& a, utree const& b)
+ {
+ return utree::visit(a, b, logical_function_or_);
+ }
+
+ inline utree operator!(utree const& a)
+ {
+ return utree::visit(a, logical_function_not_);
+ }
inline utree operator+(utree const& a, utree const& b)
{
@@ -363,26 +462,40 @@
return utree::visit(a, b, integral_function_modulus);
}
+ inline utree operator-(utree const& a)
+ {
+ return utree::visit(a, arithmetic_function_negate);
+ }
+
inline utree operator&(utree const& a, utree const& b)
{
return utree::visit(a, b, integral_function_bitand_);
}
+
inline utree operator|(utree const& a, utree const& b)
{
return utree::visit(a, b, integral_function_bitor_);
}
+
inline utree operator^(utree const& a, utree const& b)
{
return utree::visit(a, b, integral_function_bitxor_);
}
+
inline utree operator<<(utree const& a, utree const& b)
{
return utree::visit(a, b, integral_function_shift_left);
}
+
inline utree operator>>(utree const& a, utree const& b)
{
return utree::visit(a, b, integral_function_shift_right);
}
+
+ inline utree operator~(utree const& a)
+ {
+ return utree::visit(a, integral_function_invert);
+ }
}
#endif
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk