absyntax_utils/spec_init_separator.cc
changeset 181 38d6eb056260
child 265 4d222f46f8cc
equal deleted inserted replaced
180:64334c5a00b1 181:38d6eb056260
       
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 /*
       
    26  * Seperation of type specification and default value constructs
       
    27  * (for e.g. simple_spec_init_c), into a type specificiation part,
       
    28  * and a default value part.
       
    29  */
       
    30 
       
    31 #include "spec_init_separator.hh"
       
    32 
       
    33 //#define DEBUG
       
    34 #ifdef DEBUG
       
    35 #define TRACE(classname) printf("\n____%s____\n",classname);
       
    36 #else
       
    37 #define TRACE(classname)
       
    38 #endif
       
    39 
       
    40 #define ERROR error_exit(__FILE__,__LINE__)
       
    41 /* function defined in main.cc */
       
    42 extern void error_exit(const char *file_name, int line_no);
       
    43 
       
    44 
       
    45 spec_init_sperator_c *spec_init_sperator_c::get_class_instance(void) {
       
    46   if (NULL == class_instance)
       
    47     class_instance = new spec_init_sperator_c();
       
    48 
       
    49   if (NULL == class_instance)
       
    50     ERROR;
       
    51 
       
    52   return class_instance;
       
    53 }
       
    54 
       
    55  /* the only two public functions... */
       
    56 symbol_c *spec_init_sperator_c::get_spec(symbol_c *spec_init) {
       
    57    search_what = search_spec;
       
    58    return (symbol_c *)spec_init->accept(*get_class_instance());
       
    59 }
       
    60 
       
    61 symbol_c *spec_init_sperator_c::get_init(symbol_c *spec_init) {
       
    62    search_what = search_init;
       
    63    return (symbol_c *)spec_init->accept(*get_class_instance());
       
    64 }
       
    65 
       
    66 /*******************************************/
       
    67 /* B 1.1 - Letters, digits and identifiers */
       
    68 /*******************************************/
       
    69 // SYM_TOKEN(identifier_c)
       
    70 void *spec_init_sperator_c::visit(identifier_c *symbol) {
       
    71   TRACE("spec_init_sperator_c::identifier_c");
       
    72   switch (search_what) {
       
    73     /* if we ever get called sith a simple identifier_c, then it must be a previously declared type... */
       
    74     case search_spec: return symbol;
       
    75     case search_init: return NULL;
       
    76   }
       
    77   ERROR; /* should never occur */
       
    78   return NULL;
       
    79 }
       
    80 
       
    81 
       
    82 /********************************/
       
    83 /* B 1.3.3 - Derived data types */
       
    84 /********************************/
       
    85 
       
    86 /* simple_specification ASSIGN constant */
       
    87 void *spec_init_sperator_c::visit(simple_spec_init_c *symbol) {
       
    88   TRACE("spec_init_sperator_c::simple_spec_init_c");
       
    89   switch (search_what) {
       
    90     case search_spec: return symbol->simple_specification;
       
    91     case search_init: return symbol->constant;
       
    92   }
       
    93   ERROR; /* should never occur */
       
    94   return NULL;
       
    95 }
       
    96 
       
    97 /* subrange_specification ASSIGN signed_integer */
       
    98 void *spec_init_sperator_c::visit(subrange_spec_init_c *symbol) {
       
    99   TRACE("spec_init_sperator_c::subrange_spec_init_c");
       
   100   switch (search_what) {
       
   101     case search_spec: return symbol->subrange_specification->accept(*this);
       
   102     case search_init: return symbol->signed_integer;
       
   103   }
       
   104   ERROR; /* should never occur */
       
   105   return NULL;
       
   106 }
       
   107 
       
   108 /*  integer_type_name '(' subrange')' */
       
   109 void *spec_init_sperator_c::visit(subrange_specification_c *symbol) {
       
   110   TRACE("spec_init_sperator_c::subrange_specification_c");
       
   111   switch (search_what) {
       
   112     case search_spec: return symbol->integer_type_name;
       
   113     case search_init: return NULL; /* should never occur */
       
   114   }
       
   115   ERROR; /* should never occur */
       
   116   return NULL;
       
   117 }
       
   118 
       
   119 /* array_specification [ASSIGN array_initialization} */
       
   120 /* array_initialization may be NULL ! */
       
   121 void *spec_init_sperator_c::visit(array_spec_init_c *symbol) {
       
   122   TRACE("spec_init_sperator_c::array_spec_init_c");
       
   123   switch (search_what) {
       
   124     case search_spec: return symbol->array_specification;
       
   125     case search_init: return symbol->array_initialization;
       
   126   }
       
   127   return NULL;
       
   128 }
       
   129 
       
   130 /* enumerated_specification ASSIGN enumerated_value */
       
   131 void *spec_init_sperator_c::visit(enumerated_spec_init_c *symbol) {
       
   132   TRACE("spec_init_sperator_c::enumerated_spec_init_c");
       
   133   switch (search_what) {
       
   134     case search_spec: return symbol->enumerated_specification;
       
   135     case search_init: return symbol->enumerated_value;
       
   136   }
       
   137   ERROR; /* should never occur */
       
   138   return NULL;
       
   139 }
       
   140 
       
   141 /* structure_type_name ASSIGN structure_initialization */
       
   142 /* structure_initialization may be NULL ! */
       
   143 //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
       
   144 void *spec_init_sperator_c::visit(initialized_structure_c *symbol) {
       
   145   TRACE("spec_init_sperator_c::initialized_structure_c");
       
   146   switch (search_what) {
       
   147     case search_spec: return symbol->structure_type_name;
       
   148     case search_init: return symbol->structure_initialization;
       
   149   }
       
   150   ERROR; /* should never occur */
       
   151   return NULL;
       
   152 }
       
   153 
       
   154 
       
   155 /******************************************/
       
   156 /* B 1.4.3 - Declaration & Initialisation */
       
   157 /******************************************/
       
   158 
       
   159 /* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
       
   160 /* structure_initialization -> may be NULL ! */
       
   161 void *spec_init_sperator_c::visit(fb_name_decl_c *symbol) {
       
   162   TRACE("spec_init_sperator_c::fb_name_decl_c");
       
   163   switch (search_what) {
       
   164     case search_spec: return symbol->function_block_type_name;
       
   165     case search_init: return symbol->structure_initialization;
       
   166   }
       
   167   ERROR; /* should never occur */
       
   168   return NULL;
       
   169 }
       
   170 
       
   171 spec_init_sperator_c *spec_init_sperator_c ::class_instance = NULL;
       
   172 spec_init_sperator_c::search_what_t spec_init_sperator_c::search_what;