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_SYMBOL_TABLE_H___
27
#define __MATHML_SYMBOL_TABLE_H___
29
#include "MathMLSolverPrerequisites.h"
30
#include "MathMLString.h"
31
#include "MathMLASTNode.h"
32
#include "MathMLASTConstantExpression.h"
38
/** Forward Declaration. */
41
/** ScalarList type definition. */
42
typedef std::vector< AST::ConstantExpression > ScalarList;
44
/** Contains a mapping of variables and functions that may appear in an AST. */
46
class _MATHML_SOLVER_EXPORT SymbolTable
50
/** Function pointer type definition. @todo more explaination */
51
typedef void ( *FunctionPtr ) ( AST::ConstantExpression& result, const ScalarList& paramlist, ErrorHandler* errorHandler );
53
/** Struct FunctionInfo.
54
@par Holds the count of arguments and the function pointer.
59
/** Count of arguments. */
62
/** The function pointer. */
66
@param _arguments The count of arguments.
67
@param _func The function pointer.
69
FunctionInfo( int _arguments = 0, FunctionPtr _func = 0 )
70
: argc( _arguments ), func( _func )
77
/** Map type definition holding function infos. */
78
typedef std::map< String, FunctionInfo > FunctionMap;
79
/** Map type definition holding INode(s). */
80
typedef std::map< String, AST::INode* > VariableMap;
83
/** Variables map holding INode as value for its name-key. */
84
VariableMap mVariables;
85
/** Functions map holding FunctionInfo as value for its name-key. */
86
FunctionMap mFunctions;
89
ErrorHandler* mErrorHandler;
93
SymbolTable( ErrorHandler* errorHandler );
95
virtual ~SymbolTable();
97
SymbolTable(const SymbolTable& symbolTable);
98
SymbolTable& operator=(const SymbolTable& symbolTable);
100
/** Appends a SymbolTable to this SymbolTable. Variables or functions
101
with the same name were overridden.
102
@param symbolTable The SymbolTable to append.
104
virtual void appendSymbolTable(const SymbolTable& symbolTable);
106
/** Method for erasing a variable from map for the given mapping name.
107
@par Is the key is found the INode is returned, otherwise zero.
108
@param name The name key to search for in variable map.
109
@return The found INode of the variable or zero.
111
virtual AST::INode* removeVariable( const String& name );
113
/** Method for retrieving a variable from map for the given mapping name.
114
@param name The name key to search for in variable map.
115
@return The found INode of the variable or zero.
117
virtual AST::INode* getVariable( const String& name ) const;
119
/** Getter of the variables map.
120
@return The mapping of variables.
122
virtual const VariableMap& getVariables() const;
124
/** Method for setting a variable in the map.
125
@param name The name of the variable.
126
@param value The INode representing its value (constant or any other expression).
127
@todo: returning old mapping value if there is one
129
virtual void setVariable( const String& name, AST::INode* value );
130
virtual void setVariable( const String& name, double value );
131
virtual void setVariable( const String& name, long value );
132
virtual void setVariable( const String& name, bool value );
134
/** Method for retrieving the function info from map for the given mapping name.
135
@param name The name key to search for in function map.
136
@return The found FunctionInfo or 0.
137
@throws InvalidParametersException if no function with the fiven name is found.
139
virtual const FunctionInfo* getFunction( const String& name ) const;
141
/** Getter of the functions map.
142
@return The mapping of functions.
144
virtual const FunctionMap& getFunctions() const;
146
/** Method for adding a function in the map.
147
@param name The name of the function.
148
@param argc The count of arguments (for FunctionInfo).
149
@param function The function pointer (for FunctionInfo).
150
@todo: returning old mapping value if there is one
152
virtual void addFunction( const String& name, int argc, FunctionPtr function );
154
/** Method for adding a function in the map.
155
@param name The name of the function.
156
@param function The function info to add to mapping.
157
@todo: returning old mapping value if there is one
159
virtual void addFunction( const String& name, FunctionInfo function );
161
/** Method for evaluating functions with given arguments.
162
@param result The ConstantExpression reference for storing evaluation value.
163
@param name The name of the function to evaluate.
164
@param evaluatedArgs List of ConstantExpression values as arguments for the function.
166
virtual void evaluateFunction( AST::ConstantExpression& result, const String& name, const ScalarList& evaluatedArgs );
168
/** Checks if a function exits in this symbol table.
169
@param name The name of the function to evaluate.
170
@return If a function with the given name exists in this symbol table.
172
virtual bool existsFunction( const String& name ) const;
175
VariableMap::const_iterator findVariable(const String& name) const;
176
FunctionMap::const_iterator findFunction(const String& name) const;
177
VariableMap::iterator findVariable(const String& name);
178
FunctionMap::iterator findFunction(const String& name);
182
#endif //__MATHML_SYMBOL_TABLE_H___