Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. #ifndef JNP1_ZAD4_FIBIN_H
  2. #define JNP1_ZAD4_FIBIN_H
  3.  
  4. #include <type_traits>
  5. #include <cassert>
  6.  
  7. // Obsługa Var - konwertuje char* w jednoznaczny napis, sprawdza czy spełnia założenia zadania.
  8. #define VarType unsigned long long
  9.  
  10. static constexpr unsigned long long valueOfChar(const char a) {
  11. return ('0' <= a && a <= '9') ? a - '0' + 1
  12. : ('a' <= a && a <= 'z') ? a - 'a' + 11 : a - 'A' + 11;
  13. }
  14.  
  15. static constexpr bool correctName(const char* str) {
  16. int i = 0;
  17. for (; str[i] != '\0'; i++) {
  18. if (('0' <= str[i] && str[i] <= '9') || ('a' <= str[i] && str[i] <= 'z') || ('A' <= str[i] && str[i] <= 'Z')) {
  19. return false;
  20. }
  21. }
  22. return (0 < i && i < 7);
  23. }
  24.  
  25. static constexpr VarType Var(const char* str) {
  26. //assert(correctName(str));
  27. unsigned long long res = 0;
  28. for (int i = 0; str[i] != '\0'; i++) {
  29. res = res * 100 + valueOfChar(str[i]);
  30. }
  31. return res;
  32. }
  33.  
  34. // Do obsługi typów liczbowych.
  35. template <unsigned long long x>
  36. struct Numeric {
  37. static constexpr unsigned long long value = x;
  38. };
  39.  
  40. // Typy boolowskie(True, False).
  41. struct True {
  42. };
  43.  
  44. struct False {
  45. };
  46.  
  47. // Fib, przyjmujący jako agrument liczbę.
  48. template <int>
  49. struct Fib {
  50. };
  51.  
  52. // Literał Lit.
  53. template <typename>
  54. struct Lit {
  55. };
  56.  
  57. // Wyrażenie If.
  58. template <typename, typename, typename>
  59. struct If {
  60. };
  61.  
  62. // Wyrażenie Let.
  63. template <VarType Variable, typename Value, typename Expression>
  64. struct Let {
  65. };
  66.  
  67. // Odwołanie do zmiennej Ref.
  68. template <VarType Variable>
  69. struct Ref {
  70. };
  71.  
  72. // Porównanie Eq.
  73. template <typename, typename>
  74. struct Eq;
  75.  
  76. // Wyrażenie Lambda.
  77. template <VarType Variable, typename Body>
  78. struct Lambda {
  79. };
  80.  
  81. // Wywołanie funkcji Invoke.
  82. template <typename fun, typename Param>
  83. struct Invoke {
  84. };
  85.  
  86. // Obliczanie literałów Fib.
  87. template <>
  88. struct Lit<True> {
  89. };
  90.  
  91. template <>
  92. struct Lit<False> {
  93. };
  94.  
  95. template <int N>
  96. struct Lit<Fib<N> > {
  97. Numeric<Lit<Fib<N - 1> >::result::value + Lit<Fib<N - 2> >::result::value> typedef result;
  98. };
  99.  
  100. template <>
  101. struct Lit<Fib<0> > {
  102. Numeric<0> typedef result;
  103. };
  104.  
  105. template <>
  106. struct Lit<Fib<1> > {
  107. Numeric<1> typedef result;
  108. };
  109.  
  110. // Operacje arytmetyczne.
  111.  
  112. template <typename T>
  113. struct Inc1 {
  114. };
  115.  
  116. template <typename T>
  117. struct Inc10 {
  118. };
  119.  
  120. // Sum<Args...> - suma argumentów Sum
  121. template <typename...>
  122. struct Sum {
  123. };
  124.  
  125.  
  126. // Tworzenie Enviroment - przetrzymuje zmienne od ich nazw, oblicza się w czasie kompilacji.
  127. struct EmptyEnvironment {
  128. };
  129.  
  130. template <VarType Variable, typename Val, typename NextEnv>
  131. struct Environment {
  132. };
  133.  
  134. template <VarType Variable, typename Env>
  135. struct GetEnv {
  136. };
  137.  
  138. template <VarType Variable>
  139. struct GetEnv<Variable, EmptyEnvironment> {
  140. };
  141.  
  142. template <VarType Variable, typename Value, typename NextEnv>
  143. struct GetEnv<Variable, Environment<Variable, Value, NextEnv> > {
  144. Value typedef result;
  145. };
  146.  
  147. template <VarType Variable, VarType Variable2, typename Value2, typename NextEnv>
  148. struct GetEnv<Variable, Environment<Variable2, Value2, NextEnv> > {
  149. typename GetEnv<Variable, NextEnv>::result typedef result;
  150. };
  151.  
  152. // Funkcje pomocnicze do Evaluate.
  153. template <bool>
  154. struct BoolToLit {
  155. Lit<False> typedef result;
  156. };
  157.  
  158. template <>
  159. struct BoolToLit<true> {
  160. Lit<True> typedef result;
  161. };
  162.  
  163. // Evaluate - obliczane w zależności od zawartości.
  164. template <typename Expression, typename Environment>
  165. struct Evaluate {
  166. Expression typedef result;
  167. };
  168.  
  169. template <int N, typename Environment>
  170. struct Evaluate<Lit<Fib<N> >, Environment> {
  171. typename Lit<Fib<N> >::result typedef result;
  172. };
  173.  
  174. template <typename Val, typename Environment>
  175. struct Evaluate<Inc1<Val>, Environment> {
  176. Numeric<Evaluate<Val, Environment>::result::value + Lit<Fib<1> >::result::value> typedef result;
  177. };
  178.  
  179. template <typename Val, typename Environment>
  180. struct Evaluate<Inc10<Val>, Environment> {
  181. Numeric<Evaluate<Val, Environment>::result::value + Lit<Fib<10> >::result::value> typedef result;
  182. };
  183.  
  184. template <typename Arg, typename Env>
  185. struct Evaluate<Sum<Arg>, Env> {
  186. Numeric<Evaluate<Arg, Env>::result::value> typedef result;
  187. };
  188.  
  189. template <typename Arg1, typename Arg2, typename... Args, typename Env>
  190. struct Evaluate<Sum<Arg1, Arg2, Args...>, Env> {
  191. Numeric<Evaluate<Arg1,Env>::result::value + Evaluate<Sum<Arg2, Args...>, Env>::result::value> typedef result;
  192. };
  193.  
  194.  
  195. template <VarType Variable, typename Environment>
  196. struct Evaluate<Ref<Variable>, Environment> {
  197. typename GetEnv<Variable, Environment>::result typedef result;
  198. };
  199.  
  200. template <typename Then, typename Else, typename Env>
  201. struct Evaluate<If<Lit<True>, Then, Else>, Env> {
  202. typename Then::result typedef result;
  203. };
  204.  
  205. template <typename Then, typename Else, typename Env>
  206. struct Evaluate<If<Lit<False>, Then, Else>, Env> {
  207. typename Evaluate<Else, Env>::result typedef result;
  208. };
  209.  
  210. template <typename Condition, typename Then, typename Else, typename Env>
  211. struct Evaluate<If<Condition, Then, Else>, Env> {
  212. typename Evaluate<If<typename Evaluate<Condition, Env>::result, Then, Else>, Env>::result typedef result;
  213. };
  214.  
  215. template <VarType Variable, typename Value, typename Expression, typename Env>
  216. struct Evaluate<Let<Variable, Value, Expression>, Env> {
  217. typename Evaluate<Expression, Environment<Variable, typename Evaluate<Value,Env>::result, Env> >::result typedef result;
  218. };
  219.  
  220. template <typename LeftVal, typename RightVal, typename Env>
  221. struct Evaluate<Eq<LeftVal, RightVal>, Env> {
  222. typename Evaluate<Eq<typename Evaluate<LeftVal, Env>::result, typename Evaluate<RightVal, Env>::result>, Env>::result typedef result;
  223. };
  224.  
  225. template <unsigned long long x, unsigned long long y, typename Env>
  226. struct Evaluate<Eq<Numeric<x>, Numeric<y> >, Env> {
  227. typename BoolToLit<x == y>::result typedef result;
  228. };
  229.  
  230. // std::cout <<Fibin<int>::eval<Invoke<Lambda<Var("x"), Ref<Var("x")>>, Lit<Fib<0>>>>() << "\n";
  231. template <typename fun, VarType Variable, typename Value, typename Env>
  232. struct Evaluate<Invoke<Lambda<Variable, fun>, Value>, Env> {
  233. typename Evaluate<Let<Variable, fun, Evaluate<Value,Env>>, Env>::result typedef result;
  234. };
  235.  
  236. // Klasa Fibin.
  237. template <typename T, bool = std::is_integral<T>::value>
  238. class Fibin {
  239. public:
  240. template <typename Expr>
  241. static void eval() {
  242. std::cout << "Fibin doesn't support: " << typeid(T).name() << '\n';
  243. return;
  244. }
  245. };
  246.  
  247. template <typename T>
  248. class Fibin<T, true> {
  249. public:
  250. template <typename Expr>
  251. static T eval() {
  252. return Evaluate<Expr, EmptyEnvironment>::result::value;
  253. }
  254. };
  255.  
  256. #endif //JNP1_ZAD4_FIBIN_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement