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: * Seperation of type specification and default value constructs mario@181: * (for e.g. simple_spec_init_c), into a type specificiation part, mario@181: * and a default value part. mario@181: */ mario@181: mario@181: #include "spec_init_separator.hh" msousa@596: #include "../main.hh" // required for ERROR() and ERROR_MSG() macros. msousa@596: mario@181: mario@181: //#define DEBUG mario@181: #ifdef DEBUG mario@181: #define TRACE(classname) printf("\n____%s____\n",classname); mario@181: #else mario@181: #define TRACE(classname) mario@181: #endif mario@181: mario@181: mario@181: mario@181: spec_init_sperator_c *spec_init_sperator_c::get_class_instance(void) { mario@181: if (NULL == class_instance) mario@181: class_instance = new spec_init_sperator_c(); mario@181: mario@181: if (NULL == class_instance) mario@181: ERROR; mario@181: mario@181: return class_instance; mario@181: } mario@181: mario@181: /* the only two public functions... */ mario@181: symbol_c *spec_init_sperator_c::get_spec(symbol_c *spec_init) { mario@181: search_what = search_spec; mario@181: return (symbol_c *)spec_init->accept(*get_class_instance()); mario@181: } mario@181: mario@181: symbol_c *spec_init_sperator_c::get_init(symbol_c *spec_init) { mario@181: search_what = search_init; mario@181: return (symbol_c *)spec_init->accept(*get_class_instance()); mario@181: } mario@181: mario@181: /*******************************************/ mario@181: /* B 1.1 - Letters, digits and identifiers */ mario@181: /*******************************************/ mario@181: // SYM_TOKEN(identifier_c) mjsousa@945: /* visitor for identifier_c is necessary because spec_init_sperator_c will be called to analyse PROGRAM identfiers, mjsousa@945: * which are still transformed into identfier_c, instead of a derived_datatype_identifier_c mjsousa@945: */ mjsousa@945: void *spec_init_sperator_c::visit( identifier_c *symbol) { mjsousa@945: TRACE("spec_init_sperator_c::identifier_c"); mjsousa@945: switch (search_what) { mjsousa@945: /* if we ever get called sith a simple identifier_c, then it must be a previously declared type... */ mjsousa@945: case search_spec: return symbol; mjsousa@945: case search_init: return NULL; mjsousa@945: } mjsousa@945: ERROR; /* should never occur */ mjsousa@945: return NULL; mjsousa@945: } mjsousa@945: mjsousa@945: mjsousa@945: void *spec_init_sperator_c::visit(derived_datatype_identifier_c *symbol) { mario@181: TRACE("spec_init_sperator_c::identifier_c"); mario@181: switch (search_what) { mario@181: /* if we ever get called sith a simple identifier_c, then it must be a previously declared type... */ mario@181: case search_spec: return symbol; mario@181: case search_init: return NULL; mario@181: } mario@181: ERROR; /* should never occur */ mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /********************************/ mario@181: /* B 1.3.3 - Derived data types */ mario@181: /********************************/ mario@181: mario@181: /* simple_specification ASSIGN constant */ mario@181: void *spec_init_sperator_c::visit(simple_spec_init_c *symbol) { mario@181: TRACE("spec_init_sperator_c::simple_spec_init_c"); mario@181: switch (search_what) { mario@181: case search_spec: return symbol->simple_specification; mario@181: case search_init: return symbol->constant; mario@181: } mario@181: ERROR; /* should never occur */ mario@181: return NULL; mario@181: } mario@181: mario@181: /* subrange_specification ASSIGN signed_integer */ mario@181: void *spec_init_sperator_c::visit(subrange_spec_init_c *symbol) { mario@181: TRACE("spec_init_sperator_c::subrange_spec_init_c"); mario@181: switch (search_what) { mario@181: case search_spec: return symbol->subrange_specification->accept(*this); mario@181: case search_init: return symbol->signed_integer; mario@181: } mario@181: ERROR; /* should never occur */ mario@181: return NULL; mario@181: } mario@181: mario@181: /* integer_type_name '(' subrange')' */ mario@181: void *spec_init_sperator_c::visit(subrange_specification_c *symbol) { mario@181: TRACE("spec_init_sperator_c::subrange_specification_c"); mario@181: switch (search_what) { mario@181: case search_spec: return symbol->integer_type_name; mario@181: case search_init: return NULL; /* should never occur */ mario@181: } mario@181: ERROR; /* should never occur */ mario@181: return NULL; mario@181: } mario@181: mario@181: /* array_specification [ASSIGN array_initialization} */ mario@181: /* array_initialization may be NULL ! */ mario@181: void *spec_init_sperator_c::visit(array_spec_init_c *symbol) { mario@181: TRACE("spec_init_sperator_c::array_spec_init_c"); mario@181: switch (search_what) { mario@181: case search_spec: return symbol->array_specification; mario@181: case search_init: return symbol->array_initialization; mario@181: } mario@181: return NULL; mario@181: } mario@181: mjsousa@912: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ mjsousa@912: /* NOTE: this symbol may be used directly in implictly defined variables in VAR_TEMP!! mjsousa@912: * so we must consider it here too! mjsousa@912: */ mjsousa@912: void *spec_init_sperator_c::visit(array_specification_c *symbol) { mjsousa@912: TRACE("spec_init_sperator_c::array_spec_init_c"); mjsousa@912: switch (search_what) { mjsousa@912: case search_spec: return symbol; mjsousa@912: case search_init: return NULL; mjsousa@912: } mjsousa@912: return NULL; mjsousa@912: } mjsousa@912: mario@181: /* enumerated_specification ASSIGN enumerated_value */ mario@181: void *spec_init_sperator_c::visit(enumerated_spec_init_c *symbol) { mario@181: TRACE("spec_init_sperator_c::enumerated_spec_init_c"); mario@181: switch (search_what) { mario@181: case search_spec: return symbol->enumerated_specification; mario@181: case search_init: return symbol->enumerated_value; mario@181: } mario@181: ERROR; /* should never occur */ mario@181: return NULL; mario@181: } mario@181: mario@181: /* structure_type_name ASSIGN structure_initialization */ mario@181: /* structure_initialization may be NULL ! */ mario@181: //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) mario@181: void *spec_init_sperator_c::visit(initialized_structure_c *symbol) { mario@181: TRACE("spec_init_sperator_c::initialized_structure_c"); mario@181: switch (search_what) { mario@181: case search_spec: return symbol->structure_type_name; mario@181: case search_init: return symbol->structure_initialization; mario@181: } mario@181: ERROR; /* should never occur */ mario@181: return NULL; mario@181: } mario@181: msousa@810: /* function_block_type_name ASSIGN structure_initialization */ msousa@810: /* structure_initialization -> may be NULL ! */ msousa@810: //SYM_REF2(fb_spec_init_c, function_block_type_name, structure_initialization) msousa@810: void *spec_init_sperator_c::visit(fb_spec_init_c *symbol) { msousa@810: TRACE("spec_init_sperator_c::fb_spec_init_c"); msousa@810: switch (search_what) { msousa@810: case search_spec: return symbol->function_block_type_name; msousa@810: case search_init: return symbol->structure_initialization; msousa@810: } msousa@810: ERROR; /* should never occur */ msousa@810: return NULL; msousa@810: } mario@181: mjsousa@909: mjsousa@909: /* ref_spec: REF_TO (non_generic_type_name | function_block_type_name) */ mjsousa@909: // SYM_REF1(ref_spec_c, type_name) mjsousa@909: void *spec_init_sperator_c::visit(ref_spec_c *symbol) { mjsousa@909: TRACE("spec_init_sperator_c::ref_spec_c"); mjsousa@909: /* This visitor should never really be called, but there is no harm in including it... */ mjsousa@909: switch (search_what) { mjsousa@909: case search_spec: return symbol; mjsousa@909: case search_init: return NULL; /* should never occur */ mjsousa@909: } mjsousa@909: ERROR; /* should never occur */ mjsousa@909: return NULL; mjsousa@909: } mjsousa@909: mjsousa@909: mjsousa@909: /* For the moment, we do not support initialising reference data types */ mjsousa@909: /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */ mjsousa@909: /* NOTE: ref_initialization may be NULL!! */ mjsousa@909: // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization) mjsousa@909: void *spec_init_sperator_c::visit(ref_spec_init_c *symbol) { mjsousa@909: TRACE("spec_init_sperator_c::ref_spec_init_c"); mjsousa@909: switch (search_what) { mjsousa@909: case search_spec: return symbol->ref_spec; mjsousa@909: case search_init: return symbol->ref_initialization; mjsousa@909: } mjsousa@909: ERROR; /* should never occur */ mjsousa@909: return NULL; mjsousa@909: } mjsousa@909: mjsousa@909: mjsousa@909: mario@181: /******************************************/ mario@181: /* B 1.4.3 - Declaration & Initialisation */ mario@181: /******************************************/ mario@181: msousa@350: /* STRING '[' integer ']' msousa@350: * STRING ASSIGN single_byte_character_string msousa@350: * STRING '[' integer ']' ASSIGN single_byte_character_string msousa@350: */ msousa@350: void *spec_init_sperator_c::visit(single_byte_string_spec_c *symbol) { msousa@350: TRACE("spec_init_sperator_c::single_byte_string_spec_c"); msousa@350: switch (search_what) { msousa@350: case search_spec: return symbol->string_spec; msousa@350: case search_init: return symbol->single_byte_character_string; msousa@350: } msousa@350: ERROR; /* should never occur */ msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: /* WSTRING '[' integer ']' msousa@350: * WSTRING ASSIGN double_byte_character_string msousa@350: * WSTRING '[' integer ']' ASSIGN double_byte_character_string msousa@350: */ msousa@350: void *spec_init_sperator_c::visit(double_byte_string_spec_c *symbol) { msousa@350: TRACE("spec_init_sperator_c::double_byte_string_spec_c"); msousa@350: switch (search_what) { msousa@350: case search_spec: return symbol->string_spec; msousa@350: case search_init: return symbol->double_byte_character_string; msousa@350: } msousa@350: ERROR; /* should never occur */ msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: mario@181: spec_init_sperator_c *spec_init_sperator_c ::class_instance = NULL; mario@181: spec_init_sperator_c::search_what_t spec_init_sperator_c::search_what;