stage4/stage4.cc
changeset 0 fb772792efd1
child 26 fd67f54e64e1
equal deleted inserted replaced
-1:000000000000 0:fb772792efd1
       
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * 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.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * This file contains the code that stores the output generated
       
    28  * by each specific version of the 4th stage.
       
    29  */
       
    30 
       
    31 
       
    32 
       
    33 
       
    34 
       
    35 
       
    36 // #include <stdio.h>  /* required for NULL */
       
    37 #include <string>
       
    38 #include <iostream>
       
    39 
       
    40 
       
    41 
       
    42 
       
    43 #include "stage4.hh"
       
    44 
       
    45 
       
    46 
       
    47 
       
    48 
       
    49 
       
    50 stage4out_c::stage4out_c(std::string indent_level) {
       
    51   this->indent_level = indent_level;
       
    52   this->indent_spaces = "";
       
    53 }
       
    54 
       
    55 stage4out_c::~stage4out_c(void) {}
       
    56 
       
    57 
       
    58 void stage4out_c::indent_right(void) {
       
    59   indent_spaces+=indent_level;
       
    60 }
       
    61 
       
    62 void stage4out_c::indent_left(void) {
       
    63   if (indent_spaces.length() >= indent_level.length())
       
    64     indent_spaces.erase(indent_spaces.length() - indent_level.length(), indent_level.length());
       
    65   else
       
    66     indent_spaces.erase();
       
    67 }
       
    68 
       
    69 
       
    70 void *stage4out_c::print(const char *str) {
       
    71   std::cout << str;
       
    72   return NULL;
       
    73 }
       
    74 
       
    75 
       
    76 void *stage4out_c::printupper(const char *str) {
       
    77   for (int i = 0; str[i] != '\0'; i++)
       
    78     std::cout << (unsigned char)toupper(str[i]);
       
    79   return NULL;
       
    80 }
       
    81 
       
    82 
       
    83 void *stage4out_c::print(std::string str) {
       
    84   std::cout << str;
       
    85   return NULL;
       
    86 }
       
    87 
       
    88 
       
    89 void *stage4out_c::printupper(std::string str) {
       
    90   /* The string standard class does not have a converter member function to upper case.
       
    91    * We have to do it ourselves, a character at a time...
       
    92    */
       
    93 #if 0
       
    94   /* The C++ way of doint things... */
       
    95   for (string::const_iterator p = str.begin(); p != str.end(); ++p)
       
    96     std::cout << (unsigned char)toupper(*p);
       
    97 #else
       
    98   /* Or more simply... */
       
    99     printupper(str.c_str());
       
   100 #endif
       
   101   return NULL;
       
   102 }
       
   103 
       
   104 
       
   105 
       
   106 /***********************************************************************/
       
   107 /***********************************************************************/
       
   108 /***********************************************************************/
       
   109 /***********************************************************************/
       
   110 /***********************************************************************/
       
   111 /***********************************************************************/
       
   112 /***********************************************************************/
       
   113 /***********************************************************************/
       
   114 /***********************************************************************/
       
   115 /***********************************************************************/
       
   116 
       
   117 
       
   118 
       
   119 /* forward declarations... */
       
   120 /* These functions will be implemented in generate_XXX.cc */
       
   121 visitor_c *new_code_generator(stage4out_c *s4o);
       
   122 void delete_code_generator(visitor_c *code_generator);
       
   123 
       
   124 
       
   125 int stage4(symbol_c *tree_root) {
       
   126   stage4out_c s4o;
       
   127   visitor_c *generate_code = new_code_generator(&s4o);
       
   128 
       
   129   if (NULL == generate_code)
       
   130     return -1;
       
   131 
       
   132   tree_root->accept(*generate_code);
       
   133 
       
   134   delete_code_generator(generate_code);
       
   135 
       
   136   return 0;
       
   137 }
       
   138