laurent@347: /* laurent@347: * matiec - a compiler for the programming languages defined in IEC 61131-3 laurent@347: * laurent@347: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) laurent@347: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant laurent@347: * laurent@347: * This program is free software: you can redistribute it and/or modify laurent@347: * it under the terms of the GNU General Public License as published by laurent@347: * the Free Software Foundation, either version 3 of the License, or laurent@347: * (at your option) any later version. laurent@347: * laurent@347: * This program is distributed in the hope that it will be useful, laurent@347: * but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@347: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the laurent@347: * GNU General Public License for more details. laurent@347: * laurent@347: * You should have received a copy of the GNU General Public License laurent@347: * along with this program. If not, see . laurent@347: * laurent@347: * laurent@347: * This code is made available on the understanding that it will not be laurent@347: * used in safety-critical situations without a full and competent review. laurent@347: */ laurent@347: laurent@347: /* laurent@347: * An IEC 61131-3 compiler. laurent@347: * laurent@347: * Based on the laurent@347: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) laurent@347: * laurent@347: */ laurent@347: laurent@347: laurent@347: /* laurent@347: * Case element iterator. laurent@347: * Iterate through the elements of a case list. laurent@347: * laurent@347: * This is part of the 4th stage that generates laurent@347: * a c++ source program equivalent to the IL and ST laurent@347: * code. laurent@347: */ laurent@347: laurent@347: /* Given a case_list_c and a type of element, iterate through laurent@347: * each element, returning the symbol of each element if from laurent@347: * the good type...case_element_iterator_c laurent@347: */ laurent@347: laurent@347: laurent@347: laurent@347: laurent@347: #include "case_element_iterator.hh" msousa@596: #include "../main.hh" // required for ERROR() and ERROR_MSG() macros. laurent@347: laurent@347: laurent@347: //#define DEBUG laurent@347: #ifdef DEBUG laurent@347: #define TRACE(classname) printf("\n____%s____\n",classname); laurent@347: #else laurent@347: #define TRACE(classname) laurent@347: #endif laurent@347: laurent@347: laurent@347: laurent@347: laurent@347: void* case_element_iterator_c::handle_case_element(symbol_c *case_element) { laurent@347: if (current_case_element == case_element) { laurent@347: current_case_element = NULL; laurent@347: } laurent@347: else if (current_case_element == NULL) { laurent@347: current_case_element = case_element; laurent@347: return case_element; laurent@347: } laurent@347: laurent@347: /* Not found! */ laurent@347: return NULL; laurent@347: } laurent@347: laurent@347: void* case_element_iterator_c::iterate_list(list_c *list) { laurent@347: void *res; laurent@347: for (int i = 0; i < list->n; i++) { msousa@1041: res = list->get_element(i)->accept(*this); laurent@347: if (res != NULL) laurent@347: return res; laurent@347: } laurent@347: return NULL; laurent@347: } laurent@347: laurent@347: /* start off at the first case element once again... */ laurent@347: void case_element_iterator_c::reset(void) { laurent@347: current_case_element = NULL; laurent@347: } laurent@347: laurent@347: laurent@377: /* initialize the iterator object. laurent@377: * We must be given a reference to a case_list_c that will be analyzed... laurent@347: */ laurent@347: case_element_iterator_c::case_element_iterator_c(symbol_c *list, case_element_t element_type) { laurent@347: /* do some consistency check... */ laurent@347: case_list_c* case_list = dynamic_cast(list); laurent@347: laurent@347: if (NULL == case_list) ERROR; laurent@347: laurent@377: /* OK. Now initialize this object... */ laurent@347: this->case_list = list; laurent@347: this->wanted_element_type = element_type; laurent@347: reset(); laurent@347: } laurent@347: laurent@347: laurent@347: laurent@347: /* Skip to the next case element of type chosen. After object creation, laurent@347: * the object references on case element _before_ the first, so laurent@347: * this function must be called once to get the object to laurent@347: * reference the first element... laurent@347: * laurent@347: * Returns the case element's symbol! laurent@347: */ laurent@347: symbol_c *case_element_iterator_c::next(void) { laurent@347: void *res = case_list->accept(*this); laurent@347: if (res == NULL) laurent@347: return NULL; laurent@347: laurent@347: return current_case_element; laurent@347: } laurent@347: laurent@347: /******************************/ laurent@347: /* B 1.2.1 - Numeric Literals */ laurent@347: /******************************/ laurent@347: void *case_element_iterator_c::visit(integer_c *symbol) { laurent@347: switch (wanted_element_type) { laurent@347: case element_single: laurent@347: return handle_case_element(symbol); laurent@347: break; laurent@347: default: laurent@347: break; laurent@347: } laurent@347: return NULL; laurent@347: } laurent@347: laurent@347: void *case_element_iterator_c::visit(neg_integer_c *symbol) { laurent@347: switch (wanted_element_type) { laurent@347: case element_single: laurent@347: return handle_case_element(symbol); laurent@347: break; laurent@347: default: laurent@347: break; laurent@347: } laurent@347: return NULL; laurent@347: } laurent@347: laurent@347: /********************************/ laurent@347: /* B 1.3.3 - Derived data types */ laurent@347: /********************************/ laurent@347: /* signed_integer DOTDOT signed_integer */ laurent@347: void *case_element_iterator_c::visit(subrange_c *symbol) { laurent@347: switch (wanted_element_type) { laurent@347: case element_subrange: laurent@347: return handle_case_element(symbol); laurent@347: break; laurent@347: default: laurent@347: break; laurent@347: } laurent@347: return NULL; laurent@347: } laurent@347: laurent@347: /* enumerated_type_name '#' identifier */ laurent@347: void *case_element_iterator_c::visit(enumerated_value_c *symbol) { laurent@347: switch (wanted_element_type) { laurent@347: case element_single: laurent@347: return handle_case_element(symbol); laurent@347: break; laurent@347: default: laurent@347: break; laurent@347: } laurent@347: return NULL; laurent@347: } laurent@347: laurent@347: /********************************/ laurent@347: /* B 3.2.3 Selection Statements */ laurent@347: /********************************/ laurent@347: void *case_element_iterator_c::visit(case_list_c *symbol) { laurent@347: return iterate_list(symbol); laurent@347: } laurent@347: