summaryrefslogtreecommitdiffstats
path: root/kmplot/kmplot/parser.h
blob: 130980397441915172d81f1c1d591bd88c433ea5 (plain)
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
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 1998, 1999  Klaus-Dieter M�ler
*               2000, 2002 kd.moeller@t-online.de
*               
* This file is part of the KDE Project.
* KmPlot is part of the KDE-EDU Project.
*
* 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., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
*
*/

/** @file parser.h
 * \brief Contains the parser core class Parser. */
 
// Qt includes
#include <tqstring.h>
#include <tqvaluevector.h>

#include "parseriface.h"

#ifndef parser_included
#define parser_included

// Voreinstellungen bei Verwendung des Standardkonstruktors :

#define	MEMSIZE		500	///< memory size for tokens
#define	STACKSIZE	50	///< stack depth

//@{
/** Token type. */
#define KONST	0       // double value follows
#define	XWERT	1       // get x value
#define KWERT   2	// get function parameter
#define	PUSH	3       // push value to stack
#define	PLUS	4       // add
#define	MINUS	5       // subtract
#define	MULT	6       // multiply
#define	DIV	7       // divide
#define	POW	8       // exponentiate
#define NEG	9	// negate
#define FKT	10	// address to function followes
#define	UFKT	11      // address to user defined function follows
#define	ENDE	12      // end of function
#define	YWERT	13       // get y value
#define	FANZ	34	// number of mathematical functions in mfkttab[]
//@}

//@{
/** Predefined mathematical function. */
double ln(double x);
double llog(double x);
double sign(double x);
double sqr(double x);
double arsinh(double x);
double arcosh(double x);
double artanh(double x);

double sec(double x);
double cosec(double x);
double cot(double x);
double arcsec(double x);
double arccosec(double x);
double arccot(double x);

double sech(double x);
double cosech(double x);
double coth(double x);
double arsech(double x);
double arcosech(double x);
double arcoth(double x);

double lcos(double x);
double lsin(double x);
double ltan(double x);

double lcosh(double x);
double lsinh(double x);
double ltanh(double x);

double arccos(double x);
double arcsin(double x);
double arctan(double x);

/// A parameter expression and value
class ParameterValueItem
{
	public:
		ParameterValueItem(const TQString &e, double v)
		{
			expression = e;
			value = v;
		}
		ParameterValueItem() {;}
		TQString expression;
		double value;
};


/** Here are all atitrbutes for a function stored. */
class Ufkt
{
	public:
		Ufkt();
		~Ufkt();
		/// Sets the parameter 
		void setParameter(double const &p) {k = p; }
        
		uint id;
		unsigned char *mem;     ///< Pointer to the allocated memory for the tokens.
		unsigned char *mptr;    ///< Pointer to the token.
		TQString fname;          ///< Name of the function.
		TQString fvar;           ///< Dummy variable.
		TQString fpar;           ///< Parameter.
		TQString fstr;           ///< Function expression.
		double k,               ///< Function parameter.
		oldy;                   ///< The last y-value needed for Euler's method
		TQValueList<int> dep;   /// A list with all functions this function depends on
        
		bool f_mode, ///< \a f_mode == 1: draw the plot.
		f1_mode, ///< \a f1_mode == 1.  draw the 1st derivative, too.
		f2_mode,///< \a f2_mode == 1.  draw the 2nd derivative, too.
		integral_mode, ///< \a f2_mode == 1.  draw the integral, too.
		integral_use_precision; ///< The user can specify an unic precision for numeric prime-functions
		int linewidth,f1_linewidth,f2_linewidth, integral_linewidth; ///< Line width.
		/** Number of parameter values. 
		* @see FktExt::k_liste */
		TQString str_dmin, str_dmax, str_startx, str_starty ; /// Plot range, input strings.
		double dmin, ///< Custom plot range, lower boundage.
		dmax, ///< Custom plot range, upper boundage.
		/** List of parameter values. 
		* @see FktExt::k_anz */
		oldyprim,  ///< needed for Euler's method, the last y'.value
		oldx, ///< needed for Euler's method, the last x-value
		starty,///< startposition for Euler's method, the initial y-value
		startx, ///< startposition for Euler's method, the initial x-value last y'.valuenitial x-value last y'.valuenitial x-value
		integral_precision; ///<precision when drawing numeric prime-functions
		QRgb color, ///< current color.
		f1_color, f2_color, integral_color;
		int use_slider; ///< -1: none (use list), else: slider number
		TQValueList<ParameterValueItem> parameters; ///< List with parameter for the function
		bool usecustomxmin, usecustomxmax;
        	// TODO double slider_min, slider_max; ///< extreme values of the slider
};

class Constant
{
public:
	Constant( char c='A', double v=0)
	{
	constant = c;
	value = v;
	}
	
	char constant;
	double value;
};

/** @short Parser.
 *
 * Tokenizes a function equation to be evaluated.
 */
class Parser : virtual public ParserIface
{
public:
	Parser();
	~Parser();
	
	/**
       * @return A string that is safe to use as a number in a string to be
       * parsed. This is needed as e.g. "1.2e-3" is not allowed (e is a
       * constant) - so cannot use the TQString::number.
       */
      static TQString number( double value );
	
        /// Returns the result of a calculation
        double fkt(Ufkt *it, double const x);
        double fkt(uint id, double const x);
	
	/// Evaluates the given expression.
	double eval(TQString);
	/// Adds a user defined function with the given equation. The new function's ID-number is returned.
	int addfkt(TQString);
        /// Removes the function with the given id.
	bool delfkt(uint id);
        bool delfkt( Ufkt *item);
	
	/// Returns the ID-number of the function "name". If the function couldn't be found, -1 is returned.
	int fnameToId(const TQString &name);
	/// Returns the current error value. If showMessageBox is true, an error message box will appear if an error was found
	int parserError(bool showMessageBox=TRUE);
	
	/// return the angletype
	static double anglemode();
	/// Sets the angletype. TRUE is radians and FALSE degrees
	void setAngleMode(int);
	/// sets the decimal symbol
	void setDecimalSymbol(const TQString );
	
        /// reparse the function. It also do a grammer check for the expression
	void reparse(int ix);
        void reparse(Ufkt *item);
	
        uint getNewId(); /// Returns the next ID-number
        int idValue(int const ix); /// Converts an index-value to an ID-number
        int ixValue(uint const id);/// Converts an ID-numer to an index-value
        uint countFunctions(); /// Returns how many functions there are

	TQValueVector<Constant> constant;
        TQValueVector<Ufkt> ufkt;///< Points to the array of user defined functions.

private:
	/** Mathematical function. */
	struct Mfkt
	{
		const char *mfstr;
		double (*mfadr)(double);
	};
	static Mfkt mfkttab[FANZ];
	
	/// Error codes:
        /**
         * The values have following meanings:
	 * \li  0 => parse success
	 * \li  1 => syntax error
	 * \li  2 => missing bracket
	 * \li  3 => function unknown
	 * \li  4 => function variable not valid
	 * \li  5 => too much functions
	 * \li  6 => memory overflow
	 * \li  7 => stack overflow
	 * \li  8 => function name already used
	 * \li  9 => recursive function call
	 * \li  10 => didn't found the wanted constant
	 * \li   11 => emtpy function
	 * \li   12 => function name contains a capital letter
	 * \li   13 => function could not be found
	 * \li   14 => evalation expression may not use user definded constants
	 */
	int err;
	///  Position where the error occurred.
        int errpos;

        void fix_expression(TQString &, int const); ///adding extra *-characters, remove spaces and replace the locale .-character with '.'
        
	void ps_init(),
	heir1(),
	heir2(),
	heir3(),
	heir4(),
	primary(),
	addtoken(unsigned char),
	addwert(double),
	addfptr(double(*)(double)),
	addfptr(uint );
	int match(const char*);
        
	unsigned
	char evalflg, 		// 0 => String wird tokenisiert
	                    // 1 => String wird direkt ausgewertet
	*mem, 			    // Zeiger auf Speicher fr Token
	*mptr;			    // Zeiger fr Token
	const
	char *lptr;			// Zeiger fr Funktions-String
        Ufkt *current_item; // Pointer to the current function
	double *stack, 		// Zeiger auf Stackanfang
	*stkptr;		    // Stackpointer
	static double  m_anglemode;
	TQString m_decimalsymbol;
};

#endif	// parser_included