forked from alpertron/calculators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.c
419 lines (408 loc) · 12.3 KB
/
batch.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
/*
This file is part of Alpertron Calculators.
Copyright 2017 Dario Alejandro Alpern
Alpertron Calculators is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Alpertron Calculators is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bignbr.h"
#include "expression.h"
#include "factor.h"
#include "showtime.h"
#include "batch.h"
static char *ptrEndBatchFactor;
static char *ptrCurrBatchFactor;
static char *ptrNextBatchFactor;
static int firstExprProcessed;
int valuesProcessed;
char outputExpr[100000];
#ifdef __EMSCRIPTEN__
char *ptrInputText;
#endif
static void stringToHTML(char **pptrOutput, char *ptrString)
{
int character;
char *ptrOutput = *pptrOutput;
for (;;)
{
char c = *ptrString;
if (c == 0)
{
break; // End of string, so go out.
}
if ((c & 0x80) == 0)
{ // 1-byte UTF-8 character
character = c;
ptrString++;
}
else if ((c & 0x60) == 0x40)
{ // 2-byte UTF-8 character
character = (int)(c & 0x1F) * 64 + (*(ptrString + 1) & 0x3F);
ptrString += 2;
}
else
{ // 3-byte UTF-8 character
character = ((int)(c & 0x1F) << 12) +
((int)(*(ptrString + 1) & 0x3F) << 6) +
(*(ptrString + 2) & 0x3F);
ptrString += 3;
}
if (character >= ' ' && character < 127 && character != '<' &&
character != '>' && character != '&')
{ // Safe to copy character.
*ptrOutput++ = (char)character;
}
else
{ // Insert HTML entity.
*ptrOutput++ = '&';
*ptrOutput++ = '#';
int2dec(&ptrOutput, character);
*ptrOutput++ = ';';
}
}
*pptrOutput = ptrOutput;
}
static char evalExpression(char *expr, BigInteger *ptrResult)
{
char *ptrInputExpr = expr;
char *ptrOutputExpr = outputExpr;
while (*ptrInputExpr != 0)
{
char c = *ptrInputExpr;
if (c == ';')
{
break;
}
// Copy character to output expression.
*ptrOutputExpr++ = c;
ptrInputExpr++;
}
*ptrOutputExpr = 0; // Append string terminator.
return ComputeExpression(outputExpr, 1, ptrResult);
}
static void SkipSpaces(char **pptrText)
{
char *ptrText = *pptrText;
while (*ptrText == ' ' || *ptrText == 9)
{ // Skip spaces or tabs.
ptrText++;
}
*pptrText = ptrText;
}
static void BatchError(char **pptrOutput, char *batchText, const char *errorText)
{
char *ptrOutput = *pptrOutput;
stringToHTML(&ptrOutput, batchText);
*ptrOutput++ = ':';
*ptrOutput++ = ' ';
strcpy(ptrOutput, errorText);
ptrOutput += strlen(ptrOutput);
*pptrOutput = ptrOutput;
}
enum eExprErr BatchProcessing(char *batchText, BigInteger *valueFound, char **pptrOutput)
{
int endValuesProcessed;
char *ptrOutput = output;
char *NextExpr, *EndExpr;
char *ptrExprToProcess = NULL;
char *ptrConditionExpr = NULL;
enum eExprErr rc = EXPR_OK;
char *ptrCharFound, *ptrSrcString, *ptrStartExpr;
strcpy(ptrOutput, "2<ul><li>");
ptrOutput += strlen(ptrOutput);
endValuesProcessed = valuesProcessed + 1000;
if (valuesProcessed == 0)
{ // Start batch factorization.
ptrCurrBatchFactor = batchText;
ptrEndBatchFactor = batchText + strlen(batchText);
firstExprProcessed = FALSE;
}
for (; ptrCurrBatchFactor < ptrEndBatchFactor; ptrCurrBatchFactor += strlen(ptrCurrBatchFactor) + 1)
{ // Get next line.
char c;
expressionNbr = 0;
if (firstExprProcessed == FALSE)
{
valueX.nbrLimbs = 0; // Invalidate variable x and counter c.
ptrNextBatchFactor = findChar(ptrCurrBatchFactor, '\n');
if (ptrNextBatchFactor != NULL)
{
*ptrNextBatchFactor = 0; // Indicate end of line.
}
counterC = 1;
}
// Skip leading spaces.
ptrSrcString = ptrCurrBatchFactor;
SkipSpaces(&ptrSrcString);
c = *ptrSrcString;
if (c == 0)
{ // Empty line.
strcpy(ptrOutput, "<br>");
ptrOutput += strlen(ptrOutput);
}
else if (c == '#')
{ // Copy comment to output, but convert non-safe characters to entities.
*ptrOutput++ = '#';
stringToHTML(&ptrOutput, ptrCurrBatchFactor + 1);
}
else if (c == 'x' || c == 'X')
{ // Loop format: x=<orig expr>; x=<next expr>; <end expr>; <expr to factor>[; <factor cond>]
#ifdef __EMSCRIPTEN__
ptrInputText = "";
#endif
if (firstExprProcessed == FALSE)
{
ptrCharFound = findChar(ptrSrcString + 1, ';');
if (ptrCharFound == NULL)
{
BatchError(&ptrOutput, batchText,
lang ? "se esperaban tres o cuatro puntos y comas pero no hay ninguno" :
"three or four semicolons expected but none found");
continue;
}
ptrStartExpr = ptrSrcString + 1;
SkipSpaces(&ptrStartExpr);
if (*ptrStartExpr != '=')
{
BatchError(&ptrOutput, batchText,
lang ? "falta signo igual en la primera expresión" :
"equal sign missing in first expression");
continue;
}
ptrCharFound = findChar(ptrSrcString + 1, ';');
expressionNbr = 1;
rc = evalExpression(ptrStartExpr + 1, valueFound);
CopyBigInt(&valueX, valueFound);
if (rc != EXPR_OK)
{
textError(ptrOutput, rc);
ptrOutput += strlen(ptrOutput);
continue;
}
ptrStartExpr = ptrCharFound + 1;
SkipSpaces(&ptrStartExpr);
if (*ptrStartExpr != 'x' && *ptrStartExpr != 'X')
{
BatchError(&ptrOutput, batchText,
lang ? "falta variable x en la segunda expresión" :
"variable x missing in second expression");
continue;
}
ptrStartExpr++; // Skip variable 'x'.
SkipSpaces(&ptrStartExpr);
if (*ptrStartExpr != '=')
{
BatchError(&ptrOutput, batchText,
lang ? "falta signo igual en la segunda expresión" :
"equal sign missing in second expression");
continue;
}
NextExpr = ptrStartExpr + 1; // Skip equal sign.
ptrCharFound = findChar(ptrStartExpr, ';'); // Find second semicolon.
if (ptrCharFound == NULL)
{ // Third semicolon not found.
BatchError(&ptrOutput, batchText,
lang ? "se esperaban tres o cuatro puntos y comas pero solo hay uno" :
"three or four semicolons expected but there are only one");
continue;
}
EndExpr = ptrCharFound + 1; // Point to end expression.
ptrCharFound = findChar(EndExpr, ';'); // Find third semicolon.
if (ptrCharFound == NULL)
{ // Third semicolon not found.
BatchError(&ptrOutput, batchText,
lang ? "se esperaban tres o cuatro puntos y comas pero solo hay dos" :
"three or four semicolons expected but there are only two");
continue;
}
ptrExprToProcess = ptrCharFound + 1;
ptrConditionExpr = findChar(ptrExprToProcess, ';'); // Find optional fourth semicolon.
if (ptrConditionExpr != NULL)
{
ptrConditionExpr++;
}
firstExprProcessed = TRUE;
}
else
{
NextExpr = findChar(ptrCurrBatchFactor, ';') + 1; // Point to "x="
SkipSpaces(&NextExpr); // Now point to "x"
NextExpr++;
SkipSpaces(&NextExpr); // Now point to "="
NextExpr++;
SkipSpaces(&NextExpr); // Now point to next expression.
EndExpr = findChar(NextExpr, ';') + 1;
ptrExprToProcess = findChar(EndExpr, ';') + 1;
ptrConditionExpr = findChar(ptrExprToProcess, ';');
if (ptrConditionExpr != NULL)
{
ptrConditionExpr++;
}
}
while (ptrOutput < &output[sizeof(output) - 100000])
{ // Perform loop.
int processExpression = TRUE;
expressionNbr = 3;
rc = evalExpression(EndExpr, valueFound);
if (rc != EXPR_OK)
{
textError(ptrOutput, rc);
ptrOutput += strlen(ptrOutput);
break; // Cannot compute end expression, so go out.
}
if (valueFound->nbrLimbs == 1 && valueFound->limbs[0].x == 0)
{ // result is zero: end of loop
firstExprProcessed = FALSE;
break;
}
if (valuesProcessed >= endValuesProcessed)
{
output[0] = '6'; // Show Continue button.
break;
}
if (ptrConditionExpr != NULL)
{
expressionNbr = 5;
rc = evalExpression(ptrConditionExpr, valueFound);
if (rc == EXPR_OK)
{
if (valueFound->nbrLimbs == 1 && valueFound->limbs[0].x == 0)
{ // Do not compute factor expression if condition is false.
processExpression = FALSE;
}
}
else
{
textError(ptrOutput, rc);
ptrOutput += strlen(ptrOutput);
if (rc == EXPR_SYNTAX_ERROR || rc == EXPR_VAR_OR_COUNTER_REQUIRED)
{ // Do not show multiple errors.
break;
}
processExpression = FALSE;
}
}
if (processExpression)
{
expressionNbr = 4;
rc = evalExpression(ptrExprToProcess, valueFound);
if (rc == EXPR_OK)
{
batchCallback(&ptrOutput);
}
else
{
textError(ptrOutput, rc);
ptrOutput += strlen(ptrOutput);
}
if (rc == EXPR_SYNTAX_ERROR || rc == EXPR_VAR_OR_COUNTER_REQUIRED)
{ // Do not show multiple errors.
break;
}
}
expressionNbr = 2;
rc = evalExpression(NextExpr, valueFound);
if (rc != EXPR_OK)
{
textError(ptrOutput, rc);
ptrOutput += strlen(ptrOutput);
break; // Cannot compute next expression, so go out.
}
CopyBigInt(&valueX, valueFound);
if (processExpression)
{
counterC++;
strcpy(ptrOutput, "</li><li>");
valuesProcessed++;
ptrOutput += strlen(ptrOutput);
}
if (rc == EXPR_SYNTAX_ERROR || rc == EXPR_VAR_OR_COUNTER_REQUIRED)
{ // Do not show these errors multiple times.
firstExprProcessed = FALSE;
break;
}
}
if (valuesProcessed >= endValuesProcessed)
{
break;
}
}
else
{ // Factor expression (not loop).
#ifdef __EMSCRIPTEN__
ptrInputText = ptrSrcString;
#endif
if (valuesProcessed >= endValuesProcessed)
{
output[0] = '6'; // Show Continue button.
}
rc = ComputeExpression(ptrSrcString, 1, valueFound);
if (rc == EXPR_OK)
{
batchCallback(&ptrOutput);
}
else
{
textError(ptrOutput, rc);
ptrOutput += strlen(ptrOutput);
}
counterC = 2;
strcpy(ptrOutput, "</li><li>");
valuesProcessed++;
ptrOutput += strlen(ptrOutput);
}
if (counterC == 1)
{
strcpy(ptrOutput, "</li>");
ptrOutput += strlen(ptrOutput);
}
if (ptrOutput >= &output[sizeof(output) - 100000])
{
output[0] = '6'; // Show Continue button.
break;
}
if ((*ptrCurrBatchFactor & 0xDF) == 'x')
{ // Loop mode.
if (ptrConditionExpr)
{
ptrCurrBatchFactor += strlen(ptrConditionExpr) + 1;
ptrConditionExpr = NULL;
}
else
{
ptrCurrBatchFactor += strlen(ptrExprToProcess) + 1;
}
}
valueX.nbrLimbs = 0; // Invalidate variable x and counter c.
}
if (counterC > 1)
{
ptrOutput -= 4; // Erase start tag <li> without contents.
}
strcpy(ptrOutput, "</ul>");
ptrOutput += strlen(ptrOutput);
*pptrOutput = ptrOutput;
return rc;
}
char *findChar(char *str, char c)
{
while (*str != 0)
{
if (*str == c)
{
return str;
}
str++;
}
return NULL;
}