Subversion Repositories Kolibri OS

Rev

Rev 7495 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7495 Rev 7516
1
#include "func.h"
1
#include "func.h"
2
#include "parser.h"
2
#include "parser.h"
3
//#include 
3
//#include 
4
//#include 
4
//#include 
5
//#include 
5
//#include 
6
 
6
 
7
// token types
7
// token types
8
#define DELIMITER 1
8
#define DELIMITER 1
9
#define VARIABLE 2
9
#define VARIABLE 2
10
#define NUMBER 3
10
#define NUMBER 3
11
#define FUNCTION 4
11
#define FUNCTION 4
12
#define FINISHED 10
12
#define FINISHED 10
13
 
13
 
14
//#define allocmem(x) malloc(x)
14
//#define allocmem(x) malloc(x)
15
//#define freemem(x) free(x)
15
//#define freemem(x) free(x)
16
 
16
 
17
double epsilon = 1e-6;
17
double epsilon = 1e-6;
18
 
18
 
19
// structure for most parser functions
19
// structure for most parser functions
20
 
20
 
21
	char token[80];
21
	char token[80];
22
	int token_type;
22
	int token_type;
23
	char *prog;
23
	char *prog;
24
 
24
 
25
	int code;    // error code
25
	int code;    // error code
26
 
26
 
27
 
27
 
28
variable_callback *find_var;
28
variable_callback *find_var;
29
 
29
 
30
struct double_list  
30
struct double_list  
31
{
31
{
32
	double val;
32
	double val;
33
	int code;			// êîä îøèáêè
33
	int code;			// êîä îøèáêè
34
	double_list *next; 
34
	double_list *next; 
35
};
35
};
36
 
36
 
37
double tg(double d)
37
double tg(double d)
38
{
38
{
39
	double cosd = cos(d);
39
	double cosd = cos(d);
40
	if (fabs(cosd) < epsilon)
40
	if (fabs(cosd) < epsilon)
41
	{
41
	{
42
		serror(ERR_OVERFLOW);
42
		serror(ERR_OVERFLOW);
43
		return 0.0;
43
		return 0.0;
44
	}
44
	}
45
	return sin(d) / cosd;
45
	return sin(d) / cosd;
46
}
46
}
47
 
47
 
48
double ctg(double d)
48
double ctg(double d)
49
{
49
{
50
	double sind = sin(d);
50
	double sind = sin(d);
51
	if (fabs(sind) < epsilon)
51
	if (fabs(sind) < epsilon)
52
	{
52
	{
53
		serror(ERR_OVERFLOW);
53
		serror(ERR_OVERFLOW);
54
		return 0.0;
54
		return 0.0;
55
	}
55
	}
56
	return cos(d) / sind;
56
	return cos(d) / sind;
57
}
57
}
58
 
58
 
59
double exp(double x)
59
double exp(double x)
60
{
60
{
61
	__asm {
61
	__asm {
62
		fld	x 
62
		fld	x 
63
		FLDL2E
63
		FLDL2E
64
		FMUL 
64
		FMUL 
65
 
65
 
66
		FLD st(0) 
66
		FLD st(0) 
67
 
67
 
68
		FLD1
68
		FLD1
69
 
69
 
70
		FXCH 
70
		FXCH 
71
		FPREM 
71
		FPREM 
72
		F2XM1 
72
		F2XM1 
73
		fadd 
73
		fadd 
74
		FSCALE
74
		FSCALE
75
		FSTP st(1)
75
		FSTP st(1)
76
	}
76
	}
77
	
77
	
78
}
78
}
79
 
79
 
80
double log(double x)
80
double log(double x)
81
{
81
{
82
	//return 0.0;
82
	//return 0.0;
83
	if (x <= 0)
83
	if (x <= 0)
84
	{
84
	{
85
		serror(ERR_OVERFLOW);
85
		serror(ERR_OVERFLOW);
86
		//return 0.0;
86
		//return 0.0;
87
		__asm {
87
		__asm {
88
			fldz
88
			fldz
89
		}
89
		}
90
	}
90
	}
91
	__asm {
91
	__asm {
92
		FLD1
92
		FLD1
93
		FLD     x
93
		FLD     x
94
		FYL2X
94
		FYL2X
95
		FLDLN2
95
		FLDLN2
96
		FMUL
96
		FMUL
97
	}
97
	}
98
}
98
}
99
 
99
 
100
double sqrt(double x)
100
double sqrt(double x)
101
{
101
{
102
	if (x < 0)
102
	if (x < 0)
103
	{
103
	{
104
		serror(ERR_BADPARAM);
104
		serror(ERR_BADPARAM);
105
		__asm {
105
		__asm {
106
			fldz
106
			fldz
107
		}
107
		}
108
	}
108
	}
109
	__asm {
109
	__asm {
110
		fld x
110
		fld x
111
		fsqrt
111
		fsqrt
112
	}
112
	}
113
}
113
}
114
 
114
 
115
double atan(double x)
115
double atan(double x)
116
{
116
{
117
	serror(ERR_GENERAL);
117
	serror(ERR_GENERAL);
118
	return 0.0; // â ëîì
118
	return 0.0; // â ëîì
119
}
119
}
120
 
120
 
121
double pow(double x, double y)
121
double pow(double x, double y)
122
{
122
{
123
	return exp(y * log(x)); // 
123
	return exp(y * log(x)); // 
124
}
124
}
125
 
125
 
126
double func_pi()
126
double func_pi()
127
{
127
{
128
	return 3.14159265358979;
128
	return 3.14159265358979;
129
}
129
}
130
 
130
 
131
double func_eps()
131
double func_eps()
132
{
132
{
133
	return epsilon;
133
	return epsilon;
134
}
134
}
135
 
135
 
