FAUST compiler  0.9.9.6b8
node.hh
Go to the documentation of this file.
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 /*****************************************************************************
25 ******************************************************************************/
26 
49 /******************************************************************************
50 *****************************************************************************/
51 
52 
53 #ifndef __NODE__
54 #define __NODE__
55 
56 #ifdef WIN32
57 #define int64_t __int64
58 #endif
59 
60 #include <iostream>
61 #include "symbol.hh"
62 #include <sys/types.h>
63 
64 using namespace std;
65 
70 
71 
75 class Node
76 {
77  int fType;
78  union {
79  int i;
80  double f;
81  Sym s;
82  void* p;
83  int64_t v;
84  } fData;
85 
86  public:
87  // constructeurs (assume size of field f is the biggest)
88  Node (int x) : fType(kIntNode) { fData.f = 0; fData.i = x; }
89  Node (double x) : fType(kDoubleNode) { fData.f = x; }
90  Node (const char* name) : fType(kSymNode) { fData.f = 0; fData.s = symbol(name); }
91  Node (const string& name) : fType(kSymNode) { fData.f = 0; fData.s = symbol(name); }
92  Node (Sym x) : fType(kSymNode) { fData.f = 0; fData.s = x; }
93  Node (void* x) : fType(kPointerNode) { fData.f = 0; fData.p = x; }
94 
95  Node (const Node& n) : fType(n.fType) { fData = n.fData; }
96 
97  // predicats
98  bool operator == (const Node& n) const { return fType == n.fType && fData.v == n.fData.v; }
99  bool operator != (const Node& n) const { return fType != n.fType || fData.v != n.fData.v; }
100 
101  // accessors
102  int type() const { return fType; }
103 
104  int getInt() const { return fData.i; }
105  double getDouble() const { return fData.f; }
106  Sym getSym() const { return fData.s; }
107  void* getPointer() const { return fData.p; }
108 
109  // conversions and promotion for numbers
110  operator int() const { return (fType == kIntNode) ? fData.i : (fType == kDoubleNode) ? int(fData.f) : 0 ; }
111  operator double() const { return (fType == kIntNode) ? double(fData.i) : (fType == kDoubleNode) ? fData.f : 0.0 ; }
112 
113  ostream& print (ostream& fout) const;
114 };
115 
116 //printing
117 inline ostream& operator << (ostream& s, const Node& n) { return n.print(s); }
118 
119 
120 
121 //-------------------------------------------------------------------------
122 // Perdicates and pattern matching
123 //-------------------------------------------------------------------------
124 
125 // integers
126 inline bool isInt (const Node& n)
127 {
128  return (n.type() == kIntNode);
129 }
130 
131 inline bool isInt (const Node& n, int* x)
132 {
133  if (n.type() == kIntNode) {
134  *x = n.getInt();
135  return true;
136  } else {
137  return false;
138  }
139 }
140 
141 
142 // floats
143 inline bool isDouble (const Node& n)
144 {
145  return (n.type() == kDoubleNode);
146 }
147 
148 inline bool isDouble (const Node& n, double* x)
149 {
150  if (n.type() == kDoubleNode) {
151  *x = n.getDouble();
152  return true;
153  } else {
154  return false;
155  }
156 }
157 
158 
159 
160 inline bool isZero (const Node& n)
161 {
162  return (n.type() == kDoubleNode) && (n.getDouble() == 0.0)
163  || (n.type() == kIntNode) && (n.getInt() == 0);
164 }
165 
166 inline bool isGEZero (const Node& n)
167 {
168  return (n.type() == kDoubleNode) && (n.getDouble() >= 0.0)
169  || (n.type() == kIntNode) && (n.getInt() >= 0);
170 }
171 
172 inline bool isGTZero (const Node& n)
173 {
174  return (n.type() == kDoubleNode) && (n.getDouble() > 0.0)
175  || (n.type() == kIntNode) && (n.getInt() > 0);
176 }
177 
178 inline bool isOne (const Node& n)
179 {
180  return (n.type() == kDoubleNode) && (n.getDouble() == 1.0)
181  || (n.type() == kIntNode) && (n.getInt() == 1);
182 }
183 
184 inline bool isMinusOne (const Node& n)
185 {
186  return (n.type() == kDoubleNode) && (n.getDouble() == -1.0)
187  || (n.type() == kIntNode) && (n.getInt() == -1);
188 }
189 
190 
191 // numbers in general
192 inline bool isNum (const Node& n)
193 {
194  return isInt(n)||isDouble(n);
195 }
196 
197 
198 // symbols
199 inline bool isSym (const Node& n)
200 {
201  return (n.type() == kSymNode);
202 }
203 
204 inline bool isSym (const Node& n, Sym* x)
205 {
206  if (n.type() == kSymNode) {
207  *x = n.getSym();
208  return true;
209  } else {
210  return false;
211  }
212 }
213 
214 
215 // void pointer
216 inline bool isPointer (const Node& n)
217 {
218  return (n.type() == kPointerNode);
219 }
220 
221 inline bool isPointer (const Node& n, void** x)
222 {
223  if (n.type() == kPointerNode) {
224  *x = n.getPointer();
225  return true;
226  } else {
227  return false;
228  }
229 }
230 
231 
232 
233 
234 //-------------------------------------------------------------------------
235 // Mathematical operations on nodes
236 //-------------------------------------------------------------------------
237 
238 
239 // arithmetic operations
240 
241 inline const Node addNode (const Node& x, const Node& y)
242  { return (isDouble(x)||isDouble(y)) ? Node(double(x)+double(y)) : Node(int(x)+int(y)); }
243 
244 inline const Node subNode (const Node& x, const Node& y)
245  { return (isDouble(x)||isDouble(y)) ? Node(double(x)-double(y)) : Node(int(x)-int(y)); }
246 
247 inline const Node mulNode (const Node& x, const Node& y)
248  { return (isDouble(x)||isDouble(y)) ? Node(double(x)*double(y)) : Node(int(x)*int(y)); }
249 
250 inline const Node divExtendedNode (const Node& x, const Node& y)
251  { return (isDouble(x)||isDouble(y)) ? Node(double(x)/double(y))
252  : (double(int(x)/int(y))==double(x)/double(y)) ? Node(int(x)/int(y))
253  : Node(double(x)/double(y)); }
254 
255 inline const Node remNode (const Node& x, const Node& y)
256  { return Node(int(x)%int(y)); }
257 
258 // inverse functions
259 
260 inline const Node minusNode (const Node& x)
261  { return subNode(0, x); }
262 
263 inline const Node inverseNode (const Node& x)
264  { return divExtendedNode(1.0f, x); }
265 
266 
267 // bit shifting operations
268 
269 inline const Node lshNode (const Node& x, const Node& y)
270  { return Node(int(x)<<int(y)); }
271 
272 inline const Node rshNode (const Node& x, const Node& y)
273  { return Node(int(x)>>int(y)); }
274 
275 
276 // boolean operations on bits
277 
278 inline const Node andNode (const Node& x, const Node& y)
279  { return Node(int(x)&int(y)); }
280 
281 inline const Node orNode (const Node& x, const Node& y)
282  { return Node(int(x)|int(y)); }
283 
284 inline const Node xorNode (const Node& x, const Node& y)
285  { return Node(int(x)^int(y)); }
286 
287 
288 // compare operations
289 
290 inline const Node gtNode (const Node& x, const Node& y)
291  { return (isDouble(x)||isDouble(y)) ? Node(double(x)>double(y)) : Node(int(x)>int(y)); }
292 
293 inline const Node ltNode (const Node& x, const Node& y)
294  { return (isDouble(x)||isDouble(y)) ? Node(double(x)<double(y)) : Node(int(x)<int(y)); }
295 
296 inline const Node geNode (const Node& x, const Node& y)
297  { return (isDouble(x)||isDouble(y)) ? Node(double(x)>=double(y)) : Node(int(x)>=int(y)); }
298 
299 inline const Node leNode (const Node& x, const Node& y)
300  { return (isDouble(x)||isDouble(y)) ? Node(double(x)<=double(y)) : Node(int(x)<=int(y)); }
301 #if 1
302 inline const Node eqNode (const Node& x, const Node& y)
303  { return (isDouble(x)||isDouble(y)) ? Node(double(x)==double(y)) : Node(int(x)==int(y)); }
304 
305 inline const Node neNode (const Node& x, const Node& y)
306  { return (isDouble(x)||isDouble(y)) ? Node(double(x)!=double(y)) : Node(int(x)!=int(y)); }
307 #endif
308 
309 
310 
311 #endif
double f
Definition: node.hh:80
const Node leNode(const Node &x, const Node &y)
Definition: node.hh:299
Sym getSym() const
Definition: node.hh:106
const Node eqNode(const Node &x, const Node &y)
Definition: node.hh:302
A library to create and manipulate symbols with a unique name.
union Node::@10 fData
Node(int x)
Definition: node.hh:88
Class Node = (type x (int + double + Sym + void*))
Definition: node.hh:75
const Node addNode(const Node &x, const Node &y)
Definition: node.hh:241
ostream & print(ostream &fout) const
print a node on a stream
Definition: node.cpp:3
Node(Sym x)
Definition: node.hh:92
Node(const string &name)
Definition: node.hh:91
Definition: node.hh:69
Node(double x)
Definition: node.hh:89
Node(void *x)
Definition: node.hh:93
bool isZero(const Node &n)
Definition: node.hh:160
const Node divExtendedNode(const Node &x, const Node &y)
Definition: node.hh:250
const Node remNode(const Node &x, const Node &y)
Definition: node.hh:255
const Node xorNode(const Node &x, const Node &y)
Definition: node.hh:284
ostream & operator<<(ostream &s, const Node &n)
Definition: node.hh:117
int type() const
Definition: node.hh:102
double getDouble() const
Definition: node.hh:105
Node(const char *name)
Definition: node.hh:90
const Node gtNode(const Node &x, const Node &y)
Definition: node.hh:290
Node(const Node &n)
Definition: node.hh:95
bool isGEZero(const Node &n)
Definition: node.hh:166
const Node ltNode(const Node &x, const Node &y)
Definition: node.hh:293
const Node rshNode(const Node &x, const Node &y)
Definition: node.hh:272
const Node mulNode(const Node &x, const Node &y)
Definition: node.hh:247
interval operator!=(const interval &, const interval &)
Definition: interval.hh:196
const Node geNode(const Node &x, const Node &y)
Definition: node.hh:296
bool isMinusOne(const Node &n)
Definition: node.hh:184
const Node minusNode(const Node &x)
Definition: node.hh:260
bool isNum(const Node &n)
Definition: node.hh:192
bool isDouble(const Node &n)
Definition: node.hh:143
int64_t v
Definition: node.hh:83
bool isSym(const Node &n)
Definition: node.hh:199
void * p
Definition: node.hh:82
int i
Definition: node.hh:79
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
int fType
Definition: node.hh:77
const Node neNode(const Node &x, const Node &y)
Definition: node.hh:305
Symbols are unique objects with a name stored in a hash table.
Definition: symbol.hh:53
const Node subNode(const Node &x, const Node &y)
Definition: node.hh:244
int getInt() const
Definition: node.hh:104
bool isGTZero(const Node &n)
Definition: node.hh:172
bool isOne(const Node &n)
Definition: node.hh:178
Sym s
Definition: node.hh:81
const Node lshNode(const Node &x, const Node &y)
Definition: node.hh:269
interval operator==(const interval &, const interval &)
Definition: interval.hh:191
Symbol * symbol(const char *str)
Returns (and creates if new) the symbol of name str.
Definition: symbol.hh:95
bool isPointer(const Node &n)
Definition: node.hh:216
bool isInt(const Node &n)
Definition: node.hh:126
Definition: node.hh:69
const Node andNode(const Node &x, const Node &y)
Definition: node.hh:278
void * getPointer() const
Definition: node.hh:107
const Node inverseNode(const Node &x)
Definition: node.hh:263
void print(Tree t, FILE *out)
Definition: list.cpp:154
const Node orNode(const Node &x, const Node &y)
Definition: node.hh:281