1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
/*****************************************************************************
******************************************************************************/
/** \file node.hh
* A Node is a tagged unions of int, double, symbol and void* used in the implementation of CTrees.
* Nodes are completly described by the node.h file, there is no node.cpp file.
*
* <b>API:</b>
*
* Node(symbol("abcd")); : node with symbol content
* Node(10); : node with int content
* Node(3.14159); : node with double content
*
* n->type(); : kIntNode or kDoubleNode or kSymNode
*
* n->getInt(); : int content of n
* n->getDouble(); : double content of n
* n->getSym(); : symbol content of n
*
* if (isInt(n, &i)) ... : int i = int content of n
* if (isDouble(n, &f)) ... : double f = double content of n
* if (isSym(n, &s)) ... : Sym s = Sym content of n
*
*/
/******************************************************************************
*****************************************************************************/
#ifndef __NODE__
#define __NODE__
#include <iostream>
#include "symbol.hh"
using namespace std;
/**
* Tags used to define the type of a Node
*/
enum { kIntNode, kDoubleNode, kSymNode, kPointerNode };
/**
* Class Node = (type x (int + double + Sym + void*))
*/
class Node
{
int fType;
union {
int i;
double f;
Sym s;
void* p;
} fData;
public:
// constructeurs (assume size of field f is the biggest)
Node (int x) : fType(kIntNode) { fData.f = 0; fData.i = x; }
Node (double x) : fType(kDoubleNode) { fData.f = x; }
Node (const char* name) : fType(kSymNode) { fData.f = 0; fData.s = symbol(name); }
Node (const string& name) : fType(kSymNode) { fData.f = 0; fData.s = symbol(name); }
Node (Sym x) : fType(kSymNode) { fData.f = 0; fData.s = x; }
Node (void* x) : fType(kPointerNode) { fData.f = 0; fData.p = x; }
Node (const Node& n) : fType(n.fType) { fData = n.fData; }
// predicats
bool operator == (const Node& n) const { return fType == n.fType && fData.f == n.fData.f; }
bool operator != (const Node& n) const { return fType != n.fType || fData.f != n.fData.f; }
// accessors
int type() const { return fType; }
int getInt() const { return fData.i; }
double getDouble() const { return fData.f; }
Sym getSym() const { return fData.s; }
void* getPointer() const { return fData.p; }
// conversions and promotion for numbers
operator int() const { return (fType == kIntNode) ? fData.i : (fType == kDoubleNode) ? int(fData.f) : 0 ; }
operator double() const { return (fType == kIntNode) ? double(fData.i) : (fType == kDoubleNode) ? fData.f : 0.0 ; }
ostream& print (ostream& fout) const; ///< print a node on a stream
};
//printing
inline ostream& operator << (ostream& s, const Node& n) { return n.print(s); }
//-------------------------------------------------------------------------
// Perdicates and pattern matching
//-------------------------------------------------------------------------
// integers
inline bool isInt (const Node& n)
{
return (n.type() == kIntNode);
}
inline bool isInt (const Node& n, int* x)
{
if (n.type() == kIntNode) {
*x = n.getInt();
return true;
} else {
return false;
}
}
// floats
inline bool isDouble (const Node& n)
{
return (n.type() == kDoubleNode);
}
inline bool isDouble (const Node& n, double* x)
{
if (n.type() == kDoubleNode) {
*x = n.getDouble();
return true;
} else {
return false;
}
}
inline bool isZero (const Node& n)
{
return (n.type() == kDoubleNode) && (n.getDouble() == 0.0)
|| (n.type() == kIntNode) && (n.getInt() == 0);
}
inline bool isGEZero (const Node& n)
{
return (n.type() == kDoubleNode) && (n.getDouble() >= 0.0)
|| (n.type() == kIntNode) && (n.getInt() >= 0);
}
inline bool isGTZero (const Node& n)
{
return (n.type() == kDoubleNode) && (n.getDouble() > 0.0)
|| (n.type() == kIntNode) && (n.getInt() > 0);
}
inline bool isOne (const Node& n)
{
return (n.type() == kDoubleNode) && (n.getDouble() == 1.0)
|| (n.type() == kIntNode) && (n.getInt() == 1);
}
inline bool isMinusOne (const Node& n)
{
return (n.type() == kDoubleNode) && (n.getDouble() == -1.0)
|| (n.type() == kIntNode) && (n.getInt() == -1);
}
// numbers in general
inline bool isNum (const Node& n)
{
return isInt(n)||isDouble(n);
}
// symbols
inline bool isSym (const Node& n)
{
return (n.type() == kSymNode);
}
inline bool isSym (const Node& n, Sym* x)
{
if (n.type() == kSymNode) {
*x = n.getSym();
return true;
} else {
return false;
}
}
// void pointer
inline bool isPointer (const Node& n)
{
return (n.type() == kPointerNode);
}
inline bool isPointer (const Node& n, void** x)
{
if (n.type() == kPointerNode) {
*x = n.getPointer();
return true;
} else {
return false;
}
}
//-------------------------------------------------------------------------
// Mathematical operations on nodes
//-------------------------------------------------------------------------
// arithmetic operations
inline const Node addNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)+double(y)) : Node(int(x)+int(y)); }
inline const Node subNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)-double(y)) : Node(int(x)-int(y)); }
inline const Node mulNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)*double(y)) : Node(int(x)*int(y)); }
inline const Node divNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)/double(y)) : Node(int(x)/int(y)); }
inline const Node divExtendedNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)/double(y))
: (double(int(x)/int(y))==double(x)/double(y)) ? Node(int(x)/int(y))
: Node(double(x)/double(y)); }
inline const Node remNode (const Node& x, const Node& y)
{ return Node(int(x)%int(y)); }
// inverse functions
inline const Node minusNode (const Node& x)
{ return subNode(0, x); }
inline const Node inverseNode (const Node& x)
{ return divNode(1.0f, x); }
// bit shifting operations
inline const Node lshNode (const Node& x, const Node& y)
{ return Node(int(x)<<int(y)); }
inline const Node rshNode (const Node& x, const Node& y)
{ return Node(int(x)>>int(y)); }
// boolean operations on bits
inline const Node andNode (const Node& x, const Node& y)
{ return Node(int(x)&int(y)); }
inline const Node orNode (const Node& x, const Node& y)
{ return Node(int(x)|int(y)); }
inline const Node xorNode (const Node& x, const Node& y)
{ return Node(int(x)^int(y)); }
// compare operations
inline const Node gtNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)>double(y)) : Node(int(x)>int(y)); }
inline const Node ltNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)<double(y)) : Node(int(x)<int(y)); }
inline const Node geNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)>=double(y)) : Node(int(x)>=int(y)); }
inline const Node leNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)<=double(y)) : Node(int(x)<=int(y)); }
#if 1
inline const Node eqNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)==double(y)) : Node(int(x)==int(y)); }
inline const Node neNode (const Node& x, const Node& y)
{ return (isDouble(x)||isDouble(y)) ? Node(double(x)!=double(y)) : Node(int(x)!=int(y)); }
#endif
#endif
|