-
Notifications
You must be signed in to change notification settings - Fork 1
/
Console.c
1370 lines (1094 loc) · 27.8 KB
/
Console.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Console.h"
#include "Matrix.h"
#include "Utils.h"
#pragma region Definitions
#define ASSERT__FAIL(X_COND) if (!(X_COND)) goto fail;
#define IS_SPACE isspace(*in)
#define EAT_SPACE if (IS_SPACE) { in++; continue; }
#define _EAT_SPACE while (*in) { EAT_SPACE; break; }
#define IS_NUMBER isdigit(*in)
#define IS_MATRIX isupper(*in)
#define IS_NAME islower(*in)
#define IS_FUNCTION islower(*in)
#define IS_ASSIGNMENT (result->assignment = *in++ == '=')
#define IS_PARENTHESIS (*in == '(')
#define IS_PARAMETER (*in == ',')
#define IS_FACTOR (IS_NUMBER || IS_MATRIX || IS_NAME || IS_FUNCTION || IS_PARENTHESIS)
#define IS_TSIGN (*in == '+' || *in == '-')
#define IS_NEGATIVE (*in == '-')
#define IS_ONLY_FUNCTION !input->assignment && input->right && \
!input->right->next && input->right->factors && \
!input->right->factors->next && input->right->factors->type == FACTOR_FUNCTION && \
input->right->factors->value
#define ADD_TERM \
if (tail) \
tail = tail->next = calloc(1, sizeof(PTerm)); \
else \
tail = result = calloc(1, sizeof(PTerm)); \
ASSERT__FAIL(tail); \
tail->negative = negative;
#define IS_FSIGN (*in == '*' || *in == '/')
#define IS_DIVISOR (*in == '/')
#define ADD_FACTOR \
if (tail) \
tail = tail->next = calloc(1, sizeof(PFactor)); \
else \
tail = result = calloc(1, sizeof(PFactor)); \
ASSERT__FAIL(tail); \
tail->divisor = divisor; \
divisor = false;
#define CURRENT_PARAMTYPE result->argtypes[result->argcount]
#define CURRENT_PARAMETER result->args[result->argcount]
/* Function Parsing States */
#define FPS_NAME (uint8_t)1
#define FPS_SPACE (uint8_t)2
#define FPS_EMPTY_PARAM (uint8_t)3
#define FPS_PARAMETER (uint8_t)4
#define ASSERT__ERROR(X_COND, X_ERR) if (!(X_COND)) return X_ERR;
#pragma endregion
#pragma region Common Functions
EValue common_op(CMD_PARAM_DECL, bool colmode)
{
INIT_RESULT;
char c = colmode ? 'c' : 'r';
Matrix* matrix = GET_MATRIX_ARG(0), * r;
Operation op = mx_next_op(matrix, colmode, false), op2;
if (!op.type)
op = mx_next_op(matrix, colmode, true);
switch (op.type)
{
case OP_ADD:
printf("%c%d -> %c%d%+g%c%d\n", c, op.vec2 + 1, c, op.vec2 + 1, op.coeff, c, op.vec1 + 1);
break;
case OP_MULTIPLY:
printf("%c%d -> %g%c%d\n", c, op.vec1 + 1, op.coeff, c, op.vec1 + 1);
break;
case OP_SWITCH:
printf("%c%d <-> %c%d\n", c, op.vec1 + 1, c, op.vec2 + 1);
break;
default:
break;
}
r = mx2_apply_op(matrix, op);
op = mx_next_op(r, colmode, false);
op2 = mx_next_op(r, colmode, true);
setcolor(8);
if (!op2.type)
printf("Indirgenmis %s Eselon Formu\n", colmode ? "Sutun" : "Satir");
else if (!op.type)
printf("%s Eselon Formu\n", colmode ? "Sutun" : "Satir");
SET_MATRIX_RESULT(r);
RETURN_RESULT;
}
EValue common_swt(CMD_PARAM_DECL, bool colmode)
{
INIT_RESULT;
Matrix* matrix = GET_MATRIX_ARG(0);
float num1 = GET_NUMBER_ARG(1), num2 = GET_NUMBER_ARG(2);
char c = colmode ? 'c' : 'r';
uint8_t vecs = colmode ? matrix->cols : matrix->rows;
if (ceilf(num1) != num1 || ceilf(num2) != num2)
{
*error = colmode ? "Sutun numaralari tam sayi olmalidir." : "Satir numaralari tam sayi olmalidir.";
RETURN_RESULT;
}
if (num1 < MIN_MATRIX_SIZE || num2 < MIN_MATRIX_SIZE || num1 > vecs || num2 > vecs)
{
*error = colmode ? "Sutun numaralari matris boyutunu asiyor." : "Satir numaralari matris boyutunu asiyor.";
RETURN_RESULT;
}
if (num1 == num2)
{
*error = colmode ? "Sutun numaralari ayni olamaz." : "Satir numaralari ayni olamaz.";
RETURN_RESULT;
}
printf("%c%d <-> %c%d\n", c, (int)num1, c, (int)num2);
Operation op = { colmode, OP_SWITCH, (uint8_t)(num1 - 1), (uint8_t)(num2 - 1), 0.0f };
SET_MATRIX_RESULT(mx2_apply_op(matrix, op));
RETURN_RESULT;
}
EValue common_mul(CMD_PARAM_DECL, bool colmode)
{
INIT_RESULT;
Matrix* matrix = GET_MATRIX_ARG(0);
float num1 = GET_NUMBER_ARG(1), num2 = GET_NUMBER_ARG(2);
char c = colmode ? 'c' : 'r';
uint8_t vecs = colmode ? matrix->cols : matrix->rows;
if (iszero(num2))
{
*error = colmode ? "Sutun ile carpilacak katsayi 0 olamaz." : "Satir ile carpilacak katsayi 0 olamaz.";
RETURN_RESULT;
}
if (ceilf(num1) != num1)
{
*error = colmode ? "Sutun numarasi tam sayi olmalidir." : "Satir numarasi tam sayi olmalidir.";
RETURN_RESULT;
}
if (num1 < MIN_MATRIX_SIZE || num1 > vecs)
{
*error = colmode ? "Sutun numarasi matris boyutunu asiyor." : "Satir numarasi matris boyutunu asiyor.";
RETURN_RESULT;
}
printf("%c%d -> %g%c%d\n", c, (int)num1, num2, c, (int)num1);
Operation op = { colmode, OP_MULTIPLY, (uint8_t)(num1 - 1), (uint8_t)0, num2 };
SET_MATRIX_RESULT(mx2_apply_op(matrix, op));
RETURN_RESULT;
}
EValue common_add(CMD_PARAM_DECL, bool colmode)
{
INIT_RESULT;
Matrix* matrix = GET_MATRIX_ARG(0);
float num1 = GET_NUMBER_ARG(1), num2 = GET_NUMBER_ARG(2), num3 = GET_NUMBER_ARG(3);
char c = colmode ? 'c' : 'r';
uint8_t vecs = colmode ? matrix->cols : matrix->rows;
if (iszero(num3))
{
*error = colmode ? "Sutun ile carpilacak katsayi 0 olamaz." : "Satir ile carpilacak katsayi 0 olamaz.";
RETURN_RESULT;
}
if (ceilf(num1) != num1 || ceilf(num2) != num2)
{
*error = colmode ? "Sutun numaralari tam sayi olmalidir." : "Satir numaralari tam sayi olmalidir.";
RETURN_RESULT;
}
if (num1 < MIN_MATRIX_SIZE || num2 < MIN_MATRIX_SIZE || num1 > vecs || num2 > vecs)
{
*error = colmode ? "Sutun numaralari matris boyutunu asiyor." : "Satir numaralari matris boyutunu asiyor.";
RETURN_RESULT;
}
if (num1 == num2)
{
*error = colmode ? "Sutun numaralari ayni olamaz." : "Satir numaralari ayni olamaz.";
RETURN_RESULT;
}
printf("%c%d -> %c%d%+g%c%d\n", c, (int)num1, c, (int)num1, num3, c, (int)num2);
Operation op = { colmode, OP_ADD, (uint8_t)(num2 - 1), (uint8_t)(num1 - 1), num3 };
SET_MATRIX_RESULT(mx2_apply_op(matrix, op));
RETURN_RESULT;
}
#pragma endregion
PExpression* parse_formula(Memory* memory, char* in, char** out)
{
PExpression* result;
*out = in;
ASSERT__FAIL(result = calloc(1, sizeof(PExpression)));
_EAT_SPACE;
if (IS_MATRIX)
{
result->leftname = *in;
result->left = mem_query(memory, *in++);
_EAT_SPACE;
if (!IS_ASSIGNMENT)
{
result->left = NULL;
result->leftname = '\0';
in = *out;
}
}
ASSERT__FAIL(result->right = parse_expression(memory, in, &in));
*out = in;
RETURN_RESULT;
fail:
free_formula(result);
return NULL;
}
PTerm* parse_expression(Memory* memory, char* in, char** out)
{
PTerm* result = NULL;
PTerm* tail = NULL;
bool sign = false, negative = false;
*out = in;
while (*in)
{
EAT_SPACE;
if (IS_TSIGN)
{
ASSERT__FAIL(!sign);
ADD_TERM;
negative = IS_NEGATIVE;
in++;
sign = true;
}
else if (IS_FACTOR)
{
if (!tail)
ADD_TERM;
ASSERT__FAIL(tail->factors = parse_term(memory, in, &in));
tail->negative = negative;
sign = false;
}
else break;
}
if (tail && !sign)
{
*out = in;
RETURN_RESULT;
}
fail:
free_expression(result);
return NULL;
}
PFactor* parse_term(Memory* memory, char* in, char** out)
{
PFactor* result = NULL;
PFactor* tail = NULL;
bool sign = false, divisor = false;
int length;
*out = in;
while (*in)
{
EAT_SPACE;
if (IS_FSIGN)
{
ASSERT__FAIL(tail && !sign);
divisor = IS_DIVISOR;
in++;
sign = true;
continue;
}
if (IS_NUMBER)
{
ADD_FACTOR;
tail->type = FACTOR_NUMBER;
ASSERT__FAIL(tail->value = malloc(sizeof(float)));
ASSERT__FAIL(length = sscan_ufloat(in, (float*)tail->value));
in += length;
}
else if (IS_MATRIX)
{
ADD_FACTOR;
tail->type = FACTOR_NODE;
tail->value = mem_query(memory, *in);
in++;
}
else if (IS_FUNCTION)
{
ADD_FACTOR;
tail->type = FACTOR_FUNCTION;
ASSERT__FAIL(tail->value = parse_function(memory, in, &in));
}
else if (IS_PARENTHESIS)
{
ADD_FACTOR;
tail->type = FACTOR_PARENTHESIS;
in++; // ( karakteri atlandý
ASSERT__FAIL(tail->value = parse_expression(memory, in, &in));
ASSERT__FAIL(*in++ == ')');
}
else break;
sign = false;
}
if (tail && !sign)
{
*out = in;
RETURN_RESULT;
}
fail:
free_term(result);
return NULL;
}
PFunction* parse_function(Memory* memory, char* in, char** out)
{
PFunction* result;
uint8_t state = FPS_NAME;
int namelen = 0;
*out = in;
ASSERT__FAIL(result = calloc(1, sizeof(PFunction)));
while (*in)
switch (state)
{
case FPS_NAME:
{
if (IS_FUNCTION)
{
in++;
namelen++;
}
else
{
ASSERT__FAIL(namelen);
state = FPS_SPACE;
}
break;
}
case FPS_SPACE:
{
if (IS_PARENTHESIS)
state = FPS_EMPTY_PARAM;
else if (!IS_SPACE)
goto end;
in++;
break;
}
case FPS_EMPTY_PARAM:
{
if (!IS_SPACE)
{
if (*in == ')')
{
in++;
state = 0;
goto end;
}
state = FPS_PARAMETER;
break;
}
in++;
break;
}
case FPS_PARAMETER:
{
ASSERT__FAIL(CURRENT_PARAMETER = parse_expression(memory, in, &in));
result->argcount++;
if (IS_PARAMETER)
{
in++;
break;
}
else if (*in == ')')
{
in++;
state = 0;
goto end;
}
else
goto end;
}
}
end:
if (state != FPS_EMPTY_PARAM && state != FPS_PARAMETER && namelen)
{
result->name = malloc(namelen + 1);
ASSERT__FAIL(result->name);
memcpy(result->name, *out, namelen);
result->name[namelen] = '\0';
*out = in;
RETURN_RESULT;
}
fail:
free_function(result);
return NULL;
}
bool run_command(Memory* memory, PExpression* input, bool* newline)
{
*newline = true;
char* error = NULL;
if (IS_ONLY_FUNCTION)
{
Function* func;
PFunction* pfunc = input->right->factors->value;
for (uint8_t i = 0; i < CMD_COUNT; i++)
{
func = &memory->commands[i];
if (func->paramcount != pfunc->argcount || pfunc->argcount)
continue;
if (strcmp(func->name, pfunc->name))
continue;
if (func->returns)
continue;
if (!memory->commands[i].function)
return false;
setcolor(11);
(void)memory->commands[i].function(memory, 0, &error);
if (!strcmp(func->name, "clear"))
*newline = false;
else
printf("\n");
return true;
}
}
if (error = check_formula(memory, input))
{
setcolor(12);
printf("%s", error);
}
else
{
EValue result = evaluate_formula(memory, input, &error);
if (error)
{
setcolor(12);
printf("%s", error);
}
else if (result.number)
{
setcolor(11);
printf("= %g", froundf(result.value.number));
}
else
{
setcolor(11);
mx_print(result.value.matrix);
mx_free(result.value.matrix);
}
}
printf("\n");
return true;
}
char* check_formula(Memory* memory, PExpression* input)
{
ASSERT__ERROR(input, "Sozdizimi hatasi.");
return check_expression(memory, input->right);
}
char* check_expression(Memory* memory, PTerm* input)
{
ASSERT__ERROR(input, "Sozdizimi hatasi.");
PTerm* tail = input;
char* error;
do
{
ASSERT__ERROR(!(error = check_term(memory, tail->factors)), error);
}
while (tail = tail->next);
return NULL;
}
char* check_term(Memory* memory, PFactor* input)
{
ASSERT__ERROR(input, "Sozdizimi hatasi.");
PFactor* tail = input;
char* error;
ASSERT__ERROR(tail, "Kritik hata.");
ASSERT__ERROR(!tail->divisor, "Kritik hata.");
do
{
ASSERT__ERROR(!(error = check_factor(memory, tail->value, tail->type)), error);
} while (tail = tail->next);
return NULL;
}
char* check_factor(Memory* memory, void* input, uint8_t type)
{
switch (type)
{
case FACTOR_NUMBER:
return NULL;
case FACTOR_NODE:
ASSERT__ERROR(input, "Hata: Tanimlanmamis matris kullanimi.");
return NULL;
case FACTOR_FUNCTION:
ASSERT__ERROR(input, "Sozdizimi hatasi.");
Function* func;
PFunction* pfunc = input;
for (int i = 0; i < CMD_COUNT; i++)
{
func = &memory->commands[i];
if (func->paramcount != pfunc->argcount)
continue;
if (strcmp(func->name, pfunc->name))
continue;
ASSERT__ERROR(func->returns,
"Hata: Deger dondurmeyen bir fonksiyon ifade icinde kullanilamaz.");
return NULL;
}
return "Hata: Bilinmeyen fonksiyon/komut kullanimi.";
case FACTOR_PARENTHESIS:
return check_expression(memory, input);
default:
return "Hata: Bilinmeyen carpan tipi.";
}
}
EValue evaluate_formula(Memory* memory, PExpression* input, char** error)
{
*error = NULL;
EValue result = evaluate_expression(memory, input->right, error);
if (*error)
RETURN_RESULT;
if (input->assignment)
{
if (result.number)
{
*error = "Matris skaler bir sayiya esitlenemez.";
RETURN_RESULT;
}
if (input->left)
mem_remove(memory, input->left);
Matrix* copy = mx_copy(result.value.matrix);
if (!copy)
{
*error = "Kritik hafiza hatasi.";
mx_free(result.value.matrix);
RETURN_RESULT;
}
if (!(input->left = mem_add(memory, input->leftname, copy)))
{
*error = "Kritik hafiza hatasi.";
mx_free(copy);
mx_free(result.value.matrix);
RETURN_RESULT;
}
}
RETURN_RESULT;
}
EValue evaluate_expression(Memory* memory, PTerm* input, char** error)
{
PTerm* tail = input;
EValue result = evaluate_term(memory, tail->factors, error), term;
if (*error)
RETURN_RESULT;
if (tail->negative)
{
if (result.number)
result.value.number *= -1;
else
mx_multiply(result.value.matrix, -1);
}
while (tail = tail->next)
{
term = evaluate_term(memory, tail->factors, error);
if (*error)
{
if (!result.number)
mx_free(result.value.matrix);
RETURN_RESULT;
}
if (term.number)
{
if (!result.number)
{
*error = "Skaler sayilar matrislerle toplanamaz.";
mx_free(result.value.matrix);
RETURN_RESULT;
}
if (tail->negative)
term.value.number *= -1;
result.value.number += term.value.number;
}
else
{
if (result.number)
{
*error = "Skaler sayilar matrislerle toplanamaz.";
mx_free(term.value.matrix);
RETURN_RESULT;
}
if (result.value.matrix->rows != term.value.matrix->rows ||
result.value.matrix->cols != term.value.matrix->cols)
{
*error = "Toplama/cikarma yapabilmek icin matrislerin boyutlari birbirine esit olmalidir.";
mx_free(term.value.matrix);
mx_free(result.value.matrix);
RETURN_RESULT;
}
if (tail->negative)
mx_multiply(term.value.matrix, -1);
mx_add(result.value.matrix, term.value.matrix);
mx_free(term.value.matrix);
}
}
RETURN_RESULT;
}
EValue evaluate_term(Memory* memory, PFactor* input, char** error)
{
PFactor* tail = input;
EValue result = evaluate_factor(memory, tail->value, tail->type, error), factor;
if (*error)
RETURN_RESULT;
while (tail = tail->next)
{
factor = evaluate_factor(memory, tail->value, tail->type, error);
if (*error)
{
if (!result.number)
mx_free(result.value.matrix);
RETURN_RESULT;
}
if (factor.number)
{
if (tail->divisor)
factor.value.number = 1 / factor.value.number;
if (result.number)
result.value.number *= factor.value.number;
else
mx_multiply(result.value.matrix, factor.value.number);
}
else
{
if (tail->divisor)
{
*error = "Bolen bir matris olamaz.";
mx_free(factor.value.matrix);
if (!result.number)
mx_free(result.value.matrix);
RETURN_RESULT;
}
if (result.number)
{
float s = result.value.number;
result.number = false;
result.value.matrix = factor.value.matrix;
mx_multiply(result.value.matrix, s);
}
else if (result.value.matrix->cols != factor.value.matrix->rows)
{
*error = "Matris boyutlari carpmaya uygun degil.";
mx_free(factor.value.matrix);
mx_free(result.value.matrix);
RETURN_RESULT;
}
else
{
mx_dot(result.value.matrix, factor.value.matrix);
mx_free(factor.value.matrix);
}
}
}
RETURN_RESULT;
}
EValue evaluate_factor(Memory* memory, void* input, uint8_t type, char** error)
{
INIT_RESULT;
switch (type)
{
case FACTOR_NUMBER:
result.number = true;
result.value.number = *(float*)input;
break;
case FACTOR_NODE:
if (!input)
{
*error = "Hata: Tanimlanmamis matris kullanimi.";
RETURN_RESULT;
}
result.number = false;
result.value.matrix = mx_copy(((Node*)input)->matrix);
break;
case FACTOR_FUNCTION:
{
Function* func;
PFunction* pfunc = input;
for (int i = 0; i < CMD_COUNT; i++)
{
func = &memory->commands[i];
if (func->paramcount != pfunc->argcount)
continue;
if (strcmp(func->name, pfunc->name))
continue;
EValue args[CMD_PARAM_COUNT] = { 0 };
int p;
for (p = 0; p < func->paramcount; p++)
{
args[p] = evaluate_expression(memory, pfunc->args[p], error);
if (*error)
RETURN_RESULT;
if (func->params[p] != args[p].number)
goto cont;
}
result = func->function(memory, args, error);
for (p = 0; p < func->paramcount; p++)
if (!args[p].number)
mx_free(args[p].value.matrix);
RETURN_RESULT;
cont:;
}
*error = "Fonksiyon parametreleri uyusmuyor.";
break;
}
case FACTOR_PARENTHESIS:
result = evaluate_expression(memory, input, error);
break;
default:
*error = "Kritik hata."; //check fonksiyonunda zaten kontrol edildi
break;
}
RETURN_RESULT;
}
void free_formula(PExpression* expression)
{
if (!expression)
return;
free_expression(expression->right);
free(expression);
}
void free_expression(PTerm* term)
{
PTerm* temp;
while (term)
{
temp = term->next;
free_term(term->factors);
free(term);
term = temp;
}
}
void free_term(PFactor* factor)
{
PFactor* temp;
while (factor)
{
temp = factor->next;
switch (factor->type)
{
case FACTOR_NUMBER:
free(factor->value);
break;
case FACTOR_FUNCTION:
free_function(factor->value);
break;
case FACTOR_PARENTHESIS:
free_expression(factor->value);
break;
default:
break;
}
free(factor);
factor = temp;
}
}
void free_function(PFunction* function)
{
if (!function)
return;
free(function->name);
for (uint8_t i = 0; i < CMD_PARAM_COUNT; i++)
free_expression(function->args[i]);
free(function);
}
#pragma region Commands
EValue cmd_clear(CMD_PARAM_DECL)
{
INIT_RESULT;
clear();
RETURN_RESULT;
}
#pragma region Matrix
EValue cmd_list(CMD_PARAM_DECL)
{
INIT_RESULT;
int count = 0;
Node* tail = memory->tail;
while (tail)
{
if (!count++)
printf("\tAd\tBoyut\n");
printf("\t%c\t%dx%d\n", tail->name, tail->matrix->rows, tail->matrix->cols);
tail = tail->prev;
}
if (count)
printf("\n");
printf("\t%d sonuc bulundu.", count);
RETURN_RESULT;
}
EValue cmd_get(CMD_PARAM_DECL)
{
INIT_RESULT;
Matrix* matrix = GET_MATRIX_ARG(0);
float row = GET_NUMBER_ARG(1), col = GET_NUMBER_ARG(2);
if (ceilf(row) != row || ceilf(col) != col)
{
*error = "Satir ve sutun numaralari tam sayi olmalidir.";
RETURN_RESULT;
}
if (row < MIN_MATRIX_SIZE || row > matrix->rows || col < MIN_MATRIX_SIZE || col > matrix->cols)
{
*error = "Satir ve sutun numaralari matris boyutunu asiyor.";
RETURN_RESULT;
}
SET_NUMBER_RESULT(*mx_get(matrix, (uint8_t)(row - 1), (uint8_t)(col - 1)));
RETURN_RESULT;
}
EValue cmd_set(CMD_PARAM_DECL)
{
INIT_RESULT;
Matrix* matrix = GET_MATRIX_ARG(0);
float value = GET_NUMBER_ARG(1), row = GET_NUMBER_ARG(2), col = GET_NUMBER_ARG(3);
if (ceilf(row) != row || ceilf(col) != col)
{
*error = "Satir ve sutun numaralari tam sayi olmalidir.";
RETURN_RESULT;
}
if (row < MIN_MATRIX_SIZE || row > matrix->rows || col < MIN_MATRIX_SIZE || col > matrix->cols)
{
*error = "Satir ve sutun numaralari matris boyutunu asiyor.";
RETURN_RESULT;
}
mx_set(matrix, (uint8_t)(row - 1), (uint8_t)(col - 1), value);
SET_MATRIX_RESULT(mx_copy(matrix));
RETURN_RESULT;
}
EValue cmd_iseq(CMD_PARAM_DECL)
{
Matrix *matrix1 = GET_MATRIX_ARG(0), *matrix2 = GET_MATRIX_ARG(1);
RETURN_NUMBER_RESULT(mx_isequal(matrix1, matrix2));
}
EValue cmd_transpose(CMD_PARAM_DECL)
{
INIT_RESULT;
result.number = false;
result.value.matrix = mx2_transpose(GET_MATRIX_ARG(0));
RETURN_RESULT;