bind.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef META_HS_BIND_HPP
  2. #define META_HS_BIND_HPP
  3. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <lazy.hpp>
  8. #include <ast.hpp>
  9. #include <boost/mpl/at.hpp>
  10. #include <boost/mpl/insert.hpp>
  11. #include <boost/mpl/pair.hpp>
  12. template <class AST, class TopEnv, class Env>
  13. struct bind;
  14. template <class V, class TopEnv, class Env>
  15. struct bind<ast::value<V>, TopEnv, Env>
  16. {
  17. typedef lazy::value<V> type;
  18. };
  19. template <class Name, class TopEnv, class Env>
  20. struct bind<ast::ref<Name>, TopEnv, Env> :
  21. bind<typename boost::mpl::at<Env, Name>::type, TopEnv, Env>
  22. {};
  23. template <class F, class Arg, class TopEnv, class Env>
  24. struct bind<ast::application<F, Arg>, TopEnv, Env>
  25. {
  26. typedef
  27. lazy::application<
  28. typename bind<F, TopEnv, Env>::type,
  29. typename bind<Arg, TopEnv, Env>::type
  30. >
  31. type;
  32. };
  33. template <class F, class ArgName, class TopEnv, class Env>
  34. struct bind<ast::lambda<F, ArgName>, TopEnv, Env>
  35. {
  36. typedef bind type;
  37. template <class ArgValue>
  38. struct apply :
  39. bind<
  40. F,
  41. TopEnv,
  42. typename boost::mpl::insert<
  43. Env,
  44. boost::mpl::pair<ArgName, ast::value<ArgValue> >
  45. >::type
  46. >::type
  47. {};
  48. };
  49. template <class E, class TopEnv, class Env>
  50. struct bind<ast::top_bound<E>, TopEnv, Env> : bind<E, TopEnv, TopEnv> {};
  51. #endif