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: 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: mjsousa@889: /* For ex.: mjsousa@889: * VAR mjsousa@889: * A : int; mjsousa@889: * B : ARRAY [1..9] of int; mjsousa@889: * C : some_struct_t; mjsousa@889: * END_VAR mjsousa@889: * mjsousa@889: * A := 56; mjsousa@889: * B[8] := 99; mjsousa@889: * C.e := 77; mjsousa@889: * mjsousa@889: * Calling this method with symbolic_variable_c instance referencing 'A' in mjsousa@889: * the line 'A := 56', will return the string "A". mjsousa@889: * mjsousa@889: * Calling this method with array_variable_c instance referencing 'B[8]' in mjsousa@889: * the line 'B[8] := 99', will return the string "B". mjsousa@889: * mjsousa@889: * Calling this method with array_variable_c instance referencing 'C.e' in mjsousa@889: * the line 'C.e := 77', will return the string "C". mjsousa@889: */ 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: mjsousa@889: /* Return the last field of a structured variable... mjsousa@889: * mjsousa@889: * A := 56; --> returns A mjsousa@889: * B[8] := 99; --> returns B mjsousa@889: * C.e := 77; --> returns e !!! mjsousa@889: */ mjsousa@889: symbol_c *get_var_name_c::get_last_field(symbol_c *symbol) { mjsousa@889: if (NULL == singleton_instance_) singleton_instance_ = new get_var_name_c(); mjsousa@889: if (NULL == singleton_instance_) ERROR; mjsousa@889: mjsousa@889: singleton_instance_->last_field = NULL; mjsousa@889: symbol_c *res = (symbol_c*)(symbol->accept(*singleton_instance_)); mjsousa@889: return (NULL != singleton_instance_->last_field)? singleton_instance_->last_field : res; mjsousa@889: } mjsousa@889: mjsousa@889: mjsousa@889: mjsousa@889: 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) mjsousa@889: void *get_var_name_c::visit(structured_variable_c *symbol) { mjsousa@889: void *res = symbol->record_variable->accept(*this); mjsousa@889: last_field = symbol->field_selector; mjsousa@889: return res; mjsousa@889: } msousa@511: msousa@511: msousa@511: