File: printer.tcc

package info (click to toggle)
libmath%2B%2B 0.0.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 412 kB
  • ctags: 232
  • sloc: cpp: 1,311; makefile: 63; sh: 17
file content (232 lines) | stat: -rw-r--r-- 6,303 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
///////////////////////////////////////////////////////////////////////
//  Math Type Library
//  $Id: printer.tcc,v 1.7 2002/05/05 10:48:24 cparpart Exp $
//  (This file contains the expression stream printer specific template members)
//
//  Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Library General Public License for more details.
// 
//  You should have received a copy of the GNU Library General Public License
//  along with this library; see the file COPYING.LIB.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.
///////////////////////////////////////////////////////////////////////
#ifndef libmath_printer_h
#error You may not include math++/printer.tcc directly; include math++/printer.h instead.
#endif

#include <sstream>

namespace math {

template<class T>
TPrinter<T>::TPrinter(std::ostream& AOutput) :
    FStream(AOutput) {
}

template<class T>
void TPrinter<T>::printOn(std::ostream& AOutput, const TNode<T> *ANode) {
    TPrinter<T> printer(AOutput);
    const_cast<TNode<T> *>(ANode)->accept(printer);
}

template<class T>
std::string TPrinter<T>::print(const TNode<T> *ANode) {
    std::stringstream sstr;
    TPrinter<T> printer(sstr);
    const_cast<TNode<T> *>(ANode)->accept(printer);
    sstr << std::ends;

    return sstr.str();
}

template<class T>
void TPrinter<T>::savePrint(const TNode<T> *ANode, const TNode<T> *AParent) {
#if 1  // ifndef __ENABLE_DEBUG 
    // just disabled for debugging to see reproduce the node order
    bool less = ANode->priority() < AParent->priority() ||
               (AParent->nodeType() == TNode<T>::NEG_NODE &&
                 (ANode->nodeType() == TNode<T>::PLUS_NODE ||
                  ANode->nodeType() == TNode<T>::NEG_NODE));
#else
    bool less = true;//ANode->priority() < AParent->priority();
#endif
    if (less)
        FStream << "(";

    const_cast<TNode<T> *>(ANode)->accept(*this);

    if (less)
        FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TNumberNode<T> *ANode) {
    FStream << ANode->number();
}

template<class T>
void TPrinter<T>::visit(TSymbolNode<T> *ANode) {
    FStream << ANode->symbol();
}

template<class T>
void TPrinter<T>::visit(TParamNode<T> *ANode) {
    FStream << "x"; // the parameter is usually x,
                    // so keep this symbol reserved, or override me;)
}

template<class T>
void TPrinter<T>::visit(TPlusNode<T> *ANode) {
    savePrint(ANode->left(), ANode);
    if (ANode->right()->nodeType() != TNode<T>::NEG_NODE)
        FStream << "+";
    savePrint(ANode->right(), ANode);
}

template<class T>
void TPrinter<T>::visit(TNegNode<T> *ANode) {
    FStream << "-";
    savePrint(ANode->node(), ANode);
}

template<class T>
void TPrinter<T>::visit(TMulNode<T> *ANode) {
    savePrint(ANode->left(), ANode);

    // print 2x isteat of 2*x
    // print 2(x+y) instead of 2*(x+y)
    // print (a+b)(c+d) instead of (a+b)*(c+d)
    //if (!(ANode->left()->nodeType() == TNode<T>::NUMBER_NODE &&
    //      (ANode->right()->nodeType() == TNode<T>::SYMBOL_NODE ||
    //       ANode->right()->nodeType() == TNode<T>::PARAM_NODE)))
        FStream << "*";

    savePrint(ANode->right(), ANode);
}

template<class T>
void TPrinter<T>::visit(TDivNode<T> *ANode) {
    savePrint(ANode->left(), ANode);
    FStream << "/";
    savePrint(ANode->right(), ANode);
}

template<class T>
void TPrinter<T>::visit(TPowNode<T> *ANode) {
    savePrint(ANode->left(), ANode);
    FStream << "^";
    savePrint(ANode->right(), ANode);
}

template<class T>
void TPrinter<T>::visit(TSqrtNode<T> *ANode) {
    FStream << "sqrt(";
    ANode->node()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TSinNode<T> *ANode) {
    FStream << "sin(";
    ANode->node()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TCosNode<T> *ANode) {
    FStream << "cos(";
    ANode->node()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TTanNode<T> *ANode) {
    FStream << "tan(";
    ANode->node()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TLnNode<T> *ANode) {
    FStream << "ln(";
    ANode->node()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TFuncNode<T> *ANode) {
    FStream << ANode->name() << "(";
    ANode->node()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TIfNode<T> *ANode) {
    FStream << "IF(";
    ANode->condition()->accept(*this);
    FStream << ", ";
    ANode->trueExpr()->accept(*this);
    FStream << ", ";
    ANode->falseExpr()->accept(*this);
    FStream << ")";
}

template<class T>
void TPrinter<T>::visit(TEquNode<T> *ANode) {
    ANode->left()->accept(*this);
    FStream << "=";
    ANode->right()->accept(*this);
}

template<class T>
void TPrinter<T>::visit(TUnEquNode<T> *ANode) {
    ANode->left()->accept(*this);
    FStream << "<>";
    ANode->right()->accept(*this);
}

template<class T>
void TPrinter<T>::visit(TGreaterNode<T> *ANode) {
    ANode->left()->accept(*this);
    FStream << ">";
    ANode->right()->accept(*this);
}

template<class T>
void TPrinter<T>::visit(TLessNode<T> *ANode) {
    ANode->left()->accept(*this);
    FStream << "<";
    ANode->right()->accept(*this);
}

template<class T>
void TPrinter<T>::visit(TGreaterEquNode<T> *ANode) {
    ANode->left()->accept(*this);
    FStream << ">=";
    ANode->right()->accept(*this);
}

template<class T>
void TPrinter<T>::visit(TLessEquNode<T> *ANode) {
    ANode->left()->accept(*this);
    FStream << "<=";
    ANode->right()->accept(*this);
}

/*template<class T>
std::ostream& operator<< <T>(std::ostream& os, const TNode<T>& ANode) {
    TPrinter<T>::print(os, &ANode);
    return os;
}*/

} // namespace math