stage4/stage4.cc
changeset 267 0a1204bcc9af
parent 210 8387cac2aba6
child 279 c0453b7f99df
equal deleted inserted replaced
266:2f6d8866ec8d 267:0a1204bcc9af
     1 /*
     1 /*
     2  * (c) 2003 Mario de Sousa
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
     3  *
     3  *
     4  * Offered to the public under the terms of the GNU General Public License
     4  *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
     5  * as published by the Free Software Foundation; either version 2 of the
     5  *
     6  * License, or (at your option) any later version.
     6  *  This program is free software: you can redistribute it and/or modify
     7  *
     7  *  it under the terms of the GNU General Public License as published by
     8  * This program is distributed in the hope that it will be useful, but
     8  *  the Free Software Foundation, either version 3 of the License, or
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     9  *  (at your option) any later version.
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
    10  *
    11  * Public License for more details.
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
    12  *
    19  *
    13  * This code is made available on the understanding that it will not be
    20  * This code is made available on the understanding that it will not be
    14  * used in safety-critical situations without a full and competent review.
    21  * used in safety-critical situations without a full and competent review.
    15  */
    22  */
    16 
    23 
    17 /*
    24 /*
    18  * An IEC 61131-3 IL and ST compiler.
    25  * An IEC 61131-3 compiler.
    19  *
    26  *
    20  * Based on the
    27  * Based on the
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    22  *
    29  *
    23  */
    30  */
    72   }
    79   }
    73   out = file;
    80   out = file;
    74   m_file = file;
    81   m_file = file;
    75   this->indent_level = indent_level;
    82   this->indent_level = indent_level;
    76   this->indent_spaces = "";
    83   this->indent_spaces = "";
       
    84   allow_output = true;
    77 }
    85 }
    78 
    86 
    79 stage4out_c::~stage4out_c(void) {
    87 stage4out_c::~stage4out_c(void) {
    80   if(m_file)
    88   if(m_file)
    81   {
    89   {
    82     m_file->close();
    90     m_file->close();
    83     delete m_file;
    91     delete m_file;
    84   }
    92   }
    85 }
    93 }
    86 
    94 
       
    95 void stage4out_c::enable_output(void) {
       
    96   allow_output = true;
       
    97 }
       
    98     
       
    99 void stage4out_c::disable_output(void) {
       
   100   allow_output = false;
       
   101 }
    87 
   102 
    88 void stage4out_c::indent_right(void) {
   103 void stage4out_c::indent_right(void) {
    89   indent_spaces+=indent_level;
   104   indent_spaces+=indent_level;
    90 }
   105 }
    91 
   106 
    96     indent_spaces.erase();
   111     indent_spaces.erase();
    97 }
   112 }
    98 
   113 
    99 
   114 
   100 void *stage4out_c::print(const char *str) {
   115 void *stage4out_c::print(const char *str) {
       
   116   if (!allow_output) return NULL;
   101   *out << str;
   117   *out << str;
   102   return NULL;
   118   return NULL;
   103 }
   119 }
   104 
   120 
   105 void *stage4out_c::print_integer(int integer) {
   121 void *stage4out_c::print_integer(int integer) {
       
   122   if (!allow_output) return NULL;
   106   *out << integer;
   123   *out << integer;
   107   return NULL;
   124   return NULL;
   108 }
   125 }
   109 
   126 
   110 void *stage4out_c::print_long_integer(unsigned long l_integer) {
   127 void *stage4out_c::print_long_integer(unsigned long l_integer) {
       
   128   if (!allow_output) return NULL;
   111   *out << l_integer << "UL";
   129   *out << l_integer << "UL";
   112   return NULL;
   130   return NULL;
   113 }
   131 }
   114 
   132 
   115 void *stage4out_c::print_long_long_integer(unsigned long long ll_integer) {
   133 void *stage4out_c::print_long_long_integer(unsigned long long ll_integer) {
       
   134   if (!allow_output) return NULL;
   116   *out << ll_integer << "ULL";
   135   *out << ll_integer << "ULL";
   117   return NULL;
   136   return NULL;
   118 }
   137 }
   119 
   138 
   120 void *stage4out_c::printupper(const char *str) {
   139 void *stage4out_c::printupper(const char *str) {
       
   140   if (!allow_output) return NULL;
   121   for (int i = 0; str[i] != '\0'; i++)
   141   for (int i = 0; str[i] != '\0'; i++)
   122     *out << (unsigned char)toupper(str[i]);
   142     *out << (unsigned char)toupper(str[i]);
   123   return NULL;
   143   return NULL;
   124 }
   144 }
   125 
   145 
   126 void *stage4out_c::printlocation(const char *str) {
   146 void *stage4out_c::printlocation(const char *str) {
       
   147   if (!allow_output) return NULL;
   127   *out << "__";
   148   *out << "__";
   128   for (int i = 0; str[i] != '\0'; i++)
   149   for (int i = 0; str[i] != '\0'; i++)
   129     if(str[i] == '.')
   150     if(str[i] == '.')
   130       *out << '_';
   151       *out << '_';
   131     else
   152     else
   132       *out << (unsigned char)toupper(str[i]);
   153       *out << (unsigned char)toupper(str[i]);
   133   return NULL;
   154   return NULL;
   134 }
   155 }
   135 
   156 
   136 void *stage4out_c::printlocation_comasep(const char *str) {
   157 void *stage4out_c::printlocation_comasep(const char *str) {
       
   158   if (!allow_output) return NULL;
   137   *out << (unsigned char)toupper(str[0]);
   159   *out << (unsigned char)toupper(str[0]);
   138   *out << ',';
   160   *out << ',';
   139   *out << (unsigned char)toupper(str[1]);
   161   *out << (unsigned char)toupper(str[1]);
   140   *out << ',';
   162   *out << ',';
   141   for (int i = 2; str[i] != '\0'; i++)
   163   for (int i = 2; str[i] != '\0'; i++)
   146   return NULL;
   168   return NULL;
   147 }
   169 }
   148 
   170 
   149 
   171 
   150 void *stage4out_c::print(std::string str) {
   172 void *stage4out_c::print(std::string str) {
       
   173   if (!allow_output) return NULL;
   151   *out << str;
   174   *out << str;
   152   return NULL;
   175   return NULL;
   153 }
   176 }
   154 
   177 
   155 
   178 
   156 void *stage4out_c::printupper(std::string str) {
   179 void *stage4out_c::printupper(std::string str) {
       
   180   if (!allow_output) return NULL;
   157   /* The string standard class does not have a converter member function to upper case.
   181   /* The string standard class does not have a converter member function to upper case.
   158    * We have to do it ourselves, a character at a time...
   182    * We have to do it ourselves, a character at a time...
   159    */
   183    */
   160 #if 0
   184 #if 0
   161   /* The C++ way of doint things... */
   185   /* The C++ way of doint things... */
   168   return NULL;
   192   return NULL;
   169 }
   193 }
   170 
   194 
   171 
   195 
   172 void *stage4out_c::printlocation(std::string str) {
   196 void *stage4out_c::printlocation(std::string str) {
       
   197   if (!allow_output) return NULL;
   173   return printlocation(str.c_str());
   198   return printlocation(str.c_str());
   174 }
   199 }
   175 
   200 
   176 
   201 
   177 /***********************************************************************/
   202 /***********************************************************************/