Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebrainz/31ee8fd678cc182fd494 to your computer and use it in GitHub Desktop.
Save codebrainz/31ee8fd678cc182fd494 to your computer and use it in GitHub Desktop.
weird macros
#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