1 /*
2 
3   MusicXML Library
4   Copyright (C) 2006,2007  Grame
5 
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10 
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20   Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
21   research@grame.fr
22 
23 */
24 
25 #ifdef WIN32
26 #pragma warning (disable : 4786)
27 #endif
28 
29 #include <iostream>
30 
31 #include "exceptions.h"
32 #include "guidoexpression.h"
33 #include "guidoApplyValue.h"
34 #include "guidoEval.h"
35 #include "guidoEvalSusp.h"
36 #include "guidoClosureValue.h"
37 #include "guidoErrValue.h"
38 #include "guidoMixValue.h"
39 #include "guidoScoreValue.h"
40 #include "guidoSeqValue.h"
41 #include "valueRenderer.h"
42 
43 #include "AROthers.h"
44 #include "seqOperation.h"
45 #include "parOperation.h"
46 
47 using namespace std;
48 using namespace guido;
49 
50 namespace guidolang
51 {
52 
53 #ifdef WIN32
54 #define kSep "\\"
55 #else
56 #define kSep "/"
57 #endif
58 
59 //______________________________________________________________________________
render(const Sguidoexpression & exp)60 Sguidoelement valueRenderer::render(const Sguidoexpression& exp)
61 {
62 	fPos = 0;
63 	try {
64 		SguidoEnv env = guidoEnv::create();
65 		Sguidovalue val = exp->eval(env);
66 		if (val) return render(val);
67 	} catch (const TException& e)  {
68 		string file(e.file);
69 		size_t pos = file.find_last_of(kSep);
70 		if (pos != string::npos) file = file.substr(pos+1);
71 		cerr << e.msg << " - file: '" << file << "' line: " << e.line << endl;
72 	}
73 	return 0;
74 }
75 
76 //______________________________________________________________________________
render(const Sguidovalue & val)77 Sguidoelement valueRenderer::render(const Sguidovalue& val)
78 {
79 	fScore = 0;
80 cout << "valueRenderer::render " << val << endl;
81 	val->acceptIn (*this);
82 	return fScore;
83 }
84 
85 //______________________________________________________________________________
86 // the visit methods
87 //______________________________________________________________________________
visitStart(Sguidovalue & exp)88 void valueRenderer::visitStart( Sguidovalue& exp )
89 {
90 	cerr << __FILE__ << ": unexpected Sguidovalue received" << endl;
91 }
92 
93 //______________________________________________________________________________
visitStart(SguidoApplyValue & exp)94 void valueRenderer::visitStart( SguidoApplyValue& exp )
95 {
96 	fPos++;
97 	if (fPos < fLimit) {
98 		Sguidovalue v1 = exp->getArg1();
99 		Sguidovalue v2 = exp->getArg2();
100 		fScore = render (v1->apply(v2));
101 	}
102 	else {
103 		cout << "SguidoApplyValue rendering limit reached" << endl;
104 	}
105 }
106 
107 //______________________________________________________________________________
visitStart(SguidoEvalSusp & exp)108 void valueRenderer::visitStart( SguidoEvalSusp& exp )
109 {
110 	fScore = render (exp->force());
111 }
112 
113 //______________________________________________________________________________
visitStart(SguidoClosureValue & exp)114 void valueRenderer::visitStart( SguidoClosureValue& exp )
115 {
116 	cout << "valueRenderer SguidoClosureValue" << endl;
117 }
118 
119 //______________________________________________________________________________
visitStart(SguidoSeqValue & exp)120 void valueRenderer::visitStart( SguidoSeqValue& exp )
121 {
122 	Sguidovalue v2 = exp->getArg2();
123 	SARMusic s1 = dynamic_cast<ARMusic*> ((guidoelement*)render(exp->getArg1()));
124 	SARMusic s2 = dynamic_cast<ARMusic*> ((guidoelement*)render(v2));
125 	if (s1 && s2) {
126 		seqOperation seq;
127 		fScore = seq(s1, s2);
128 	}
129 	else {
130 		SguidoErrValue err1 = dynamic_cast<guidoErrValue*> ((guidovalue*)exp->getArg1());
131 		SguidoErrValue err2 = dynamic_cast<guidoErrValue*> ((guidovalue*)v2);
132 		cout << "SguidoSeqValue err " << (void*)err1 << " - " << (void*)err2 << endl;
133 		if (err2) fScore = s1;
134 		else fScore = 0;
135 	}
136 }
137 
138 //______________________________________________________________________________
visitStart(SguidoScoreValue & exp)139 void valueRenderer::visitStart( SguidoScoreValue& exp )
140 {
141 	fScore = exp->getScore();
142 }
143 
144 //______________________________________________________________________________
visitStart(SguidoMixValue & exp)145 void valueRenderer::visitStart( SguidoMixValue& exp )
146 {
147 	SARMusic s1 = dynamic_cast<ARMusic*> ((guidoelement*)render(exp->getArg1()));
148 	SARMusic s2 = dynamic_cast<ARMusic*> ((guidoelement*)render(exp->getArg2()));
149 	if (s1 && s2) {
150 		parOperation mix;
151 		fScore = mix(s1, s2);
152 	}
153 	else fScore = 0;
154 }
155 
156 }
157