Last active
August 29, 2015 14:11
-
-
Save codebrainz/31ee8fd678cc182fd494 to your computer and use it in GitHub Desktop.
weird macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <setjmp.h> | |
#include <stdio.h> | |
#define WHILE(while_expr) { \ | |
int jumped____; \ | |
jmp_buf redo_target____; \ | |
int restarted____; \ | |
restarted____ = setjmp(redo_target____); \ | |
jumped____ = 1; \ | |
while ((while_expr)||(jumped____=0)) { | |
#define UNTIL(until_expr) WHILE(!(until_expr)) | |
#define DO | |
#define REDO longjmp(redo_target____, 1) | |
#define THEN } if (!jumped____) { | |
#define ELSE } else { | |
#define FINALLY } { | |
#define END } } | |
int main() | |
{ | |
int i=0, j=11, k=3; | |
UNTIL (i == 10) | |
DO | |
printf("i: %d\n", i); | |
if (i > j) { | |
printf("Leaving loop\n"); | |
break; | |
} | |
else if (i == k) { | |
k = j + 1; | |
i = 0; | |
printf("Restarting loop\n"); | |
REDO; | |
} | |
i++; | |
THEN | |
printf("Counted to end\n"); | |
ELSE | |
printf("Exited loop early\n"); | |
FINALLY | |
printf("And in both cases came here\n"); | |
END | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment