etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * This file contains the code that stores the output generated etisserant@0: * by each specific version of the 4th stage. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: // #include /* required for NULL */ etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: #include "stage4.hh" etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: stage4out_c::stage4out_c(std::string indent_level) { etisserant@0: this->indent_level = indent_level; etisserant@0: this->indent_spaces = ""; etisserant@0: } etisserant@0: etisserant@0: stage4out_c::~stage4out_c(void) {} etisserant@0: etisserant@0: etisserant@0: void stage4out_c::indent_right(void) { etisserant@0: indent_spaces+=indent_level; etisserant@0: } etisserant@0: etisserant@0: void stage4out_c::indent_left(void) { etisserant@0: if (indent_spaces.length() >= indent_level.length()) etisserant@0: indent_spaces.erase(indent_spaces.length() - indent_level.length(), indent_level.length()); etisserant@0: else etisserant@0: indent_spaces.erase(); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *stage4out_c::print(const char *str) { etisserant@0: std::cout << str; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *stage4out_c::printupper(const char *str) { etisserant@0: for (int i = 0; str[i] != '\0'; i++) etisserant@0: std::cout << (unsigned char)toupper(str[i]); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *stage4out_c::print(std::string str) { etisserant@0: std::cout << str; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *stage4out_c::printupper(std::string str) { etisserant@0: /* The string standard class does not have a converter member function to upper case. etisserant@0: * We have to do it ourselves, a character at a time... etisserant@0: */ etisserant@0: #if 0 etisserant@0: /* The C++ way of doint things... */ etisserant@0: for (string::const_iterator p = str.begin(); p != str.end(); ++p) etisserant@0: std::cout << (unsigned char)toupper(*p); etisserant@0: #else etisserant@0: /* Or more simply... */ etisserant@0: printupper(str.c_str()); etisserant@0: #endif etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* forward declarations... */ etisserant@0: /* These functions will be implemented in generate_XXX.cc */ etisserant@0: visitor_c *new_code_generator(stage4out_c *s4o); etisserant@0: void delete_code_generator(visitor_c *code_generator); etisserant@0: etisserant@0: etisserant@0: int stage4(symbol_c *tree_root) { etisserant@0: stage4out_c s4o; etisserant@0: visitor_c *generate_code = new_code_generator(&s4o); etisserant@0: etisserant@0: if (NULL == generate_code) etisserant@0: return -1; etisserant@0: etisserant@0: tree_root->accept(*generate_code); etisserant@0: etisserant@0: delete_code_generator(generate_code); etisserant@0: etisserant@0: return 0; etisserant@0: } etisserant@0: