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) { laurent@382: return (void *)symbol->field_selector->accept(*this); mario@181: } mario@181: mario@181: current_recursive_variable_name = symbol; mario@181: return symbol->record_variable->accept(*this); mario@181: } mario@181: laurent@382: /********************************/ laurent@382: /* B 2.2 - Operators */ laurent@382: /********************************/ laurent@382: void *decompose_var_instance_name_c::visit(LD_operator_c *symbol) {return (void *)&LD_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(S_operator_c *symbol) {return (void *)&S_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(R_operator_c *symbol) {return (void *)&R_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(S1_operator_c *symbol) {return (void *)&S1_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(R1_operator_c *symbol) {return (void *)&R1_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(CLK_operator_c *symbol) {return (void *)&CLK_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(CU_operator_c *symbol) {return (void *)&CU_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(CD_operator_c *symbol) {return (void *)&CD_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(PV_operator_c *symbol) {return (void *)&PV_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(IN_operator_c *symbol) {return (void *)&IN_operator_name;} laurent@382: void *decompose_var_instance_name_c::visit(PT_operator_c *symbol) {return (void *)&PT_operator_name;} laurent@382: laurent@382: identifier_c decompose_var_instance_name_c::LD_operator_name("LD"); laurent@382: identifier_c decompose_var_instance_name_c::S_operator_name("S"); laurent@382: identifier_c decompose_var_instance_name_c::R_operator_name("R"); laurent@382: identifier_c decompose_var_instance_name_c::S1_operator_name("S1"); laurent@382: identifier_c decompose_var_instance_name_c::R1_operator_name("R1"); laurent@382: identifier_c decompose_var_instance_name_c::CLK_operator_name("CLK"); laurent@382: identifier_c decompose_var_instance_name_c::CU_operator_name("CU"); laurent@382: identifier_c decompose_var_instance_name_c::CD_operator_name("CD"); laurent@382: identifier_c decompose_var_instance_name_c::PV_operator_name("PV"); laurent@382: identifier_c decompose_var_instance_name_c::IN_operator_name("IN"); laurent@382: identifier_c decompose_var_instance_name_c::PT_operator_name("PT"); mario@181: mario@181: