stage1_2/derived_conversion_functions.cc
changeset 745 26cb3fa00d29
child 747 d1c1a0254e4f
equal deleted inserted replaced
734:49853bded539 745:26cb3fa00d29
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
       
     6  *
       
     7  *
       
     8  *  This program is free software: you can redistribute it and/or modify
       
     9  *  it under the terms of the GNU General Public License as published by
       
    10  *  the Free Software Foundation, either version 3 of the License, or
       
    11  *  (at your option) any later version.
       
    12  *
       
    13  *  This program is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16  *  GNU General Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License
       
    19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    20  *
       
    21  *
       
    22  * This code is made available on the understanding that it will not be
       
    23  * used in safety-critical situations without a full and competent review.
       
    24  */
       
    25 
       
    26 /*
       
    27  * An IEC 61131-3 compiler.
       
    28  *
       
    29  * Based on the
       
    30  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    31  *
       
    32  */
       
    33 
       
    34 
       
    35 #include "derived_conversion_functions.hh"
       
    36 #include <sstream>
       
    37 
       
    38 static const int debug = 0;
       
    39 
       
    40 derived_conversion_functions_c::derived_conversion_functions_c(symbol_c *ignore) {
       
    41 
       
    42 }
       
    43 
       
    44 derived_conversion_functions_c::~derived_conversion_functions_c(void) {
       
    45 
       
    46 }
       
    47 
       
    48 std::string &derived_conversion_functions_c::get_declaration(symbol_c *root) {
       
    49     text = "";
       
    50     if (NULL != root) {
       
    51         root->accept(*this);
       
    52     }
       
    53 
       
    54     return text;
       
    55 }
       
    56 
       
    57 void *derived_conversion_functions_c::visit(identifier_c *symbol) {
       
    58     currentToken = symbol->value;
       
    59 
       
    60     return NULL;
       
    61 }
       
    62 
       
    63 void *derived_conversion_functions_c::visit(enumerated_type_declaration_c *symbol) {
       
    64     std::string enumerateName;
       
    65     std::string functionName;
       
    66     std::list <std::string> enumerateValues;
       
    67 
       
    68     symbol->enumerated_type_name->accept(*this);
       
    69     enumerateName = currentToken;
       
    70 
       
    71     symbol->enumerated_spec_init->accept(*this);
       
    72     enumerateValues = currentTokenList;
       
    73 
       
    74     printStringToEnum  (enumerateName, enumerateValues);
       
    75     printEnumToString  (enumerateName, enumerateValues);
       
    76     for (size_t s = 8; s <= 64; s*= 2) {
       
    77         printIntegerToEnum (enumerateName, enumerateValues, true , s);
       
    78         printEnumToInteger (enumerateName, enumerateValues, true , s);
       
    79         printIntegerToEnum (enumerateName, enumerateValues, false, s);
       
    80         printEnumToInteger (enumerateName, enumerateValues, false, s);
       
    81     }
       
    82     if (debug) std::cout << text << std::endl;
       
    83     
       
    84     return NULL;
       
    85 }
       
    86 
       
    87 void *derived_conversion_functions_c::visit(enumerated_value_list_c *symbol) {
       
    88     list_c *list;
       
    89 
       
    90     currentTokenList.clear();
       
    91     list = (list_c *)symbol;
       
    92     for (int i = 0; i < list->n; i++) {
       
    93         list->elements[i]->accept(*this);
       
    94         currentTokenList.push_back(currentToken);
       
    95     }
       
    96 
       
    97     return NULL;
       
    98 }
       
    99 
       
   100 std::string derived_conversion_functions_c::getIntegerName(bool isSigned, size_t size) {
       
   101     std::string integerType = "";
       
   102     if (! isSigned) {
       
   103         integerType = "U";
       
   104     }
       
   105     switch(size) {
       
   106     case 8 : integerType += "S"; break;
       
   107     case 16:                     break;
       
   108     case 32: integerType += "D"; break;
       
   109     case 64: integerType += "L"; break;
       
   110     default:                     break;
       
   111     }
       
   112     integerType +="INT";
       
   113 
       
   114     return integerType;
       
   115 }
       
   116 
       
   117 void derived_conversion_functions_c::printStringToEnum  (std::string &enumerateName, std::list<std::string> &enumerateValues) {
       
   118     std::list <std::string>::const_iterator itr;
       
   119     std::string functionName;
       
   120 
       
   121     functionName = "STRING_TO_" + enumerateName;
       
   122     text += "FUNCTION " + functionName + " : " + enumerateName;
       
   123     text += "\nVAR_INPUT\nIN : STRING;\nEND_VAR\n";
       
   124     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   125        std::string value = *itr;
       
   126        text += "IF IN = '" + value + "' THEN\n";
       
   127        text += " " + functionName + " := " + value + ";\n";
       
   128        text += " RETURN;\n";
       
   129        text += "END_IF;\n";
       
   130     }
       
   131     text += "END_FUNCTION\n\n";
       
   132 }
       
   133 
       
   134 void derived_conversion_functions_c::printEnumToString  (std::string &enumerateName, std::list<std::string> &enumerateValues) {
       
   135     std::list <std::string>::const_iterator itr;
       
   136     std::string functionName;
       
   137 
       
   138     functionName = enumerateName + "_TO_STRING";
       
   139     text += "FUNCTION " + functionName + " : STRING";
       
   140     text += "\nVAR_INPUT\nIN : " + enumerateName + ";\nEND_VAR\n";
       
   141     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   142         std::string value = *itr;
       
   143         text += "IF IN = " + value + " THEN\n";
       
   144         text += " " + functionName + " := '" + value + "';\n";
       
   145         text += " RETURN;\n";
       
   146         text += "END_IF;\n";
       
   147     }
       
   148     text += "END_FUNCTION\n\n";
       
   149 }
       
   150 
       
   151 void derived_conversion_functions_c::printIntegerToEnum (std::string &enumerateName, std::list<std::string> &enumerateValues, bool isSigned, size_t size) {
       
   152     std::list <std::string>::const_iterator itr;
       
   153     std::string functionName;
       
   154     std::string integerType;
       
   155     int count;
       
   156 
       
   157     integerType  = getIntegerName(isSigned, size);
       
   158     functionName = integerType + "_TO_" + enumerateName;
       
   159     text += "FUNCTION " + functionName + " : " + enumerateName;
       
   160     text += "\nVAR_INPUT\nIN : " + integerType + ";\nEND_VAR\n";
       
   161     count = 0;
       
   162     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   163         std::string value = *itr;
       
   164         std::stringstream out;
       
   165         out << count;
       
   166         text += "IF IN = " + out.str() + " THEN\n";
       
   167         text += " " + functionName + " := " + value + ";\n";
       
   168         text += " RETURN;\n";
       
   169         text += "END_IF;\n";
       
   170         count++;
       
   171     }
       
   172     text += "END_FUNCTION\n\n";
       
   173 }
       
   174 
       
   175 void derived_conversion_functions_c::printEnumToInteger (std::string &enumerateName, std::list<std::string> &enumerateValues, bool isSigned, size_t size) {
       
   176     std::list <std::string>::const_iterator itr;
       
   177     std::string functionName;
       
   178     std::string integerType;
       
   179     int count;
       
   180 
       
   181     integerType  = getIntegerName(isSigned, size);
       
   182     functionName = enumerateName + "_TO_" + integerType;
       
   183     text += "FUNCTION " + functionName + " : " + integerType;
       
   184     text += "\nVAR_INPUT\nIN : " + enumerateName + ";\nEND_VAR\n";
       
   185     count = 0;
       
   186     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   187         std::string value = *itr;
       
   188         std::stringstream out;
       
   189         out << count;
       
   190         text += "IF IN = " + value + " THEN\n";
       
   191         text += " " + functionName + " := " + out.str() + ";\n";
       
   192         text += " RETURN;\n";
       
   193         text += "END_IF;\n";
       
   194         count++;
       
   195     }
       
   196     text += "END_FUNCTION\n\n";
       
   197 }
       
   198 
       
   199 
       
   200