-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.c
More file actions
275 lines (226 loc) · 6.71 KB
/
Copy pathmath.c
File metadata and controls
275 lines (226 loc) · 6.71 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
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
#include "main.h"
/* These are all defined in run.c */
extern size_t programCounter;
extern bool thereWasAnError;
extern bool keepRunning;
size_t count_math_symbols(Line line, char symbol) {
bool in_quotes;
size_t i, length, total;
in_quotes = false;
length = strlen(line);
total = 0;
for (i=0; i<length; i++) {
if (line[i] == '"')
in_quotes = !in_quotes;
if (in_quotes) continue;
if (line[i] == symbol) total++;
}
return total;
}
void eval_expr(Line line, VarList variables) {
/* Handle string functions first */
replace_chr(line);
replace_tab(line);
combine_strings(line);
/* Unfortunately, all this needs to be handled first (see below) */
replace_asc(line);
replace_columns(line);
replace_fre(line);
replace_rc(line);
replace_rows(line);
/* Bug I'll need to address in 0.3:
If I call this first (which is what I'd like),
all the one-letter variables get replaced with zeroes.
This makes sense (cuz the only variables 0.2 supports
are capital letters A-Z, which default to zero), but
I think is_var will have to check that the variable is
doesn't contain a keyword before replacing */
replace_vars_with_values(line, variables);
/* Then we can crunch numbers (which again, I would like to do first)*/
multiply(line, LINE_SIZE);
divide(line, LINE_SIZE);
add(line, LINE_SIZE);
subtract(line, LINE_SIZE);
}
size_t get_start(char* line, size_t symbol) {
/* Move past the math symbol (+*-/) */
symbol--;
if (symbol <= 0) return 0;
/* Move past any spaces */
while(line[symbol] == ' ') symbol--;
if (symbol <= 0) return 0;
/* Here there should be digits */
while (symbol > 0 && is_digit(line[symbol])) symbol--;
if (symbol <= 0) return 0;
/* Move past the decimal point, if there is one */
if (line[symbol] != '.' && line[symbol] != '\0') return symbol;
if (symbol <= 0) return 0;
/* Move past the whole (not factional) digits, if there were any */
while (symbol > 0 && is_digit(line[symbol])) symbol--;
if (symbol <= 0) return 0;
/* If there was a sign, move past that */
if (line[symbol] == '+' || line[symbol] == '-') symbol--;
/* And we're done */
symbol--;
return symbol <= 0 ? 0 : symbol;
}
size_t get_end(char* line, size_t symbol) {
/* First, move past the math symbol (+*-/) */
symbol++;
/* Move past spaces */
while(line[symbol] == ' ') symbol++;
/* If there is a sign, move past that */
if (line[symbol] == '+' || line[symbol] == '-') symbol++;
/* Move past digits */
while (is_digit(line[symbol])) symbol++;
/* Here we might have reached the end of the string, or there is
something other than a decimal point after the numbers */
if (line[symbol] != '.') return symbol;
symbol++;
/* If the next char was a decimal point, expect more digits */
while (is_digit(line[symbol])) symbol++;
/* And we're done */
return symbol;
}
void add(char* line, size_t length) {
/* Declare vars */
size_t i, start, end, count;
bool in_quotes;
float left, right;
/* Count how many *s are in th eline, not
counting any stars between quotation marks */
count = count_math_symbols(line, '+');
/* Now for that many times, do the math */
while(count > 0) {
in_quotes = false;
for (i=0; i<length; i++) {
/* Skip anything in quotes */
if (line[i] == '"')
in_quotes = !in_quotes;
if (in_quotes) continue;
/* Not the char we want, not a problem */
if (line[i] != '+') continue;
/* Otherwise, go for it */
start = get_start(line, i);
end = get_end(line, i);
left = atof(line + start);
right = atof(line + i + 1);
replace_with_float(line, start, end, left + right);
}
/* And continue the loop */
count--;
if (count == 0) break;
}
}
void subtract(char* line, size_t length) {
/* Declare vars */
size_t i, start, end, count;
bool in_quotes;
float left, right;
/* Count how many *s are in th eline, not
counting any stars between quotation marks */
count = count_math_symbols(line, '-');
/* Now for that many times, do the math */
while(count > 0) {
in_quotes = false;
for (i=0; i<length; i++) {
/* Skip anything in quotes */
if (line[i] == '"')
in_quotes = !in_quotes;
if (in_quotes) continue;
/* Not the char we want, not a problem */
if (line[i] != '-') continue;
/* Otherwise, go for it */
start = get_start(line, i);
end = get_end(line, i);
left = atof(line + start);
right = atof(line + i + 1);
replace_with_float(line, start, end, left - right);
}
/* And continue the loop */
count--;
if (count == 0) break;
}
}
void multiply(char* line, size_t length) {
/* Declare vars */
size_t i, start, end, count;
bool in_quotes;
float left, right;
/* Count how many *s are in th eline, not
counting any stars between quotation marks */
count = count_math_symbols(line, '*');
/* Now for that many times, do the math */
while(count > 0) {
in_quotes = false;
for (i=0; i<length; i++) {
/* Skip anything in quotes */
if (line[i] == '"')
in_quotes = !in_quotes;
if (in_quotes) continue;
/* Not the char we want, not a problem */
if (line[i] != '*') continue;
/* Otherwise, go for it */
start = get_start(line, i);
end = get_end(line, i);
left = atof(line + start);
right = atof(line + i + 1);
replace_with_float(line, start, end, left * right);
}
/* And continue the loop */
count--;
if (count == 0) break;
}
}
void divide(char* line, size_t length) {
/* Declare vars */
size_t i, start, end, count;
bool in_quotes;
float left, right;
/* Count how many /s are in th eline, not
counting any stars between quotation marks */
count = count_math_symbols(line, '/');
/* Now for that many times, do the math */
while(count > 0) {
length = strlen(line);
in_quotes = false;
for (i=0; i<length; i++) {
/* Skip anything in quotes */
if (line[i] == '"')
in_quotes = !in_quotes;
if (in_quotes) continue;
/* Not the char we want, not a problem */
if (line[i] != '/') continue;
/* Otherwise, go for it */
start = get_start(line, i);
end = get_end(line, i);
left = atof(line + start);
right = atof(line + i + 1);
/* Handle division by zero */
if (right == 0) {
printf("?DIVISION BY ZERO ERROR");
if (keepRunning) printf(" IN %ld", programCounter);
printf("\n");
keepRunning = false;
thereWasAnError = true;
return;
}
/* Okay, now we're good */
replace_with_float(line, start, end, left / right);
}
/* And continue the loop */
count--;
if (count == 0) break;
}
}
bool parens_match(char* string) {
size_t i, length, extras;
length = strlen(string);
extras = 0;
for (i = 0; i<length; i++) {
if (string[i] == '(') extras++;
else if (string[i] == ')') extras--;
}
return extras == 0;
}
// End of math functions