-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatlang2c.c
2140 lines (2087 loc) · 84.5 KB
/
matlang2c.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 <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char * varlist[500];//list of variables
char * vardimlist[500];//list of vectors and matrices
int dimlistx[500];//list of x dimensions of matrices
int dimlisty[500];//list of y dimensions of matrices (dimlistx,dimlisty and vardimlist have the same index for the same element and its dimensions)
int typelist[500];//list of types of elements(varlist and typelist have the same index for element and its corresponding type )
int isTerm(char * tokenline[], int s, int e);
int isMoreterms(char * tokenline[], int s, int e);
int isFactor(char * tokenline[], int s, int e);
int isMoreFactors(char * tokenline[], int s, int e);
int isExpression(char * tokenline[], int s, int e);
int * MatrixSizeCheck2(char * tokenline[], int s, int e);
int * MatrixSizeCheck3(char * tokenline[], int s, int e);
int * MatrixSizeCheck4(char * tokenline[], int s, int e);
int * MatrixSizeCheck5(char * tokenline[], int s, int e);
int linelenghts[500];//list to hold line lenghts
char * processedtokenline[500][500];//list to hold and modify the output
int processlinenum;//number of line that is being processed
int check[500][500];//list to check whether that location is processed or not
int varcount = 0;//number of variables
void ERROR(int linenum){//prints error message
printf("Error (Line %d)",linenum+1);
exit(0);
}
void initilize(FILE * out){//initilizes the output file
fprintf(out,"#include <stdio.h>\n"
"#include <string.h>\n"
"#include <ctype.h>\n"
"#include <stdlib.h>\n"
"#include <math.h>\n"
"\n"
"int dimx[1500];\n"
"int dimy[1500];\n"
"double * dimlist[500];\n"
"int dimpos = 0;\n"
"\n"
"void printsep(){\n"
" printf(\"------------\\n\");\n"
"}\n"
"void print(double element){\n"
" if(fabs(((int)element) - element) < 0.0000009){\n"
" printf(\"%%d\\n\",(int)element);\n"
" return;\n"
" }\n"
" printf(\"%%f\\n\",element);\n"
"}\n"
"\n"
"int getlenghtx(double * M){\n"
" for(int i = 0; i < 1500; i++){\n"
" if(dimlist[i] == M){\n"
" return dimx[i];\n"
" }\n"
" }\n"
"}\n"
"int getlenghty(double * M){\n"
" for(int i = 0; i < 1500; i++){\n"
" if(dimlist[i] == M){\n"
" return dimy[i];\n"
" }\n"
" }\n"
"}\n"
"void pushlenght(double * M, int x, int y){\n"
" dimx[dimpos] = x;\n"
" dimy[dimpos] = y;\n"
" dimlist[dimpos] = M;\n"
" dimpos++;\n"
"\n"
"}\n"
" double * matrixMultp(double * M1, double * M2){\n"
" int x1 = getlenghtx(M1);\n"
" int y1 = getlenghty(M1);\n"
" int x2 = getlenghtx(M2);\n"
" int y2 = getlenghty(M2);\n"
" int res = x1 * y2;\n"
" double * MM = malloc(sizeof(double) * res);\n"
" double * result = MM;\n"
" for (int i = 0; i < x1; i++) {\n"
" for (int j = 0; j < y2; j++) {\n"
" double sum = 0;\n"
" for (int k = 0; k < y1; k++)\n"
" sum = sum + M1[i * y1 + k] * M2[k * y2 + j];\n"
" MM[i * y2 + j] = sum;\n"
" }\n"
" }\n"
" pushlenght(result,x1,y2);\n"
" return result;\n"
"}\n"
"double * matrixScalar(double s, double * M){\n"
" double * MM = malloc(sizeof(double) * getlenghtx(M) * getlenghty(M));\n"
" double * result = MM;\n"
" int k = getlenghtx(M) * getlenghty(M);\n"
" double temp;\n"
" for(int i = 0; i < k; i++){\n"
" temp = s * M[i];\n"
" MM[i] = temp;\n"
" }\n"
" pushlenght(result, getlenghtx(M), getlenghty(M));\n"
" return result;\n"
"}\n"
"\n"
"double * matrixSum(double * M1 , double * M2){\n"
" double * MM = malloc(sizeof(double) * getlenghtx(M1) * getlenghty(M1));\n"
" double * result = MM;\n"
" int k = getlenghtx(M1) * getlenghty(M1);\n"
" for(int i = 0; i < k; i++){\n"
" MM[i] = M1[i] + M2[i];\n"
" }\n"
" pushlenght(result, getlenghtx(M1), getlenghty(M1));\n"
" return result;\n"
"\n"
"}\n"
"\n"
"\n"
"double * matrixDiff(double * M1 , double * M2){\n"
" double * MM = malloc(sizeof(double) * getlenghtx(M1) * getlenghty(M1));\n"
" double * result = MM;\n"
" int k = getlenghtx(M1) * getlenghty(M1);\n"
" for(int i = 0; i < k; i++){\n"
" MM[i] = M1[i] - M2[i];\n"
" }\n"
" pushlenght(result, getlenghtx(M1), getlenghty(M1));\n"
" return result;\n"
"}\n"
"\n"
"double choose(double e1, double e2, double e3, double e4){\n"
" if(e1 == 0){\n"
" return e2;\n"
" }\n"
" if(e1 > 0){\n"
" return e3;\n"
" }\n"
" if(e1 < 0){\n"
" return e4;\n"
" }\n"
"}\n"
"\n"
"double * trM(double * M){\n"
" double * MM = malloc(sizeof(double) * getlenghtx(M) * getlenghty(M));\n"
" int x,y;\n"
" double * result = MM;\n"
" for(int i = 0; i < getlenghty(M) * getlenghtx(M); i++){\n"
" y = i / getlenghty(M);\n"
" x = i %% getlenghty(M);\n"
"\n"
" MM[getlenghtx(M)*x+y] = M[i];\n"
"\n"
" }\n"
" pushlenght(result, getlenghty(M), getlenghtx(M));\n"
" return result;\n"
"\n"
"}\n"
"double tr(double x){\n"
" return x;\n"
"}\n"
"int main() {\n");
}
int id_finder(char * element){// checks whether the varible exists or not
for(int i = 0; i < 500; i++){
if(varlist[i] == NULL){
break;
}
if(strcmp(element,varlist[i]) == 0){
return i;
}
}
return -1;
}
int min(int a, int b){// minimum function
if(a == -1){
return b;
}
else if(b == -1){
return a;
}
else{
if(a > b){
return b;
}
else{
return a;
}
}
}
int finder(char * element ,char * tokenline[],int s, int e);
int isnumber(char * element){//checks whether the element is a real number or not
int isdouble = 0;
for(int i = 0; i < strlen(element) ; i++){
if(i == 0 && element[i] == '.' ){
return 0;
}
if(element[i] == '.' ){
if(isdouble == 0){
isdouble = 1;
continue;
}
else{
return 0;
}
}
if(isdigit(element[i]) == 0){
return 0;
}
}
return 1;
}
int finder(char * element ,char * tokenline[],int s, int e){// finds element in the given range and returns the index
for(int i = s; i <= e ; i++){
if(strcmp(element,tokenline[i]) == 0){
return i;
}
}
return -1;
}
int isLegit(char * varname){//checks whether the varname is legit or not
if(isdigit(varname[0]) != 0){
return -1;
}
for(int i = 0; i < 500; i++){
if(varname[i] == '\0'){
break;
}
if(isalpha(varname[i]) == 0){
if(isdigit(varname[i]) == 0){
if(varname[i] != '_'){
return -1;
}
}
}
}
return 1;
}
int gettype(char * element ){//returns the type of element
for(int i = 0; i < varcount; i++){
if(strcmp(varlist[i],element) == 0){
return typelist[i];
}
}
return 0;
}
int getdimensionsx(char * element){//returns the x dimension of given matrix
for(int i = 0; i < 500; i++){
if(strcmp(vardimlist[i],element) == 0){
return dimlistx[i];
}
}
return 0;
}
int getdimensionsy(char * element){//returns the y dimension of given matrix
for(int i = 0; i < 500; i++){
if(strcmp(vardimlist[i],element) == 0){
return dimlisty[i];
}
}
return 0;
}
void pushtype(char * element , int freepos, int type){// pushes the element to the typelist and varlist
varlist[freepos] = strdup(element);
typelist[freepos] = type;
varcount++;
}
void pushdimensions(char * element, int freepos,int a, int b){//pushes matrix's dimensions to the dimension lists
vardimlist[freepos] = strdup(element);
dimlistx[freepos] = a;
dimlisty[freepos] = b;
}
int tr_count = 0;
int p_count = 0;
int * MatrixSizeCheck1(char * tokenline[], int s, int e);
int * MatrixSizeCheck6(char * tokenline[], int s, int e);
int * MatrixSizeCheck(char * tokenline[], int s, int e){//Recursive function to check matrix sizes
static int size[2];
int error = -1;
if(finder("+", tokenline, s, e) == -1){//tries to find + operator
if(MatrixSizeCheck1(tokenline, s , e)[0] == -1){
size[0] = -1;
return size;
}
else if (MatrixSizeCheck1(tokenline, s , e)[0] != -1){//if there is no + operator it calls the function that finds - operator
return MatrixSizeCheck1(tokenline, s , e);
}
else{
size[0] = -1;
return size;
}
}
int opindex = finder("+", tokenline, s, e);
while(MatrixSizeCheck1(tokenline, s, opindex-1)[0] == -1){//if the left side of the operator is not a - operator function(it happens when there are parenthesis), it looks for another operator
opindex = finder("+", tokenline, opindex+1, e);
if(opindex == -1 ) {
error = 1;
break;
}
}
if(error == 1){
if(MatrixSizeCheck1(tokenline, s, e)[0] == -1){
size[0] = -1;
return size;
}
return MatrixSizeCheck1(tokenline, s, e);
}
else{
if(MatrixSizeCheck3(tokenline, opindex+1, e)[0] == -1){
size[0] = -1;
return size;
}
}
if(MatrixSizeCheck1(tokenline,s,opindex-1)[0] != MatrixSizeCheck3(tokenline,opindex+1,e)[0] || MatrixSizeCheck1(tokenline,s,opindex-1)[1] != MatrixSizeCheck3(tokenline,opindex+1,e)[1]){
//compares the dimensions of the matrices
size[0] = -1;
return size;
}
else{
if(check[processlinenum][opindex] != 1){//processes the input, omits the + and replaces them with corresponding function
if(strcmp(tokenline[opindex],"+") == 0){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixSum(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
}
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck1(tokenline,s,opindex-1);
}
}
int * MatrixSizeCheck1(char * tokenline[], int s, int e){//Recursive function to check matrix sizes
static int size[2];
int error = -1;
if(finder("-", tokenline, s, e) == -1 ){//tries to find - operator
if(MatrixSizeCheck2(tokenline, s , e)[0] == -1){
size[0] = -1;
return size;
}
else if (MatrixSizeCheck2(tokenline, s , e)[0] != -1){//if there is no - operator it calls the function corresponding to Term
return MatrixSizeCheck2(tokenline, s , e);
}
else{
size[0] = -1;
return size;
}
}
int opindex = finder("-", tokenline, s, e);;
while(MatrixSizeCheck2(tokenline, s, opindex-1)[0] == -1){//if the left side of the operator is not a term(it happens when there are parenthesis), it looks for another operator
opindex = finder("-", tokenline, opindex+1, e);
if(opindex == -1 ) {
error = 1;
break;
}
}
if(error == 1){
if(MatrixSizeCheck2(tokenline, s, e)[0] == -1){
size[0] = -1;
return size;
}
return MatrixSizeCheck2(tokenline, s, e);
}
else{
if(MatrixSizeCheck6(tokenline, opindex+1, e)[0] == -1){
size[0] = -1;
return size;
}
}
if(MatrixSizeCheck2(tokenline,s,opindex-1)[0] != MatrixSizeCheck6(tokenline,opindex+1,e)[0] || MatrixSizeCheck2(tokenline,s,opindex-1)[1] != MatrixSizeCheck6(tokenline,opindex+1,e)[1]){
//compares the dimensions of the matrices
size[0] = -1;
return size;
}
else{
if(check[processlinenum][opindex] != 1){//processes the input, omits the - and replaces them with corresponding functions
if(strcmp(tokenline[opindex],"-") == 0){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixDiff(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
}
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck2(tokenline,s,opindex-1);
}
}
int * MatrixSizeCheck2(char * tokenline[], int s, int e){//Matrix size check corresponding to Term Check
static int size[2];
int error = -1;
if(finder("*", tokenline, s, e) == -1 ){//Looks for *
if(MatrixSizeCheck4(tokenline, s , e)[0] == -1){
size[0] = -1;
return size;
}
else{
return MatrixSizeCheck4(tokenline, s , e);//if there is no * then it calls the function corresponding to Factor
}
}
int opindex = finder("*", tokenline, s, e);
while(MatrixSizeCheck4(tokenline, s, opindex-1)[0] == -1){//if the left side of the operator is not a Factor, it looks for another one
opindex = finder("*", tokenline, opindex+1, e);
if(opindex == -1 ) {
error = 1;
break;
}
}
if(error == 1){
if(MatrixSizeCheck4(tokenline, s, e)[0] == -1){
size[0] = -1;
return size;
}
}
else{
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -1){
size[0] = -1;
return size;
}
}
if(MatrixSizeCheck4(tokenline,s,opindex-1)[0] == -2 ){
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
return MatrixSizeCheck5(tokenline, opindex+1, e);//if both factors are scalar
}
else{
if(check[processlinenum][opindex] != 1){//if only one of them is scalar then it omits the * and puts the corresponing function
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixScalar(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
}
else{
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){//Same thing just reversed
if(check[processlinenum][opindex] != 1){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixScalar(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck4(tokenline,s,opindex-1);
}
}
if(MatrixSizeCheck4(tokenline,s,opindex-1)[0] == -2 ){//double check
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
else{
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
}
else{
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
return MatrixSizeCheck4(tokenline,s,opindex-1);
}
}
if(MatrixSizeCheck4(tokenline,s,opindex-1)[1] != MatrixSizeCheck5(tokenline,opindex+1,e)[0]){//compares the dimensions of the multiplied matrices
size[0] = -1;
return size;
}
else{
if(check[processlinenum][opindex] != 1){//replaces the * with the corresponding function
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixMultp(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex + 1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][opindex] = 1;
}
size[0] = MatrixSizeCheck4(tokenline,s,opindex-1)[0];//returns the x dimension of the matrix that is on the left and the y dimension of the matrix that is on the right
size[1] = MatrixSizeCheck5(tokenline,opindex+1,e)[1];
return size;
}
}
int * MatrixSizeCheck3(char * tokenline[], int s, int e){//MoreTerms, does exactly the same thing as the MatrixSizeCheck
static int size[2];
int error = -1;
if(finder("+", tokenline, s, e) == -1 ){
if(MatrixSizeCheck1(tokenline, s , e)[0] == -1){
size[0] = -1;
return size;
}
else if (MatrixSizeCheck1(tokenline, s , e)[0] != -1){
return MatrixSizeCheck1(tokenline, s , e);
}
else{
size[0] = -1;
return size;
}
}
int opindex = finder("+", tokenline, s, e);
while(MatrixSizeCheck1(tokenline, s, opindex-1)[0] == -1){
opindex = finder("+", tokenline, opindex+1, e);
if(opindex == -1) {
error = 1;
break;
}
}
if(error == 1){
if(MatrixSizeCheck1(tokenline, s, e)[0] == -1){
size[0] = -1;
return size;
}
return MatrixSizeCheck1(tokenline, s, e);
}
else{
if(MatrixSizeCheck3(tokenline, opindex+1, e)[0] == -1){
size[0] = -1;
return size;
}
}
if(MatrixSizeCheck1(tokenline,s,opindex-1)[0] != MatrixSizeCheck3(tokenline,opindex+1,e)[0] || MatrixSizeCheck1(tokenline,s,opindex-1)[1] != MatrixSizeCheck3(tokenline,opindex+1,e)[1]){
size[0] = -1;
return size;
}
else{
if(check[processlinenum][opindex] != 1){
if(strcmp(tokenline[opindex],"+") == 0){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixSum(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
}
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck1(tokenline,s,opindex-1);
}
}
int * MatrixSizeCheck4(char * tokenline[], int s, int e){//Function corresponding to isFactor
//If the thing between s and e are scalar it returns an array of 2 integers with first element -2, else it returns an array of 2 integers with first element as x dimension and the second as y
static int size[2];
if(strcmp(tokenline[s],"(") == 0){
if(strcmp(tokenline[e],")") != 0){
size[0] = -1;
return size;
}
else{
return MatrixSizeCheck(tokenline, s+1, e-1);
}
}
if(strcmp(tokenline[s],"tr") == 0){//transpose function
if(strcmp(tokenline[s+1],"(") != 0){
size[0] = -1;
return size;
}
else if(strcmp(tokenline[e],")") != 0){
size[0] = -1;
return size;
}
else{
if(MatrixSizeCheck(tokenline, s+2, e-1)[0] == -1){
size[0] = -1;
return size;
}
else if(MatrixSizeCheck(tokenline, s+2, e-1)[0] == -2){
size[0] = -2;
return size;
}
else{
if(check[processlinenum][s] != 1){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "trM");//there are 2 transpose functions in the out file, one for transposing matrices and the other is for transposing scalars, trM is for matrices the unedited one is for scalars
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][s] = 1;
}
static int size1[2];
size1[0] = MatrixSizeCheck(tokenline, s+2, e-1)[1];
size1[1] = MatrixSizeCheck(tokenline, s+2, e-1)[0];
return size1;
}
}
}
if(s==e){//scalar or vector or matrix or a number
if(isnumber(tokenline[s]) == 1){
size[0] = -2;
return size;
}
else if(gettype(tokenline[s]) == 1){
size[0] = -2;
return size;
}
else{
size[0] = getdimensionsx(tokenline[s]);
size[1] = getdimensionsy(tokenline[s]);
return size;
}
}
if(strcmp(tokenline[s],"choose") == 0){//choose always returns scalar
size[0] = -2;
return size;
}
if(strcmp(tokenline[s],"sqrt") == 0){//sqrt always returns scalar
size[0] = -2;
return size;
}
if(gettype(tokenline[s]) == 3){//element of a matrix is a scalar
if(strcmp(tokenline[s+1],"[") == 0){
size[0] = -2;
return size;
}
}
if(gettype(tokenline[s]) == 2){//element of a vector is a scalar
if(strcmp(tokenline[s+1],"[") == 0){
size[0] = -2;
return size;
}
}
return size;
}
int * MatrixSizeCheck5(char * tokenline[], int s, int e){//Exactly the same as MatrixSizeCheck2
static int size[2];
int error = -1;
if(finder("*", tokenline, s, e) == -1 ){
if(MatrixSizeCheck4(tokenline, s , e)[0] == -1){
size[0] = -1;
return size;
}
else if(MatrixSizeCheck4(tokenline, s , e)[0] == -2){
return MatrixSizeCheck4(tokenline, s , e);
}
else{
return MatrixSizeCheck4(tokenline, s , e);
}
}
int opindex = finder("*", tokenline, s, e);
while(MatrixSizeCheck4(tokenline, s, opindex-1)[0] == -1){
opindex = finder("*", tokenline, opindex+1, e);
if(opindex == -1 ) {
error = 1;
break;
}
}
if(error == 1){
if(MatrixSizeCheck4(tokenline, s, e)[0] == -1){
size[0] = -1;
return size;
}
else{
return MatrixSizeCheck4(tokenline, s, e);
}
}
else{
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -1){
size[0] = -1;
return size;
}
}
if(MatrixSizeCheck4(tokenline,s,opindex-1)[0] == -2 ){
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
else{
if(check[processlinenum][opindex] != 1){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixScalar(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
}
else{
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
if(check[processlinenum][opindex] != 1){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixScalar(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck4(tokenline,s,opindex-1);
}
}
if(MatrixSizeCheck4(tokenline,s,opindex-1)[0] == -2 ){
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
else{
return MatrixSizeCheck5(tokenline, opindex+1, e);
}
}
else{
if(MatrixSizeCheck5(tokenline, opindex+1, e)[0] == -2 ){
return MatrixSizeCheck4(tokenline,s,opindex-1);
}
}
if(MatrixSizeCheck4(tokenline,s,opindex-1)[1] != MatrixSizeCheck5(tokenline,opindex+1,e)[0]){
size[0] = -1;
return size;
}
else{
if(check[processlinenum][opindex] != 1){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixMultp(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex + 1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
check[processlinenum][opindex] = 1;
}
size[0] = MatrixSizeCheck4(tokenline,s,opindex-1)[0];
size[1] = MatrixSizeCheck5(tokenline,opindex+1,e)[1];
return size;
}
}
int * MatrixSizeCheck6(char * tokenline[], int s, int e){//Does exactly the same thing as MatrixSizeCheck1
static int size[2];
int error = -1;
if(finder("-", tokenline, s, e) == -1 ){//tries to find - operator
if(MatrixSizeCheck2(tokenline, s , e)[0] == -1){
size[0] = -1;
return size;
}
else if (MatrixSizeCheck2(tokenline, s , e)[0] != -1){//if there is no - operator it calls the function corresponding to Term
return MatrixSizeCheck2(tokenline, s , e);
}
else{
size[0] = -1;
return size;
}
}
int opindex = finder("-", tokenline, s, e);;
while(MatrixSizeCheck2(tokenline, s, opindex-1)[0] == -1){//if the left side of the operator is not a term(it happens when there are parenthesis), it looks for another operator
opindex = finder("-", tokenline, opindex+1, e);
if(opindex == -1 ) {
error = 1;
break;
}
}
if(error == 1){
if(MatrixSizeCheck2(tokenline, s, e)[0] == -1){
size[0] = -1;
return size;
}
return MatrixSizeCheck2(tokenline, s, e);
}
else{
if(MatrixSizeCheck6(tokenline, opindex+1, e)[0] == -1){
size[0] = -1;
return size;
}
}
if(MatrixSizeCheck2(tokenline,s,opindex-1)[0] != MatrixSizeCheck6(tokenline,opindex+1,e)[0] || MatrixSizeCheck2(tokenline,s,opindex-1)[1] != MatrixSizeCheck6(tokenline,opindex+1,e)[1]){
//compares the dimensions of the matrices
size[0] = -1;
return size;
}
else{
if(check[processlinenum][opindex] != 1){//processes the input, omits the - and replaces them with corresponding function
if(strcmp(tokenline[opindex],"-") == 0){
char * a;
a = malloc(sizeof(char) * 1000);
sprintf(a, "matrixDiff(");
processedtokenline[processlinenum][opindex] = " ";
for(int i = s; i <= opindex - 1; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,",");
for(int i = opindex+1; i <= e; i++ ){
strcat(a, strdup(processedtokenline[processlinenum][i]));
processedtokenline[processlinenum][i] = " ";
}
strcat(a,")");
processedtokenline[processlinenum][s] = strdup(a);
}
check[processlinenum][opindex] = 1;
}
return MatrixSizeCheck2(tokenline,s,opindex-1);
}
}
void spacer(char * line ){//puts spaces between elements
if(strcmp(&line[0],"\n") ==0){
return;
}
char templine[256];
memset(templine,0,256);
for(int i = 0; i < 256; i++){
if(line[i] == '\0'){
break;
}
if(isalnum(line[i])){
if(line[i+1] == '_'){
continue;
}
}
if(line[i] == '_'){
if(isalnum(line[i+1])){
continue;
}
}
if( line[i] ==')' || line[i] ==']' || line[i] =='}' || line[i] =='{'|| line[i] =='('|| line[i] =='['|| line[i] =='+'|| line[i] =='-'|| line[i] =='*'){
strncpy(templine, line, i+1);
templine[i+1] = '\0';
strcat(templine, " ");
strcat(templine, line +i+1 );
strncpy(line, templine, 256);
continue;
}
if(isspace(line[i])){
continue;
}
if(line[i] == '.'){
continue;
}
if(line[i+1] == '.'){
continue;
}
if(isalnum(line[i]) || line[i] == '_'){
if(isalnum(line[i+1]) ){
} else{
strncpy(templine, line, i+1);
templine[i+1] = '\0';
strcat(templine, " ");
strcat(templine, line +i+1 );
strncpy(line, templine, 256);
}
}
else{
if(isalnum(line[i+1]) || line[i] == '_'){
strncpy(templine, line, i+1);
templine[i+1] = '\0';
strcat(templine, " ");
strcat(templine, line + i+1);
strncpy(line, templine, 256);
}
}
}
}
void tokenizer(char * element, char * tokenline[], int tokenpos){//Used to tokenize the lines now just adds them to tokenline list
tokenline[tokenpos] = strdup(element);
}
int isExpression(char * tokenline[], int s, int e){//Checks whether the interval between s and e is expression or not, also checks type compatibility
int error = -1;
if(finder("+", tokenline, s, e) == -1 && finder("-", tokenline, s, e) == -1 ){//looks for + or -
if(isTerm(tokenline, s , e) == -1){
return -1;
}
else if (isTerm(tokenline, s , e) == 1){//it is a full term if no + or -
return 1;
}
else{
return 2;
}
}
int opindex1 = finder("+", tokenline, s, e);
int opindex2 = finder("-", tokenline, s, e);
int opindex = min(opindex1, opindex2);
while(isTerm(tokenline, s, opindex-1) == -1){//if the left side of the operator is not a term it looks for another operator
opindex1 = finder("+", tokenline, opindex+1, e);
opindex2 = finder("-", tokenline, opindex+1, e);
opindex = min(opindex1, opindex2);
if(opindex1 == -1 && opindex2 == -1) {
error = 1;
break;
}
}
if(error == 1){
if(isTerm(tokenline, s, e) == -1){
return -1;
}
return isTerm(tokenline, s, e);
}
else{
if(isMoreterms(tokenline, opindex+1, e) == -1){//Checks the right side of the operator
return -1;
}
}
if(isTerm(tokenline,s,opindex-1) != isMoreterms(tokenline,opindex+1,e)){//checks the type compatibility