Advertisement
phicr

Generic Heterogeneous Tuples in C

Jan 29th, 2023 (edited)
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.45 KB | Gaming | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <assert.h>
  6.  
  7. #ifdef __clang__
  8.   #define auto __auto_type
  9. #endif
  10.  
  11. #define TEST(...) TA(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  12. #define TA(X,Y) T##X Y
  13. #define T(...)  false
  14. #define TT(...) true
  15.  
  16. #define COUNT_ARGS(...) CA0P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  17. #define CA0P(X,Y) CA0T##X Y
  18. #define CA0T(...) 0
  19. #define CA0TT(X,...) CA1(__VA_ARGS__)
  20.  
  21. #define CA1(...) CA1P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  22. #define CA1P(X,Y) CA1T##X Y
  23. #define CA1T(...) 1
  24. #define CA1TT(X,...) CA2(__VA_ARGS__)
  25.  
  26. #define CA2(...) CA2P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  27. #define CA2P(X,Y) CA2T##X Y
  28. #define CA2T(...) 2
  29. #define CA2TT(X,...) CA3(__VA_ARGS__)
  30.  
  31. #define CA3(...) CA3P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  32. #define CA3P(X,Y) CA3T##X Y
  33. #define CA3T(...) 3
  34. #define CA3TT(X,...) CA4(__VA_ARGS__)
  35.  
  36. #define CA4(...) CA4P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  37. #define CA4P(X,Y) CA4T##X Y
  38. #define CA4T(...) 4
  39. #define CA4TT(X,...) CA5(__VA_ARGS__)
  40.  
  41. #define CA5(...) CA5P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  42. #define CA5P(X,Y) CA5T##X Y
  43. #define CA5T(...) 5
  44. #define CA5TT(X,...) CA6(__VA_ARGS__)
  45.  
  46. #define CA6(...) CA6P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  47. #define CA6P(X,Y) CA6T##X Y
  48. #define CA6T(...) 6
  49. #define CA6TT(X,...) CA7(__VA_ARGS__)
  50.  
  51. #define CA7(...) CA7P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  52. #define CA7P(X,Y) CA7T##X Y
  53. #define CA7T(...) 7
  54. #define CA7TT(X,...) CA8(__VA_ARGS__)
  55.  
  56. #define CA8(...) CA8P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  57. #define CA8P(X,Y) CA8T##X Y
  58. #define CA8T(...) 8
  59. #define CA8TT(X,...) CA9(__VA_ARGS__)
  60.  
  61. #define CA9(...) CA9P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  62. #define CA9P(X,Y) CA9T##X Y
  63. #define CA9T(...) 9
  64. #define CA9TT(X,...) CA10(__VA_ARGS__)
  65.  
  66. #define CA10(...) CA10P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  67. #define CA10P(X,Y) CA10T##X Y
  68. #define CA10T(...) 10
  69. #define CA10TT(X,...) CA11(__VA_ARGS__)
  70.  
  71. #define CA11(...) CA11P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  72. #define CA11P(X,Y) CA11T##X Y
  73. #define CA11T(...) 11
  74. #define CA11TT(X,...) CA12(__VA_ARGS__)
  75.  
  76. #define CA12(...) CA12P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  77. #define CA12P(X,Y) CA12T##X Y
  78. #define CA12T(...) 12
  79. #define CA12TT(X,...) CA13(__VA_ARGS__)
  80.  
  81. #define CA13(...) CA13P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  82. #define CA13P(X,Y) CA13T##X Y
  83. #define CA13T(...) 13
  84. #define CA13TT(X,...) CA14(__VA_ARGS__)
  85.  
  86. #define CA14(...) CA14P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  87. #define CA14P(X,Y) CA14T##X Y
  88. #define CA14T(...) 14
  89. #define CA14TT(X,...) CA15(__VA_ARGS__)
  90.  
  91. #define CA15(...) CA15P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  92. #define CA15P(X,Y) CA15T##X Y
  93. #define CA15T(...) 15
  94. #define CA15TT(X,...) CA16(__VA_ARGS__)
  95.  
  96. #define CA16(...) CA16P(__VA_OPT__(T), (__VA_OPT__(__VA_ARGS__)))
  97. #define CA16P(X,Y) CA16T##X Y
  98. #define CA16T(...) 16
  99. #define CA16TT(X,...) CA17(__VA_ARGS__)
  100.  
  101. #define UNWRAP(...) __VA_ARGS__
  102.  
  103. #define FMAP(F,X) FMAP_EX(COUNT_ARGS X, F, UNWRAP X)
  104. #define FMAP_EX(C,F,...) FMAP_EX1(C, F __VA_OPT__(,) __VA_ARGS__)
  105. #define FMAP_EX1(C,F,...) FMAP##C(F __VA_OPT__(,) __VA_ARGS__)
  106. #define FMAP0(F) ()
  107. #define FMAP1(F,A0) (F(A0))
  108. #define FMAP2(F,A0,A1) (F(A0),F(A1))
  109. #define FMAP3(F,A0,A1,A2) (F(A0),F(A1),F(A2))
  110. #define FMAP4(F,A0,A1,A2,A3) (F(A0),F(A1),F(A2),F(A3))
  111. #define FMAP5(F,A0,A1,A2,A3,A4) (F(A0),F(A1),F(A2),F(A3),F(A4))
  112. #define FMAP6(F,A0,A1,A2,A3,A4,A5) \
  113.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5))
  114. #define FMAP7(F,A0,A1,A2,A3,A4,A5,A6) \
  115.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  116.    F(A6))
  117. #define FMAP8(F,A0,A1,A2,A3,A4,A5,A6,A7) \
  118.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  119.    F(A6),F(A7))
  120. #define FMAP9(F,A0,A1,A2,A3,A4,A5,A6,A7,A8) \
  121.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  122.    F(A6),F(A7),F(A8))
  123. #define FMAP10(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9) \
  124.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  125.    F(A6),F(A7),F(A8),F(A9))
  126. #define FMAP11(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) \
  127.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  128.    F(A6),F(A7),F(A8),F(A9),F(A10))
  129. #define FMAP12(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11) \
  130.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  131.    F(A6),F(A7),F(A8),F(A9),F(A10),F(A11))
  132. #define FMAP13(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12) \
  133.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  134.    F(A6),F(A7),F(A8),F(A9),F(A10),F(A11),\
  135.    F(A12))
  136. #define FMAP14(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13) \
  137.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  138.    F(A6),F(A7),F(A8),F(A9),F(A10),F(A11),\
  139.    F(A12),F(A13))
  140. #define FMAP15(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14) \
  141.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  142.    F(A6),F(A7),F(A8),F(A9),F(A10),F(A11),\
  143.    F(A12),F(A13),F(A14))
  144. #define FMAP16(F,A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15) \
  145.   (F(A0),F(A1),F(A2),F(A3),F(A4),F(A5),\
  146.    F(A6),F(A7),F(A8),F(A9),F(A10),F(A11),\
  147.    F(A12),F(A13),F(A14),F(A15))
  148.  
  149. #define gtuple(...) (tupletypeof(__VA_ARGS__)) tuple(__VA_ARGS__)
  150.  
  151. #define deftuple(VAR,TUP)  tupletypeof TUP VAR = tuple TUP
  152.  
  153.  
  154. typedef struct tuple_len0 {} tuple_len0;
  155. typedef struct tuple_len1 {} tuple_len1;
  156. typedef struct tuple_len2 {} tuple_len2;
  157. typedef struct tuple_len3 {} tuple_len3;
  158. typedef struct tuple_len4 {} tuple_len4;
  159. typedef struct tuple_len5 {} tuple_len5;
  160. typedef struct tuple_len6 {} tuple_len6;
  161. typedef struct tuple_len7 {} tuple_len7;
  162. typedef struct tuple_len8 {} tuple_len8;
  163. typedef struct tuple_len9 {} tuple_len9;
  164. typedef struct tuple_len10 {} tuple_len10;
  165. typedef struct tuple_len11 {} tuple_len11;
  166. typedef struct tuple_len12 {} tuple_len12;
  167. typedef struct tuple_len13 {} tuple_len13;
  168. typedef struct tuple_len14 {} tuple_len14;
  169. typedef struct tuple_len15 {} tuple_len15;
  170. typedef struct tuple_len16 {} tuple_len16;
  171.  
  172. typedef struct tuple_empty {} tuple_empty;
  173. typedef struct tuple0 {
  174.   tuple_empty p0;
  175.   tuple_empty p1;
  176.   tuple_empty p2;
  177.   tuple_empty p3;
  178.   tuple_empty p4;
  179.   tuple_empty p5;
  180.   tuple_empty p6;
  181.   tuple_empty p7;
  182.   tuple_empty p8;
  183.   tuple_empty p9;
  184.   tuple_empty p10;
  185.   tuple_empty p11;
  186.   tuple_empty p12;
  187.   tuple_empty p13;
  188.   tuple_empty p14;
  189.   tuple_empty p15;
  190.   tuple_len0 len;
  191. } tuple0;
  192.  
  193. #define tuple(...) TUPLE_EX(COUNT_ARGS(__VA_ARGS__), (__VA_ARGS__))
  194. #define TUPLE_EX(X,Y)  TUPLE_EX1(X,Y)
  195. #define TUPLE_EX1(X,Y) TUPLE##X Y
  196. #define TUPLE0() {{}}
  197. #define TUPLE1(P0) {P0,{}}
  198. #define TUPLE2(P0,P1) {P0,P1,{}}
  199. #define TUPLE3(P0,P1,P2) {P0,P1,P2,{}}
  200. #define TUPLE4(P0,P1,P2,P3) {P0,P1,P2,P3,{}}
  201. #define TUPLE5(P0,P1,P2,P3,P4) {P0,P1,P2,P3,P4,{}}
  202. #define TUPLE6(P0,P1,P2,P3,P4,P5) {P0,P1,P2,P3,P4,P5,{}}
  203. #define TUPLE7(P0,P1,P2,P3,P4,P5,P6) {P0,P1,P2,P3,P4,P5,P6,{}}
  204. #define TUPLE8(P0,P1,P2,P3,P4,P5,P6,P7) {P0,P1,P2,P3,P4,P5,P6,P7,{}}
  205. #define TUPLE9(P0,P1,P2,P3,P4,P5,P6,P7,P8) {P0,P1,P2,P3,P4,P5,P6,P7,P8,{}}
  206. #define TUPLE10(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) \
  207.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,{}}
  208. #define TUPLE11(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10) \
  209.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,{}}
  210. #define TUPLE12(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11) \
  211.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,{}}
  212. #define TUPLE13(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12) \
  213.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,{}}
  214. #define TUPLE14(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13) \
  215.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,{}}
  216. #define TUPLE15(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14) \
  217.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,{}}
  218. #define TUPLE16(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15) \
  219.   {P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,{}}
  220.  
  221. #define tupletypeof(...) TUPLETYPEOF_EX(FMAP(typeof, (__VA_ARGS__)))
  222. #define TUPLETYPEOF_EX(TUP) TUPLETYPEOF_EX1(TUP)
  223. #define TUPLETYPEOF_EX1(TUP) tuple_t TUP
  224.  
  225. #define _TUPLETYPE_EX(X,Y)  TUPLETYPE_EX1(X,Y)
  226. #define _TUPLETYPE_EX1(X,Y) TUPLETYPE##X Y
  227.  
  228. typedef void* tuple_t;
  229.  
  230. #define tuple_t(...) TUPLE_T_EX(COUNT_ARGS(__VA_ARGS__), (__VA_ARGS__))
  231. #define TUPLE_T_EX(X,Y)  TUPLE_T_EX1(X,Y)
  232. #define TUPLE_T_EX1(X,Y) TUPLE_T##X Y
  233.  
  234. #define TUPLE_T0() tuple0
  235.  
  236. #define TUPLE_T1(P0) \
  237.   struct {P0 p0; tuple_len1 len;\
  238.           tuple_empty p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  239. #define TUPLE_T2(P0,P1) \
  240.   struct {P0 p0; P1 p1; tuple_len2 len;\
  241.           tuple_empty p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  242.  
  243. #define TUPLE_T3(P0,P1,P2) \
  244.   struct {P0 p0; P1 p1; P2 p2; tuple_len3 len;\
  245.           tuple_empty p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  246.  
  247. #define TUPLE_T4(P0,P1,P2,P3) \
  248.   struct {P0 p0; P1 p1; P2 p2; P3 p3; tuple_len4 len;\
  249.           tuple_empty p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  250.  
  251. #define TUPLE_T5(P0,P1,P2,P3,P4) \
  252.   struct {P0 p0; P1 p1; P2 p2; P3 p3; P4 p4; tuple_len5 len;\
  253.           tuple_empty p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  254.  
  255. #define TUPLE_T6(P0,P1,P2,P3,P4,P5) \
  256.   struct {P0 p0; P1 p1; P2 p2; P3 p3; P4 p4; P5 p5; tuple_len6 len;\
  257.           tuple_empty p6,p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  258.  
  259. #define TUPLE_T7(P0,P1,P2,P3,P4,P5,P6) \
  260.   struct {P0 p0; P1 p1; P2 p2; P3 p3; P4 p4; P5 p5; P6 p6; tuple_len7 len;\
  261.           tuple_empty p7,p8,p9,p10,p11,p12,p13,p14,p15; }
  262.  
  263. #define TUPLE_T8(P0,P1,P2,P3,P4,P5,P6,P7) \
  264.   struct {P0 p0; P1 p1; P2 p2; P3 p3; P4 p4; P5 p5; P6 p6; P7 p7; tuple_len8 len;\
  265.           tuple_empty p8,p9,p10,p11,p12,p13,p14,p15; }
  266.  
  267. #define TUPLE_T9(P0,P1,P2,P3,P4,P5,P6,P7,P8) \
  268.   struct {P0 p0; P1 p1; P2 p2; P3 p3; P4 p4; P5 p5; P6 p6; P7 p7; P8 p8; tuple_len9 len;\
  269.           tuple_empty p9,p10,p11,p12,p13,p14,p15; }
  270.  
  271. #define TUPLE_T10(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) \
  272.   struct {P0 p0; P1 p1; P2 p2; P3 p3; P4 p4; P5 p5; P6 p6; P7 p7; P8 p8; P9 p9; tuple_len10 len;\
  273.           tuple_empty p10,p11,p12,p13,p14,p15; }
  274.  
  275. #define TUPLE_T11(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10) \
  276.   struct {P0  p0;  P1 p1; P2 p2; P3 p3; P4 p4;\
  277.           P5  p5;  P6 p6; P7 p7; P8 p8; P9 p9;\
  278.           P10 p10; tuple_len11 len;\
  279.           tuple_empty p11,p12,p13,p14,p15; }
  280.  
  281. #define TUPLE_T12(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11) \
  282.   struct {P0  p0;  P1  p1;  P2 p2; P3 p3; P4 p4;\
  283.           P5  p5;  P6  p6;  P7 p7; P8 p8; P9 p9;\
  284.           P10 p10; P11 p11; tuple_len12 len;\
  285.           tuple_empty p12,p13,p14,p15; }
  286. #define TUPLE_T13(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12) \
  287.   struct {P0  p0;  P1  p1;  P2  p2;  P3 p3; P4 p4;\
  288.           P5  p5;  P6  p6;  P7  p7;  P8 p8; P9 p9;\
  289.           P10 p10; P11 p11; P12 p12; tuple_len13 len;\
  290.           tuple_empty p13,p14,p15; }
  291.  
  292. #define TUPLE_T14(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13) \
  293.   struct {P0  p0;  P1  p1;  P2  p2;  P3  p3;  P4 p4;\
  294.           P5  p5;  P6  p6;  P7  p7;  P8  p8;  P9 p9;\
  295.           P10 p10; P11 p11; P12 p12; P13 p13; tuple_len14 len;\
  296.           tuple_empty p14,p15; }
  297.  
  298. #define TUPLE_T15(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14) \
  299.   struct {P0  p0;  P1  p1;  P2  p2;  P3  p3;  P4  p4;\
  300.           P5  p5;  P6  p6;  P7  p7;  P8  p8;  P9  p9;\
  301.           P10 p10; P11 p11; P12 p12; P13 p13; P14 p14; tuple_len15 len;\
  302.           tuple_empty p15; }
  303.  
  304. #define TUPLE_T16(P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15) \
  305.   struct {P0  p0;  P1  p1;  P2  p2;  P3  p3;  P4  p4;\
  306.           P5  p5;  P6  p6;  P7  p7;  P8  p8;  P9  p9;\
  307.           P10 p10; P11 p11; P12 p12; P13 p13; P14 p14;\
  308.           P15 p15; tuple_len16 len;}
  309.  
  310.  
  311. typedef struct tuple_index0 {bool dummy;} tuple_index0;
  312. typedef struct tuple_index1 {bool dummy;} tuple_index1;
  313. typedef struct tuple_index2 {bool dummy;} tuple_index2;
  314. typedef struct tuple_index3 {bool dummy;} tuple_index3;
  315. typedef struct tuple_index4 {bool dummy;} tuple_index4;
  316. typedef struct tuple_index5 {bool dummy;} tuple_index5;
  317. typedef struct tuple_index6 {bool dummy;} tuple_index6;
  318. typedef struct tuple_index7 {bool dummy;} tuple_index7;
  319. typedef struct tuple_index8 {bool dummy;} tuple_index8;
  320. typedef struct tuple_index9 {bool dummy;} tuple_index9;
  321. typedef struct tuple_index10 {bool dummy;} tuple_index10;
  322. typedef struct tuple_index11 {bool dummy;} tuple_index11;
  323. typedef struct tuple_index12 {bool dummy;} tuple_index12;
  324. typedef struct tuple_index13 {bool dummy;} tuple_index13;
  325. typedef struct tuple_index14 {bool dummy;} tuple_index14;
  326. typedef struct tuple_index15 {bool dummy;} tuple_index15;
  327.  
  328. #define settuple(VAR,TUP) VAR = (typeof(VAR))tuple TUP
  329. #define tuple_nth(TUP,INDEX) _Generic(((TUP).p##INDEX),\
  330.   tuple_empty: tuple_index_error(tuplen(TUP), INDEX),\
  331.   default: (TUP).p##INDEX\
  332. )
  333.  
  334.  
  335. #define print_tuple_nth(TUP,N) do {\
  336.     auto _prtp_nth = tuple_nth(TUP,N);\
  337.     printf(_Generic(_prtp_nth,\
  338.         int: "%d",\
  339.         long int: "%ld",\
  340.         unsigned int: "%u",\
  341.         long unsigned int: "%lu",\
  342.         char*: "\"%s\"",\
  343.         char: "%c",\
  344.         float: "%f",\
  345.         double: "%lf"), _prtp_nth);\
  346.   } while (false)
  347.  
  348.  
  349. #define print_tuple(TUP) do {\
  350.   size_t _prtp_len = tuplen(TUP);\
  351.   printf("(");\
  352.   if (tuplen(TUP) > 0) {\
  353.     print_tuple_nth(TUP,0);\
  354.     if (tuplen(TUP) > 1) {\
  355.       printf(", ");\
  356.       print_tuple_nth(TUP,1);\
  357.       if (tuplen(TUP) > 2) {\
  358.         printf(", ");\
  359.         print_tuple_nth(TUP,2);\
  360.         if (tuplen(TUP) > 3) {\
  361.           printf(", ");\
  362.           print_tuple_nth(TUP,3);\
  363.           if (tuplen(TUP) > 4) {\
  364.             printf(", ");\
  365.             print_tuple_nth(TUP,4);\
  366.             if (tuplen(TUP) > 5) {\
  367.               printf(", ");\
  368.               print_tuple_nth(TUP,5);\
  369.               if (tuplen(TUP) > 6) {\
  370.                 printf(", ");\
  371.                 print_tuple_nth(TUP,6);\
  372.                 if (tuplen(TUP) > 7) {\
  373.                   printf(", ");\
  374.                   print_tuple_nth(TUP,7);\
  375.                   if (tuplen(TUP) > 8) {\
  376.                     printf(", ");\
  377.                     print_tuple_nth(TUP,8);\
  378.                     if (tuplen(TUP) > 9) {\
  379.                       printf(", ");\
  380.                       print_tuple_nth(TUP,9);\
  381.                       if (tuplen(TUP) > 10) {\
  382.                         printf(", ");\
  383.                         print_tuple_nth(TUP,10);\
  384.                         if (tuplen(TUP) > 11) {\
  385.                           printf(", ");\
  386.                           print_tuple_nth(TUP,11);\
  387.                           if (tuplen(TUP) > 12) {\
  388.                             printf(", ");\
  389.                             print_tuple_nth(TUP,12);\
  390.                             if (tuplen(TUP) > 13) {\
  391.                               printf(", ");\
  392.                               print_tuple_nth(TUP,13);\
  393.                               if (tuplen(TUP) > 14) {\
  394.                                 printf(", ");\
  395.                                 print_tuple_nth(TUP,14);\
  396.                                 if (tuplen(TUP) > 15) {\
  397.                                   printf(", ");\
  398.                                   print_tuple_nth(TUP,15);\
  399.                                   if (tuplen(TUP) > 16) {\
  400.                                     printf(", ");\
  401.                                     print_tuple_nth(TUP,15);\
  402.                                   }\
  403.                                 }\
  404.                               }\
  405.                             }\
  406.                           }\
  407.                         }\
  408.                       }\
  409.                     }\
  410.                   }\
  411.                 }\
  412.               }\
  413.             }\
  414.           }\
  415.         }\
  416.       }\
  417.     }\
  418.     printf(")");\
  419.   }\
  420. } while (false)
  421.  
  422. #define tuplen(TUP) _Generic((TUP).len,\
  423.   tuple_len0: 0,\
  424.   tuple_len1: 1,\
  425.   tuple_len2: 2,\
  426.   tuple_len3: 3,\
  427.   tuple_len4: 4,\
  428.   tuple_len5: 5,\
  429.   tuple_len6: 6,\
  430.   tuple_len7: 7,\
  431.   tuple_len8: 8,\
  432.   tuple_len9: 9,\
  433.   tuple_len10: 10,\
  434.   tuple_len11: 11,\
  435.   tuple_len12: 12,\
  436.   tuple_len13: 13,\
  437.   tuple_len14: 14,\
  438.   tuple_len15: 15,\
  439.   tuple_len16: 16\
  440. )
  441.  
  442. float tuple_function(tuple_t x, tuple_t y) {
  443.   tuple_t(int, float)* yp = (typeof(yp))y;
  444.   return (float)yp->p0 + yp->p1;
  445. }
  446.  
  447. void test() {
  448.   tuple_t() x;
  449.   tuple_t(int, float) y;
  450.  
  451.   settuple(x, ());
  452.   settuple(y, (1, 1.0));
  453.  
  454.   assert(sizeof(x) == 0);
  455.  
  456.   assert(y.p0 == 1);
  457.   assert(y.p1 == 1.0);
  458.   assert(tuple_function((tuple_t)&x, (tuple_t)&y) == 2.0);
  459.  
  460.   int k[2] = {1,2};
  461.  
  462.   deftuple(t0, ());
  463.   deftuple(t1, ("a"));
  464.   deftuple(t2, (x, 2));
  465.   deftuple(t3, (t0, t2, 0.0));
  466.   deftuple(t4, ((int*)k,"abc",2500,t0));
  467.   deftuple(t5, (1,2,3,4,5));
  468.   deftuple(t6, ("a",2,3.0,'d',0x5,0b110));
  469.   deftuple(t7, (1,2,3,4,5,6,7));
  470.   deftuple(t8, (1,2,3,4,5,6,7,8));
  471.   deftuple(t9, (1,2,3,4,5,6,7,8,9));
  472.   deftuple(t10, (1,2,3,4,5,6,7,8,9,10));
  473.   deftuple(t11, (1,2,3,4,5,6,7,8,9,10,11));
  474.   deftuple(t12, (1,2,3,4,5,6,7,8,9,10,11,12));
  475.   deftuple(t13, (1,2,3,4,5,6,7,8,9,10,11,12,13));
  476.   deftuple(t14, (1,2,3,4,5,6,7,8,9,10,11,12,13,14));
  477.   deftuple(t15, (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15));
  478.   deftuple(t16, (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16));
  479.  
  480.   assert(tuplen(t0) == 0);
  481.   assert(tuplen(t1) == 1);
  482.   assert(tuplen(t2) == 2);
  483.   assert(tuplen(t3) == 3);
  484.   assert(tuplen(t4) == 4);
  485.   assert(tuplen(t5) == 5);
  486.   assert(tuplen(t6) == 6);
  487.   assert(tuplen(t7) == 7);
  488.   assert(tuplen(t8) == 8);
  489.   assert(tuplen(t9) == 9);
  490.   assert(tuplen(t10) == 10);
  491.   assert(tuplen(t11) == 11);
  492.   assert(tuplen(t12) == 12);
  493.   assert(tuplen(t13) == 13);
  494.   assert(tuplen(t14) == 14);
  495.   assert(tuplen(t15) == 15);
  496.   assert(tuplen(t16) == 16);
  497. }
  498.  
  499. [[noreturn]] int tuple_index_error(size_t length, size_t index) {
  500.   fprintf(stderr, "Attempt to get index %zd of Tuple of length %zd.\n", index, length);
  501.   exit(1);
  502. }
  503.  
  504. int main([[maybe_unused]] int argc,[[maybe_unused]] char** argv) {
  505.   deftuple(tup, (1,"abc",1.7));
  506.   print_tuple(tup);
  507.   printf("\n");
  508.   test();
  509.   return 0;
  510. }
  511.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement