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