static_loop
for(s_0_0 ; s_0_1 ; s_0_2){
block_0_0
for(s_1_0 ; s_1_1 ; s_1_2){
block_1_0
for(s_2_0 ; s_2_1 ; s_2_2){
block_2_0
block_2_1
}
block_1_1
}
block_0_1
}
dynamic_loop
const int depth = 3;
int d;
int firstFlg[depth] = {1,1,1};
for(d=0 ;; ){
if( firstFlg[d] ){
firstFlg[d] = 0;
s_d_0;
}
if( s_d_1 ){
block_d_0
if( d < depth - 1 ){
++d;
continue;
}
CONTINUE_POINT:
block_d_1
}else{
firstFlg[d] = 1;
--d;
if(d == -1){
break;
}else{
goto CONTINUE_POINT;
}
}
s_d_2;
}
sample.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MESSAGE_FIRST(d) \
do{ \
int _i;\
for(_i=0 ; _i<d ; _i++){ \
printf("\t"); \
} \
printf("s%d_%d\n",d,i[d]); \
}while(0)
#define MESSAGE_LAST(d) \
do{ \
int _i;\
for(_i=0 ; _i<d ; _i++){ \
printf("\t"); \
} \
printf("e%d_%d\n",d,i[d]); \
}while(0)
int main(int argc,char *argv[]){
int *firstFlg;
int *l;
int *i; // カウンタ変数
int depth; // ループの深さ
int d; //現在のループの深さ
depth = argc > 1 ? atoi(argv[1]) : 3;
firstFlg = (int *)malloc(sizeof(int) * depth);
l = (int *)malloc(sizeof(int) * depth);
i = (int *)malloc(sizeof(int) * depth);
srand((unsigned)time(NULL));
for(d = 0 ; d<depth ; ++d){ l[d] = (rand() % 5) + 1; }
for(d = 0 ; d<depth ; ++d){ firstFlg[d] = 1; }
for(d=0 ;; ){
if( firstFlg[d] ){
firstFlg[d] = 0;
/* s1 */
i[d] = 0;
}
// if( s2 )
if( i[d] < l[d] ){
MESSAGE_FIRST(d);
if( d < depth - 1 ){
++d;
continue;
}
CONTINUE_POINT:
MESSAGE_LAST(d);
}else{
firstFlg[d] = 1;
--d;
if(d == -1){
d = 0;
break;
}else{
goto CONTINUE_POINT;
}
}
/* s3 */
i[d] += 1;
}
free(firstFlg);
free(i);
free(l);
return 0;
}