Debian Codegraph @

src/dotgenerator.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "dotgenerator.h"
24 #include "opentryparser.h"
25 #include "node.h"
26
27 DotGenerator::DotGenerator(QString fileName, QString orcText, OpEntryParser *opcodeTree) :
28 m_fileName(fileName), m_orcText(orcText), m_opcodeTree(opcodeTree)
29 {
30 }
31
32
33 DotGenerator::~DotGenerator()
34 {
35 }
36
37 QString DotGenerator::getDotText()
38 {
39 QString dotText = "digraph csd {\n fontsize=18\n label=\"" + m_fileName + "\"\n";
40 QStringList instruments;
41 while (m_orcText.contains(QRegExp("\\b*instr"))) {
42 int index = m_orcText.indexOf(QRegExp("\\b*instr"));
43 int end = m_orcText.indexOf("endin", index) + 6 ;
44 QString instrument = m_orcText.mid(index, end - index);
45 instruments.append(instrument);
46 m_orcText.remove(instrument);
47 }
48 QVector<QVector <Node> > instrumentGraphs;
49 QVector<QVector <QPair <QString, int> > > allInstrumentLabels;
50 QVector<QString> instrumentNames;
51 for (int i = 0; i < instruments.size(); i++) {
52 QString instrument = instruments[i];
53 QVector <Node> instrumentGraph;
54 QVector <QPair <QString, int> > instrumentLabels;
55 QStringList lines = instrument.split(QRegExp("\n"), QString::SkipEmptyParts);
56 for (int j = 0; j < lines.size(); j++) {
57 QString line = lines[j];
58 if (line.contains("instr")) {
59 instrumentNames.append(line.remove("instr").trimmed());
60 }
61 else if (line.contains("endin")) { // Do nothing
62 }
63 else {
64 QStringList parts = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
65 // qDebug() << parts << "----------";
66 if (parts.size() > 0) {
67 if (parts[0].startsWith(";")) {
68 //TODO handle comments
69 } //TODO handle if, gotos and labels
70 else if (parts[0] == "if") {
71
72 }
73 else if (parts[0] == "else") {
74
75 }
76 else if (parts[0] == "elseif") {
77
78 }
79 else if (parts[0] == "endif") {
80
81 }
82 else if (line.trimmed().contains(QRegExp("^[\\w]+:"))) { // a label
83 QPair<QString, int> l;
84 l.first = parts[0].trimmed();
85 l.second = instrumentGraph.size();
86 instrumentLabels.append(l);
87 qDebug() << l;
88 }
89 else {
90 instrumentGraph.append(createNode(parts));
91 }
92 }
93 }
94 }
95 instrumentGraphs.append(instrumentGraph);
96 allInstrumentLabels.append(instrumentLabels);
97 }
98 QHash<QString, QString> tokenSource;
99 for (int i = 0; i < instrumentGraphs.size(); i++) {
100 dotText += "subgraph cluster_" + QString::number(i) + " {\n color=black\n fontsize=18\n";
101 dotText += "label=\"instr " + instrumentNames[i] + "\"\n";
102 int labelCounter = 0;
103 for (int j = 0; j < instrumentGraphs[i].size(); j++) {
104 if (allInstrumentLabels[i].size() < labelCounter && allInstrumentLabels[i][labelCounter].second == j) {
105 // qDebug() << "allInstrument " << allInstrumentLabels[i].size() << " " << allInstrumentLabels[i][labelCounter].second;
106 if (labelCounter > 0) { // Close previous label
107 dotText += "} /* closes cluster_l" + QString::number(labelCounter - 1) +" */\n";
108 }
109 dotText +="subgraph cluster_l" + QString::number(labelCounter) + " {\n style=filled;\ncolor=lightgrey\n fontsize=18\n";
110 dotText += "label=\"" + allInstrumentLabels[i][labelCounter].first + "\"\n";
111 labelCounter++;
112 }
113 QVector<Port> inputs = instrumentGraphs[i][j].getInputs();
114 QVector<Port> outputs = instrumentGraphs[i][j].getOutputs();
115 dotText += makeNodeText(i,j,instrumentGraphs[i][j].getName(),inputs ,outputs);
116
117 for (int n = 0; n < inputs.size(); n++) {
118 dotText += makeinputConnection(i, j, n, inputs[n], tokenSource);
119 }
120 for (int w = 0; w < outputs.size(); w++) {
121 if (outputs[w].name.startsWith("i") || outputs[w].name.startsWith("k")
122 || outputs[w].name.startsWith("a") || outputs[w].name.startsWith("f")
123 || outputs[w].name.startsWith("w") || outputs[w].name.startsWith("g")) {
124 tokenSource[outputs[w].name] = "Node" + QString::number(i) + "_" + QString::number(j) + ":o" + QString::number(w);
125 // qDebug() << "tokenSource" << outputs[w].name << "--" << tokenSource[outputs[w].name];
126 }
127 }
128 }
129 if (allInstrumentLabels[i].size() > 0) {
130 dotText += "} /*close last label in instrument */\n"; //close last label subgraph
131 }
132 dotText += "} /* close instrument subgraph */\n"; //close subgraph
133 }
134 dotText += "} /* close main graph */\n"; //close main graph
135 return dotText;
136 }
137
138
139 Node DotGenerator::createNode(QStringList parts)
140 {
141 qDebug() << "DotGenerator::createNode " << parts;
142 Node node;
143 QVector <Node> instrumentGraph;
144 for (int i = 0; i < parts.size(); i++){
145 QString inArgs = "";
146 QString opcodeName = "";
147 QString outArgs = "";
148 // determine if part is an opcode
149 if (m_opcodeTree->isOpcode(parts[i])) {
150 opcodeName = parts[i];
151 for (int j = 0; j < i; j++) {
152 outArgs += parts[j];
153 }
154 for (int j = i + 1; j < parts.size(); j++) {
155 if (parts[j] == ";") { // TODO: get rid of slash asterisk comments too
156 break;
157 }
158 inArgs += parts[j];
159 }
160 node.setName(opcodeName);
161 int commentIndex = inArgs.indexOf(";");
162 // TODO when comment is removed above, no need for these lines
163 QString comment = "";
164 if (commentIndex > 0) {
165 comment = inArgs.mid(commentIndex);
166 }
167 inArgs.remove(comment);
168 QStringList args = inArgs.split(QRegExp("[,\\s]+"), QString::SkipEmptyParts);
169 bool string = false;
170 QString stringArg = "";
171 foreach (QString arg, args) {
172 if (arg.startsWith("\"") || string) {
173 string = true;
174 stringArg += arg + " ";
175 }
176 if (arg.endsWith("\"")) {
177 string = false;
178 stringArg.chop(1); //remove last space
179 arg = stringArg;
180 stringArg = "";
181 }
182 if (!string) {
183 Port port;
184 port.name = arg;
185 port.connected = false;
186 node.newInput(port);
187 }
188 }
189 args = outArgs.split(QRegExp("[\\s,]+"), QString::SkipEmptyParts);
190 foreach (QString arg, args) {
191 // qDebug() << "outArg:" << arg;
192 Port port;
193 // TODO dive into expressions
194 port.name = arg;
195 port.connected = false;
196 node.newOutput(port);
197 }
198 break;
199 }
200 }
201 m_opcodeTree->getOpcodeArgNames(node);
202 return node;
203 }
204
205 QString DotGenerator::makeNodeText(const int i,
206 const int j,
207 const QString name,
208 QVector<Port> &inputs,
209 QVector<Port> &outputs)
210 {
211 QString nodeText = " Node" + QString::number(i) + "_" + QString::number(j) + "[label =\"{";
212 if (inputs.size() > 0 && name != "=") {
213 nodeText += "{";
214 for (int x = 0; x < inputs.size(); x++) {
215 QString name = inputs[x].argName;
216 if (name != "\\") { //hack for multiline opcode lines, this should be taken care of earier....
217 name.replace("\"", "\\\"");
218 nodeText += "<i" + QString::number(x) + "> " + name + " |";
219 }
220 }
221 nodeText.chop(1); // remove last |
222 nodeText += "}|";
223 }
224 nodeText += name;
225 if (outputs.size() > 0 && name != "=") {
226 nodeText += "|{";
227 for (int x = 0; x < outputs.size(); x++) {
228 QString name = outputs[x].argName;
229 name.replace("\"", "\\\"");
230 nodeText += "<o" + QString::number(x) + "> " + name + "|";
231 }
232 nodeText.chop(1); // remove last |
233 nodeText += "} ";
234 }
235 nodeText += "}\" shape=Mrecord fontsize=11 fontname=\"Arial\" repulsiveforce=2 rank=sink]\n ";
236 return nodeText;
237 }
238
239
240 QString DotGenerator::makeinputConnection(int i, int j, int n, Port input, QHash<QString, QString> &tokenSource)
241 {
242 QString text = "";
243 if (tokenSource.value(input.name) != "") {
244 // qDebug() << "token Found" << tokenSource[input.name];
245 text += " " + tokenSource[input.name];
246 text += "->Node" + QString::number(i) + "_" + QString::number(j) + ":i";
247 text += QString::number(n) + " [label=\"" + input.name + "\" fontsize=8]\n";
248 // instrumentGraphs[i][j].setInputPortConnected(true, n);
249 }
250 else /*if (!instrumentGraphs[i][j].inputPortConnected(n))*/ {// no available source
251 // qDebug() << tokenSource.keys();
252 QString name = input.name;
253 QString label = input.name;
254 if (label == "\\") { // Ignore line continuation character
255 return QString();
256 }
257 label.replace("\"", "\\\""); // Escape quotes
258 QString nodeName = "ArgNode" + QString::number(i) + "_" + QString::number(j) + "_" + QString::number(n);
259 text += " " + nodeName + " [label=\"" + label + "\" shape=box fontname=\"Arial\" fontsize=10]\n";
260 QList<QString> keys = tokenSource.keys();
261
262 qDebug() << keys;
263 bool connected = false;
264 QStringList expTokens = label.split(QRegExp("[\\+\\-\\*\\/\\^\\)\\(\\%\\!]+"), QString::SkipEmptyParts);
265 if (expTokens.size() > 1 && !label.contains("\"")) { //means it is an expression
266 text += " " + nodeName + "->";
267 text += " Node" + QString::number(i) + "_" + QString::number(j) + ":i" + QString::number(n);
268 text += " [fontsize=10]\n";
269 connected = true;
270 }
271 foreach (QString key, keys) {
272 foreach (QString token, expTokens) {
273 token.remove(QRegExp("\\s+"));
274 if (token == key) {
275 if (key != "") {
276 // qDebug() << "expTokens match" << token;
277 text += " " + tokenSource[token] + "->" + nodeName;
278 // text += "->Node" + QString::number(i) + "_" + QString::number(j) + ":i";
279 text += " [label=\"" + token + "\" fontsize=8 ]\n";
280 connected = true;
281 break;
282 }
283 }
284 }
285 }
286 if (connected == false) {
287 // qDebug() << "Connect argument without origin";
288 text += " " + nodeName;
289 text += "->Node" + QString::number(i) + "_" + QString::number(j) + ":i" + QString::number(n) + "\n";
290 }
291 }
292 return text;
293 }