home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / coven / coven-utils-1.1.tgz / coven-utils-1.1.tar / utils / coven-module-parser / module_parser.y < prev    next >
Text File  |  2003-01-28  |  8KB  |  317 lines

  1. /*
  2.  * (C) 2001 Clemson University
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7.  
  8. %{
  9.  
  10. #include <module_lang.h>
  11. #include <symbol.h>
  12. #include <stringtbl.h>
  13.  
  14. char module_name[256];
  15.  
  16. extern FILE *yyout;
  17.  
  18. %}
  19.  
  20. %union{
  21.     int i;
  22.     double f;
  23.     char *c;
  24.     sym_ent *s;
  25.     argument_ent *a;
  26. }
  27.  
  28. %token <i> COVEN_MODULE
  29. %token <c> IDENTIFIER
  30. %token <i> CONST
  31. %token <i> INPUT
  32. %token <i> OUTPUT
  33. %token <i> INOUT
  34. %token <i> STATIC
  35. %token <i> BUFFER
  36. %token <i> UNSIGNED
  37. %token <i> ASTERIX
  38. %token <i> COMMA
  39. %token <i> LPAREN
  40. %token <i> RPAREN
  41. %token <i> LSBRACE
  42. %token <c> ARRAY_END_STRING
  43.  
  44. %type <i> mod_static, mod_direction, mod_buffer, mod_unsigned, mod_star
  45.  
  46. %type <f>
  47.  
  48. %type <c> identifier, mod_type, mod_varname, mod_array
  49.  
  50. %type <s> 
  51.  
  52. %type <a> 
  53.  
  54. %start coven_module
  55.  
  56. %%
  57.  
  58. identifier        : IDENTIFIER
  59.               {
  60.                $$ = enter_string($1);
  61.               }
  62.             ;
  63.  
  64. coven_module        : coven_mod_section
  65.             ;
  66.  
  67. /* --------------------------------------------------------------------------*/
  68. /* BEGIN: Coven_Module section */
  69. /* --------------------------------------------------------------------------*/
  70. coven_mod_section    : COVEN_MODULE identifier LPAREN 
  71.               {
  72.                char outstr[512];
  73.                /* save the module name for later in cleanup */
  74.                strcpy(module_name, $2);
  75.  
  76.                if(header_flag) {
  77.                   snprintf(outstr, 512, "module %s {",
  78.                   $2);
  79.                }
  80.                else {
  81.                    snprintf(outstr, 512, "COVEN_MODULE(%s, "
  82.                        "_default_tph, _const_tph, _static_tph)\n{", $2);
  83.                    fprintf(yyout,"/* COVEN MODULE: BEGIN */\n");
  84.                }
  85.                fprintf(yyout, "%s\n", outstr);
  86.               }
  87.               inside_mod RPAREN
  88.               {
  89.               if(header_flag) {
  90.                   fprintf(yyout,"}\n");
  91.               }
  92.               }
  93.             ;
  94.  
  95. inside_mod        : /* empty */
  96.             | mod_decl list_of_mod_decls 
  97.             ;
  98.  
  99. list_of_mod_decls    : /* empty */
  100.             | COMMA mod_decl list_of_mod_decls
  101.             ;
  102.  
  103. mod_decl        : mod_static mod_direction mod_buffer mod_unsigned
  104.               mod_type mod_star mod_varname mod_array
  105.               {
  106.                char outstr[512];
  107.                char ctype[64];
  108.                char tph[64];
  109.                int _static = $1;
  110.                int _direction = $2;
  111.                int _buffer = $3;
  112.                int _unsigned = $4;
  113.                char * _type = $5;
  114.                int _asterix = $6;
  115.                char * _varname = $7;
  116.                char * _array = $8;
  117.                int unknown_type = 0;
  118.                
  119.                if(header_flag) {
  120.                    fprintf(yyout,"\t");
  121.                 if(_static == STATIC) fprintf(yyout,"static ");
  122.                 if(_static != STATIC ||
  123.                    (_static == STATIC && _buffer == BUFFER)) {
  124.                        if(_direction == CONST) fprintf(yyout,"const ");
  125.                     else if(_direction == INPUT) 
  126.                         fprintf(yyout,"input ");
  127.                     else if(_direction == OUTPUT) 
  128.                         fprintf(yyout,"output ");
  129.                     else if(_direction == INOUT) 
  130.                         fprintf(yyout,"inout ");
  131.                 }
  132.                 if(_buffer == BUFFER) fprintf(yyout,"buffer ");
  133.                 else fprintf(yyout,"parameter ");
  134.                 fprintf(yyout,"%s %s,\n", _type, _varname);
  135.                 /* this does a "return" :) */
  136.                 break;
  137.                }
  138.  
  139.                /* imagine there's an else if(!header_flag) here */
  140.                if(_static == STATIC) 
  141.                    strcpy(tph, "_static_tph");
  142.                else
  143.                    strcpy(tph, "_default_tph");
  144.  
  145.                if(strcmp(_type, "int")==0) {
  146.                    if(_unsigned == UNSIGNED) 
  147.                        strcpy(ctype, "UINT");
  148.                    else 
  149.                        strcpy(ctype, "INT");
  150.                }
  151.                else if(strcmp(_type, "char")==0) {
  152.                    if(_unsigned == UNSIGNED)
  153.                        strcpy(ctype, "UCHAR");
  154.                    else
  155.                        strcpy(ctype, "CHAR");
  156.                }
  157.                else if(strcmp(_type, "short")==0) {
  158.                    if(_unsigned == UNSIGNED)
  159.                        strcpy(ctype, "USHORT");
  160.                    else
  161.                        strcpy(ctype, "SHORT");
  162.                }
  163.                else if(strcmp(_type, "string")==0) {
  164.                    strcpy(ctype, "STRING");
  165.                    strcpy(_type, "char *");
  166.                }
  167.                else if(strcmp(_type, "float")==0) 
  168.                    strcpy(ctype, "FLOAT");
  169.                else if(strcmp(_type, "double")==0)
  170.                    strcpy(ctype, "DOUBLE");
  171.                else {
  172.                    strcpy(ctype, _type);
  173.                    unknown_type = 1;
  174.                }
  175.  
  176.                if(_buffer == BUFFER) {
  177.                    char dtype[64], bctype[128];
  178.                    if(unknown_type) {
  179.                        strcpy(bctype, ctype);
  180.                    }
  181.                    else {
  182.                        snprintf(bctype, 128, "COVEN_%s_BUFFER",
  183.                      ctype);
  184.                    }
  185.                    if(_direction == INPUT) strcpy(dtype, "INPUT");
  186.                    else if(_direction == OUTPUT) strcpy(dtype, 
  187.                      "OUTPUT");
  188.                    else if(_direction == INOUT) strcpy(dtype, 
  189.                      "INOUT");
  190.                    
  191.                    if(_direction == OUTPUT) {
  192.                        /* error, if it's an output buffer we 
  193.                     * require a size specified in the array
  194.                     * field! */
  195.                        if(_array == NULL) {
  196.                        yyerror("output buffers require a "
  197.                          "size of the buffer specified "
  198.                      "with the array [ ] functionality.");
  199.                    }
  200.                        snprintf(outstr, 512, "%s_TYPE"
  201.                      " %s = "
  202.                          "COVEN_%s(%s, %s, %s, "
  203.                      "\"%s\");",
  204.                          bctype, _varname, dtype, tph, bctype, 
  205.                      _array, _varname);
  206.                        fprintf(yyout,"  %s\n", outstr);
  207.                    }
  208.                    else {
  209.                        snprintf(outstr, 512, "%s_TYPE"
  210.                      " %s = "
  211.                      "COVEN_%s(%s, %s, \"%s\");",
  212.                      bctype, _varname, dtype, tph,
  213.                      bctype, _varname);
  214.                        fprintf(yyout,"  %s\n", outstr);
  215.                    if(_array != NULL) {
  216.                        snprintf(outstr, 512, "int %s = "
  217.                          "COVEN_COUNT(%s);", _array, _varname);
  218.                            fprintf(yyout,"  %s\n", outstr);
  219.                    }
  220.                    }
  221.                }
  222.                else {
  223.                    if(_direction == CONST) {
  224.                        snprintf(outstr, 512, "%s %s = COVEN_%s_"
  225.                      "CONST(\"%s\");", _type, _varname, ctype,
  226.                      _varname);
  227.                        fprintf(yyout,"  %s\n", outstr);
  228.                    }
  229.                    else if(_direction == INPUT) {
  230.                        snprintf(outstr, 512, "%s %s = COVEN_%s_"
  231.                      "ATTRIB_GET(%s, \"%s\");", _type, 
  232.                      _varname, ctype, tph, _varname);
  233.                        fprintf(yyout,"  %s\n", outstr);
  234.                    }
  235.                    else if(_direction == OUTPUT) {
  236.                        if(_asterix != ASTERIX) {
  237.                        yyerror("output types require pointer "
  238.                          "'*' type.");
  239.                    }
  240.                    else {
  241.                        snprintf(outstr, 512, "%s * %s = COVEN_"
  242.                          "%s_ATTRIB_PUT(%s, \"%s\");",
  243.                          _type, _varname, ctype, tph, _varname);
  244.                        fprintf(yyout,"  %s\n", outstr);
  245.                    }
  246.                    }
  247.                    else if(_direction == INOUT) {
  248.                        if(_asterix != ASTERIX) {
  249.                        yyerror("inout types require pointer "
  250.                          "'*' type.");
  251.                    }
  252.                    else {
  253.                        snprintf(outstr, 512, "%s * %s = COVEN_"
  254.                          "%s_ATTRIB_GET_PTR(%s, \"%s\");",
  255.                      _type, _varname, ctype, tph, _varname);
  256.                        fprintf(yyout,"  %s\n", outstr);
  257.                    }
  258.                    }
  259.                }
  260.               }
  261.             ;
  262.  
  263. mod_static        : /* empty */ { $$ = -1; }
  264.             | STATIC
  265.             ;
  266.  
  267. mod_direction        : CONST
  268.             | INPUT
  269.             | OUTPUT
  270.             | INOUT
  271.             ;
  272.  
  273. mod_buffer        : /* empty */ { $$ = -1; }
  274.             | BUFFER
  275.             ;
  276.  
  277. mod_unsigned        : /* empty */ { $$ = -1; }
  278.             | UNSIGNED;
  279.             ;
  280.  
  281. mod_type        : identifier
  282.             ;
  283.  
  284. mod_star        : /* empty */ { $$ = -1; }
  285.             | ASTERIX;
  286.             ;
  287.  
  288. mod_varname        : identifier
  289.             ;
  290.  
  291. mod_array        : /* empty */ { $$ = NULL; }
  292.             | LSBRACE ARRAY_END_STRING
  293.               {
  294.                $$ = enter_string($2);
  295.               }
  296.             ;
  297.  
  298. %%
  299.  
  300. yywrap()
  301. {
  302.     return 1;
  303. }
  304.  
  305. module_parser_done()
  306. {
  307.     char outstr[512];
  308.     if(!header_flag) {
  309.         fprintf(yyout,"\n}\n/* COVEN MODULE: END */\n");
  310.         fprintf(yyout,"\n/* COVEN INTERFACE: BEGIN */\n");
  311.         snprintf(outstr, 512, "struct COVEN_Interface _module_interface = "
  312.           "{%s};", module_name);
  313.         fprintf(yyout,"%s\n", outstr);
  314.         fprintf(yyout,"/* COVEN INTERFACE: END*/\n");
  315.     }
  316. }
  317.