v1.c 1.1 KB

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