136
double func_if(double_list *p)
136
double func_if(double_list *p)
137
{
137
{
138
	double_list *a, *b, *c;
138
	double_list *a, *b, *c;
139
	a = p;
139
	a = p;
140
	b = a->next;
140
	b = a->next;
141
	if (!b)
141
	if (!b)
142
	{
142
	{
143
		serror(ERR_BADPARAM);
143
		serror(ERR_BADPARAM);
144
		return 0.0;
144
		return 0.0;
145
	}
145
	}
146
	c = b->next;
146
	c = b->next;
147
	if (!c || c->next)
147
	if (!c || c->next)
148
	{
148
	{
149
		serror(ERR_BADPARAM);
149
		serror(ERR_BADPARAM);
150
		return 0.0;
150
		return 0.0;
151
	}
151
	}
152
	if (a->val != 0.0)
152
	if (a->val != 0.0)
153
	{
153
	{
154
		if (b->code)
154
		if (b->code)
155
			code = b->code;
155
			code = b->code;
156
		return b->val;
156
		return b->val;
157
	}
157
	}
158
	else
158
	else
159
	{
159
	{
160
		if (c->code)
160
		if (c->code)
161
			code = c->code;
161
			code = c->code;
162
		return c->val;
162
		return c->val;
163
	}
163
	}
164
}
164
}
165
 
165
 
166
double sum(double_list *p)
166
double sum(double_list *p)
167
{
167
{
168
	double res = 0.0;
168
	double res = 0.0;
169
	while (p)
169
	while (p)
170
	{
170
	{
171
		res += p->val;
171
		res += p->val;
172
		if (p->code)
172
		if (p->code)
173
			code = p->code;
173
			code = p->code;
174
		p = p->next;
174
		p = p->next;
175
	}
175
	}
176
	return res;
176
	return res;
177
}
177
}
178
 
178
 
179
double func_min(double_list *p)
179
double func_min(double_list *p)
180
{
180
{
181
	if (!p)
181
	if (!p)
182
		serror(ERR_BADPARAM);
182
		serror(ERR_BADPARAM);
183
	double res = p->val;
183
	double res = p->val;
184
	p = p->next;
184
	p = p->next;
185
	while (p)
185
	while (p)
186
	{
186
	{
187
		if (p->code)
187
		if (p->code)
188
			code = p->code;
188
			code = p->code;
189
		if (p->val < res)
189
		if (p->val < res)
190
			res = p->val;
190
			res = p->val;
191
		p = p->next;
191
		p = p->next;
192
	}
192
	}
193
	return res;
193
	return res;
194
}
194
}
195
 
195
 
196
double func_max(double_list *p)
196
double func_max(double_list *p)
197
{
197
{
198
	if (!p)
198
	if (!p)
199
		serror(ERR_BADPARAM);
199
		serror(ERR_BADPARAM);
200
	double res = p->val;
200
	double res = p->val;
201
	p = p->next;
201
	p = p->next;
202
	while (p)
202
	while (p)
203
	{
203
	{
204
		if (p->code)
204
		if (p->code)
205
			code = p->code;
205
			code = p->code;
206
		if (p->val > res)
206
		if (p->val > res)
207
			res = p->val;
207
			res = p->val;
208
		p = p->next;
208
		p = p->next;
209
	}
209
	}
210
	return res;
210
	return res;
211
}
211
}
212
 
212
 
213
double avg(double_list *p)
213
double avg(double_list *p)
214
{
214
{
215
	double res = 0.0;
215
	double res = 0.0;
216
	int count = 0;
216
	int count = 0;
217
	while (p)
217
	while (p)
218
	{
218
	{
219
		if (p->code)
219
		if (p->code)
220
			code = p->code;
220
			code = p->code;
221
		res += p->val;
221
		res += p->val;
222
		count++;
222
		count++;
223
		p = p->next;
223
		p = p->next;
224
	}
224
	}
225
	return res / count;
225
	return res / count;
226
}
226
}
227
 
227
 
228
double func_isnull(char *str)
228
double func_isnull(char *str)
229
{
229
{
230
	if (code != 0)
230
	if (code != 0)
231
		return 0.0;
231
		return 0.0;
232
	double tmp = find_var(str);
232
	double tmp = find_var(str);
233
	int c = code;
233
	int c = code;
234
	code = 0;
234
	code = 0;
235
	if (c != 0)
235
	if (c != 0)
236
		return 1.0;
236
		return 1.0;
237
	return 0.0;
237
	return 0.0;
238
}
238
}
239
 
239
 
240
const double HALF = 0.5;
240
const double HALF = 0.5;
241
double func_ceil(double val)	// õîòåë round, à ïîëó÷èëñÿ ceil...
241
double func_ceil(double val)	// õîòåë round, à ïîëó÷èëñÿ ceil...
242
{
242
{
243
	int x;
243
	int x;
244
	__asm fld val
244
	__asm fld val
245
	__asm fld HALF				// äà, êðèâîðóêî ^_^
245
	__asm fld HALF				// äà, êðèâîðóêî ^_^
246
	__asm fadd
246
	__asm fadd
247
	__asm fistp x
247
	__asm fistp x
248
	__asm fild x
248
	__asm fild x
249
}
249
}
250
 
250
 
251
double func_round(double val)
251
double func_round(double val)
252
{
252
{
253
	int x;
253
	int x;
254
	__asm fld val
254
	__asm fld val
255
	__asm fld epsilon
255
	__asm fld epsilon
256
	__asm fadd
256
	__asm fadd
257
	__asm fistp x
257
	__asm fistp x
258
	__asm fild x
258
	__asm fild x
259
}
259
}
260
 
260
 
