mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 mario@181: * msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant mario@181: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * mario@181: * mario@181: * This code is made available on the understanding that it will not be mario@181: * used in safety-critical situations without a full and competent review. mario@181: */ mario@181: mario@181: /* msousa@265: * An IEC 61131-3 compiler. mario@181: * mario@181: * Based on the mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) mario@181: * mario@181: */ mario@181: mario@181: mario@181: /* Decomposes a variable instance name into its constituents, mario@181: * example: mario@181: * window.points[1].coordinate.x mario@181: * mario@181: * will succesfully return mario@181: * - window mario@181: * - points mario@181: * - coordinate mario@181: * - x mario@181: * on succesive calls to decompose_var_instance_name_c::next_part() mario@181: */ mario@181: mario@181: mario@181: mario@181: #include "decompose_var_instance_name.hh" mario@181: mario@181: decompose_var_instance_name_c::decompose_var_instance_name_c(symbol_c *variable_instance_name) { mario@181: variable_name = variable_instance_name; mario@181: next_variable_name = NULL; mario@181: current_recursive_variable_name = NULL; mario@181: previously_returned_variable_name = NULL; mario@181: } mario@181: laurent@238: symbol_c *decompose_var_instance_name_c::next_part(bool increment) { mario@181: /* We must always start from the top! mario@181: * See note in the structured_variable_c visitor mario@181: * to understand why... mario@181: */ mario@181: symbol_c *res = (symbol_c *)variable_name->accept(*this); laurent@238: if (increment) laurent@238: next_variable_name = current_recursive_variable_name; mario@181: mario@181: if (previously_returned_variable_name == res) laurent@238: return NULL; laurent@238: if (increment) laurent@238: previously_returned_variable_name = res; mario@181: return res; mario@181: } mario@181: ccb@202: /*************************/ ccb@202: /* B.1 - Common elements */ ccb@202: /*************************/ ccb@202: /*******************************************/ ccb@202: /* B 1.1 - Letters, digits and identifiers */ ccb@202: /*******************************************/ ccb@202: /* sometimes (e.g. FB calls) the name of the variable is stored directly in an identifier_c object */ ccb@202: void *decompose_var_instance_name_c::visit(identifier_c *symbol) {return (void *)symbol;} ccb@202: mario@181: /*********************/ mario@181: /* B 1.4 - Variables */ mario@181: /*********************/ mario@181: void *decompose_var_instance_name_c::visit(symbolic_variable_c *symbol) {return (void *)(symbol->var_name);} mario@181: mario@181: /********************************************/ mario@181: /* B.1.4.1 Directly Represented Variables */ mario@181: /********************************************/ mario@181: void *decompose_var_instance_name_c::visit(direct_variable_c *symbol) {return (void *)symbol;} mario@181: mario@181: /*************************************/ mario@181: /* B.1.4.2 Multi-element Variables */ mario@181: /*************************************/ mario@181: /* subscripted_variable '[' subscript_list ']' */ mario@181: // SYM_REF2(array_variable_c, subscripted_variable, subscript_list) mario@181: void *decompose_var_instance_name_c::visit(array_variable_c *symbol) { mario@181: /* NOTE: the subscripted_variable may itself be a structure!, mario@181: * so we must recursevily visit! mario@181: */ mario@181: return symbol->subscripted_variable->accept(*this); mario@181: } mario@181: mario@181: /* record_variable '.' field_selector */ mario@181: /* WARNING: input and/or output variables of function blocks mario@181: * may be accessed as fields of a tructured variable! mario@181: * Code handling a structured_variable_c must take mario@181: * this into account! mario@181: */ mario@181: //SYM_REF2(structured_variable_c, record_variable, field_selector) mario@181: void *decompose_var_instance_name_c::visit(structured_variable_c *symbol) { mario@181: /* NOTE: The following code will not work, as structured_variable_c mario@181: * are grouped on the left, and not on the right! mario@181: * mario@181: * example: window.origin.x mario@181: * will result in mario@181: * s1 = structured_variable_c("window, "origin"); mario@181: * s2 = structured_variable_c(s1, "x"); mario@181: * AND NOT mario@181: * s1 = structured_variable_c("origin", "x"); mario@181: * s2 = structured_variable_c("window", s1); mario@181: * mario@181: * as the following code assumes!! mario@181: * mario@181: current_variable_name = symbol->field_selector; mario@181: return symbol->record_variable->accept(*this); mario@181: */ mario@181: mario@181: /* The correct code, is therefore more complex... */ mario@181: if (next_variable_name == symbol) { mario@181: /* NOTE: field_selector is always an identifier_c, mario@181: * so we do not have to recursevily visit it again... mario@181: * return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!! mario@181: */ laurent@238: return (void *)symbol->field_selector; mario@181: } mario@181: mario@181: current_recursive_variable_name = symbol; mario@181: return symbol->record_variable->accept(*this); mario@181: } mario@181: mario@181: mario@181: mario@181: