Skip to content

Instantly share code, notes, and snippets.

@m1lkweed
Last active January 28, 2025 17:25
Show Gist options
  • Save m1lkweed/4339f3c1196ec3acf995e3d671c1927b to your computer and use it in GitHub Desktop.
Save m1lkweed/4339f3c1196ec3acf995e3d671c1927b to your computer and use it in GitHub Desktop.
Porting python's `for`/`else` to C with no `setjmp`.
#include <stdio.h>
#define py_for(...) _Pragma("GCC diagnostic push")_Pragma("GCC diagnostic ignored \"-Wdangling-else\"") \
for(struct{int count; bool broke; bool once; bool copy;}py_for = {}; \
(py_for.count < 2) && !py_for.broke; ++py_for.count)if(!py_for.count)for(__VA_ARGS__) \
if(py_for.broke){break;}else for(py_for.once = true; (py_for.copy = py_for.broke), \
(py_for.broke = py_for.once), py_for.once && !py_for.copy; py_for.broke = py_for.once = false)
_Pragma("GCC diagnostic pop")
#define py_whl(...) py_for(;__VA_ARGS__;)
int main(){
py_for(int i = 0; i < 5; ++i){
printf("%d\n", i);
}else{
puts("egg");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment