msousa@511: /* msousa@511: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@511: * msousa@511: * Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt) msousa@511: * msousa@511: * This program is free software: you can redistribute it and/or modify msousa@511: * it under the terms of the GNU General Public License as published by msousa@511: * the Free Software Foundation, either version 3 of the License, or msousa@511: * (at your option) any later version. msousa@511: * msousa@511: * This program is distributed in the hope that it will be useful, msousa@511: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@511: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@511: * GNU General Public License for more details. msousa@511: * msousa@511: * You should have received a copy of the GNU General Public License msousa@511: * along with this program. If not, see . msousa@511: * msousa@511: * msousa@511: * This code is made available on the understanding that it will not be msousa@511: * used in safety-critical situations without a full and competent review. msousa@511: */ msousa@511: msousa@511: /* msousa@511: * An IEC 61131-3 compiler. msousa@511: * msousa@511: * Based on the msousa@511: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) msousa@511: * msousa@511: */ msousa@511: msousa@511: /* msousa@511: * A small helper visitor class, that will msousa@511: * return the name (tokn_c *) of a variable, as it will msousa@511: * appear in the variable declaration. msousa@511: */ msousa@511: msousa@511: /* For ex.: msousa@511: * VAR msousa@511: * A : int; msousa@511: * B : ARRAY [1..9] of int; msousa@511: * C : some_struct_t; msousa@511: * END_VAR msousa@511: * msousa@511: * A := 56; msousa@511: * B[8] := 99; msousa@511: * C.e := 77; msousa@511: * msousa@511: * Calling this visitor class with symbolic_variable_c instance referencing 'A' in msousa@511: * the line 'A := 56', will return the string "A". msousa@511: * msousa@511: * Calling this visitor class with array_variable_c instance referencing 'B[8]' in msousa@511: * the line 'B[8] := 99', will return the string "B". msousa@511: * msousa@511: * Calling this visitor class with array_variable_c instance referencing 'C.e' in msousa@511: * the line 'C.e := 77', will return the string "C". msousa@511: */ msousa@511: msousa@511: msousa@511: #include "absyntax_utils.hh" msousa@511: msousa@511: msousa@511: msousa@511: msousa@511: msousa@511: msousa@511: get_var_name_c *get_var_name_c::singleton_instance_ = NULL; msousa@511: msousa@511: msousa@511: msousa@511: msousa@511: token_c *get_var_name_c::get_name(symbol_c *symbol) { msousa@511: if (NULL == singleton_instance_) singleton_instance_ = new get_var_name_c(); msousa@511: if (NULL == singleton_instance_) ERROR; msousa@511: msousa@511: return (token_c *)(symbol->accept(*singleton_instance_)); msousa@511: } msousa@511: msousa@511: msousa@511: /*************************/ msousa@511: /* B.1 - Common elements */ msousa@511: /*************************/ msousa@511: /*******************************************/ msousa@511: /* B 1.1 - Letters, digits and identifiers */ msousa@511: /*******************************************/ msousa@511: // SYM_TOKEN(identifier_c) msousa@511: void *get_var_name_c::visit(identifier_c *symbol) {return (void *)symbol;} msousa@511: msousa@511: /*********************/ msousa@511: /* B 1.4 - Variables */ msousa@511: /*********************/ msousa@511: // SYM_REF2(symbolic_variable_c, var_name, unused) msousa@511: void *get_var_name_c::visit(symbolic_variable_c *symbol) {return symbol->var_name->accept(*this);} msousa@511: msousa@511: /********************************************/ msousa@511: /* B.1.4.1 Directly Represented Variables */ msousa@511: /********************************************/ msousa@511: // SYM_TOKEN(direct_variable_c) msousa@511: msousa@511: /*************************************/ msousa@511: /* B.1.4.2 Multi-element Variables */ msousa@511: /*************************************/ msousa@511: /* subscripted_variable '[' subscript_list ']' */ msousa@511: // SYM_REF2(array_variable_c, subscripted_variable, subscript_list) msousa@511: void *get_var_name_c::visit(array_variable_c *symbol) {return symbol->subscripted_variable->accept(*this);} msousa@511: msousa@511: /* subscript_list ',' subscript */ msousa@511: // SYM_LIST(subscript_list_c) msousa@511: msousa@511: /* record_variable '.' field_selector */ msousa@511: /* WARNING: input and/or output variables of function blocks msousa@511: * may be accessed as fields of a tructured variable! msousa@511: * Code handling a structured_variable_c must take msousa@511: * this into account! msousa@511: */ msousa@511: // SYM_REF2(structured_variable_c, record_variable, field_selector) msousa@511: void *get_var_name_c::visit(structured_variable_c *symbol) {return symbol->record_variable->accept(*this);} msousa@511: msousa@511: msousa@511: