SHARE
TWEET

Untitled

a guest Oct 15th, 2012 181 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright (C) 2012 Pubby
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5.  
  6. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7.  
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9. */
  10.  
  11. template<typename...>
  12. struct stack;
  13.  
  14. template<typename X, typename... Xs>
  15. struct stack<X, Xs...> {
  16.   using type = stack;
  17.   using top = X;
  18.  
  19.   template<typename T>
  20.   using push = stack<T, X, Xs...>;
  21.  
  22.   using pop = stack<Xs...>;
  23. };
  24. template<>
  25. struct stack<> {
  26.   using type = stack;
  27.  
  28.   template<typename T>
  29.   using push = stack<T>;
  30. };
  31.  
  32. template<typename Stack, int N>
  33. struct stack_drop {
  34.   using stack = typename stack_drop<typename Stack::pop, N - 1>::stack;
  35. };
  36. template<typename Stack>
  37. struct stack_drop<Stack, 0> {
  38.   using stack = Stack;
  39. };
  40.  
  41. template<typename Stack, int N>
  42. struct stack_index {
  43.   using stack = typename stack_index<typename Stack::pop, N - 1>::stack;
  44. };
  45. template<typename Stack>
  46. struct stack_index<Stack, 0> {
  47.   using stack = typename Stack::top;
  48. };
  49.  
  50. ////
  51.  
  52. struct id {
  53.   template<typename Stack>
  54.   struct apply {
  55.     // only catches empty parameter pack
  56.     template<typename...>
  57.     struct continuation {
  58.       using stack = Stack;
  59.     };
  60.  
  61.     template<typename Word, typename... Remaining>
  62.     struct continuation<Word, Remaining...> {
  63.       using stack =
  64.         typename Word::template apply<Stack>
  65.         ::template continuation<Remaining...>::stack;
  66.     };
  67.   };
  68. };
  69.  
  70.  
  71. template<typename Word, typename Stack = stack<>>
  72. struct eval {
  73.   using stack = typename id::apply<Stack>::template continuation<Word>::stack;
  74. };
  75.  
  76. template<typename... Words>
  77. struct word {
  78.   template<typename Stack>
  79.   struct apply {
  80.     template<typename... Remaining>
  81.     struct continuation {
  82.       using stack = typename id::apply<
  83.         typename id::apply<Stack>::template continuation<Words...>::stack
  84.       >::template continuation<Remaining...>::stack;
  85.     };
  86.   };
  87. };
  88.  
  89. //
  90.  
  91. struct swap {
  92.   template<typename Stack>
  93.   struct apply {
  94.     template<typename... Remaining>
  95.     struct continuation {
  96.       using stack = typename id::apply<
  97.         typename stack_drop<Stack, 2>::stack
  98.         ::template push<typename Stack::top>
  99.         ::template push<typename stack_index<Stack, 1>::stack>
  100.       >::template continuation<Remaining...>::stack;
  101.     };
  102.   };
  103. };
  104.  
  105. template<typename T>
  106. struct push {
  107.   template<typename Stack>
  108.   struct apply {
  109.     template<typename... Remaining>
  110.     struct continuation {
  111.       using stack = typename id::apply<
  112.         typename Stack
  113.         ::template push<T>
  114.       >::template continuation<Remaining...>::stack;
  115.     };
  116.   };
  117. };
  118.  
  119. template<int I>
  120. struct int_wrapper {
  121.   static int const value = I;
  122. };
  123.  
  124. template<int N>
  125. struct int_ : push<int_wrapper<N>> {};
  126.  
  127. struct add {
  128.   template<typename Stack>
  129.   struct apply {
  130.     template<typename... Remaining>
  131.     struct continuation {
  132.       using stack = typename id::apply<
  133.         typename stack_drop<Stack, 2>::stack
  134.         ::template push<int_wrapper<
  135.           stack_index<Stack, 0>::stack::value
  136.           +
  137.           stack_index<Stack, 1>::stack::value
  138.         >>
  139.       >::template continuation<Remaining...>::stack;
  140.     };
  141.   };
  142. };
  143.  
  144.  
  145. struct five : int_<5> {};
  146. struct ten_and_five : word<five, int_<10>> {};
  147. struct foo : word<ten_and_five, word<swap, word<int_<20>>>, word<word<add>>> {};
  148.  
  149. #include <iostream>
  150.  
  151. int main() {
  152.   std::cout << eval<foo>::stack::top::value;
  153.   foo x;
  154. }
RAW Paste Data
Top