-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
188 lines (178 loc) · 6.05 KB
/
helpers.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
#include <stdio.h>
#include <math.h>
#include "token.h"
#include "tokenList.h"
#include "error.h"
// slice a string starting start, ending end, and put it into the destination
void sliceString(char *string, char *destination, int start, int end) {
for (int i = 0; i < end - start; ++i) {
destination[i] = string[start + i];
}
}
// counts how many same type tokens are
int tokenTypeCounter(TokenType type, Token tokens[], int size) {
int count = 0;
for(int i=0 ; i<size ; ++i) {
if(tokens[i].tokenType == type) {
count++;
}
}
return count;
}
// counts how many parentheses are (output: 1 is error / 0 is no error)
int parCheck(Token tokens[], int size) {
int count = 0;
for(int i=0 ; i<size ; ++i) {
if(tokens[i].tokenType == PARANTHESIS_OPENING) {
count++;
} else if(tokens[i].tokenType == PARANTHESIS_CLOSING) {
count--;
}
if (count<0) {
return 1;
}
}
if (count!=0) {
return 1;
}
return 0;
}
// there is at least one sequential
// 0: hep art ardalar 1: en az bir kez art arda değiller
int isNotSeqArr(Token tokens[], int size, TokenType tokenTypes1[], int size1, TokenType tokenTypes2[], int size2) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size1; ++j) {
if (tokens[i].tokenType == tokenTypes1[j]) {
for (int k = 0; k < size2; ++k) {
if (tokens[i+1].tokenType != tokenTypes2[k]) {
return 1;
}
}
}
}
}
return 0;
}
// there is at least one sequential
int isSeq(Token tokens[], int size, TokenType tokenType1, TokenType tokenType2) {
for (int i = 0; i < size - 1; ++i) {
if (tokens[i].tokenType == tokenType1) {
if (tokens[i+1].tokenType == tokenType2) {
return 1;
}
}
}
return 0;
}
// there is at least one not sequential
// 0: there are never not sequential 1: there is at least one time, they come after them
int isSeqArr(Token tokens[], int size, TokenType tokenTypes1[], int size1, TokenType tokenTypes2[], int size2) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size1; ++j) {
if (tokens[i].tokenType == tokenTypes1[j]) {
for (int k = 0; k < size2; ++k) {
if (tokens[i+1].tokenType == tokenTypes2[k]) {
return 1;
}
}
}
}
}
return 0;
}
//checks that whether there are two tokentype sequential
int isNotSeq(Token tokens[], int size, TokenType tokenType1, TokenType tokenType2) {
for (int i = 0; i < size - 1; ++i) {
if (tokens[i].tokenType == tokenType1) {
if (tokens[i+1].tokenType != tokenType2) {
return 1;
}
}
}
return 0;
}
//if there is two-args-function check its requirements
int twoArgFunc(Token tokens[], int size, int startIndex) {
if (startIndex==size+1) {
return 0;
} else if (startIndex==size-1) {
return 0;
} else if (startIndex==size) {
//printf("3");
return 1;
}
Token retTokens1[256];
int index;
Token retTokens2[256];
int index2;
int index0 = 0;
int a = 0;
int i;
for (i = startIndex; i < size; i++) {
if ((a==0) && (tokens[i].tokenType == FUNCTION_LR || tokens[i].tokenType == FUNCTION_LS || tokens[i].tokenType == FUNCTION_RR ||
tokens[i].tokenType == FUNCTION_RS || tokens[i].tokenType == FUNCTION_XOR)) {
index = 0;
index0 = i;
int whichComma = 0;
for (int k = i+2; k < size; k++) {
if (tokens[k].tokenType == SEPERATOR_COMMA) {
//printf("comma: %d\n", whichComma);
if(whichComma==0) {
a=1;
i=k;
if (index==0) {
//printf("1");
return 1;
}
break;
} else {
whichComma--;
//printf("C rettokens1: %d\n", k);
retTokens1[index] = tokens[k];
index++;
}
} else if (tokens[k].tokenType == FUNCTION_LR || tokens[k].tokenType == FUNCTION_LS || tokens[k].tokenType == FUNCTION_RR ||
tokens[k].tokenType == FUNCTION_RS || tokens[k].tokenType == FUNCTION_XOR) {
whichComma++;
//printf("A rettokens1: %d\n", k);
retTokens1[index] = tokens[k];
index++;
} else {
//printf("B rettokens1: %d\n", k);
retTokens1[index] = tokens[k];
index++;
}
}
}
else if (a==1)
{
int count = 0;
index2 = 0;
while(i < size) {
if(tokens[i].tokenType == PARANTHESIS_OPENING) {
count++;
} else if(tokens[i].tokenType == PARANTHESIS_CLOSING) {
count--;
}
if (count==-1) {
if (index2==0) {
//printf("2");
return 1;
}
break;
}
//printf("rettokens2: %d count: %d\n", i, count);
retTokens2[index2] = tokens[i];
index2++;
i++;
}
break;
} else return 0;
}
int isEr = isError(retTokens1, index);
int isEr2 = isError(retTokens2, index2);
int twoArg = twoArgFunc(tokens, size, index0+index+index2+5);
//printf("isEr: %d isEr2: %d twoArg: %d\n", isEr, isEr2, twoArg);
//printf("index: %d index2: %d\n", index, index2);
return isEr || isEr2 || twoArg;
}