stage1_2/create_enumtype_conversion_functions.cc
changeset 751 0a5c050d64bb
parent 749 76c87fdb5fc8
child 753 5c389814f496
equal deleted inserted replaced
744:6ecb38715724 751:0a5c050d64bb
       
     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 #include <sstream>
       
    35 #include "create_enumtype_conversion_functions.hh"
       
    36 
       
    37 /* set to 1 to see debug info during execution */
       
    38 static const int debug = 0;
       
    39 
       
    40 /*
       
    41  * functionDataType array contains all supported data type conversion.
       
    42  */
       
    43 const char *create_enumtype_conversion_functions_c::functionDataType[] = {
       
    44 		"STRING",
       
    45 		"SINT"  ,
       
    46 		"INT"   ,
       
    47 		"DINT"  ,
       
    48 		"LINT"  ,
       
    49 		"USINT" ,
       
    50 		"UNIT"  ,
       
    51 		"UDINT" ,
       
    52 		"ULINT" ,
       
    53 		NULL
       
    54 };
       
    55 
       
    56 create_enumtype_conversion_functions_c::create_enumtype_conversion_functions_c(symbol_c *ignore) {
       
    57 
       
    58 }
       
    59 
       
    60 create_enumtype_conversion_functions_c::~create_enumtype_conversion_functions_c(void) {
       
    61 
       
    62 }
       
    63 
       
    64 std::string &create_enumtype_conversion_functions_c::get_declaration(symbol_c *root) {
       
    65     text = "";
       
    66     if (NULL != root) {
       
    67         root->accept(*this);
       
    68     }
       
    69 
       
    70     return text;
       
    71 }
       
    72 
       
    73 void *create_enumtype_conversion_functions_c::visit(identifier_c *symbol) {
       
    74     currentToken = symbol->value;
       
    75 
       
    76     return NULL;
       
    77 }
       
    78 
       
    79 /**********************/
       
    80 /* B 1.3 - Data types */
       
    81 /**********************/
       
    82 /********************************/
       
    83 /* B 1.3.3 - Derived data types */
       
    84 /********************************/
       
    85 void *create_enumtype_conversion_functions_c::visit(enumerated_type_declaration_c *symbol) {
       
    86     std::string enumerateName;
       
    87     std::string functionName;
       
    88     std::list <std::string> enumerateValues;
       
    89 
       
    90     symbol->enumerated_type_name->accept(*this);
       
    91     enumerateName = currentToken;
       
    92 
       
    93     symbol->enumerated_spec_init->accept(*this);
       
    94     enumerateValues = currentTokenList;
       
    95 
       
    96     printStringToEnum  (enumerateName, enumerateValues);
       
    97     printEnumToString  (enumerateName, enumerateValues);
       
    98     for (size_t s = 8; s <= 64; s*= 2) {
       
    99         printIntegerToEnum (enumerateName, enumerateValues, true , s);
       
   100         printEnumToInteger (enumerateName, enumerateValues, true , s);
       
   101         printIntegerToEnum (enumerateName, enumerateValues, false, s);
       
   102         printEnumToInteger (enumerateName, enumerateValues, false, s);
       
   103     }
       
   104     if (debug) std::cout << text << std::endl;
       
   105     
       
   106     return NULL;
       
   107 }
       
   108 
       
   109 void *create_enumtype_conversion_functions_c::visit(enumerated_value_list_c *symbol) {
       
   110     list_c *list;
       
   111 
       
   112     currentTokenList.clear();
       
   113     list = (list_c *)symbol;
       
   114     for (int i = 0; i < list->n; i++) {
       
   115         list->elements[i]->accept(*this);
       
   116         currentTokenList.push_back(currentToken);
       
   117     }
       
   118 
       
   119     return NULL;
       
   120 }
       
   121 
       
   122 /*
       
   123  * getIntegerName function generate a integer data name from signed and size.
       
   124  */
       
   125 std::string create_enumtype_conversion_functions_c::getIntegerName(bool isSigned, size_t size) {
       
   126     std::string integerType = "";
       
   127     if (! isSigned) {
       
   128         integerType = "U";
       
   129     }
       
   130     switch(size) {
       
   131     case 8 : integerType += "S"; break;
       
   132     case 16:                     break;
       
   133     case 32: integerType += "D"; break;
       
   134     case 64: integerType += "L"; break;
       
   135     default:                     break;
       
   136     }
       
   137     integerType +="INT";
       
   138 
       
   139     return integerType;
       
   140 }
       
   141 
       
   142 /*
       
   143  * printStringToEnum function print conversion function from STRING to <ENUM>:
       
   144  * ST Output:
       
   145  *
       
   146 
       
   147  FUNCTION STRING_TO_<ENUM> : <ENUM>
       
   148   VAR_INPUT
       
   149   IN: STRING;
       
   150   END_VAR
       
   151   IF IN = '<ENUM.VALUE_1>' THEN
       
   152    STRING_TO_<ENUM> := <ENUM.VALUE_1>;
       
   153    RETURN;
       
   154   END_IF;
       
   155   ...
       
   156   IF IN = '<ENUM.VALU_N>' THEN
       
   157    STRING_TO_<ENUM> := <ENUM.VALUE_N>;
       
   158    RETURN;
       
   159   END_IF;
       
   160   END_FUNCTION
       
   161 
       
   162   Note: if you change code below remember to update this comment.
       
   163  */
       
   164 void create_enumtype_conversion_functions_c::printStringToEnum  (std::string &enumerateName, std::list<std::string> &enumerateValues) {
       
   165     std::list <std::string>::const_iterator itr;
       
   166     std::string functionName;
       
   167 
       
   168     functionName = "STRING_TO_" + enumerateName;
       
   169     text += "FUNCTION " + functionName + " : " + enumerateName;
       
   170     text += "\nVAR_INPUT\nIN : STRING;\nEND_VAR\n";
       
   171     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   172        std::string value = *itr;
       
   173        text += "IF IN = '" + value + "' THEN\n";
       
   174        text += " " + functionName + " := " + value + ";\n";
       
   175        text += " RETURN;\n";
       
   176        text += "END_IF;\n";
       
   177     }
       
   178     text += "END_FUNCTION\n\n";
       
   179 }
       
   180 
       
   181 /*
       
   182  * printEnumToString function print conversion function from <ENUM> to STRING:
       
   183  * ST Output:
       
   184  *
       
   185 
       
   186  FUNCTION <ENUM>_TO_STRING : STRING
       
   187   VAR_INPUT
       
   188   IN: <ENUM>;
       
   189   END_VAR
       
   190   IF IN = <ENUM.VALUE_1> THEN
       
   191    <ENUM>_TO_STRING := '<ENUM.VALUE_1>';
       
   192    RETURN;
       
   193   END_IF;
       
   194   ...
       
   195   IF IN = <ENUM.VALUE_N> THEN
       
   196    <ENUM>_TO_STRING := '<ENUM.VALUE_N>';
       
   197    RETURN;
       
   198   END_IF;
       
   199   END_FUNCTION
       
   200 
       
   201   Note: if you change code below remember to update this comment.
       
   202  */
       
   203 void create_enumtype_conversion_functions_c::printEnumToString  (std::string &enumerateName, std::list<std::string> &enumerateValues) {
       
   204     std::list <std::string>::const_iterator itr;
       
   205     std::string functionName;
       
   206 
       
   207     functionName = enumerateName + "_TO_STRING";
       
   208     text += "FUNCTION " + functionName + " : STRING";
       
   209     text += "\nVAR_INPUT\nIN : " + enumerateName + ";\nEND_VAR\n";
       
   210     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   211         std::string value = *itr;
       
   212         text += "IF IN = " + value + " THEN\n";
       
   213         text += " " + functionName + " := '" + value + "';\n";
       
   214         text += " RETURN;\n";
       
   215         text += "END_IF;\n";
       
   216     }
       
   217     text += "END_FUNCTION\n\n";
       
   218 }
       
   219 
       
   220 /*
       
   221  * printIntegerToEnum function print conversion function from <INTEGER> to <ENUM>:
       
   222  * ST Output:
       
   223  *
       
   224 
       
   225  FUNCTION <INTEGER>_TO_<ENUM> : <ENUM>
       
   226   VAR_INPUT
       
   227   IN: <INTEGER>;
       
   228   END_VAR
       
   229   IF IN = 1 THEN
       
   230    <INTEGER>_TO_<ENUM> := <ENUM.VALUE_1>;
       
   231    RETURN;
       
   232   END_IF;
       
   233   ...
       
   234   IF IN = N THEN
       
   235    <INTEGER>_TO_<ENUM> := <ENUM.VALUE_N>;
       
   236    RETURN;
       
   237   END_IF;
       
   238   END_FUNCTION
       
   239 
       
   240   Note: if you change code below remember to update this comment.
       
   241  */
       
   242 void create_enumtype_conversion_functions_c::printIntegerToEnum (std::string &enumerateName, std::list<std::string> &enumerateValues, bool isSigned, size_t size) {
       
   243     std::list <std::string>::const_iterator itr;
       
   244     std::string functionName;
       
   245     std::string integerType;
       
   246     int count;
       
   247 
       
   248     integerType  = getIntegerName(isSigned, size);
       
   249     functionName = integerType + "_TO_" + enumerateName;
       
   250     text += "FUNCTION " + functionName + " : " + enumerateName;
       
   251     text += "\nVAR_INPUT\nIN : " + integerType + ";\nEND_VAR\n";
       
   252     count = 0;
       
   253     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   254         std::string value = *itr;
       
   255         std::stringstream out;
       
   256         out << count;
       
   257         text += "IF IN = " + out.str() + " THEN\n";
       
   258         text += " " + functionName + " := " + value + ";\n";
       
   259         text += " RETURN;\n";
       
   260         text += "END_IF;\n";
       
   261         count++;
       
   262     }
       
   263     text += "END_FUNCTION\n\n";
       
   264 }
       
   265 
       
   266 /*
       
   267  * printEnumToInteger function print conversion function from <ENUM> to <INTEGER>:
       
   268  * ST Output:
       
   269  *
       
   270 
       
   271  FUNCTION <ENUM>_TO_<INTEGER> : <INTEGER>
       
   272   VAR_INPUT
       
   273   IN: <INTEGER>;
       
   274   END_VAR
       
   275   IF IN = <ENUM.VALUE_1> THEN
       
   276    <ENUM>_TO_<INTEGER> := 1;
       
   277    RETURN;
       
   278   END_IF;
       
   279   ...
       
   280   IF IN = <ENUM.VALUE_N> THEN
       
   281    <ENUM>_TO_<INTEGER> := N;
       
   282    RETURN;
       
   283   END_IF;
       
   284   END_FUNCTION
       
   285 
       
   286   Note: if you change code below remember to update this comment.
       
   287  */
       
   288 void create_enumtype_conversion_functions_c::printEnumToInteger (std::string &enumerateName, std::list<std::string> &enumerateValues, bool isSigned, size_t size) {
       
   289     std::list <std::string>::const_iterator itr;
       
   290     std::string functionName;
       
   291     std::string integerType;
       
   292     int count;
       
   293 
       
   294     integerType  = getIntegerName(isSigned, size);
       
   295     functionName = enumerateName + "_TO_" + integerType;
       
   296     text += "FUNCTION " + functionName + " : " + integerType;
       
   297     text += "\nVAR_INPUT\nIN : " + enumerateName + ";\nEND_VAR\n";
       
   298     count = 0;
       
   299     for (itr = enumerateValues.begin(); itr != enumerateValues.end(); ++itr) {
       
   300         std::string value = *itr;
       
   301         std::stringstream out;
       
   302         out << count;
       
   303         text += "IF IN = " + value + " THEN\n";
       
   304         text += " " + functionName + " := " + out.str() + ";\n";
       
   305         text += " RETURN;\n";
       
   306         text += "END_IF;\n";
       
   307         count++;
       
   308     }
       
   309     text += "END_FUNCTION\n\n";
       
   310 }
       
   311 
       
   312 
       
   313