261
//const double ALMOST_HALF = 0.5 - epsilon;
261
//const double ALMOST_HALF = 0.5 - epsilon;
262
double ALMOST_HALF;
262
double ALMOST_HALF;
263
extern void ALMOST_HALF_init(void)
263
extern void ALMOST_HALF_init(void)
264
{ ALMOST_HALF = 0.5 - epsilon; }
264
{ ALMOST_HALF = 0.5 - epsilon; }
265
double func_floor(double val)
265
double func_floor(double val)
266
{
266
{
267
	int x;
267
	int x;
268
	__asm fld val
268
	__asm fld val
269
	__asm fld ALMOST_HALF
269
	__asm fld ALMOST_HALF
270
	__asm fsub
270
	__asm fsub
271
	__asm fistp x
271
	__asm fistp x
272
	__asm fild x
272
	__asm fild x
273
}
273
}
274
 
274
 
275
double logic_xor(double a, double b)
275
double logic_xor(double a, double b)
276
{
276
{
277
	if (a == 0.0)
277
	if (a == 0.0)
278
		if (b == 0.0)
278
		if (b == 0.0)
279
			return 0.0;
279
			return 0.0;
280
		else
280
		else
281
			return 1.0;
281
			return 1.0;
282
	else
282
	else
283
		if (b == 0.0)
283
		if (b == 0.0)
284
			return 1.0;
284
			return 1.0;
285
		else
285
		else
286
			return 0.0;
286
			return 0.0;
287
}
287
}
288
 
288
 
289
double logic_and(double a, double b)
289
double logic_and(double a, double b)
290
{
290
{
291
	if (a == 0.0)
291
	if (a == 0.0)
292
		return 0.0;
292
		return 0.0;
293
	else
293
	else
294
		if (b == 0.0)
294
		if (b == 0.0)
295
			return 0.0;
295
			return 0.0;
296
		else
296
		else
297
			return 1.0;
297
			return 1.0;
298
}
298
}
299
 
299
 
300
double logic_or(double a, double b)
300
double logic_or(double a, double b)
301
{
301
{
302
	if (a == 0.0)
302
	if (a == 0.0)
303
		if (b == 0.0)
303
		if (b == 0.0)
304
			return 0.0;
304
			return 0.0;
305
		else
305
		else
306
			return 1.1;
306
			return 1.1;
307
	else
307
	else
308
		return 1.0;
308
		return 1.0;
309
}
309
}
310
 
310
 
311
double rand_seed;
311
double rand_seed;
312
double func_rand(double max)
312
double func_rand(double max)
313
{
313
{
314
	double q = (257.0 * rand_seed + 739.0);	// ÷èñëà îò áàëäû. íàäî âñòàâèòü ïðàâèëüíûå.
314
	double q = (257.0 * rand_seed + 739.0);	// ÷èñëà îò áàëäû. íàäî âñòàâèòü ïðàâèëüíûå.
315
	rand_seed = q - 65536.0 * func_floor(q / 65536.0); // äëÿ õîðîøåãî ðàñïðåäåëåíèÿ
315
	rand_seed = q - 65536.0 * func_floor(q / 65536.0); // äëÿ õîðîøåãî ðàñïðåäåëåíèÿ
316
	return q - max * func_floor(q / max);	 // äëÿ ìîäóëÿ
316
	return q - max * func_floor(q / max);	 // äëÿ ìîäóëÿ
317
}
317
}
318
 
318
 
319
/*
319
/*
320
double func_case(double_list *p)
320
double func_case(double_list *p)
321
{
321
{
322
	if (!p || !p->next)
322
	if (!p || !p->next)
323
	{
323
	{
324
		serror(ERR_BADPARAM);
324
		serror(ERR_BADPARAM);
325
		return 0.0;
325
		return 0.0;
326
	}
326
	}
327
	double x = p->val;
327
	double x = p->val;
328
	int count = (int)p->next->val;
328
	int count = (int)p->next->val;
329
	int i, k;
329
	int i, k;
330
 
330
 
331
	double_list *cur = p->next->next;
331
	double_list *cur = p->next->next;
332
	k = count;
332
	k = count;
333
	for (i = 0; i < count; i++)
333
	for (i = 0; i < count; i++)
334
	{
334
	{
335
		if (!cur)
335
		if (!cur)
336
		{
336
		{
337
			serror(ERR_BADPARAM);
337
			serror(ERR_BADPARAM);
338
			return 0.0;
338
			return 0.0;
339
		}
339
		}
340
		if (fabs(x - cur->val) < epsilon)
340
		if (fabs(x - cur->val) < epsilon)
341
		{
341
		{
342
			if (k != count + 1)
342
			if (k != count + 1)
343
			{
343
			{
344
				serror(ERR_GENERAL);
344
				serror(ERR_GENERAL);
345
				return 0.0;
345
				return 0.0;
346
			}
346
			}
347
			k = i;
347
			k = i;
348
		}
348
		}
349
		cur = cur->next;
349
		cur = cur->next;
350
	}
350
	}
351
 
351
 
352
	for (i = 0; i < k; i++)
352
	for (i = 0; i < k; i++)
353
	{
353
	{
354
		if (!cur)
354
		if (!cur)
355
		{
355
		{
356
			serror(ERR_BADPARAM);
356
			serror(ERR_BADPARAM);
357
			return 0.0;
357
			return 0.0;
358
		}
358
		}
359
		cur = cur->next;
359
		cur = cur->next;
360
	}
360
	}
361
	if (!cur)					// ïðîâåðêè áèï. äîñòàëè áèï.
361
	if (!cur)					// ïðîâåðêè áèï. äîñòàëè áèï.
362
	{
362
	{
363
		serror(ERR_BADPARAM);
363
		serror(ERR_BADPARAM);
364
		return 0.0;
364
		return 0.0;
365
	}
365
	}
366
	if (cur->code)
366
	if (cur->code)
367
		code = cur->code;
367
		code = cur->code;
368
	return cur->val;
368
	return cur->val;
369
}
369
}
370
*/
370
*/
371
 
371
 
372
#define INF_ARGS -1
372
#define INF_ARGS -1
373
#define STR_ARG -2
373
#define STR_ARG -2
374
 
374
 
375
// represents general mathematical function
375
// represents general mathematical function
376
typedef double(*matfunc0)();
376
typedef double(*matfunc0)();
377
typedef double(*matfunc)(double);
377
typedef double(*matfunc)(double);
378
typedef double(*matfunc2)(double,double);
378
typedef double(*matfunc2)(double,double);
379
typedef double(*matfunc3)(double,double,double);
379
typedef double(*matfunc3)(double,double,double);
380
typedef double(*matfunc_inf)(double_list*);
380
typedef double(*matfunc_inf)(double_list*);
381
typedef double(*matfunc_str)(char*);
381
typedef double(*matfunc_str)(char*);
382
 
382
 
383
// used to link function name to the function
383
// used to link function name to the function
384
typedef struct  
384
typedef struct  
385
{
385
{
386
	char name[10];
386
	char name[10];
387
	int args;
387
	int args;
388
	void * f;
388
	void * f;
389
} func;
389
} func;
390
 
390
 
391
// the list of functions
391
// the list of functions
392
const int max_func = 28;
392
const int max_func = 28;
393
func functions[max_func] = 
393
func functions[max_func] = 
394
{
394
{
395
	"", 1, NULL,								// íå ïîìíþ, ñ êàêîé öåëüþ
395
	"", 1, NULL,								// íå ïîìíþ, ñ êàêîé öåëüþ
396
	"sin", 1, &sin,
396
	"sin", 1, &sin,
397
	"cos", 1, &cos,
397
	"cos", 1, &cos,
398
	"exp", 1, &exp,
398
	"exp", 1, &exp,
399
	"sqrt", 1, &sqrt,
399
	"sqrt", 1, &sqrt,
400
	"log", 1, &log,
400
	"log", 1, &log,
401
	"tg", 1, &tg,
401
	"tg", 1, &tg,
402
	"ctg", 1, &ctg,
402
	"ctg", 1, &ctg,
403
	"arcsin", 1, &asin,
403
	"arcsin", 1, &asin,
404
	"arccos", 1, &acos,
404
	"arccos", 1, &acos,
405
	"arctg", 1, &atan,							// íå ðåàëèçîâàíî. âîçâðàùàåò îøèáêó ERR_GENERAL
405
	"arctg", 1, &atan,							// íå ðåàëèçîâàíî. âîçâðàùàåò îøèáêó ERR_GENERAL
406
	"abs", 1, &fabs,
406
	"abs", 1, &fabs,
407
	"pow", 2, &pow,
407
	"pow", 2, &pow,
408
	"if", INF_ARGS, &func_if,
408
	"if", INF_ARGS, &func_if,
409
	"sum",INF_ARGS,&sum,
409
	"sum",INF_ARGS,&sum,
410
	"isnull",STR_ARG,&func_isnull,				// ñëåãêà ÷/æ
410
	"isnull",STR_ARG,&func_isnull,				// ñëåãêà ÷/æ
411
	"min",INF_ARGS,&func_min,
411
	"min",INF_ARGS,&func_min,
412
	"max",INF_ARGS,&func_max,
412
	"max",INF_ARGS,&func_max,
413
	"avg",INF_ARGS,&avg,
413
	"avg",INF_ARGS,&avg,
414
	"ceil",1,&func_ceil,
414
	"ceil",1,&func_ceil,
415
	"round",1,&func_round,
415
	"round",1,&func_round,
416
	"floor",1,&func_floor,
416
	"floor",1,&func_floor,
417
	"and",2,&logic_and,
417
	"and",2,&logic_and,
418
	"or",2,&logic_or,
418
	"or",2,&logic_or,
419
	"xor",2,&logic_xor,
419
	"xor",2,&logic_xor,
420
	"rand",1,&func_rand,
420
	"rand",1,&func_rand,
421
	//"case",INF_ARGS,&func_case,
421
	//"case",INF_ARGS,&func_case,
422
	"pi",0,&func_pi,
422
	"pi",0,&func_pi,
423
	"eps",0,&func_eps
423
	"eps",0,&func_eps
424
};
424
};
425
 
425
 
426
// all delimiters
426
// all delimiters
427
#define MAXDELIM 17
427
#define MAXDELIM 17
428
const char delim[MAXDELIM]="+-*^/%=;(),><#! ";		// not bad words
428
const char delim[MAXDELIM]="+-*^/%=;(),><#! ";		// not bad words
429
 
429
 
430
 
430
 
431
int isdelim(char c)
431
int isdelim(char c)
432
{
432
{
433
	//return strchr(delim, c) != 0;
433
	//return strchr(delim, c) != 0;
434
	for (int i = 0; i < MAXDELIM; i++)
434
	for (int i = 0; i < MAXDELIM; i++)
435
		if (c == delim[i])
435
		if (c == delim[i])
436
			return 1;
436
			return 1;
437
	return 0;
437
	return 0;
438
}
438
}
439
 
439
 
440
int isdigit(char c)
440
int isdigit(char c)
441
{
441
{
442
	return (c >= '0' && c <= '9');
442
	return (c >= '0' && c <= '9');
443
}
443
}
444
 
444
 
445
int isalpha2(char c)
445
int isalpha2(char c)
446
{
446
{
447
	return ((c >= 'a' && c <= 'z')
447
	return ((c >= 'a' && c <= 'z')
448
		|| (c >= 'A' && c <= 'Z') || (c=='$'));
448
		|| (c >= 'A' && c <= 'Z') || (c=='$'));
449
}
449
}
450
 
450
 
451
int iswhite(char c)
451
int iswhite(char c)
452
{
452
{
453
	return (c==' ' || c=='\t');
453
	return (c==' ' || c=='\t');
454
}
454
}
455
 
455
 
456
 
456
 
457
void serror(int acode)
457
void serror(int acode)
458
{
458
{
459
	if (acode != 0)
459
	if (acode != 0)
460
		code = acode;
460
		code = acode;
461
}
461
}
462
 
462
 
463
void set_exp(char *exp)
463
void set_exp(char *exp)
464
{
464
{
465
	prog = exp;
465
	prog = exp;
466
}
466
}
467
 
467
 
468
int get_token()
468
int get_token()
469
{
469
{
470
	int tok;
470
	int tok;
471
	char *temp;
471
	char *temp;
472
	(token_type) = 0;
472
	(token_type) = 0;
473
	tok = 0;
473
	tok = 0;
474
	temp = (token);
474
	temp = (token);
475
 
475
 
476
	if (*(prog) == '\0')
476
	if (*(prog) == '\0')
477
	{
477
	{
478
		*(token) = 0;
478
		*(token) = 0;
479
		tok = FINISHED;
479
		tok = FINISHED;
480
		return ((token_type) = DELIMITER);
480
		return ((token_type) = DELIMITER);
481
	}
481
	}
482
	while (iswhite(*(prog))) ++(prog);
482
	while (iswhite(*(prog))) ++(prog);
483
	if (isdelim(*(prog)))
483
	if (isdelim(*(prog)))
484
	{
484
	{
485
		char t=*temp = *(prog);
485
		char t=*temp = *(prog);
486
		(prog)++;
486
		(prog)++;
487
		temp++;
487
		temp++;
488
		if ((t == '>' || t == '<' || t == '!') && (*prog) && (*prog == '='))
488
		if ((t == '>' || t == '<' || t == '!') && (*prog) && (*prog == '='))
489
		{
489
		{
490
			*temp = *(prog);
490
			*temp = *(prog);
491
			(prog)++;
491
			(prog)++;
492
			temp++;
492
			temp++;
493
		}
493
		}
494
		*temp = 0;
494
		*temp = 0;
495
		return ((token_type) = DELIMITER);
495
		return ((token_type) = DELIMITER);
496
	}
496
	}
497
	if (isdigit(*(prog)))
497
	if (isdigit(*(prog)))
498
	{
498
	{
499
		while (!isdelim(*(prog)))
499
		while (!isdelim(*(prog)))
500
			*temp++=*(prog)++;
500
			*temp++=*(prog)++;
501
		*temp = '\0';
501
		*temp = '\0';
502
		return ((token_type) = NUMBER);
502
		return ((token_type) = NUMBER);
503
	}
503
	}
504
	if (isalpha2(*(prog)))
504
	if (isalpha2(*(prog)))
505
	{
505
	{
506
		while (!isdelim(*(prog)))
506
		while (!isdelim(*(prog)))
507
			*temp++=*(prog)++;
507
			*temp++=*(prog)++;
508
		(token_type) = VARIABLE;
508
		(token_type) = VARIABLE;
509
	}
509
	}
510
	*temp = '\0';
510
	*temp = '\0';
511
	if ((token_type) == VARIABLE)
511
	if ((token_type) == VARIABLE)
512
	{
512
	{
513
		tok = look_up((token));
513
		tok = look_up((token));
514
		if (tok)
514
		if (tok)
515
			(token_type) = FUNCTION;
515
			(token_type) = FUNCTION;
516
	}
516
	}
517
	return (token_type);
517
	return (token_type);
518
}
518
}
519
 
519
 
520
double sign(double d)
520
double sign(double d)
521
{
521
{
522
  if (d > 0.0)
522
  if (d > 0.0)
523
    return 1.0;
523
    return 1.0;
524
  if (d < 0.0)
524
  if (d < 0.0)
525
    return -1.0;
525
    return -1.0;
526
  return 0.0;
526
  return 0.0;
527
}
527
}
528
 
528
 
529
void putback()
529
void putback()
530
{
530
{
531
	char *t;
531
	char *t;
532
	t = (token);
532
	t = (token);
533
	for (;*t;t++)
533
	for (;*t;t++)
534
		(prog)--;
534
		(prog)--;
535
}
535
}
536
 
536
 
537
int get_exp(double *hold)
537
int get_exp(double *hold)
538
{
538
{
539
	code = 0;
539
	code = 0;
540
 
540
 
541
	get_token();
541
	get_token();
542
	if (!*(token))
542
	if (!*(token))
543
	{
543
	{
544
		return 0;
544
		return 0;
545
	}
545
	}
546
	level1( hold);
546
	level1( hold);
547
	putback();
547
	putback();
548
  return code==0;
548
  return code==0;
549
}
549
}
550
 
550
 
551
void level1(double *hold)
551
void level1(double *hold)
552
{
552
{
553
	char op[2];
553
	char op[2];
554
	double h;
554
	double h;
555
 
555
 
556
	level1_5( hold);
556
	level1_5( hold);
557
	while (op[0] = *token, op[1] = (*(token+1)) ? *(token + 1) : 0,
557
	while (op[0] = *token, op[1] = (*(token+1)) ? *(token + 1) : 0,
558
		*op == '<' || *op == '>' || *op == '=' || *op == '#' || *op == '!')
558
		*op == '<' || *op == '>' || *op == '=' || *op == '#' || *op == '!')
559
	{
559
	{
560
		get_token();
560
		get_token();
561
		level1_5( &h);
561
		level1_5( &h);
562
		logic(op, hold, &h);
562
		logic(op, hold, &h);
563
	}
563
	}
564
}
564
}
565
 
565
 
566
void level1_5(double *hold)
566
void level1_5(double *hold)
567
{
567
{
568
	char op;
568
	char op;
569
 
569
 
570
	op = 0;
570
	op = 0;
571
	if (((token_type) == DELIMITER) && *(token) == '!')
571
	if (((token_type) == DELIMITER) && *(token) == '!')
572
	{
572
	{
573
		op = *(token);
573
		op = *(token);
574
		get_token();
574
		get_token();
575
	}
575
	}
576
	level2( hold);
576
	level2( hold);
577
 
577
 
578
	if (op)
578
	if (op)
579
	{
579
	{
580
		if (*hold == 0.0)
580
		if (*hold == 0.0)
581
			*hold = 1.0;
581
			*hold = 1.0;
582
		else
582
		else
583
			*hold = 0.0;
583
			*hold = 0.0;
584
	}
584
	}
585
}
585
}
586
 
586
 
587
void level2(double *hold)
587
void level2(double *hold)
588
{
588
{
589
	char op;
589
	char op;
590
	double h;
590
	double h;
591
 
591
 
592
	level3( hold);
592
	level3( hold);
593
	while ((op=*(token)) == '+' || op == '-')
593
	while ((op=*(token)) == '+' || op == '-')
594
	{
594
	{
595
		get_token();
595
		get_token();
596
		level3( &h);
596
		level3( &h);
597
		arith(op, hold, &h);
597
		arith(op, hold, &h);
598
	}
598
	}
599
}
599
}
600
 
600
 
601
void level3(double *hold)
601
void level3(double *hold)
602
{
602
{
603
	char op;
603
	char op;
604
	double h;
604
	double h;
605
 
605
 
606
	level4( hold);
606
	level4( hold);
607
	while ((op=*(token)) == '*' || op == '/' || op == '%')
607
	while ((op=*(token)) == '*' || op == '/' || op == '%')
608
	{
608
	{
609
		get_token();
609
		get_token();
610
		level4( &h);
610
		level4( &h);
611
		arith( op, hold, &h);
611
		arith( op, hold, &h);
612
	}
612
	}
613
}
613
}
614
 
614
 
615
void level4(double *hold)
615
void level4(double *hold)
616
{
616
{
617
	double h;
617
	double h;
618
	level5( hold);
618
	level5( hold);
619
 
619
 
620
	if (*(token) == '^')
620
	if (*(token) == '^')
621
	{
621
	{
622
		get_token();
622
		get_token();
623
		level5( &h);
623
		level5( &h);
624
		arith( '^', hold, &h);
624
		arith( '^', hold, &h);
625
	}
625
	}
626
}
626
}
627
 
627
 
628
void level5(double *hold)
628
void level5(double *hold)
629
{
629
{
630
	char op;
630
	char op;
631
 
631
 
632
	op = 0;
632
	op = 0;
633
	if (((token_type) == DELIMITER) && *(token) == '+' || *(token) == '-')
633
	if (((token_type) == DELIMITER) && *(token) == '+' || *(token) == '-')
634
	{
634
	{
635
		op = *(token);
635
		op = *(token);
636
		get_token();
636
		get_token();
637
	}
637
	}
638
	level6( hold);
638
	level6( hold);
639
 
639
 
640
	if (op)
640
	if (op)
641
		unary(op, hold);
641
		unary(op, hold);
642
}
642
}
643
 
643
 
644
void level6(double *hold)
644
void level6(double *hold)
645
{
645
{
646
	if ((*(token) == '(') && ((token_type) == DELIMITER))
646
	if ((*(token) == '(') && ((token_type) == DELIMITER))
647
	{
647
	{
648
		get_token();
648
		get_token();
649
		level1( hold);
649
		level1( hold);
650
		if (*(token) != ')')
650
		if (*(token) != ')')
651
			  serror( ERR_NOBRACKET);
651
			  serror( ERR_NOBRACKET);
652
		get_token();
652
		get_token();
653
	}
653
	}
654
	else
654
	else
655
		primitive( hold);
655
		primitive( hold);
656
}
656
}
657
 
657
 
658
void calc_function(double *hold)
658
void calc_function(double *hold)
659
{
659
{
660
  double_list *args = NULL, *last = NULL, *t;		
660
  double_list *args = NULL, *last = NULL, *t;		
661
  double d;
661
  double d;
662
	int i,argc=0,save_code;
662
	int i,argc=0,save_code;
663
 
663
 
664
	save_code = code;
664
	save_code = code;
665
	code = 0;
665
	code = 0;
666
	i = look_up(token);
666
	i = look_up(token);
667
 
667
 
668
	if (i == 0)
668
	if (i == 0)
669
		serror(ERR_BADFUNCTION);	// error
669
		serror(ERR_BADFUNCTION);	// error
670
 
670
 
671
	get_token();
671
	get_token();
672
	if (*(token) != '(')
672
	if (*(token) != '(')
673
		serror(ERR_NOBRACKET);	// error
673
		serror(ERR_NOBRACKET);	// error
674
	//get_token();
674
	//get_token();
675
	if (functions[i].args == STR_ARG)
675
	if (functions[i].args == STR_ARG)
676
	{
676
	{
677
		get_token();
677
		get_token();
678
		d = ((matfunc_str)(functions[i].f))(token);
678
		d = ((matfunc_str)(functions[i].f))(token);
679
		*hold = d;
679
		*hold = d;
680
		get_token();
680
		get_token();
681
		if (save_code)
681
		if (save_code)
682
			code = save_code;
682
			code = save_code;
683
		return;
683
		return;
684
	}
684
	}
685
 
685
 
686
	//last = args = (double_list*)malloc(sizeof(double_list));
686
	//last = args = (double_list*)malloc(sizeof(double_list));
687
	//args->next = NULL;
687
	//args->next = NULL;
688
	//level1(&args->val);
688
	//level1(&args->val);
689
	//get_token();
689
	//get_token();
690
	argc=0;
690
	argc=0;
691
	do 
691
	do 
692
	{
692
	{
693
		get_token();
693
		get_token();
694
		if (*token == ')')
694
		if (*token == ')')
695
			break;
695
			break;
696
		t = (double_list*)allocmem(sizeof(double_list));
696
		t = (double_list*)allocmem(sizeof(double_list));
697
		code = 0;
697
		code = 0;
698
		level1(&t->val);
698
		level1(&t->val);
699
		t->code = code;
699
		t->code = code;
700
		t->next = NULL;
700
		t->next = NULL;
701
		if (last)
701
		if (last)
702
			last->next = t;
702
			last->next = t;
703
		else
703
		else
704
			args = t;
704
			args = t;
705
		last = t;
705
		last = t;
706
		argc++;
706
		argc++;
707
	} while (*token == ',');
707
	} while (*token == ',');
708
 
708
 
709
	code = save_code;
709
	code = save_code;
710
 
710
 
711
	if (argc != functions[i].args && functions[i].args >= 0)
711
	if (argc != functions[i].args && functions[i].args >= 0)
712
	{
712
	{
713
		serror(ERR_BADPARAM);
713
		serror(ERR_BADPARAM);
714
	}
714
	}
715
	else
715
	else
716
	{
716
	{
717
		switch (functions[i].args)
717
		switch (functions[i].args)
718
		{
718
		{
719
			case 0:
719
			case 0:
720
				d = ((matfunc0)(functions[i].f))();
720
				d = ((matfunc0)(functions[i].f))();
721
				break;
721
				break;
722
			case 1:
722
			case 1:
723
				d = ((matfunc)(functions[i].f))(args->val);
723
				d = ((matfunc)(functions[i].f))(args->val);
724
				break;			
724
				break;			
725
			case 2:
725
			case 2:
726
				d = ((matfunc2)(functions[i].f))(args->val,args->next->val);
726
				d = ((matfunc2)(functions[i].f))(args->val,args->next->val);
727
				break;			
727
				break;			
728
			case 3:
728
			case 3:
729
				d = ((matfunc3)(functions[i].f))(args->val,args->next->val,args->next->next->val);
729
				d = ((matfunc3)(functions[i].f))(args->val,args->next->val,args->next->next->val);
730
				break;		
730
				break;		
731
			case INF_ARGS:
731
			case INF_ARGS:
732
				d = ((matfunc_inf)(functions[i].f))(args);
732
				d = ((matfunc_inf)(functions[i].f))(args);
733
				break;
733
				break;
734
		}
734
		}
735
	}
735
	}
736
 
736
 
737
	t = args;
737
	t = args;
738
	while (t)
738
	while (t)
739
	{
739
	{
740
		args = t->next;
740
		args = t->next;
741
		freemem(t);
741
		freemem(t);
742
		t = args;
742
		t = args;
743
	}
743
	}
744
	
744
	
745
  *hold = d;
745
  *hold = d;
746
//  else
746
//  else
747
//    serror( ERR_OVERFLOW);
747
//    serror( ERR_OVERFLOW);
748
 
748
 
749
}
749
}
750
 
750
 
751
void primitive(double *hold)
751
void primitive(double *hold)
752
{
752
{
753
	switch (token_type)
753
	switch (token_type)
754
	{
754
	{
755
	case VARIABLE:
755
	case VARIABLE:
756
		*hold = find_var(token);
756
		*hold = find_var(token);
757
		get_token();
757
		get_token();
758
		return;
758
		return;
759
	case NUMBER:
759
	case NUMBER:
760
    //
760
    //
761
		*hold = atof((token));
761
		*hold = atof((token));
762
    //if (sscanf(token, "%lf", hold) != 1)
762
    //if (sscanf(token, "%lf", hold) != 1)
763
	*hold = convert(token);
763
	*hold = convert(token);
764
	if (convert_error == ERROR)
764
	if (convert_error == ERROR)
765
      serror( ERR_BADNUMER);
765
      serror( ERR_BADNUMER);
766
		get_token();
766
		get_token();
767
		return;
767
		return;
768
	case FUNCTION:
768
	case FUNCTION:
769
		calc_function( hold);
769
		calc_function( hold);
770
		if (*token != ')')
770
		if (*token != ')')
771
			serror(ERR_NOBRACKET);
771
			serror(ERR_NOBRACKET);
772
		get_token();
772
		get_token();
773
		return;
773
		return;
774
	default:	// error
774
	default:	// error
775
		return;
775
		return;
776
	}
776
	}
777
}
777
}
778
 
778
 
779
void arith(char op, double *r, double *h)
779
void arith(char op, double *r, double *h)
780
{
780
{
781
	double t;
781
	double t;
782
	switch(op)
782
	switch(op)
783
	{
783
	{
784
	case '-':
784
	case '-':
785
		*r = *r - *h;
785
		*r = *r - *h;
786
		break;
786
		break;
787
	case '+':
787
	case '+':
788
		*r = *r + *h;
788
		*r = *r + *h;
789
		break;
789
		break;
790
	case '*':
790
	case '*':
791
		*r = *r * *h;
791
		*r = *r * *h;
792
		break;
792
		break;
793
	case '/':
793
	case '/':
794
	    if (fabs(*h) < epsilon)
794
	    if (fabs(*h) < epsilon)
795
			serror( ERR_OVERFLOW);
795
			serror( ERR_OVERFLOW);
796
		else
796
		else
797
		  *r = (*r) / (*h);
797
		  *r = (*r) / (*h);
798
		break;
798
		break;
799
	case '%':
799
	case '%':
800
	    if (fabs(*h) < epsilon)
800
	    if (fabs(*h) < epsilon)
801
			serror( ERR_OVERFLOW);
801
			serror( ERR_OVERFLOW);
802
		else
802
		else
803
		{
803
		{
804
			t = func_floor ((*r) / (*h));
804
			t = func_floor ((*r) / (*h));
805
			*r = *r - (t * (*h));
805
			*r = *r - (t * (*h));
806
		}
806
		}
807
		break;
807
		break;
808
	case '^':
808
	case '^':
809
		*r = pow(*r, *h);
809
		*r = pow(*r, *h);
810
		break;
810
		break;
811
	}
811
	}
812
}
812
}
813
 
813
 
