absyntax_utils/decompose_var_instance_name.cc
changeset 849 1f8885ae539a
parent 826 1e6bf9839ece
equal deleted inserted replaced
823:c95f42f28b69 849:1f8885ae539a
    50 decompose_var_instance_name_c::decompose_var_instance_name_c(symbol_c *variable_instance_name) {
    50 decompose_var_instance_name_c::decompose_var_instance_name_c(symbol_c *variable_instance_name) {
    51   variable_name = variable_instance_name;
    51   variable_name = variable_instance_name;
    52   next_variable_name = NULL;
    52   next_variable_name = NULL;
    53   current_recursive_variable_name = NULL;
    53   current_recursive_variable_name = NULL;
    54   previously_returned_variable_name = NULL;
    54   previously_returned_variable_name = NULL;
       
    55   current_array_subscript_list = NULL;
    55 }
    56 }
    56 
    57 
    57 symbol_c *decompose_var_instance_name_c::next_part(bool increment) {
    58 /* Get the next element in the strcutured variable */
       
    59 symbol_c *decompose_var_instance_name_c::get_next() {
    58   /* We must always start from the top!
    60   /* We must always start from the top!
    59    * See note in the structured_variable_c visitor
    61    * See note in the structured_variable_c visitor
    60    * to understand why...
    62    * to understand why...
    61    */
    63    */
       
    64   current_array_subscript_list = NULL;
    62   symbol_c *res = (symbol_c *)variable_name->accept(*this);
    65   symbol_c *res = (symbol_c *)variable_name->accept(*this);
    63   if (increment)
    66   next_variable_name = current_recursive_variable_name;
    64     next_variable_name = current_recursive_variable_name;
       
    65 
    67 
    66   if (previously_returned_variable_name == res)
    68   if (previously_returned_variable_name == res)
    67 	  return NULL;
    69     return NULL;
    68   if (increment)
    70   
    69     previously_returned_variable_name = res;
    71   previously_returned_variable_name = res;
    70   return res;
    72   return res;
    71 }
    73 }
       
    74 
       
    75 /* If the current element in the structured variable is an array, return its subscript_list, 
       
    76  * otherwise return NULL
       
    77  */
       
    78 list_c *decompose_var_instance_name_c::get_current_arraysubs_list(void) {return current_array_subscript_list;}
    72 
    79 
    73 /*************************/
    80 /*************************/
    74 /* B.1 - Common elements */
    81 /* B.1 - Common elements */
    75 /*************************/
    82 /*************************/
    76 /*******************************************/
    83 /*******************************************/
    93 /* B.1.4.2   Multi-element Variables */
   100 /* B.1.4.2   Multi-element Variables */
    94 /*************************************/
   101 /*************************************/
    95 /*  subscripted_variable '[' subscript_list ']' */
   102 /*  subscripted_variable '[' subscript_list ']' */
    96 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   103 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
    97 void *decompose_var_instance_name_c::visit(array_variable_c *symbol) {
   104 void *decompose_var_instance_name_c::visit(array_variable_c *symbol) {
    98   /* NOTE: the subscripted_variable may itself be a structure!,
   105   if (NULL == symbol->subscript_list) ERROR; // array may not have an empty subscript list!
    99    * so we must recursevily visit!
   106   current_array_subscript_list = dynamic_cast<list_c *>(symbol->subscript_list);
       
   107   if (NULL == current_array_subscript_list) ERROR; // if it does not point to a subscript_list_c, then the abstract syntax tree has been changed, and this code needs to be fixed accordingly!
       
   108   
       
   109   /* NOTE: the subscripted_variable may itself be a structure or an array!, so we must recursevily visit! */
       
   110   /* the next line will call either:
       
   111    *   - visit(structured_variable_c *) or visit(array_variable_c *), if the array variable is itself an element of another array os structure
       
   112    *   - visit(symbolic_variable_c *) if it is a simple array variable
   100    */
   113    */
   101   return symbol->subscripted_variable->accept(*this);
   114   return symbol->subscripted_variable->accept(*this);
   102 }
   115 }
   103 
   116 
   104 /*  record_variable '.' field_selector */
   117 /*  record_variable '.' field_selector */
   129   /* The correct code, is therefore more complex... */
   142   /* The correct code, is therefore more complex... */
   130   if (next_variable_name == symbol) {
   143   if (next_variable_name == symbol) {
   131     return (void *)symbol->field_selector->accept(*this);
   144     return (void *)symbol->field_selector->accept(*this);
   132   }
   145   }
   133 
   146 
       
   147   current_array_subscript_list = NULL;
   134   current_recursive_variable_name = symbol;
   148   current_recursive_variable_name = symbol;
   135   return symbol->record_variable->accept(*this);
   149   /* the next line will call either:
       
   150    *   - visit(structured_variable_c *) or visit(array_variable_c *), if the record variable has more elements to visit
       
   151    *   - visit(symbolic_variable_c *) if it is the last element in the record variable
       
   152    */
       
   153   return symbol->record_variable->accept(*this); 
   136 }
   154 }
   137 
   155 
   138 /********************************/
   156 /********************************/
   139 /* B 2.2 - Operators */
   157 /* B 2.2 - Operators */
   140 /********************************/
   158 /********************************/