Removed from tracking a bunch files: ".o" object files, executables (faust and fausti...
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / utils / names.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23 /**
24 * \file eval.cpp
25 * Interface for names propagation.
26 *
27 **/
28
29
30 #include "ppsig.hh"
31 #include "names.hh"
32 #include "property.hh"
33 #include "ppsig.hh"
34 #include "doc_Text.hh"
35 #include "Text.hh"
36 #include <assert.h>
37
38
39 // History
40 // 2009/09/08 : get/setDefNameProperty
41
42
43 extern int gMaxNameSize;
44
45
46 /**
47 * Definition name property : a property to keep track of the definition name
48 * of an expression. Whenever an identifier is evaluated, it is attached as a
49 * property of its definitionObviously there is no perfect solution since a same
50 * definition quand be given to different names.
51 */
52 Tree DEFNAMEPROPERTY = tree(symbol("DEFNAMEPROPERTY"));
53
54 void setDefNameProperty(Tree t, Tree id)
55 {
56 //cerr << "setDefNameProperty : " << *id << " FOR " << t << "#" << boxpp(t) << endl;
57 setProperty(t, DEFNAMEPROPERTY, id);
58 }
59
60 void setDefNameProperty(Tree t, const string& name)
61 {
62 //cerr << "setDefNameProperty : " << name << " FOR " << t << "#" << boxpp(t) << endl;
63 int n = name.size();
64 int m = (gMaxNameSize>1023) ? 1023 : gMaxNameSize;
65 if (n > m) {
66 // the name is too long we reduce it to 2/3 of maxsize
67 char buf[1024];
68 int i = 0;
69 // copy first third
70 for (; i < m/3; i++) { buf[i] = name[i]; }
71 // add ...
72 buf[i++] = '.';
73 buf[i++] = '.';
74 buf[i++] = '.';
75 // copy last third
76 for (int c = n-m/3; c<n; c++, i++) { buf[i] = name[c]; }
77 buf[i] = 0;
78 setProperty(t, DEFNAMEPROPERTY, tree(buf));
79 } else {
80 setProperty(t, DEFNAMEPROPERTY, tree(name.c_str()));
81 }
82
83 }
84
85 bool getDefNameProperty(Tree t, Tree& id)
86 {
87 //cerr << "getDefNameProperty of : " << t << endl;
88 return getProperty(t, DEFNAMEPROPERTY, id);
89 }
90
91
92 /**
93 * Convert a definition name (can be long) into a short nickname
94 * that can be used as an equation name in latex
95 * @todo Simplify long definition names.
96 */
97 string defName2NickName (const string& defname)
98 {
99 return defname;
100 }
101
102 Tree NICKNAMEPROPERTY = tree(symbol("NICKNAMEPROPERTY"));
103
104
105 /**
106 * Set the nickname property of a signal
107 */
108 void setSigNickname(Tree t, const string& id)
109 {
110 Tree s,d;
111 if (isSigFixDelay(t,s,d) && isZero(d)) {
112 setProperty(s, NICKNAMEPROPERTY, tree(id));
113 } else {
114 setProperty(t, NICKNAMEPROPERTY, tree(id));
115 }
116 }
117
118
119 /**
120 * Get the nickname property of a signal
121 */
122 bool getSigNickname(Tree t, Tree& id)
123 {
124 bool r = getProperty(t, NICKNAMEPROPERTY, id);
125 return r;
126 }
127
128
129
130 /**
131 * set the nickname property of a list of signals. If the list
132 * contains more than one signal, adds an index to the nickname
133 */
134 void setSigListNickName (Tree lsig, const string& nickname)
135 {
136 assert(isList(lsig));
137
138 if (isNil(tl(lsig))) {
139 setSigNickname(hd(lsig), nickname);
140 } else {
141 int i=0;
142 while (!isNil(lsig)) {
143 setSigNickname(hd(lsig), subst("$0_$1", nickname, T(++i)));
144 lsig = tl(lsig);
145 }
146 }
147 }
148
149
150
151