C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

output_context.cc

Go to the documentation of this file.
00001 // $Id: output_context.cc,v 1.4 2003/08/07 23:14:02 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #include "c_breeze.h"
00039 
00040 int output_context::wrap_width = 80;
00041 
00042 // ------------------------------------------------------------
00043 // Output operators
00044 // ------------------------------------------------------------
00045 
00046 output_context & output_context::operator<<(const string & s)
00047 {
00048   _out << s;
00049 
00050   int l = s.length();
00051   if (l > 0)
00052     _last = s[l-1];
00053 
00054   _pos += s.length();
00055 
00056   return * this;
00057 }
00058 
00059 output_context & output_context::operator<<(const char * s)
00060 {
00061   _out << s;
00062 
00063   int l = strlen(s);
00064   if (l>0)
00065     _last = s[l-1];
00066 
00067   _pos += strlen(s);
00068 
00069   return * this;
00070 }
00071 
00072 output_context & output_context::operator<<(const char c)
00073 {
00074   _out << c;
00075 
00076   if ((c == '(') || (c == '{'))
00077     _parens.push_front(_pos+1);
00078 
00079   if ((c == ')') || (c == '}'))
00080     _parens.pop_front();
00081 
00082   _last = c;
00083 
00084   _pos++;
00085 
00086   return * this;
00087 }
00088 
00089 // ------------------------------------------------------------
00090 // Internal methods
00091 // ------------------------------------------------------------
00092 
00093 // --- Internal new-line function makes sure that the offset
00094 // is reset;
00095 
00096 void output_context::_new_line()
00097 {
00098   _out << endl;
00099 
00100   _last = '\n';
00101 
00102   _pos = 0;
00103   _line++;
00104 }
00105 
00106 // --- Indent out by printing some spaces...
00107 
00108 void output_context::_indent(const int num)
00109 {
00110   for (int i = 0; i < num; ++i)
00111     _out << ' ';
00112 
00113   if (num > 0)
00114     _last = ' ';
00115 
00116   _pos += num;
00117 }
00118 
00119 // ------------------------------------------------------------
00120 // Start a new line
00121 // ------------------------------------------------------------
00122 
00123 void output_context::new_line()
00124 {
00125   _new_line();
00126   _indent(_depth * 2);
00127 }
00128 
00129 // -- Check the current line length. If longer that the threshold,
00130 // insert a new-line and indent to the proper depth.
00131 
00132 void output_context::continue_line()
00133 {
00134   if (pos() > wrap_width) {
00135     if (! _parens.empty()) {
00136       _new_line();
00137       _indent(_parens.front());
00138     }
00139     else
00140       new_line();
00141   }
00142 }
00143 
00144 // -- Add a space, if it's needed. That need is determined by looking
00145 // at the last character sent to output.
00146 
00147 void output_context::space()
00148 {
00149   if ((_last != ' ') &&
00150       (_last != '\n') &&
00151       (_last != '{') &&
00152       (_last != '(')) {
00153     _out << ' ';
00154     _last = ' ';
00155     _pos++;
00156   }
00157 }
00158 
00159 
00160 // pnav
00161 // remember a suespecNode so it is not printed again
00162 void output_context::printed(suespecNode * ssn) {
00163   _ssNodes.insert(ssn);
00164 }
00165 
00166 // pnav
00167 // return whther a suespecNode has been printed
00168 bool output_context::isPrinted(suespecNode * ssn) {
00169   return (_ssNodes.find(ssn) != _ssNodes.end());
00170 }

Generated on August 27, 2003
Back to the C-Breeze home page