home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / glib / part02 / ctomenu.l < prev    next >
Text File  |  1989-05-21  |  6KB  |  269 lines

  1. %{
  2. /* $Id: ctomenu.l,v 1.6 89/05/06 17:13:13 lee Exp $
  3.  * Greg Lee, lee@uhccux.uhcc.hawaii.edu, 3/7/89
  4.  *
  5.  * Ctomenu helps translate a synthesizer-specific C file into .menu
  6.  * form by generating the '#MENU ... #END' part for the labelinfo
  7.  * array and the '#O' lines for the paraminfo array.
  8.  *
  9.  * $Log:    ctomenu.l,v $
  10.  * Revision 1.6  89/05/06  17:13:13  lee
  11.  * rel. to comp.sources.misc
  12.  * 
  13.  */
  14. #include <string.h>
  15.  
  16. #define SCOLS 81
  17. #define SROWS 24
  18. #define MAXPARAM 300
  19.  
  20.     int i, j, offset, reservesize = 20;
  21.     int row = -2, col = -2 , coord;
  22.     char labelname[30];
  23.     char paramname[30];
  24.     char temps[30];
  25.     char *screen;
  26.     char paraml[MAXPARAM][30];
  27.     char paramv[MAXPARAM][20];
  28.     int paramcoord[MAXPARAM][6];
  29.     int paramno = 0, pcount = 0;
  30.     int havename = 0;
  31.     char paramtemps[80];
  32.     int paramtemplen;
  33. %}
  34.  
  35. %s A B C D E
  36. wh [ \t\n]*
  37.  
  38. %%
  39.  
  40. <A>^"#define"[ \t]+"RESERVESIZE"[ \t]*[0-9]+ {
  41.         for (i=0; yytext[i] != 'Z'; i++) ;
  42.         reservesize = atoi(yytext+i+2);
  43. };
  44.  
  45. <A>^"struct labelinfo".*\n  {
  46.         for (i=0; i < 30 && yytext[i+16] != '{'; i++)
  47.             labelname[i] = yytext[i+16];
  48.         labelname[i] = 0;
  49.         BEGIN(B);
  50. };
  51.  
  52. <C>^"struct paraminfo".*\n  {
  53.         for (i=0; i < 30 && yytext[i+16] != '{'; i++)
  54.             paramname[i] = yytext[i+16];
  55.         paramname[i] = 0;
  56.         havename = 0;
  57.         BEGIN(D);
  58. };
  59.  
  60. <B,D>{wh}        ;
  61. <B,D>","        ;
  62. <D>"-1"            ;
  63. <D>"NULL"        ;
  64.  
  65. <B,D>[0-9]+    {
  66.         coord = atoi(yytext);
  67.         if (row == -2) row = coord;
  68.         else if (col == -2) col = coord;
  69.         else {
  70.             fprintf(stderr, "coordinate mixup\n");
  71.             exit(1);
  72.         }
  73.  
  74.         if (havename == 2 && row != -2 && col != -2) {
  75.             strncpy(screen+(row*SCOLS)+col,paramtemps,paramtemplen);
  76.             row = col = -2;
  77.             havename = 0;
  78.         }
  79. };
  80.  
  81. <B>\"[^\"\n]*\"  {
  82.         if (row == -2 || col == -2) {
  83.             fprintf(stderr, "missing coord before %s\n", yytext);
  84.             exit(1);
  85.         }
  86.  
  87.         strncpy(screen+(row*SCOLS)+col, yytext+1, yyleng-2);
  88.         row = col = -2;
  89. };
  90.  
  91. <D>\"[^\"\n]*\"  {
  92.         if (!havename) {
  93.             strncpy(paraml[paramno], yytext+1, yyleng-2);
  94.             paraml[paramno][yyleng-2] = 0;
  95.             havename = 1;
  96.         } else {
  97.             strncpy(paramtemps, yytext+1, yyleng-2);
  98.             paramtemplen = yyleng - 2;
  99.             havename = 2;
  100.         }
  101. };
  102.  
  103. <D>"vis"[0-9A-Za-z_]+  {
  104.         if (row == -2 || col == -2) {
  105.             fprintf(stderr, "missing coord before %s\n", yytext);
  106.             fprintf(stderr, "param no. %d, havename %d\n",
  107.                 paramno, havename);
  108.             exit(1);
  109.         }
  110.         strncpy(paramv[paramno], yytext+3, yyleng-3);
  111.         paramcoord[paramno][0] = row;
  112.         paramcoord[paramno][1] = col;
  113.         row = col = -2;
  114.         BEGIN(E);
  115. };
  116.  
  117. <D>^"NULL".*\n        ;
  118.  
  119. <E>[ \t,]        ;
  120.  
  121. <E>[0-9]+    {
  122.         if (pcount > 3) {
  123.             fprintf(stderr, "extra value\n");
  124.             exit(1);
  125.         }
  126.         paramcoord[paramno][2 + pcount++] = atoi(yytext);
  127. };
  128.  
  129. <E>\n  {
  130.         pcount = 0;
  131.         paramno++;
  132.         if (paramno > MAXPARAM) {
  133.             fprintf(stderr, "too many parameters\n");
  134.             exit(1);
  135.         }
  136.         havename = 0;
  137.         BEGIN(D);
  138. };
  139.  
  140. <B>.*"NULL".*\n     ;
  141.  
  142. <B,D>^"};"  {
  143.         BEGIN(C);
  144. };
  145.  
  146. <C>^[ \t]*"data[RESERVESIZE"[ \+]*[0-9]+"]"[ \t]*"="[ \t]*"getval(".*\n  {
  147.         for (i=0; yytext[i] != '+'; i++) ;
  148.         offset = atoi(yytext+i+1);
  149.         while (yytext[i++] != '"')  ;
  150.         for (j=0; yytext[i] != '"'; i++,j++)
  151.             temps[j] = yytext[i];
  152.         temps[j] = 0;
  153.         for (i=0; i<paramno; i++)
  154.             if (strcmp(temps, paraml[i]) == 0) break;
  155.         if (i < paramno)
  156.             paramcoord[i][5] = offset + reservesize;
  157.         /* else error ? */
  158. };
  159.  
  160.  
  161. <C>^[ \t]*"data["[0-9]+"]"[ \t]*"="[ \t]*"getval(".*\n  {
  162.         for (i=0; yytext[i] != '['; i++) ;
  163.         offset = atoi(yytext+i+1);
  164.         if (offset < reservesize) {
  165.             fprintf(stderr, "offset < RESERVESIZE\n");
  166.             exit(1);
  167.         }
  168.         while (yytext[i++] != '"')  ;
  169.         for (j=0; yytext[i] != '"'; i++,j++)
  170.             temps[j] = yytext[i];
  171.         temps[j] = 0;
  172.         for (i=0; i<paramno; i++)
  173.             if (strcmp(temps, paraml[i]) == 0) break;
  174.         if (i < paramno)
  175.             paramcoord[i][5] = offset;
  176.         /* else error ? */
  177. };
  178.  
  179. <A,C>.|\n    ;
  180.  
  181. %%
  182. main()
  183. {    int i;
  184.     char *s;
  185.     char *malloc();
  186.  
  187.     if ( (screen = malloc(SROWS * SCOLS)) == NULL) {
  188.         fprintf(stderr, "not enough memory\n");
  189.         exit(1);
  190.     }
  191.     s = screen;
  192.     for (i=0; i < SROWS*SCOLS; i++) *s++ = ' ';
  193.  
  194.     BEGIN(A);
  195.     yylex();
  196.  
  197.     printf("\n#define RESERVESIZE %d\n\n", reservesize);
  198.     menudump();
  199.     paramdump();
  200. }
  201.  
  202. menudump()
  203. {    int i, j;
  204.  
  205.     for (i=0; i<paramno; i++) {
  206.         row = paramcoord[i][0];
  207.         col = paramcoord[i][1];
  208.         if (row >= SROWS || col >= SCOLS) {
  209.             fprintf(stderr, "bad coords\n");
  210.             exit(1);
  211.         }
  212.         screen[row*SCOLS + col] = '%';
  213.     }
  214.  
  215.     printf("struct labelinfo %s {\n", labelname);
  216.     printf("#MENU\n");
  217.     for (i=0; i<SROWS; i++) {
  218.         for (j=SCOLS-1; j>=0 && screen[i*SCOLS+j] == ' '; j--)
  219.             screen[i*SCOLS+j] = 0;
  220.         printf("%s\n", screen+i*SCOLS);
  221.     }
  222.     printf("#END\n");
  223.     printf("-1,-1,NULL\n};\n\n");
  224. }
  225.  
  226. paramdump()
  227. {    int i, j, notdone, colmin, rowmin;
  228.     char *fmt;
  229.  
  230.     printf("struct paraminfo %s {\n", paramname);
  231.     printf("/*\nNAME\t\tTYPE\t\tPOS\tMAX\tOFFSET\tMASK\tSHIFT\tADHOC\n");
  232.     printf(" */\n");
  233.  
  234.     notdone = 1;
  235.     while (notdone) {
  236.         colmin = rowmin = 1000;
  237.         for (i = 0; i < paramno; i++)
  238.             if (paramcoord[i][0] < rowmin)
  239.                 rowmin = paramcoord[i][0];
  240.         if (rowmin < 1000) {
  241.             for (i = 0; i < paramno; i++)
  242.             if (paramcoord[i][0] == rowmin) {
  243.                 if (paramcoord[i][1] < colmin) {
  244.                     colmin = paramcoord[i][0];
  245.                     j = i;
  246.                 }
  247.             }
  248.             if (colmin < 1000) {
  249.             fmt = "#O %s\t%s\t%%%%\t%d\t%d";
  250.             if (strlen(paraml[j]) < 5)
  251.                 fmt = "#O %s\t\t%s\t%%%%\t%d\t%d";
  252.             if (paramcoord[j][4])
  253.                 paramcoord[j][5] = reservesize-paramcoord[j][4];
  254.             printf(fmt,
  255.                 paraml[j], paramv[j],
  256.                 paramcoord[j][3],
  257.                 paramcoord[j][5] - reservesize);
  258.             if (paramcoord[j][2] == 1)
  259.                 printf("\t*5");
  260.             printf("\n");
  261.             paramcoord[j][0] = 1000;
  262.             paramcoord[j][1] = 1000;
  263.             }
  264.         } else notdone = 0;
  265.     }
  266.  
  267.     printf("NULL,NULL,-1,-1,-1,-1,visnum,0,0,0,0\n};\n\n");
  268. }
  269.