lbessard@70: /* lbessard@70: * (c) 2003 Mario de Sousa lbessard@70: * lbessard@70: * Offered to the public under the terms of the GNU General Public License lbessard@70: * as published by the Free Software Foundation; either version 2 of the lbessard@70: * License, or (at your option) any later version. lbessard@70: * lbessard@70: * This program is distributed in the hope that it will be useful, but lbessard@70: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@70: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General lbessard@70: * Public License for more details. lbessard@70: * lbessard@70: * This code is made available on the understanding that it will not be lbessard@70: * used in safety-critical situations without a full and competent review. lbessard@70: */ lbessard@70: lbessard@70: /* lbessard@70: * An IEC 61131-3 IL and ST compiler. lbessard@70: * lbessard@70: * Based on the lbessard@70: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) lbessard@70: * lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: /* Decomposes a variable instance name into its constituents, lbessard@70: * example: lbessard@70: * window.points[1].coordinate.x lbessard@70: * lbessard@70: * will succesfully return lbessard@70: * - window lbessard@70: * - points lbessard@70: * - coordinate lbessard@70: * - x lbessard@70: * on succesive calls to decompose_var_instance_name_c::next_part() lbessard@70: */ lbessard@70: class decompose_var_instance_name_c: null_visitor_c { lbessard@70: lbessard@70: private: lbessard@70: symbol_c *variable_name; lbessard@70: symbol_c *next_variable_name; lbessard@70: symbol_c *current_recursive_variable_name; lbessard@70: symbol_c *previously_returned_variable_name; lbessard@70: lbessard@70: public: lbessard@70: decompose_var_instance_name_c(symbol_c *variable_instance_name) { lbessard@70: variable_name = variable_instance_name; lbessard@70: next_variable_name = NULL; lbessard@70: current_recursive_variable_name = NULL; lbessard@70: previously_returned_variable_name = NULL; lbessard@70: } lbessard@70: lbessard@70: public: lbessard@70: symbol_c *next_part(void) { lbessard@70: /* We must always start from the top! lbessard@70: * See note in the structured_variable_c visitor lbessard@70: * to understand why... lbessard@70: */ lbessard@70: symbol_c *res = (symbol_c *)variable_name->accept(*this); lbessard@70: next_variable_name = current_recursive_variable_name; lbessard@70: lbessard@70: if (previously_returned_variable_name == res) lbessard@70: return NULL; lbessard@70: previously_returned_variable_name = res; lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: public: lbessard@70: /*********************/ lbessard@70: /* B 1.4 - Variables */ lbessard@70: /*********************/ lbessard@70: void *visit(symbolic_variable_c *symbol) {return (void *)(symbol->var_name);} lbessard@70: lbessard@70: /********************************************/ lbessard@70: /* B.1.4.1 Directly Represented Variables */ lbessard@70: /********************************************/ lbessard@70: void *visit(direct_variable_c *symbol) {return (void *)symbol;} lbessard@70: lbessard@70: /*************************************/ lbessard@70: /* B.1.4.2 Multi-element Variables */ lbessard@70: /*************************************/ lbessard@70: /* subscripted_variable '[' subscript_list ']' */ lbessard@70: // SYM_REF2(array_variable_c, subscripted_variable, subscript_list) lbessard@70: void *visit(array_variable_c *symbol) { lbessard@70: /* NOTE: the subscripted_variable may itself be a structure!, lbessard@70: * so we must recursevily visit! lbessard@70: */ lbessard@70: return symbol->subscripted_variable->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* record_variable '.' field_selector */ lbessard@70: /* WARNING: input and/or output variables of function blocks lbessard@70: * may be accessed as fields of a tructured variable! lbessard@70: * Code handling a structured_variable_c must take lbessard@70: * this into account! lbessard@70: */ lbessard@70: //SYM_REF2(structured_variable_c, record_variable, field_selector) lbessard@70: void *visit(structured_variable_c *symbol) { lbessard@70: /* NOTE: The following code will not work, as structured_variable_c lbessard@70: * are grouped on the left, and not on the right! lbessard@70: * lbessard@70: * example: window.origin.x lbessard@70: * will result in lbessard@70: * s1 = structured_variable_c("window, "origin"); lbessard@70: * s2 = structured_variable_c(s1, "x"); lbessard@70: * AND NOT lbessard@70: * s1 = structured_variable_c("origin", "x"); lbessard@70: * s2 = structured_variable_c("window", s1); lbessard@70: * lbessard@70: * as the following code assumes!! lbessard@70: * lbessard@70: current_variable_name = symbol->field_selector; lbessard@70: return symbol->record_variable->accept(*this); lbessard@70: */ lbessard@70: lbessard@70: /* The correct code, is therefore more complex... */ lbessard@70: if (next_variable_name == symbol) { lbessard@70: /* NOTE: field_selector is always an identifier_c, lbessard@98: * so we do not have to recursevily visit it again... lbessard@98: * return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!! lbessard@98: */ lbessard@98: return (void *)symbol->field_selector; lbessard@70: } lbessard@70: lbessard@70: current_recursive_variable_name = symbol; lbessard@70: return symbol->record_variable->accept(*this); lbessard@70: } lbessard@70: }; lbessard@70: lbessard@70: lbessard@70: lbessard@70: