1
/******************************************************************************
2
Copyright (c) 2007 netAllied GmbH, Tettnang
4
Permission is hereby granted, free of charge, to any person
5
obtaining a copy of this software and associated documentation
6
files (the "Software"), to deal in the Software without
7
restriction, including without limitation the rights to use,
8
copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the
10
Software is furnished to do so, subject to the following
13
The above copyright notice and this permission notice shall be
14
included in all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
OTHER DEALINGS IN THE SOFTWARE.
24
******************************************************************************/
26
#ifndef __MATHML_AST_CONSTANT_EXPRESSION_H___
27
#define __MATHML_AST_CONSTANT_EXPRESSION_H___
29
#include "MathMLSolverPrerequisites.h"
30
#include "MathMLASTNode.h"
31
#include "MathMLASTArithmeticExpression.h"
32
#include "MathMLASTUnaryArithmeticExpression.h"
33
#include "MathMLASTBinaryComparisionExpression.h"
34
#include "MathMLASTLogicExpression.h"
35
#include "MathMLError.h"
41
/** Forward Declaration. */
47
/** Specialized constant node implementing INode. */
49
class _MATHML_SOLVER_EXPORT ConstantExpression : public INode
53
/** The type of scalar value. */
63
/** Type of scalar. */
66
/** Value of scalar. */
69
/** The string value... */
73
ErrorHandler* mErrorHandler;
76
/** Enable copy construction to avoid expensive deep copies of big trees.
78
ConstantExpression( const ConstantExpression& ref );
80
// see INode::accept(IVisitor* )
81
virtual void accept( IVisitor* visitor ) const;
83
// see INode::getType()
84
virtual NodeType getNodeType() const { return CONSTANT; }
86
/** @return a copy of this node. */
87
virtual INode* clone(CloneFlags cloneFlags) const;
93
virtual ~ConstantExpression();
95
/** Assignment from the string received from the parser.
96
@param str The string containing the constant value.
97
@param type The scalar type to set.
99
ConstantExpression( const MathML::String& str, Type type );
101
/** constructor for folded constant values.
102
@param str The string containing the string value (NOT for scalar value).
104
ConstantExpression( const MathML::String& str );
106
/** Initializing constructor.
107
@param val The double value to initialize with.
109
ConstantExpression( double val );
111
/** Initializing constructor.
112
@param val The long value to initialize with.
114
ConstantExpression( long val );
116
/** Initializing constructor.
117
@param val The bool value to initialize with.
119
ConstantExpression( bool val );
121
/** Setter for double value.
122
@param str The string to set as value.
123
@param type The type of scalar value.
125
virtual void setValue( const String& str, Type type );
127
/** Setter for double value.
128
@param val The double value to set.
130
virtual void setValue( double val );
132
/** Setter for double value.
133
@param val The double value to set.
135
virtual void setValue( long val );
137
/** Setter for bool value.
138
@param val The bool value to set.
140
virtual void setValue( bool val );
142
/** Getting the type of the scalar.
143
@return The type of the scalar value.
145
virtual Type getType() const;
147
/** Getting the double value.
148
@return The double value of scalar.
150
virtual double getDoubleValue() const;
152
/** Getting the long value.
153
@return The long value of scalar.
155
virtual long getLongValue() const;
157
/** Getting the unsigned long value.
158
@return The unsigned long value of scalar.
160
virtual unsigned long getUnsignedLongValue() const;
162
/** Getting the bool value.
163
@return The bool value of scalar.
165
virtual bool getBoolValue() const;
167
/** Returns the string value. */
168
virtual const MathML::String& getStringValue() const;
170
/** Sets the string value. */
171
virtual void setStringValue( const MathML::String& );
173
/** Returns the ConstantExpression string. */
174
virtual MathML::String toString() const;
177
/** @return Currently set error handler. */
178
virtual ErrorHandler* getErrorHandler() {return mErrorHandler;}
179
/** Sets error handler to be used. */
180
virtual void setErrorHandler( ErrorHandler* errorHandler){mErrorHandler = errorHandler;}
182
inline ConstantExpression& operator=( const ConstantExpression &a )
184
// check if we are to assign ourself
206
inline ConstantExpression operator==( const ConstantExpression &a ) const
208
ConstantExpression result;
209
relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::EQ );
213
inline ConstantExpression operator!=( const ConstantExpression &a ) const
215
ConstantExpression result;
216
relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::NEQ );
220
inline ConstantExpression operator<( const ConstantExpression &a ) const
222
ConstantExpression result;
223
relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::LT );
227
inline ConstantExpression operator>( const ConstantExpression &a ) const
229
ConstantExpression result;
230
relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::GT );
234
inline ConstantExpression operator<=( const ConstantExpression &a ) const
236
ConstantExpression result;
237
relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::LTE );
241
inline ConstantExpression operator>=( const ConstantExpression &a ) const
243
ConstantExpression result;
244
relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::GTE );
248
inline ConstantExpression operator+() const
250
ConstantExpression result;
251
unaryOperation( result, AST::UnaryExpression::ADD );
255
inline ConstantExpression operator-() const
257
ConstantExpression result;
258
unaryOperation( result, AST::UnaryExpression::SUB );
262
inline ConstantExpression operator!() const
264
ConstantExpression result;
265
unaryOperation( result, AST::UnaryExpression::NOT );
269
inline ConstantExpression operator+( const ConstantExpression &b ) const
271
ConstantExpression result;
272
arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::ADD );
276
inline ConstantExpression operator-( const ConstantExpression &b ) const
278
ConstantExpression result;
279
arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::SUB );
283
inline ConstantExpression operator*( const ConstantExpression &b ) const
285
ConstantExpression result;
286
arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::MUL );
290
inline ConstantExpression operator/( const ConstantExpression &b ) const
292
ConstantExpression result;
293
arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::DIV );
297
inline ConstantExpression operator&&( const ConstantExpression &b ) const
299
ConstantExpression result;
300
logicalBinaryOperation( result, b, AST::LogicExpression::AND );
304
inline ConstantExpression operator||( const ConstantExpression &b ) const
306
ConstantExpression result;
307
logicalBinaryOperation( result, b, AST::LogicExpression::OR );
311
inline ConstantExpression operator^( const ConstantExpression &b ) const
313
ConstantExpression result;
314
logicalBinaryOperation( result, b, AST::LogicExpression::XOR );
320
/** Called by operators to perform operations: +,-,*,/.
321
Does type checking and calls template method to perform the operation.
322
@param result The ConstantExpression reference for storing the result of the operation.
323
@param rhs The second operand (the first is 'this').
324
@param op The operator to use.
326
void arithmeticalBinaryOperation( ConstantExpression& result, const ConstantExpression& rhs, ArithmeticExpression::Operator op ) const;
328
/** Called by operators to perform operations: +,-,*,/.
329
Performs the operation without type checking.
330
Intended type parameters are long and double.
331
@param result The ConstantExpression reference for storing the result of the operation.
332
@param lhs The first operand.
333
@param rhs The second operand.
334
@param op The operator to use.
337
template < typename T >
338
void arithmeticalBinaryOperation( ConstantExpression& result, const T& lhs, const T& rhs, ArithmeticExpression::Operator op ) const;
340
/** Called by operators to perform unary operations: +,-,!.
341
Does type checking and calls template method to perform the operation.
342
Logical not is performed directly.
343
@param result The ConstantExpression reference for storing the result of the operation.
344
@param op The operator to use.
346
void unaryOperation( ConstantExpression& result, UnaryExpression::Operator op ) const;
348
/** Called by operators to perform unary operations: +,-.
349
Performs the operation without type checking.
350
Intended type parameters are long and double.
351
@param operand The operand.
352
@param op The operator to use.
353
@return Returns the result of the operation as primitive type.
355
template < typename T >
356
T unaryOperation( const T& operand, UnaryExpression::Operator op ) const;
358
/** Called by operators to perform operations: ==,!=,<,>,<=,>=.
359
Does type checking and performs the operation.
360
@param result The ConstantExpression reference for storing the result of the operation.
361
@param rhs The second operand (the first is 'this').
362
@param op The operator to use.
364
void relationalBinaryOperation( ConstantExpression& result, const ConstantExpression& rhs, BinaryComparisonExpression::Operator op ) const;
366
/** Called by operators to perform operations: &&,||,^.
367
Does type checking and performs the operation.
368
@param result The ConstantExpression reference for storing the result of the operation.
369
@param rhs The second operand (the first is 'this').
370
@param op The operator to use.
372
void logicalBinaryOperation( ConstantExpression& result, const ConstantExpression& rhs, LogicExpression::Operator op ) const;
376
//-------------------------------------------------------------------------
378
template < typename T >
379
void ConstantExpression::arithmeticalBinaryOperation( ConstantExpression& result, const T& lhs, const T& rhs, ArithmeticExpression::Operator op ) const
384
case AST::ArithmeticExpression::ADD:
385
result.setValue( lhs + rhs );
388
case AST::ArithmeticExpression::SUB:
389
result.setValue( lhs - rhs );
392
case AST::ArithmeticExpression::MUL:
393
result.setValue( lhs * rhs );
396
case AST::ArithmeticExpression::DIV:
402
Error error( Error::ERR_INVALIDPARAMS, "division by zero" );
403
mErrorHandler->handleError( &error );
404
result.setValue( 0. );
409
result.setValue( lhs / rhs );
415
Error error( Error::ERR_INVALIDPARAMS, "invalid operator: " + ArithmeticExpression::operatorString( op ) );
416
mErrorHandler->handleError( &error );
417
result.setValue( 0. );
423
//-------------------------------------------------------------------------
424
template < typename T >
425
T ConstantExpression::unaryOperation( const T& operand, UnaryExpression::Operator op ) const
430
case AST::UnaryExpression::ADD:
434
case AST::UnaryExpression::SUB:
437
case AST::UnaryExpression::NOT:
441
std::ostringstream oss;
442
oss << "invalid operator: " << AST::UnaryExpression::operatorString( op ) << ", cause operand not of type 'bool' [f, t]";
445
Error error( Error::ERR_INVALIDPARAMS, oss.str() );
446
mErrorHandler->handleError( &error );
457
#endif //__MATHML_AST_CONSTANT_EXPRESSION_H___