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;
mjsousa@826: current_array_subscript_list = NULL;
mario@181: }
mario@181:
mjsousa@826: /* Get the next element in the strcutured variable */
mjsousa@826: symbol_c *decompose_var_instance_name_c::get_next() {
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: */
mjsousa@826: current_array_subscript_list = NULL;
mario@181: symbol_c *res = (symbol_c *)variable_name->accept(*this);
mjsousa@825: next_variable_name = current_recursive_variable_name;
mario@181:
mario@181: if (previously_returned_variable_name == res)
mjsousa@825: return NULL;
mjsousa@825:
mjsousa@825: previously_returned_variable_name = res;
mario@181: return res;
mario@181: }
mario@181:
mjsousa@826: /* If the current element in the structured variable is an array, return its subscript_list,
mjsousa@826: * otherwise return NULL
mjsousa@826: */
mjsousa@826: list_c *decompose_var_instance_name_c::get_current_arraysubs_list(void) {return current_array_subscript_list;}
mjsousa@826:
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) {
mjsousa@826: if (NULL == symbol->subscript_list) ERROR; // array may not have an empty subscript list!
mjsousa@826: current_array_subscript_list = dynamic_cast(symbol->subscript_list);
mjsousa@826: if (NULL == current_array_subscript_list) ERROR; // if it does not point to a subscript_list_c, then the abstract syntax tree has been changed, and this code needs to be fixed accordingly!
mjsousa@826:
mjsousa@826: /* NOTE: the subscripted_variable may itself be a structure or an array!, so we must recursevily visit! */
mjsousa@826: /* the next line will call either:
mjsousa@826: * - visit(structured_variable_c *) or visit(array_variable_c *), if the array variable is itself an element of another array os structure
mjsousa@826: * - visit(symbolic_variable_c *) if it is a simple array variable
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:
mjsousa@826: current_array_subscript_list = NULL;
mario@181: current_recursive_variable_name = symbol;
mjsousa@826: /* the next line will call either:
mjsousa@826: * - visit(structured_variable_c *) or visit(array_variable_c *), if the record variable has more elements to visit
mjsousa@826: * - visit(symbolic_variable_c *) if it is the last element in the record variable
mjsousa@826: */
mjsousa@826: 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: