forked from MetalOxideSemi/EJack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpret.c
666 lines (640 loc) · 20.1 KB
/
interpret.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#include "interpretOther.h"
Numeric *handleArithmeticExp(char *input, Environment *env, Numeric *num);
void calculate(Stack *numericStack, SStack *symbolStack);
void pushOperation(Stack *numericStack, SStack *symbolStack);
int parser(char *input, int now, Environment *env, Stack *numericStack, SStack *symbolStack);
static Operation operation;
//extern Numeric result;
void *interpret(char *input, Environment *env)
{
#ifdef DEBUG
static int ct = 0;
printf("%dth call interpret(%s)\n", ++ct, input);
#endif
Numeric num;
Numeric_new(&num);
if (isLetrec(input))
{
return handleLetrec(input, env);
}
else if (isLet(input)) //let //void*
{
return handleLet(input, env);
}
else if (isIf(input))
{
return handleIf(input, env);
}
else if (isNum(input)) //number //void*
{
return handleNum(input);
}
else if (isClosure(input)) // Closure
{
return handleClosure(input, env); //void*
}
else if (isAlphaExp(input)) // handle alphaExp (only alpha)
{
return handleAlphaExp(input, env); //void*
}
else if (isCall(input)) //Call function
{
return handleCall(input, env); //void*
}
else // Arithmetic expression
{
Numeric *result = malloc(sizeof(Numeric));
Numeric_new(result);
Numeric_assign(result, handleArithmeticExp(input, env, &num));
// return handleArithmeticExp(input, env); //numeric*
return result;
}
}
Numeric *handleArithmeticExp(char *input, Environment *env, Numeric *num)
{
Stack numericStack;
SStack symbolStack;
Stack_new(&numericStack);
SStack_new(&symbolStack);
int now = 0;
int parserValue = 0;
while (1)
{
parserValue = parser(input + now, now, env, &numericStack, &symbolStack);
if (parserValue == -1)
{
break;
}
now += parserValue;
}
while (SStack_empty(&symbolStack) != 1)
{
calculate(&numericStack, &symbolStack);
}
Stack_pop(&numericStack, num);
Stack_free(&numericStack);
SStack_free(&symbolStack);
return num;
}
int parser(char *input, int now, Environment *env, Stack *numericStack, SStack *symbolStack)
{
int i = 0, j = 0;
Numeric num;
char buf[BUFFER_LENTH] = "";
Numeric_new(&num);
while (input[j] == ' ')
{
++j;
}
if (input[j] == '-')
{
if (now == 0 || input[j - 1] == '(')
{
operation = CALSTACK_NEGATIVE;
++j;
}
else
{
operation = CALSTACK_SUB;
++j;
}
pushOperation(numericStack, symbolStack);
}
else if (isdigit(input[j]))
{
while (isdigit(input[j]) || input[j] == '.')
{
buf[i++] = input[j++];
}
buf[i] = '\0';
strtoNumeric(buf, &num);
Stack_push(numericStack, &num);
}
else if (input[j] == '(' || input[j] == ')' || input[j] == '+' || input[j] == '*' || input[j] == '/' ||
input[j] == '^' || input[j] == '$')
{
switch (input[j])
{
case '(':
operation = CALSTACK_LBRACKET;
++j;
pushOperation(numericStack, symbolStack);
break;
case ')':
operation = CALSTACK_RBRACKET;
++j;
pushOperation(numericStack, symbolStack);
break;
case '+':
operation = CALSTACK_ADD;
++j;
pushOperation(numericStack, symbolStack);
break;
case '*':
operation = CALSTACK_MULT;
++j;
pushOperation(numericStack, symbolStack);
break;
case '/':
operation = CALSTACK_DIV;
++j;
pushOperation(numericStack, symbolStack);
break;
case '^':
operation = CALSTACK_POW;
pushOperation(numericStack, symbolStack);
++j;
break;
case '$': //call lambda function
while (1)
{
buf[i++] = input[j++];
if (input[j] == '{')
{
int endFlag = 1;
while (1)
{
buf[i++] = input[j++];
if (input[j] == '{')
{
++endFlag;
}
else if (input[j] == '}')
{
--endFlag;
}
if (endFlag == 0)
{
buf[i++] = input[j++];
if (input[j] == '[')
{
endFlag = 1;
while (1)
{
buf[i++] = input[j++];
if (input[j] == '[')
{
++endFlag;
}
else if (input[j] == ']')
{
--endFlag;
}
if (endFlag == 0)
{
buf[i++] = input[j++];
break;
}
}
}
break;
}
}
break;
}
}
buf[i] = '\0';
Stack_push(numericStack, interpret(buf, env));
break;
default:
printf("Error: not found in '+ - * / ^ $ ( )'");
}
}
else if (isalpha(input[j]))
{
if (strlen(input + j) >= 3 && !strncmp(input + j, "ln(", 3))
{
operation = CALSTACK_LN;
pushOperation(numericStack, symbolStack);
j += 3;
}
else if (strlen(input + j) >= 3 && !strncmp(input + j, "lg(", 3))
{
operation = CALSTACK_LG;
pushOperation(numericStack, symbolStack);
j += 3;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "sin(", 4))
{
operation = CALSTACK_SIN;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "cos(", 4))
{
operation = CALSTACK_COS;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "tan(", 4))
{
operation = CALSTACK_TAN;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "sec(", 4))
{
operation = CALSTACK_SEC;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "csc(", 4))
{
operation = CALSTACK_CSC;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "cot(", 4))
{
operation = CALSTACK_COT;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "log(", 4))
{
operation = CALSTACK_LOG;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "exp(", 4))
{
operation = CALSTACK_EXP;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 4 && !strncmp(input + j, "abs(", 4))
{
operation = CALSTACK_ABS;
pushOperation(numericStack, symbolStack);
j += 4;
}
else if (strlen(input + j) >= 5 && !strncmp(input + j, "log2(", 5))
{
operation = CALSTACK_LOG2;
pushOperation(numericStack, symbolStack);
j += 5;
}
else if (strlen(input + j) >= 5 && !strncmp(input + j, "sqrt(", 5))
{
operation = CALSTACK_SQRT;
pushOperation(numericStack, symbolStack);
j += 5;
}
else if (strlen(input + j) >= 5 && !strncmp(input + j, "fact(", 5))
{
operation = CALSTACK_FACT;
pushOperation(numericStack, symbolStack);
j += 5;
}
else if (strlen(input + j) >= 7 && !strncmp(input + j, "arcsin(", 7))
{
operation = CALSTACK_ARCSIN;
pushOperation(numericStack, symbolStack);
j += 7;
}
else if (strlen(input + j) >= 7 && !strncmp(input + j, "arccos(", 7))
{
operation = CALSTACK_ARCCOS;
pushOperation(numericStack, symbolStack);
j += 7;
}
else if (strlen(input + j) >= 7 && !strncmp(input + j, "arctan(", 7))
{
operation = CALSTACK_ARCTAN;
pushOperation(numericStack, symbolStack);
j += 7;
}
else if (strlen(input + j) >= 7 && !strncmp(input + j, "arcsec(", 7))
{
operation = CALSTACK_ARCSEC;
pushOperation(numericStack, symbolStack);
j += 7;
}
else if (strlen(input + j) >= 7 && !strncmp(input + j, "arccsc(", 7))
{
operation = CALSTACK_ARCCSC;
pushOperation(numericStack, symbolStack);
j += 7;
}
else if (strlen(input + j) >= 7 && !strncmp(input + j, "arccot(", 7))
{
operation = CALSTACK_ARCCOT;
pushOperation(numericStack, symbolStack);
j += 7;
}
// else if (strlen(input + j) >= 8 && !strncmp(input + j, "integral", 8))
// {
// j += 8;
// char name[NAME_LENTH] = "";
// char num1[BUFFER_LENTH] = "";
// char num2[BUFFER_LENTH] = "";
// Numeric left, right;
// Numeric_new(&left);
// Numeric_new(&right);
// Closure *function;
// while (input[j] == ' ')
// {
// ++j;
// }
// while (isalpha(input[j]))
// {
// name[i++] = input[j++];
// }
// name[i] = '\0';
// i = 0;
// while (input[j] == ' ')
// {
// ++j;
// }
// while (isdigit(input[j]) || input[j] == '.')
// {
// num1[i++] = input[j++];
// }
// num1[i] = '\0';
// i = 0;
// while (input[j] == ' ')
// {
// ++j;
// }
// while (isdigit(input[j]) || input[j] == '.')
// {
// num2[i++] = input[j++];
// }
// strtoNumeric(num1, &left);
// strtoNumeric(num2, &right);
// function = interpret(name, env);
//
// integral(function, &left, &right);
// }
//handle diff f value
else if (strlen(input + j) >= 4 && !strncmp(input + j, "diff", 4))
{
prec_opt = false;
char name[NAME_LENTH] = "";
Closure *f = NULL;
const Numeric *dx = Numeric_calculus();
Numeric *num = NULL;
Numeric value1, value2;
Numeric_new(&value1);
Numeric_new(&value2);
void *localEnv = NULL;
j += 4;
while (input[j] == ' ')
{
++j;
}
while (isalpha(input[j]))
{
name[i++] = input[j++];
}
name[i] = '\0';
while (input[j] == ' ')
{
++j;
}
i = 0;
if (input[j] == '-' || input[j] == '+')
{
buf[i++] = input[j++];
}
while ( input[j] == '.'||isalnum(input[j]))
{
buf[i++] = input[j++];
}
buf[i] = '\0';
num = interpret(buf, env);
f = interpret(name, env);
localEnv = ext_env(f->varName, Numeric_add(num, dx), &f->lenv);
Numeric_assign(&value1, interpret(f->exp, localEnv));
localEnv = ext_env(f->varName, Numeric_sub(num, dx), &f->lenv);
Numeric_assign(&value2, interpret(f->exp, localEnv));
//function diff(Clouse* f,Numeric* num,Numeric* dx)
Numeric_assign(num, Numeric_div(Numeric_sub(&value1, &value2), Numeric_add(dx, dx)));
//function over
Stack_push(numericStack, num);
Numeric_free(&value1);
Numeric_free(&value2);
prec_opt = true;
}
else //variable or function
{
while (1)
{
buf[i++] = input[j++];
if (!isalpha(input[j]))
{
if (input[j] == '[')
{
int endFlag = 1;
while (1)
{
buf[i++] = input[j++];
if (input[j] == '[')
{
++endFlag;
}
else if (input[j] == ']')
{
--endFlag;
}
if (endFlag == 0 && input[j + 1] != '[')
{
buf[i++] = input[j++];
break;
}
}
}
break;
}
}
buf[i] = '\0';
Stack_push(numericStack, interpret(buf, env));
}
}
if (input[j] == '\0' || input[j] == ';' || input[j] == '\n')
{
Numeric_free(&num);
return -1;
}
else
{
Numeric_free(&num);
return j;
}
}
void pushOperation(Stack *numericStack, SStack *symbolStack)
{
if (operation == CALSTACK_NEGATIVE)
{
Stack_push(numericStack,Numeric_negative_one());
operation = CALSTACK_MULT;
pushOperation(numericStack, symbolStack);
}
else if (SStack_empty(symbolStack) == 1)
{
SStack_push(symbolStack, operation);
}
else if (operation == CALSTACK_LBRACKET)
{
SStack_push(symbolStack, operation);
}
else if (operation == CALSTACK_RBRACKET)
{
while (SStack_top(symbolStack) < CALSTACK_SIN)
{
calculate(numericStack, symbolStack);
}
calculate(numericStack, symbolStack);
}
else if (operation >= CALSTACK_SIN && operation <= CALSTACK_FACT)
{
SStack_push(symbolStack, operation);
}
else if (operation >= CALSTACK_MULT && operation <= CALSTACK_POW)
{
if (SStack_top(symbolStack) < CALSTACK_MULT ||
(SStack_top(symbolStack) >= CALSTACK_SIN && SStack_top(symbolStack) <= CALSTACK_LBRACKET))
{
SStack_push(symbolStack, operation);
}
else
{
while (SStack_top(symbolStack) >= CALSTACK_MULT && SStack_top(symbolStack) <= CALSTACK_POW)
{
calculate(numericStack, symbolStack);
}
SStack_push(symbolStack, operation);
}
}
else if (operation >= CALSTACK_ADD && operation <= CALSTACK_SUB)
{
while (SStack_top(symbolStack) >= CALSTACK_ADD && SStack_top(symbolStack) <= CALSTACK_POW)
{
calculate(numericStack, symbolStack);
}
SStack_push(symbolStack, operation);
}
}
void calculate(Stack *numericStack, SStack *symbolStack)
{
Numeric a, b, n;
Numeric_new(&a);
Numeric_new(&b);
Numeric_new(&n);
switch (SStack_pop(symbolStack))
{
case CALSTACK_LBRACKET:
break;
case CALSTACK_FACT:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_fact(&n));
break;
case CALSTACK_ABS:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_abs(&n));
break;
case CALSTACK_EXP:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_exp(&n));
break;
case CALSTACK_SQRT:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_sqrt(&n));
break;
case CALSTACK_LN:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_ln(&n));
break;
case CALSTACK_LG:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_lg(&n));
break;
case CALSTACK_LOG2:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_log2(&n));
break;
case CALSTACK_LOG:
Stack_pop(numericStack, &a);
Stack_pop(numericStack, &b);
Stack_push(numericStack, Numeric_log(&b, &a));
break;
case CALSTACK_ARCCOT:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_arccot(&n));
break;
case CALSTACK_ARCCSC:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_arccot(&n));
break;
case CALSTACK_ARCSEC:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_arcsec(&n));
break;
case CALSTACK_ARCTAN:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_arctan(&n));
break;
case CALSTACK_ARCCOS:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_arccos(&n));
break;
case CALSTACK_ARCSIN:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_arcsin(&n));
break;
case CALSTACK_COT:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_cot(&n));
break;
case CALSTACK_CSC:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_csc(&n));
break;
case CALSTACK_SEC:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_sec(&n));
break;
case CALSTACK_TAN:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_tan(&n));
break;
case CALSTACK_COS:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_cos(&n));
break;
case CALSTACK_SIN:
Stack_pop(numericStack, &n);
Stack_push(numericStack, Numeric_sin(&n));
break;
case CALSTACK_POW:
Stack_pop(numericStack, &a);
Stack_pop(numericStack, &b);
Stack_push(numericStack, Numeric_pow(&b, &a));
break;
case CALSTACK_DIV:
Stack_pop(numericStack, &a);
Stack_pop(numericStack, &b);
Stack_push(numericStack, Numeric_div(&b, &a));
break;
case CALSTACK_MULT:
Stack_pop(numericStack, &a);
Stack_pop(numericStack, &b);
Stack_push(numericStack, Numeric_mult(&b, &a));
break;
case CALSTACK_SUB:
Stack_pop(numericStack, &a);
Stack_pop(numericStack, &b);
Stack_push(numericStack, Numeric_sub(&b, &a));
break;
case CALSTACK_ADD:
Stack_pop(numericStack, &a);
Stack_pop(numericStack, &b);
Stack_push(numericStack, Numeric_add(&b, &a));
break;
default:
printf("Error: not found operation");
}
Numeric_free(&a);
Numeric_free(&b);
Numeric_free(&n);
}