calc3.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //[ calc3
  7. #include <boost/yap/expression.hpp>
  8. #include <boost/hana/maximum.hpp>
  9. #include <iostream>
  10. // Look! A transform! This one transforms the expression tree into the arity
  11. // of the expression, based on its placeholders.
  12. //[ calc3_get_arity_xform
  13. struct get_arity
  14. {
  15. // Base case 1: Match a placeholder terminal, and return its arity as the
  16. // result.
  17. template <long long I>
  18. boost::hana::llong<I> operator() (boost::yap::expr_tag<boost::yap::expr_kind::terminal>,
  19. boost::yap::placeholder<I>)
  20. { return boost::hana::llong_c<I>; }
  21. // Base case 2: Match any other terminal. Return 0; non-placeholders do
  22. // not contribute to arity.
  23. template <typename T>
  24. auto operator() (boost::yap::expr_tag<boost::yap::expr_kind::terminal>, T &&)
  25. {
  26. using namespace boost::hana::literals;
  27. return 0_c;
  28. }
  29. // Recursive case: Match any expression not covered above, and return the
  30. // maximum of its children's arities.
  31. template <boost::yap::expr_kind Kind, typename... Arg>
  32. auto operator() (boost::yap::expr_tag<Kind>, Arg &&... arg)
  33. {
  34. return boost::hana::maximum(
  35. boost::hana::make_tuple(
  36. boost::yap::transform(
  37. boost::yap::as_expr(std::forward<Arg>(arg)),
  38. get_arity{}
  39. )...
  40. )
  41. );
  42. }
  43. };
  44. //]
  45. int main ()
  46. {
  47. using namespace boost::yap::literals;
  48. // These lambdas wrap our expressions as callables, and allow us to check
  49. // the arity of each as we call it.
  50. auto expr_1 = 1_p + 2.0;
  51. auto expr_1_fn = [expr_1](auto &&... args) {
  52. auto const arity = boost::yap::transform(expr_1, get_arity{});
  53. static_assert(arity.value == sizeof...(args), "Called with wrong number of args.");
  54. return evaluate(expr_1, args...);
  55. };
  56. auto expr_2 = 1_p * 2_p;
  57. auto expr_2_fn = [expr_2](auto &&... args) {
  58. auto const arity = boost::yap::transform(expr_2, get_arity{});
  59. static_assert(arity.value == sizeof...(args), "Called with wrong number of args.");
  60. return evaluate(expr_2, args...);
  61. };
  62. auto expr_3 = (1_p - 2_p) / 2_p;
  63. auto expr_3_fn = [expr_3](auto &&... args) {
  64. auto const arity = boost::yap::transform(expr_3, get_arity{});
  65. static_assert(arity.value == sizeof...(args), "Called with wrong number of args.");
  66. return evaluate(expr_3, args...);
  67. };
  68. // Displays "5"
  69. std::cout << expr_1_fn(3.0) << std::endl;
  70. // Displays "6"
  71. std::cout << expr_2_fn(3.0, 2.0) << std::endl;
  72. // Displays "0.5"
  73. std::cout << expr_3_fn(3.0, 2.0) << std::endl;
  74. // Static-asserts with "Called with wrong number of args."
  75. //std::cout << expr_3_fn(3.0) << std::endl;
  76. // Static-asserts with "Called with wrong number of args."
  77. //std::cout << expr_3_fn(3.0, 2.0, 1.0) << std::endl;
  78. return 0;
  79. }
  80. //]