-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
491 lines (382 loc) · 8.66 KB
/
parser.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "jac.h"
#include "lists.h"
#include <math.h>
#define TAIL_OP 'x' /* Operator of the tail node */
#define ERROR '?'
#ifndef M_PIl
#define M_PIl 3.141592653589793238462643383279502884L
#endif
enum functions {COS,SIN,ASINH,ASIN,ATAN,ACOS,SQRT,SINH,COSH,TANH,TAN,LOG,LN,MULT_DIV_POW_MOD,NONE};
void reverse(struct node** head_ref);
void add_item (struct node **ptr, long double data);
void delNextNode (struct node *node_pt);
long double calculate (struct node **head);
long double evaluateFuncResult (struct control *jac, enum functions func);
long double switchFunc(enum functions *func, const long double *number);
unsigned long factorial(unsigned long f);
/*int bin_dec(long long n);
long long dec_bin(int n);
*/
long double parseConstants(struct control *jac);
bool searchBinaryFunction (struct control *jac, struct node *head);
bool searchPowFunction (struct control *jac, long double *number);
enum functions searchFunction (struct control *jac);
long double mult_div_pow_mod(long double number)
{
return number;
}
long double my_cosine(long double number)
{
long double precision = 0.01;
long double mod_pi = fmodl(number, M_PI/2);
if (mod_pi <= precision)
return 0;
else
return (cosl(number));
}
long double my_sine(long double number)
{
long double precision = 0.01;
long double mod_pi = fmodl(number, M_PI);
if (mod_pi <= precision)
return 0;
else
return (sinl(number));
}
long double abort_parsing(struct node *head)
{
if (head != NULL)
free(head);
return -2;
}
long double parse_evaluate_expr(struct control *jac, bool inFunc)
{
long double number;
unsigned int n = 0;
struct node *head = NULL;
enum functions func = NONE;
while (*jac->buf != '\0' && jac->buf[0] != ERROR)
{
if (1 == sscanf(jac->buf, "%Lf%n", &number, &n))
{
jac->buf += n;
add_item(&head, number);
if (*jac->buf == '(' || *jac->buf == '[' || *jac->buf == '{' || isalpha(*jac->buf)) /* For situations like 5(3+2) */
head->op = '*';
}
else if (*jac->buf == '-' || *jac->buf == '+') /* Important for situation like -(5+3) which jac translates into -1*(5+3) */
{
if (*jac->buf == '-')
add_item(&head, -1);
else
add_item(&head, 1);
jac->buf++;
head->op = '*';
}
else if (*jac->buf == '(' || *jac->buf == '[' || *jac->buf == '{')
{
jac->buf++;
jac->par++;
add_item(&head, parse_evaluate_expr(jac, inFunc));
if (inFunc == true)
{
number = calculate(&head);
free(head);
return number;
}
if (*jac->buf != '\0') /* For situations like (3+2)5 */
if (isalpha(*jac->buf) || isdigit(*jac->buf))
head->op = '*';
}
else if (*jac->buf == ')' || *jac->buf == ']' || *jac->buf == '}')
{
jac->buf++;
jac->par--;
if (head != NULL && jac->par == 0)
{
number = calculate(&head);
free(head);
return number;
}
}
else if (searchBinaryFunction(jac, head))
;
else if (isalpha(*jac->buf))
{
/* Parsing for functions like sin, cos, etc */
if ((func = searchFunction(jac)) != NONE)
add_item(&head, evaluateFuncResult(jac, func));
/* Parse for constants */
if ((number = parseConstants(jac)) != 0)
{
add_item(&head, number);
if (*jac->buf == '(') /* For situations like 5(3+2) */
head->op = '*';
}
if (*jac->buf == 'E')
{
add_item(&head, 10);
jac->buf++;
if (1 == sscanf(jac->buf, "%Lf%n", &number, &n))
{
jac->buf += n;
head->value = pow(head->value, number);
}
else
jac->buf[0] = ERROR;
}
}
else
continue;
} /* End of parsing */
/* Once parsed, jac evaluates the expression */
if (jac->buf[0] == ERROR)
return abort_parsing(head);
else if (head == NULL)
{
jac->buf[0] = ERROR;
return -2;
}
else
{
number = calculate(&head);
free(head);
return number;
}
}
bool searchBinaryFunction (struct control *jac, struct node *head)
{
if (*jac->buf == '/')
{
jac->buf++;
head->value = head->value / (evaluateFuncResult(jac, MULT_DIV_POW_MOD));
}
else if (*jac->buf == '*')
{
jac->buf++;
head->value = head->value * (evaluateFuncResult(jac, MULT_DIV_POW_MOD));
head->op = TAIL_OP; /* we don't want head->op to contain '*' and cause interference in calculate() */
}
else if (*jac->buf == '^')
{
jac->buf++;
head->value = pow(head->value, evaluateFuncResult(jac, MULT_DIV_POW_MOD));
}
else if (*jac->buf == '!') /* Factorial */
{
jac->buf++;
head->value = factorial(head->value);
}
else if (*jac->buf == '%')
{
jac->buf++;
head->value = fmodl(head->value, evaluateFuncResult(jac, MULT_DIV_POW_MOD));
}
else
return false;
return true;
}
long double parseConstants(struct control *jac)
{
long double data = 0;
if (strncmp(jac->buf, "pi", 2) == 0)
{
data = M_PIl;
jac->buf += 2;
}
else if (strncmp(jac->buf, "m_p", 3) == 0) /* Proton Mass */
{
data = PROTON_MASS;
jac->buf += 3;
}
else if (*jac->buf == 'e')
{
data = M_E;
jac->buf += 1;
}
else if (strncmp(jac->buf, "m_e", 3) == 0) /* Electron mass */
{
data = ELECTRON_MASS;
jac->buf += 3;
}
else if (strncmp(jac->buf, "c_0", 3) == 0) /* Speed of light in vacuum m/s (exact) */
{
data = SPEED_LIGHT;
jac->buf += 3;
}
else if (*jac->buf == 'q') /* elementary charge*/
{
data = CHARGE;
jac->buf++;
}
else if (*jac->buf == 'h') /* Plank's constant*/
{
data = PLANK;
jac->buf++;
}
else if (strncmp(jac->buf, "e_0", 3) == 0) /* Permittivity of free space */
{
data = E_0;
jac->buf += 3;
}
else if (strncmp(jac->buf, "n_a", 3) == 0) /* Avogadros's number */
{
data = AVOGADRO;
jac->buf += 3;
}
else if (*jac->buf == 'k') /* Boltzmann's constant */
{
data = K_B;
jac->buf++;
}
else
return data;
return data;
}
long double evaluateFuncResult (struct control *jac, enum functions func)
{
long double number;
unsigned int n;
typedef long double (*my_func)(long double);
enum functions func2;
static my_func array[] = {my_cosine, my_sine, asinhl, asinl, atanl, acosl, sqrtl, sinhl, coshl, tanhl, tanl, log10l, logl, mult_div_pow_mod};
if (1 == sscanf(jac->buf, "%Lf%n", &number, &n))
{
jac->buf += n;
while(searchPowFunction(jac, &number)) /* For cases like 5^2^3^.. */
{;}
return (array[func](number));
}
else if (isalpha(*jac->buf)) /* Parse functions and constants */
{
if ((func2 = searchFunction(jac)) != NONE)
return evaluateFuncResult(jac, func2);
else
{
number = parseConstants(jac);
if (number != 0)
return (array[func](number));
}
}
else
number = parse_evaluate_expr(jac, true);
return (array[func](number));
}
enum functions searchFunction (struct control *jac)
{
enum functions func = NONE;
if (strncmp(jac->buf, "cos", 3) == 0)
{
jac->buf += 3;
func = COS;
}
else if (strncmp(jac->buf, "sin", 3) == 0)
{
jac->buf += 3;
func = SIN;
}
else if (strncmp(jac->buf, "asinhl", 6) == 0)
{
jac->buf += 6;
func = ASINH;
}
else if (strncmp(jac->buf, "asin", 4) == 0)
{
jac->buf += 4;
func = ASIN;
}
else if (strncmp(jac->buf, "atan", 4) == 0)
{
jac->buf += 4;
func = ATAN;
}
else if (strncmp(jac->buf, "acos", 4) == 0)
{
jac->buf += 4;
func = ACOS;
}
else if (strncmp(jac->buf, "sqrt", 4) == 0)
{
jac->buf += 4;
func = SQRT;
}
else if (strncmp(jac->buf, "sinh", 4) == 0)
{
jac->buf += 4;
func = SINH;
}
else if (strncmp(jac->buf, "cosh", 4) == 0)
{
jac->buf += 4;
func = COSH;
}
else if (strncmp(jac->buf, "tanh", 4) == 0)
{
jac->buf += 4;
func = TANH;
}
else if (strncmp(jac->buf, "tan", 3) == 0)
{
jac->buf += 3;
func = TAN;
}
else if (strncmp(jac->buf, "log", 3) == 0)
{
jac->buf += 3;
func = LOG;
}
else if (strncmp(jac->buf, "ln", 2) == 0)
{
jac->buf += 2;
func = LN;
}
else
return NONE;
return func;
}
long double calculate (struct node **head)
{
reverse(head);
struct node *tmp = *head;
long double result;
while (tmp->next != NULL) /* this cycle is needed to evaluate situations like 5(..) */
{
if (tmp->op == '*')
{
tmp->value = tmp->value * tmp->next->value;
delNextNode(tmp);
}
else
tmp = tmp->next;
}
tmp = *head;
/* Add up all numbers with their signs */
while (tmp->next != NULL)
{
tmp->value = tmp->value + tmp->next->value;
delNextNode(tmp);
}
result = (*head)->value;
return result;
}
bool searchPowFunction (struct control *jac, long double *number)
{
if (*jac->buf == '^')
{
jac->buf += 1;
*number = pow(*number, evaluateFuncResult(jac, MULT_DIV_POW_MOD));
}
else if (*jac->buf == '!') /* Factorial */
{
jac->buf += 1;
*number = factorial(*number);
}
else if (*jac->buf == '%')
{
jac->buf += 1;
*number = fmodl(*number, evaluateFuncResult(jac, MULT_DIV_POW_MOD));
}
else
return false;
return true;
}