Advertisement
tyler569

I actually really like this C syntax....

Jul 28th, 2018
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1.  
  2. #define CHECK_N(x, n, ...) n
  3. #define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
  4. #define PROBE(x) x, 1,
  5. #define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
  6. #define IS_PAREN_PROBE(...) PROBE(~)
  7. #define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
  8. #define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
  9. #define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
  10. #define COMPL_0 1
  11. #define COMPL_1 0
  12. #define IIF(c) PRIMITIVE_CAT(IIF_, c)
  13. #define IIF_0(t, ...) __VA_ARGS__
  14. #define IIF_1(t, ...) t
  15. #define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
  16. #define NOT_0 PROBE(~)
  17. #define BOOL(x) COMPL(NOT(x))
  18. #define IF(c) IIF(BOOL(c))
  19. #define EAT(...)
  20. #define EXPAND(...) __VA_ARGS__
  21. #define WHEN(c) IF(c)(EXPAND, EAT)
  22.  
  23. #define auto () // maybe don't do this in arbitrary code
  24.  
  25. #define let(v, type) IF(NOT(IS_PAREN(type)))(typeof(type) v, __auto_type v)
  26.  
  27. int main() {
  28.     let(x, int) = 10;
  29.     let(y, int*) = &x;
  30.     let(z, auto) = 100;
  31.     return *y;
  32. }
  33.  
  34. int bar(let(x, int)) {
  35.     return x + 1;
  36. }
  37.  
  38. let(foo, int(*)(int)) = bar;
  39. let(baz, auto) = bar;
  40.  
  41. let(boo, char[4096]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement