1 /*
2 GUIDO Library
3 Copyright (C) 2008 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #ifndef __guidoExpPrinter__
25 #define __guidoExpPrinter__
26
27 #include <iostream>
28
29 #include "guidoExpPrinter.h"
30 #include "guidoexpression.h"
31 #include "guidoScoreExpr.h"
32 #include "guidoNamedExpr.h"
33 #include "tree_browser.h"
34
35 using namespace std;
36 using namespace guido;
37
38 //#define printDebug
39 #ifdef printDebug
40 #define debug(msg) cout << " -> " << msg << " pending: " << fPendingOp.size() << ": "; stackPrint()
41 #else
42 #define debug(msg)
43 #endif
44
45 namespace guidolang
46 {
47
48 //______________________________________________________________________________
stackPrint()49 void guidoExpPrinter::stackPrint ()
50 {
51 stack<const char *> s;
52 int size = fPendingOp.size();
53 for (int i=0; i< size; i++) {
54 const char * str = fPendingOp.top();
55 fPendingOp.pop();
56 s.push(str);
57 cout << (str ? str : "0") << " ";
58 }
59 for (int i=0; i< size; i++) {
60 const char * str = s.top();
61 s.pop();
62 fPendingOp.push(str);
63 }
64 cout << endl;
65 }
66
67 //______________________________________________________________________________
checkOp(bool pop)68 void guidoExpPrinter::checkOp (bool pop)
69 {
70 if (fPendingOp.size()) {
71 const char * op = fPendingOp.top();
72 if (op) fOut << op << " ";
73 if (pop || op) fPendingOp.pop();
74 }
75 }
76
77 //______________________________________________________________________________
push(const char * opString)78 void guidoExpPrinter::push (const char* opString)
79 {
80 fPendingOp.push(opString);
81 fPendingOp.push(0);
82 }
83
84 //______________________________________________________________________________
85 // the visit methods
86 //______________________________________________________________________________
visitStart(Sguidoexpression & exp)87 void guidoExpPrinter::visitStart (Sguidoexpression& exp)
88 {
89 if (fInNamed) return;
90 debug("guidoExpPrinter::visitStart Sguidoexpression " << exp->getName());
91 checkOp ();
92 push(exp->getName().c_str());
93 }
94
95 //______________________________________________________________________________
visitStart(SguidoGroupedExpr & exp)96 void guidoExpPrinter::visitStart (SguidoGroupedExpr& exp)
97 {
98 debug("guidoExpPrinter::visitStart SguidoGroupedExpr");
99 checkOp (false);
100 fOut << "(" ;
101 }
102
103 //______________________________________________________________________________
visitEnd(SguidoGroupedExpr & exp)104 void guidoExpPrinter::visitEnd (SguidoGroupedExpr& exp)
105 {
106 fOut << ")" << endl;
107 }
108
109 //______________________________________________________________________________
visitStart(SguidoNamedExpr & exp)110 void guidoExpPrinter::visitStart (SguidoNamedExpr& exp)
111 {
112 debug("guidoExpPrinter::visitStart SguidoNamedExpr");
113 checkOp ();
114 fOut << exp->getName() << endl;
115 fInNamed = true;
116 }
117
118 //______________________________________________________________________________
visitEnd(SguidoNamedExpr & exp)119 void guidoExpPrinter::visitEnd (SguidoNamedExpr& exp)
120 {
121 fInNamed = false;
122 }
123
124 //______________________________________________________________________________
visitStart(SguidoIdentExpr & exp)125 void guidoExpPrinter::visitStart( SguidoIdentExpr& exp)
126 {
127 // don't print the identifier but let the enclosed expression be printed
128 }
129
130 //______________________________________________________________________________
visitStart(SguidoAbstractExpr & exp)131 void guidoExpPrinter::visitStart( SguidoAbstractExpr& exp)
132 {
133 if (fInNamed) return;
134 debug("guidoExpPrinter::visitStart SguidoAbstractExpr");
135 checkOp ();
136 fOut << "#";
137 push(exp->getName().c_str());
138 }
139
140 //______________________________________________________________________________
visitStart(SguidoScoreExpr & exp)141 void guidoExpPrinter::visitStart (SguidoScoreExpr& exp)
142 {
143 if (fInNamed) return;
144 debug("guidoExpPrinter::visitStart SguidoScoreExpr");
145 checkOp ();
146 fOut << exp->getScore() << endl;
147 }
148
149
150 } // namespace
151
152 #endif
153