814
void logic(char *op, double *r, double *h)
814
void logic(char *op, double *r, double *h)
815
{
815
{
816
	double t;
816
	double t;
817
	switch (*op)
817
	switch (*op)
818
	{
818
	{
819
	case '<':
819
	case '<':
820
		if (*(op+1) && *(op+1) == '=')
820
		if (*(op+1) && *(op+1) == '=')
821
			t = *r <= *h + epsilon ? 1.0 : 0.0;
821
			t = *r <= *h + epsilon ? 1.0 : 0.0;
822
		else				
822
		else				
823
			t = *r < *h - epsilon? 1.0 : 0.0;	
823
			t = *r < *h - epsilon? 1.0 : 0.0;	
824
		break;
824
		break;
825
	case '>':
825
	case '>':
826
		if (*(op+1) && *(op+1) == '=')
826
		if (*(op+1) && *(op+1) == '=')
827
			t = *r >= *h - epsilon ? 1.0 : 0.0;
827
			t = *r >= *h - epsilon ? 1.0 : 0.0;
828
		else				
828
		else				
829
			t = *r > *h + epsilon ? 1.0 : 0.0;
829
			t = *r > *h + epsilon ? 1.0 : 0.0;
830
		break;
830
		break;
831
	case '=':
831
	case '=':
832
		t = fabs(*r - *h) <= epsilon ? 1.0 : 0.0;
832
		t = fabs(*r - *h) <= epsilon ? 1.0 : 0.0;
833
		break;
833
		break;
834
	case '#':
834
	case '#':
835
		t = fabs(*r - *h) > epsilon ? 1.0 : 0.0;
835
		t = fabs(*r - *h) > epsilon ? 1.0 : 0.0;
836
		break;
836
		break;
837
	case '!':
837
	case '!':
838
		if (*(op+1) && *(op+1) == '=')
838
		if (*(op+1) && *(op+1) == '=')
839
			t = fabs(*r - *h) > epsilon ? 1.0 : 0.0;
839
			t = fabs(*r - *h) > epsilon ? 1.0 : 0.0;
840
		else
840
		else
841
			serror(ERR_GENERAL);
841
			serror(ERR_GENERAL);
842
		break;
842
		break;
843
	}
843
	}
844
	*r = t;
844
	*r = t;
845
}
845
}
846
 
846
 
847
 
847
 
848
void unary(char op, double *r)
848
void unary(char op, double *r)
849
{
849
{
850
	if (op == '-')
850
	if (op == '-')
851
		*r = -(*r);
851
		*r = -(*r);
852
}
852
}
853
 
853
 
854
bool strcmp(char *s1, char *s2)
854
bool strcmp(char *s1, char *s2)
855
{
855
{
856
	int i;
856
	int i;
857
 
857
 
858
	if (s1 == NULL)
858
	if (s1 == NULL)
859
		if (s2 == NULL)
859
		if (s2 == NULL)
860
			return 0;
860
			return 0;
861
		else
861
		else
862
			return 1;
862
			return 1;
863
	else
863
	else
864
		if (s2 == NULL)
864
		if (s2 == NULL)
865
			return 1;
865
			return 1;
866
 
866
 
867
	for (i = 0;;i++)
867
	for (i = 0;;i++)
868
	{
868
	{
869
		if (s1[i] == '\0')
869
		if (s1[i] == '\0')
870
			if (s2[i] == '\0')
870
			if (s2[i] == '\0')
871
				return 0;
871
				return 0;
872
			else
872
			else
873
				return 1;
873
				return 1;
874
		else
874
		else
875
			if (s2[i] == '\0')
875
			if (s2[i] == '\0')
876
				return 1;
876
				return 1;
877
			else
877
			else
878
			{
878
			{
879
				if (s1[i] != s2[i])
879
				if (s1[i] != s2[i])
880
					return 1;
880
					return 1;
881
			}
881
			}
882
	}
882
	}
883
	return 0;
883
	return 0;
884
}
884
}
885
 
885
 
886
 
886
 
887
bool strncmp(char *s1, char *s2, int n)
887
bool strncmp(char *s1, char *s2, int n)
888
{
888
{
889
	int i;
889
	int i;
890
 
890
 
891
	if (s1 == NULL)
891
	if (s1 == NULL)
892
		if (s2 == NULL)
892
		if (s2 == NULL)
893
			return 0;
893
			return 0;
894
		else
894
		else
895
			return 1;
895
			return 1;
896
	else
896
	else
897
		if (s2 == NULL)
897
		if (s2 == NULL)
898
			return 1;
898
			return 1;
899
 
899
 
900
	for (i = 0;i
900
	for (i = 0;i
901
	{
901
	{
902
		if (s1[i] == '\0')
902
		if (s1[i] == '\0')
903
			if (s2[i] == '\0')
903
			if (s2[i] == '\0')
904
				return 0;
904
				return 0;
905
			else
905
			else
906
				return 1;
906
				return 1;
907
		else
907
		else
908
			if (s2[i] == '\0')
908
			if (s2[i] == '\0')
909
				return 1;
909
				return 1;
910
			else
910
			else
911
			{
911
			{
912
				if (s1[i] != s2[i])
912
				if (s1[i] != s2[i])
913
					return 1;
913
					return 1;
914
			}
914
			}
915
	}
915
	}
916
	return 0;
916
	return 0;
917
}
917
}
918
 
918
 
919
int look_up(char *s)
919
int look_up(char *s)
920
{
920
{
921
	int i;
921
	int i;
922
 
922
 
923
	for (i = 0; i < max_func; i++)
923
	for (i = 0; i < max_func; i++)
924
		if (strcmp(s, functions[i].name) == 0)
924
		if (strcmp(s, functions[i].name) == 0)
925
			return i;
925
			return i;
926
	return 0;	// search command/function name
926
	return 0;	// search command/function name
927
}
927
}
928
>
928
 
929
>
929
unsigned int chrnum(char* text, char symbol)
-
 
930
{
-
 
931
	int num = 0;
-
 
932
	int i = 0;
-
 
933
	while(text[i])
-
 
934
	{ 
-
 
935
		if (text[i] == symbol) num++;
-
 
936
		i++;
-
 
937
	}
-
 
938
	return num;
-
 
939
}
-
 
940
>
-
 
941
>