v2.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define init_defer() \
  6. __label__ DO_DEFER; \
  7. char _run_defer = 0; \
  8. void** _next_defer = NULL; \
  9. DO_DEFER: \
  10. if(_run_defer) { \
  11. void* jump = _next_defer[1]; \
  12. _next_defer = (void**)_next_defer[0]; \
  13. goto *jump; \
  14. } \
  15. void run_defer(char* d) { \
  16. _run_defer++; \
  17. if(_run_defer == 1) goto DO_DEFER; \
  18. }; \
  19. __attribute__((cleanup(run_defer))) char _start_jump; \
  20. #define defer(...) \
  21. do { \
  22. __label__ _def; \
  23. _def: \
  24. if(_run_defer) { \
  25. __VA_ARGS__; \
  26. if(_next_defer) { \
  27. void* jump = _next_defer[1]; \
  28. _next_defer = (void**)_next_defer[0]; \
  29. goto *jump;\
  30. } \
  31. else goto DONE; \
  32. } \
  33. else { \
  34. void** link = alloca(16); \
  35. link[0] = (void*)_next_defer; \
  36. link[1] = &&_def; \
  37. _next_defer = link; \
  38. } \
  39. } while(0)
  40. int main(int argc, char **argv) {
  41. init_defer()
  42. defer(printf("crowbar"));
  43. printf("some junk");
  44. defer(printf("foobar"));
  45. return 0;
  46. DONE:
  47. }