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@27: #include etisserant@0: etisserant@0: etisserant@0: #include "stage4.hh" etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@27: stage4out_c::stage4out_c(std::string indent_level): etisserant@27: m_file(NULL) { etisserant@27: out = &std::cout; etisserant@0: this->indent_level = indent_level; etisserant@0: this->indent_spaces = ""; etisserant@0: } etisserant@0: lbessard@46: stage4out_c::stage4out_c(const char *dir, const char *radix, const char *extension, std::string indent_level) { etisserant@27: std::string filename(radix); etisserant@27: filename += "."; etisserant@27: filename += extension; lbessard@46: std::string filepath(""); lbessard@46: if (dir != NULL) { lbessard@46: filepath += dir; lbessard@46: filepath += "/"; lbessard@46: } lbessard@46: filepath += filename; lbessard@46: std::fstream *file = new std::fstream(filepath.c_str(), std::fstream::out); etisserant@41: if(file->fail()){ etisserant@41: std::cerr << "Cannot open " << filename << " for write access \n"; etisserant@41: exit(EXIT_FAILURE); etisserant@41: }else{ etisserant@41: std::cout << filename << "\n"; etisserant@41: } etisserant@27: out = file; etisserant@27: m_file = file; etisserant@27: this->indent_level = indent_level; etisserant@27: this->indent_spaces = ""; etisserant@27: } etisserant@27: etisserant@27: stage4out_c::~stage4out_c(void) { etisserant@27: if(m_file) etisserant@27: { etisserant@27: m_file->close(); etisserant@27: delete m_file; etisserant@27: } etisserant@27: } 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@27: *out << str; etisserant@0: return NULL; etisserant@0: } etisserant@0: lbessard@28: void *stage4out_c::print_integer(int integer) { lbessard@28: *out << integer; lbessard@28: return NULL; lbessard@28: } etisserant@0: etisserant@0: void *stage4out_c::printupper(const char *str) { etisserant@0: for (int i = 0; str[i] != '\0'; i++) etisserant@27: *out << (unsigned char)toupper(str[i]); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@26: void *stage4out_c::printlocation(const char *str) { etisserant@27: *out << "__"; etisserant@26: for (int i = 0; str[i] != '\0'; i++) etisserant@26: if(str[i] == '.') etisserant@27: *out << '_'; etisserant@26: else etisserant@27: *out << (unsigned char)toupper(str[i]); etisserant@26: return NULL; etisserant@26: } etisserant@0: etisserant@0: void *stage4out_c::print(std::string str) { etisserant@27: *out << 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@27: *out << (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@26: void *stage4out_c::printlocation(std::string str) { etisserant@27: return printlocation(str.c_str()); etisserant@26: } 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 */ lbessard@46: visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir); etisserant@0: void delete_code_generator(visitor_c *code_generator); etisserant@0: etisserant@0: lbessard@46: int stage4(symbol_c *tree_root, const char *builddir) { etisserant@0: stage4out_c s4o; lbessard@46: visitor_c *generate_code = new_code_generator(&s4o, builddir); 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: