/* lain2.c (lain.c) - Don Yang (uguu.org) Preprocess 2: Optimize pass 1 08/11/00 */ #include #include #include #define MAX_TEMPLATE_SIZE 512 #define MAX_OUTPUT_SIZE 9999 #define INIT_OUTPUT_SIZE 183 #define INIT_LEAF_COUNT 4 #define EXPR_COUNT 8 #define TERMINAL_COUNT 16 int i, j, k, e, s; int length = INIT_OUTPUT_SIZE; int leaf = INIT_LEAF_COUNT; int length0, leaf0; char code[MAX_OUTPUT_SIZE] = "Zenum" "{" "x=Y127,y,z" "};" "Zchar t[z];" "Yint i,j,k;" "Zmain(Yint c,Zchar**a)" "{" "Yfor(i=j=t[x]=t[y]=0;i ' ' ? template[tindex] > 0 ? ++template[tindex] | charcount++ : (template[++tindex] = 1) | charcount++ : template[tindex] < 0 ? template[tindex]-- : (template[++tindex] = -1) : (template[++tindex] = 0); } template[++tindex] = MAX_OUTPUT_SIZE; for(; length < charcount;) { k = rand() % leaf; for(i = j = 0; j <= k; i++) { if( code[i] == '@' ) j++; } i--; length0 = length--; leaf0 = leaf--; do { e = rand() % EXPR_COUNT; length0 = length; leaf0 = leaf; for(j = 0; expr[e][j]; j++) { if( expr[e][j] == '@' ) leaf0++; length0++; } } while( length0 > charcount + 1 ); length = length0; leaf = leaf0; for(s = 0; code[s]; s++); for(; s >= i; code[s-- + j - 1] = code[s]); for(k = 0; expr[e][k]; code[i + k++] = expr[e][k]); } for(i = 0; code[i]; i++) { if( code[i] == '@' ) code[i] = terminal[rand() % TERMINAL_COUNT]; } for(tindex = i = k = 0; code[i]; tindex++) { if( template[tindex] ) { if( template[tindex] > 0 ) { for( j = (code[i] >= 'X' && code[i] <= 'Z') ? code[i++] - 'V' : 1; j-- > 0; template[tindex]--) e = k ? k-- : putchar(code[i++]); e = template[tindex] > 0 ? tindex-- : (k = -template[tindex]); } else { for(; template[tindex]; template[tindex]++) e = k ? k-- : putchar(' '); } } else { putchar('\n'); k = 0; } } putchar('\n'); return 0; }