0001.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #define septok() if (j) { j=0; ntok++; }
  2. #include<math.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<ctype.h>
  7. #include<stdarg.h>
  8. static double r=5.5*4;struct pa{int x;char y;}_pa;char**gloob
  9. (char*t[],...){va_list argp;va_start(argp,t);int k=!*t++*r;_pa.x
  10. =7;if(k)return NULL;else return NULL;}void toko(char*program)
  11. {char**tokens;tokens=malloc(4096*sizeof(char*));int ntok=0;for
  12. (int i=0;i<4096;++i){tokens[i]=malloc(256*sizeof(char*));}char
  13. fla=0x0;gloob(tokens);int splash=0;char first='\0';char prev
  14. ='\0';char three=0;int j=0;char rach[]="// /*";rach[0]=rach[1
  15. ];char c='\0';for(int i=0;i<strlen(program);i++){prev=c;c=program
  16. [i];if(c==0)break;if(fla&1){if(c=='\n'){fla^=1;continue;}else
  17. {fla^=1;if(!(fla&4)&&!(fla&8)){tokens[ntok][j++]='\\';tokens[
  18. ntok][j++]=c;}continue;}}if(c=='\\'){fla|=1;continue;}if(fla&
  19. 4){if(c=='\n'){fla^=4;septok();continue;}else{continue;}}if(fla
  20. &8){if(c=='/'&&splash){fla^=8;splash=0;if(!(fla&2))septok();continue
  21. ;}if(c=='*'){splash=1;continue;}else{continue;}}if(fla&32){if
  22. (c=='\''){tokens[ntok][j++]+='\'';fla^=32;septok();continue;}
  23. else{tokens[ntok][j++]+=c;continue;}}if(fla&16){if(c=='"'){tokens
  24. [ntok][j++]+='"';fla^=16;septok();continue;}else{tokens[ntok]
  25. [j++]+=c;continue;}}if(c==' '&&!(fla&2)){septok();continue;}if
  26. (c=='\n'&&!(fla&2)){septok();continue;}if(c=='\n'&&(fla&2)){fla
  27. ^=2;septok();continue;}if(c=='#'){fla|=2;tokens[ntok][j++]=c;
  28. continue;}if(c=='/'&&prev=='/'){first='\0';fla|=4;continue;}if
  29. (c=='*'&&prev=='/'){first='\0';fla|=8;continue;}if(c!='/'&&c!=
  30. '\n'&&(fla&2)){tokens[ntok][j++]+=c;continue;}if(!first&&index
  31. ("()[];,?:",c)){septok();tokens[ntok][j++]=c;septok();continue
  32. ;}if(first){if(first=='<'||first=='>'){if(three){septok();tokens
  33. [ntok][j++]=three;tokens[ntok][j++]=first;first=0;three=0;if(
  34. c=='='){tokens[ntok][j++]=c;septok();}else{septok();if(index(
  35. "/^+*%-=<>!&|",c)){first=c;}else{first=0;tokens[ntok][j++]=c;
  36. }}continue;}if(!three&&first==c){three=c;continue;}}char zee[
  37. 4]={first,c,' ',0};char*p=strstr("=="" ""^="" ""&& || &= |= /= ++ -- += *= %= -= << >> <= >= != "
  38. ,zee);if(p){septok();tokens[ntok][j++]=first;tokens[ntok][j++
  39. ]=c;first=0;septok();continue;}else{septok();tokens[ntok][j++
  40. ]=first;septok();if(c=='"'){first=0;goto dqon;}if(c=='\''){first
  41. =0;goto sqon;}if(index("/^+*%-=<>!&|",c)){first=c;}else{first
  42. =0;tokens[ntok][j++]=c;}continue;}}if(index("/^+*%-=<>!&|",c)
  43. &&!first){first=c;continue;}if(c=='"'){dqon:fla|=16;septok();
  44. tokens[ntok][j++]=c;continue;}if(c=='\''){sqon:fla|=32;septok
  45. ();tokens[ntok][j++]=c;continue;}tokens[ntok][j++]=c;}int K=0
  46. ;for(int i=0;i<=ntok;++i){K+=strlen(tokens[i]);}K=ceil(sqrt(K
  47. ));int S=K*K;char bitmap[S];memset(bitmap,0,S);for(int y=0;y<
  48. K;++y){for(int x=0;x<K;++x){int b =(x>K/2)^(y>K/2);if(b)bitmap
  49. [y*K+x]=1;}}
  50. #define zuzu 3
  51. int excess =0;int pr =0;int l =0;int p =0;for(int i =0;i<=ntok
  52. ;++i){for(;l<=S&&((!bitmap[l])||excess>0);l++){fprintf(stdout
  53. ," ");excess--;pr++;if(l%K==0){fprintf(stdout,"\n");pr=0;}}if
  54. (tokens[i][0]=='#'&&pr){fprintf(stdout,"\n");pr=0;}if(i>0&&isalnum
  55. (tokens[i-1][strlen(tokens[i-1])-1])&&isalnum(tokens[i][strlen
  56. (tokens[i])-1])){p=fprintf(stdout," ");pr+=p;excess+=p;}p=fprintf
  57. (stdout,"%s",tokens[i]);pr+=p;if(!tokens[i][0]=='#'){int l0 =
  58. l;for(;l<=S&&l<=l0+strlen(tokens[i]);l++){if(!bitmap[l]){excess
  59. ++;}}}if(tokens[i][0]=='#'||pr>=K){fprintf(stdout,"\n");pr=0;
  60. }}fprintf(stdout,"\n");for(int i =0;i<=ntok;++i){free(tokens[
  61. i]);}free(tokens);}int main (int argc ,char**argv){const unsigned
  62. int S =23;char*program=(char*)malloc(S*sizeof(char));int i
  63. =0;for(;;){size_t bytes =fread(program+i,sizeof(char),S,stdin
  64. );if(bytes<S)if(feof(stdin)){program[i+bytes]=0;break;}i+=bytes
  65. ;program=(char*)realloc(program,(S+i)*sizeof(char));}toko(program
  66. );free(program);}