-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
209 lines (176 loc) · 7.18 KB
/
parser.c
File metadata and controls
209 lines (176 loc) · 7.18 KB
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
#include "parser.h"
#include "logger.h"
polynomial polynomial_parse(const char *in) {
const char *last, *tkStart, *tkEnd;
char *temp;
polynomial_component currPC;
polynomial newPolynomial = NULL;
char var = 0;
double valA = 0, valB = 0;
int fraction = 0;
int sign;
enum _PARSE_CURR {
SIGN,
COEFFICIENT_START,
COEFFICIENT_END,
VAR_START,
EXPONENT_START,
EXPONENT_END,
} currSearch = SIGN;
//char * strs[] = {"SIGN", "COEFFICIENT_START", "COEFFICIENT_END", "VAR_START", "EXPONENT_START", "EXPONENT_END"};
last = in;
last--;
do {
last++;
//l->print_debug("looking at %c with value %s\n", *last, strs[currSearch]);
//printf("looking at %c with value %s\n", *last, strs[currSearch]);
switch (currSearch) {
case SIGN:
if (*last == '-') { /* we do have a sign */
sign = -1;
currSearch = COEFFICIENT_START;
} else if (*last == '+') { /* we do have a sign */
sign = 1;
currSearch = COEFFICIENT_START;
} else if (!((*last <= '9' && *last >= '0')||(*last <= 'z' && *last >= 'a')||(*last <= 'Z' && *last >= 'A'))) { /* no sign, no number, so keep going */
} else {
/* no sign, but there is a number */
sign = 1;
currSearch = COEFFICIENT_START;
goto COEFFICIENT_START_case;
}
break;
case COEFFICIENT_START:
COEFFICIENT_START_case:
if (*last <= '9' && *last >= '0') {
tkStart = last;
currSearch = COEFFICIENT_END;
break;
} else if (*last == ' ') {
break;
}
/* we are on a letter, so we can assume that the coefficient is 1 */
currPC.coefficient = 1*sign;
currSearch = VAR_START;
goto VAR_START_case;
case COEFFICIENT_END:
if (*last == '/') { /* we hit a fraction */
tkEnd = last;
temp = strndup(tkStart, tkEnd-tkStart);
tkStart = last+1;
sscanf(temp, "%lf", &valA);
free(temp);
fraction = 1;
break;
} else if ((*last < '.' || *last > '9') && *last != '\0') { /* number end */
tkEnd = last;
temp = strndup(tkStart, tkEnd-tkStart);
if (fraction) { /* handle fraction */
sscanf(temp, "%lf", &valB);
if (valB == 0) {
//l->print_error("Diving by zero: %f/%f!", valA, valB);
return NULL;
}
valA = valA/valB;
} else { /* normal handling */
sscanf(temp, "%lf", &valA);
}
currPC.coefficient = valA*sign; /* will be -1 or 1 already */
currSearch = VAR_START;
free(temp);
/* reset fraction flag */
fraction = 0;
} else { /* is a number, keep going */
break;
}
/* do not break in this case; go directly to VAR_START */
case VAR_START:
VAR_START_case:
if (var == 0) { /* no var set yet, so we'll use this one */
var = *last;
//logger->print_info("`%c` detected as variable in polynomial", var);
currSearch = EXPONENT_START;
} else if (*last == var) { /* found the var */
currSearch = EXPONENT_START;
if (*(last+1) != '^') { /* no exponent */
currPC.degree = 1;
goto ADD_POLYNOMIAL;
}
} else if (*last == '+' || *last == '-') { /* no variable, it is a constant */
currPC.degree = 0;
last--;
goto ADD_POLYNOMIAL;
}
/* no var found, keep going */
break;
case EXPONENT_START:
if (*last <= '9' && *last >= '0') {
tkStart = last;
currSearch = EXPONENT_END;
} else if (*last == '+' || *last == '-') { /* exponent is 1 */
currPC.degree = 1;
last--;
goto ADD_POLYNOMIAL;
}
break;
case EXPONENT_END:
if (*last < '0' || *last > '9') { /* exponent ended */
tkEnd = last;
temp = strndup(tkStart, tkEnd-tkStart);
currPC.degree = atoi(temp);
free(temp);
ADD_POLYNOMIAL:
//printf("adding %f%c^%d to the polynomial\n", currPC.coefficient, var, currPC.degree);
/* add the polynomial component to the list */
polynomial_addTerm(&newPolynomial, currPC);
currPC.degree = 0;
currPC.coefficient = 0.0;
valA = 0;
currSearch = SIGN;
if (*last == '+' || *last == '-') { /* need to go back so we can catch SIGN again*/
last--;
}
}
break;
}
} while (*last != '\0');
if (currSearch != SIGN && currSearch != VAR_START && currSearch != COEFFICIENT_END
&& !(currSearch == EXPONENT_START && *last == '\0')) {
//logger->print_error("Parsing ended unexpectedly.");
printf("Parsing ended unexpectedly.\n");
}
/* degree 1 polynomial */
if (currSearch == EXPONENT_START) {
currPC.degree = 1;
tkEnd = last;
temp = strndup(tkStart, tkEnd-tkStart);
if (valA == 0.0) sscanf(temp, "%lf", &valA);
currPC.coefficient = valA*sign; /* will be -1 or 1 already */
currSearch = VAR_START;
free(temp);
//printf("adding %fx^%d to the polynomial\n", currPC.coefficient, currPC.degree);
/* add the polynomial component to the list */
polynomial_addTerm(&newPolynomial, currPC);
} else if (currSearch == COEFFICIENT_END || (currSearch == VAR_START)) { /* degree 0 polynomial */
currPC.degree = 0;
tkEnd = last;
temp = strndup(tkStart, tkEnd-tkStart);
if (fraction) { /* handle fraction */
sscanf(temp, "%lf", &valB);
if (valB == 0) {
//l->print_error("Diving by zero: %f/%f!", valA, valB);
return NULL;
}
valA = valA/valB;
} else { /* normal handling */
sscanf(temp, "%lf", &valA);
}
currPC.coefficient = valA*sign; /* will be -1 or 1 already */
currSearch = VAR_START;
free(temp);
//printf("adding %fx^%d to the polynomial\n", currPC.coefficient, currPC.degree);
/* add the polynomial component to the list */
polynomial_addTerm(&newPolynomial, currPC);
}
return newPolynomial;
}