absyntax_utils/get_var_name.cc
changeset 511 b22ae67d8003
child 889 5f380b99e95e
equal deleted inserted replaced
510:9317e04c1dde 511:b22ae67d8003
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *
       
     6  *  This program is free software: you can redistribute it and/or modify
       
     7  *  it under the terms of the GNU General Public License as published by
       
     8  *  the Free Software Foundation, either version 3 of the License, or
       
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  *
       
    20  * This code is made available on the understanding that it will not be
       
    21  * used in safety-critical situations without a full and competent review.
       
    22  */
       
    23 
       
    24 /*
       
    25  * An IEC 61131-3 compiler.
       
    26  *
       
    27  * Based on the
       
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    29  *
       
    30  */
       
    31 
       
    32 /*
       
    33  *  A small helper visitor class, that will   
       
    34  *  return the name (tokn_c *) of a variable, as it will
       
    35  *  appear in the variable declaration.
       
    36  */
       
    37 
       
    38 /*  For ex.:
       
    39  *       VAR
       
    40  *          A : int;
       
    41  *          B : ARRAY [1..9] of int;
       
    42  *          C : some_struct_t;
       
    43  *       END_VAR
       
    44  *
       
    45  *          A    := 56;
       
    46  *          B[8] := 99;
       
    47  *          C.e  := 77;
       
    48  *
       
    49  *       Calling this visitor class with symbolic_variable_c instance referencing 'A' in
       
    50  *       the line 'A := 56', will return the string "A".
       
    51  *
       
    52  *       Calling this visitor class with array_variable_c instance referencing 'B[8]' in
       
    53  *       the line 'B[8] := 99', will return the string "B".
       
    54  *
       
    55  *       Calling this visitor class with array_variable_c instance referencing 'C.e' in
       
    56  *       the line 'C.e := 77', will return the string "C".
       
    57  */
       
    58 
       
    59 
       
    60 #include "absyntax_utils.hh"
       
    61 
       
    62 
       
    63 
       
    64    
       
    65     
       
    66 
       
    67 get_var_name_c *get_var_name_c::singleton_instance_ = NULL;
       
    68 
       
    69 
       
    70 
       
    71 
       
    72 token_c *get_var_name_c::get_name(symbol_c *symbol) {
       
    73   if (NULL == singleton_instance_) singleton_instance_ = new get_var_name_c(); 
       
    74   if (NULL == singleton_instance_) ERROR; 
       
    75   
       
    76   return (token_c *)(symbol->accept(*singleton_instance_));
       
    77 }
       
    78 
       
    79 
       
    80 /*************************/
       
    81 /* B.1 - Common elements */
       
    82 /*************************/
       
    83 /*******************************************/
       
    84 /* B 1.1 - Letters, digits and identifiers */
       
    85 /*******************************************/
       
    86 // SYM_TOKEN(identifier_c)
       
    87 void *get_var_name_c::visit(identifier_c *symbol) {return (void *)symbol;}
       
    88 
       
    89 /*********************/
       
    90 /* B 1.4 - Variables */
       
    91 /*********************/
       
    92 // SYM_REF2(symbolic_variable_c, var_name, unused)
       
    93 void *get_var_name_c::visit(symbolic_variable_c *symbol) {return symbol->var_name->accept(*this);}
       
    94 
       
    95 /********************************************/
       
    96 /* B.1.4.1   Directly Represented Variables */
       
    97 /********************************************/
       
    98 // SYM_TOKEN(direct_variable_c)
       
    99 
       
   100 /*************************************/
       
   101 /* B.1.4.2   Multi-element Variables */
       
   102 /*************************************/
       
   103 /*  subscripted_variable '[' subscript_list ']' */
       
   104 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
       
   105 void *get_var_name_c::visit(array_variable_c *symbol) {return symbol->subscripted_variable->accept(*this);}
       
   106 
       
   107 /* subscript_list ',' subscript */
       
   108 // SYM_LIST(subscript_list_c)
       
   109 
       
   110 /*  record_variable '.' field_selector */
       
   111 /*  WARNING: input and/or output variables of function blocks
       
   112  *           may be accessed as fields of a tructured variable!
       
   113  *           Code handling a structured_variable_c must take
       
   114  *           this into account!
       
   115  */
       
   116 // SYM_REF2(structured_variable_c, record_variable, field_selector)
       
   117 void *get_var_name_c::visit(structured_variable_c *symbol) {return symbol->record_variable->accept(*this);}
       
   118 
       
   119 
       
   120