1 using System;
2 using System.Collections;
3 using System.Lex;
4 using System.Parsing;
5 using System;
6 using System.Numerics.Multiprecision;
7 using BigNumTokens;
8 using BigNumCalc;
9
10
11
12 public static class BigNumExpressionParser
13 {
14 public static void Parse(BigNumLexer& lexer, EvaluationStack* s)
15 {
16 #if (DEBUG)
17 if (lexer.Log() != null)
18 {
19 lexer.Log()->WriteBeginRule(u"parse");
20 lexer.Log()->IncIndent();
21 }
22 #endif
23 ++lexer;
24 System.Lex.Span span = lexer.GetSpan();
25 Match match = Expr(lexer, s);
26 if (match.hit)
27 {
28 if (*lexer == System.Lex.END_TOKEN)
29 {
30 return;
31 }
32 else
33 {
34 lexer.ThrowExpectationFailure(lexer.GetSpan(), GetEndTokenInfo());
35 }
36 }
37 else
38 {
39 lexer.ThrowExpectationFailure(span, u"Expr");
40 }
41 return;
42 }
43 public static Match Expr(BigNumLexer& lexer, EvaluationStack* s)
44 {
45 #if (DEBUG)
46 Span debugSpan;
47 bool writeToLog = lexer.Log() != null;
48 if (writeToLog)
49 {
50 debugSpan = lexer.GetSpan();
51 System.Lex.WriteBeginRuleToLog(lexer, u"Expr");
52 }
53 #endif
54 uchar op;
55 Match match(false);
56 Match* parentMatch0 = &match;
57 {
58 Match match = BigNumExpressionParser.Term(lexer, s);
59 *parentMatch0 = match;
60 }
61 if (match.hit)
62 {
63 Match match(false);
64 Match* parentMatch1 = &match;
65 {
66 Match match(true);
67 Match* parentMatch2 = &match;
68 {
69 while (true)
70 {
71 long save = lexer.GetPos();
72 {
73 Match match(false);
74 Match* parentMatch3 = &match;
75 {
76 Match match(false);
77 Match* parentMatch4 = &match;
78 {
79 Match match(false);
80 Match* parentMatch5 = &match;
81 {
82 long pos = lexer.GetPos();
83 Match match(false);
84 Match* parentMatch6 = &match;
85 {
86 Match match(false);
87 Match* parentMatch7 = &match;
88 {
89 long save = lexer.GetPos();
90 Match match(false);
91 if (*lexer == PLUS)
92 {
93 ++lexer;
94 match.hit = true;
95 }
96 *parentMatch7 = match;
97 if (!match.hit)
98 {
99 Match match(false);
100 Match* parentMatch8 = &match;
101 lexer.SetPos(save);
102 {
103 Match match(false);
104 if (*lexer == MINUS)
105 {
106 ++lexer;
107 match.hit = true;
108 }
109 *parentMatch8 = match;
110 }
111 *parentMatch7 = match;
112 }
113 }
114 *parentMatch6 = match;
115 }
116 if (match.hit)
117 {
118 op = lexer.GetUChar(pos);
119 }
120 *parentMatch5 = match;
121 }
122 *parentMatch4 = match;
123 }
124 if (match.hit)
125 {
126 Match match(false);
127 Match* parentMatch9 = &match;
128 {
129 Match match(false);
130 Match* parentMatch10 = &match;
131 {
132 long pos = lexer.GetPos();
133 Match match = BigNumExpressionParser.Term(lexer, s);
134 if (match.hit)
135 {
136 BigValue* r = s->Pop();
137 BigValue* l = s->Pop();
138 s->Push(Evaluate(l, r, op));
139 }
140 *parentMatch10 = match;
141 }
142 *parentMatch9 = match;
143 }
144 *parentMatch4 = match;
145 }
146 *parentMatch3 = match;
147 }
148 if (match.hit)
149 {
150 *parentMatch2 = match;
151 }
152 else
153 {
154 lexer.SetPos(save);
155 break;
156 }
157 }
158 }
159 }
160 *parentMatch1 = match;
161 }
162 *parentMatch0 = match;
163 }
164 #if (DEBUG)
165 if (writeToLog)
166 {
167 if (match.hit)
168 {
169 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Expr");
170 }
171 else
172 {
173 System.Lex.WriteFailureToLog(lexer, u"Expr");
174 }
175 }
176 #endif
177 if (!match.hit)
178 {
179 match.value = null;
180 }
181 return match;
182 }
183 public static Match Term(BigNumLexer& lexer, EvaluationStack* s)
184 {
185 #if (DEBUG)
186 Span debugSpan;
187 bool writeToLog = lexer.Log() != null;
188 if (writeToLog)
189 {
190 debugSpan = lexer.GetSpan();
191 System.Lex.WriteBeginRuleToLog(lexer, u"Term");
192 }
193 #endif
194 uchar op;
195 Match match(false);
196 Match* parentMatch0 = &match;
197 {
198 Match match = BigNumExpressionParser.Factor(lexer, s);
199 *parentMatch0 = match;
200 }
201 if (match.hit)
202 {
203 Match match(false);
204 Match* parentMatch1 = &match;
205 {
206 Match match(true);
207 Match* parentMatch2 = &match;
208 {
209 while (true)
210 {
211 long save = lexer.GetPos();
212 {
213 Match match(false);
214 Match* parentMatch3 = &match;
215 {
216 Match match(false);
217 Match* parentMatch4 = &match;
218 {
219 Match match(false);
220 Match* parentMatch5 = &match;
221 {
222 long pos = lexer.GetPos();
223 Match match(false);
224 Match* parentMatch6 = &match;
225 {
226 Match match(false);
227 Match* parentMatch7 = &match;
228 {
229 long save = lexer.GetPos();
230 Match match(false);
231 if (*lexer == MUL)
232 {
233 ++lexer;
234 match.hit = true;
235 }
236 *parentMatch7 = match;
237 if (!match.hit)
238 {
239 Match match(false);
240 Match* parentMatch8 = &match;
241 lexer.SetPos(save);
242 {
243 Match match(false);
244 if (*lexer == DIV)
245 {
246 ++lexer;
247 match.hit = true;
248 }
249 *parentMatch8 = match;
250 }
251 *parentMatch7 = match;
252 }
253 }
254 *parentMatch6 = match;
255 }
256 if (match.hit)
257 {
258 op = lexer.GetUChar(pos);
259 }
260 *parentMatch5 = match;
261 }
262 *parentMatch4 = match;
263 }
264 if (match.hit)
265 {
266 Match match(false);
267 Match* parentMatch9 = &match;
268 {
269 Match match(false);
270 Match* parentMatch10 = &match;
271 {
272 long pos = lexer.GetPos();
273 Match match = BigNumExpressionParser.Factor(lexer, s);
274 if (match.hit)
275 {
276 BigValue* r = s->Pop();
277 BigValue* l = s->Pop();
278 s->Push(Evaluate(l, r, op));
279 }
280 *parentMatch10 = match;
281 }
282 *parentMatch9 = match;
283 }
284 *parentMatch4 = match;
285 }
286 *parentMatch3 = match;
287 }
288 if (match.hit)
289 {
290 *parentMatch2 = match;
291 }
292 else
293 {
294 lexer.SetPos(save);
295 break;
296 }
297 }
298 }
299 }
300 *parentMatch1 = match;
301 }
302 *parentMatch0 = match;
303 }
304 #if (DEBUG)
305 if (writeToLog)
306 {
307 if (match.hit)
308 {
309 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Term");
310 }
311 else
312 {
313 System.Lex.WriteFailureToLog(lexer, u"Term");
314 }
315 }
316 #endif
317 if (!match.hit)
318 {
319 match.value = null;
320 }
321 return match;
322 }
323 public static Match Factor(BigNumLexer& lexer, EvaluationStack* s)
324 {
325 #if (DEBUG)
326 Span debugSpan;
327 bool writeToLog = lexer.Log() != null;
328 if (writeToLog)
329 {
330 debugSpan = lexer.GetSpan();
331 System.Lex.WriteBeginRuleToLog(lexer, u"Factor");
332 }
333 #endif
334 Match match(false);
335 Match* parentMatch0 = &match;
336 {
337 long save = lexer.GetPos();
338 Match match(false);
339 Match* parentMatch1 = &match;
340 {
341 long save = lexer.GetPos();
342 Match match(false);
343 Match* parentMatch2 = &match;
344 {
345 long save = lexer.GetPos();
346 Match match(false);
347 Match* parentMatch3 = &match;
348 {
349 Match match(false);
350 if (*lexer == MINUS)
351 {
352 ++lexer;
353 match.hit = true;
354 }
355 *parentMatch3 = match;
356 }
357 if (match.hit)
358 {
359 Match match(false);
360 Match* parentMatch4 = &match;
361 {
362 Match match(false);
363 Match* parentMatch5 = &match;
364 {
365 long pos = lexer.GetPos();
366 Match match = BigNumExpressionParser.Factor(lexer, s);
367 if (match.hit)
368 {
369 s->Push(Evaluate(s->Pop(), '-'));
370 }
371 *parentMatch5 = match;
372 }
373 *parentMatch4 = match;
374 }
375 *parentMatch3 = match;
376 }
377 *parentMatch2 = match;
378 if (!match.hit)
379 {
380 Match match(false);
381 Match* parentMatch6 = &match;
382 lexer.SetPos(save);
383 {
384 Match match(false);
385 Match* parentMatch7 = &match;
386 {
387 Match match(false);
388 if (*lexer == PLUS)
389 {
390 ++lexer;
391 match.hit = true;
392 }
393 *parentMatch7 = match;
394 }
395 if (match.hit)
396 {
397 Match match(false);
398 Match* parentMatch8 = &match;
399 {
400 Match match(false);
401 Match* parentMatch9 = &match;
402 {
403 long pos = lexer.GetPos();
404 Match match = BigNumExpressionParser.Factor(lexer, s);
405 if (match.hit)
406 {
407 s->Push(Evaluate(s->Pop(), '+'));
408 }
409 *parentMatch9 = match;
410 }
411 *parentMatch8 = match;
412 }
413 *parentMatch7 = match;
414 }
415 *parentMatch6 = match;
416 }
417 *parentMatch2 = match;
418 }
419 }
420 *parentMatch1 = match;
421 if (!match.hit)
422 {
423 Match match(false);
424 Match* parentMatch10 = &match;
425 lexer.SetPos(save);
426 {
427 Match match = BigNumExpressionParser.Number(lexer, s);
428 *parentMatch10 = match;
429 }
430 *parentMatch1 = match;
431 }
432 }
433 *parentMatch0 = match;
434 if (!match.hit)
435 {
436 Match match(false);
437 Match* parentMatch11 = &match;
438 lexer.SetPos(save);
439 {
440 Match match(false);
441 Match* parentMatch12 = &match;
442 {
443 Match match(false);
444 Match* parentMatch13 = &match;
445 {
446 Match match(false);
447 if (*lexer == LPAREN)
448 {
449 ++lexer;
450 match.hit = true;
451 }
452 *parentMatch13 = match;
453 }
454 if (match.hit)
455 {
456 Match match(false);
457 Match* parentMatch14 = &match;
458 {
459 Match match = BigNumExpressionParser.Expr(lexer, s);
460 *parentMatch14 = match;
461 }
462 *parentMatch13 = match;
463 }
464 *parentMatch12 = match;
465 }
466 if (match.hit)
467 {
468 Match match(false);
469 Match* parentMatch15 = &match;
470 {
471 Match match(false);
472 if (*lexer == RPAREN)
473 {
474 ++lexer;
475 match.hit = true;
476 }
477 *parentMatch15 = match;
478 }
479 *parentMatch12 = match;
480 }
481 *parentMatch11 = match;
482 }
483 *parentMatch0 = match;
484 }
485 }
486 #if (DEBUG)
487 if (writeToLog)
488 {
489 if (match.hit)
490 {
491 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Factor");
492 }
493 else
494 {
495 System.Lex.WriteFailureToLog(lexer, u"Factor");
496 }
497 }
498 #endif
499 if (!match.hit)
500 {
501 match.value = null;
502 }
503 return match;
504 }
505 public static Match Number(BigNumLexer& lexer, EvaluationStack* s)
506 {
507 #if (DEBUG)
508 Span debugSpan;
509 bool writeToLog = lexer.Log() != null;
510 if (writeToLog)
511 {
512 debugSpan = lexer.GetSpan();
513 System.Lex.WriteBeginRuleToLog(lexer, u"Number");
514 }
515 #endif
516 Match match(false);
517 Match* parentMatch0 = &match;
518 {
519 long save = lexer.GetPos();
520 Match match(false);
521 Match* parentMatch1 = &match;
522 {
523 long save = lexer.GetPos();
524 Match match(false);
525 Match* parentMatch2 = &match;
526 {
527 long pos = lexer.GetPos();
528 Match match(false);
529 if (*lexer == FLOAT)
530 {
531 ++lexer;
532 match.hit = true;
533 }
534 if (match.hit)
535 {
536 s->Push(ParseBigFloat(lexer.GetToken(pos)));
537 }
538 *parentMatch2 = match;
539 }
540 *parentMatch1 = match;
541 if (!match.hit)
542 {
543 Match match(false);
544 Match* parentMatch3 = &match;
545 lexer.SetPos(save);
546 {
547 Match match(false);
548 Match* parentMatch4 = &match;
549 {
550 long pos = lexer.GetPos();
551 Match match(false);
552 if (*lexer == RATIONAL)
553 {
554 ++lexer;
555 match.hit = true;
556 }
557 if (match.hit)
558 {
559 s->Push(ParseBigRational(lexer.GetToken(pos)));
560 }
561 *parentMatch4 = match;
562 }
563 *parentMatch3 = match;
564 }
565 *parentMatch1 = match;
566 }
567 }
568 *parentMatch0 = match;
569 if (!match.hit)
570 {
571 Match match(false);
572 Match* parentMatch5 = &match;
573 lexer.SetPos(save);
574 {
575 Match match(false);
576 Match* parentMatch6 = &match;
577 {
578 long pos = lexer.GetPos();
579 Match match(false);
580 if (*lexer == INTEGER)
581 {
582 ++lexer;
583 match.hit = true;
584 }
585 if (match.hit)
586 {
587 s->Push(ParseBigInteger(lexer.GetToken(pos)));
588 }
589 *parentMatch6 = match;
590 }
591 *parentMatch5 = match;
592 }
593 *parentMatch0 = match;
594 }
595 }
596 #if (DEBUG)
597 if (writeToLog)
598 {
599 if (match.hit)
600 {
601 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Number");
602 }
603 else
604 {
605 System.Lex.WriteFailureToLog(lexer, u"Number");
606 }
607 }
608 #endif
609 if (!match.hit)
610 {
611 match.value = null;
612 }
613 return match;
614 }
615 }