Update code to run under both Python 2 and Python 3.
Chris Pressey
3 years ago
6 | 6 | import random |
7 | 7 | import re |
8 | 8 | import sys |
9 | ||
10 | ||
11 | DEBUG = False | |
9 | 12 | |
10 | 13 | |
11 | 14 | class AST(object): |
103 | 106 | if self.scan_pattern(r'.', 'unknown character'): |
104 | 107 | return |
105 | 108 | else: |
106 | raise ValueError, "this should never happen, self.text=(%s)" % self.text | |
109 | raise ValueError("this should never happen, self.text=(%s)" % self.text) | |
107 | 110 | |
108 | 111 | def expect(self, token): |
109 | 112 | if self.token == token: |
311 | 314 | if rhs == 0: |
312 | 315 | return 0 |
313 | 316 | else: |
314 | return lhs / rhs | |
317 | return lhs // rhs | |
315 | 318 | else: |
316 | 319 | raise NotImplementedError(e.value) |
317 | 320 | |
347 | 350 | |
348 | 351 | def exec_stmt(stmt, store, ring, infile=sys.stdin, outfile=sys.stdout): |
349 | 352 | r""" |
350 | >>> from StringIO import StringIO | |
353 | >>> try: | |
354 | ... # Python 2 | |
355 | ... from StringIO import StringIO | |
356 | ... except ImportError: | |
357 | ... # Python 3 | |
358 | ... from io import StringIO | |
359 | ||
351 | 360 | >>> out = StringIO() |
352 | 361 | >>> exec_stmt(Parser('PRINT "HI"').stmt(), {'B%': {0: 88}}, None, outfile=out) |
353 | 362 | >>> out.getvalue() |
445 | 454 | |
446 | 455 | def exec_line(line, store, ring, infile=sys.stdin, outfile=sys.stdout): |
447 | 456 | r""" |
448 | >>> from StringIO import StringIO | |
457 | >>> try: | |
458 | ... # Python 2 | |
459 | ... from StringIO import StringIO | |
460 | ... except ImportError: | |
461 | ... # Python 3 | |
462 | ... from io import StringIO | |
463 | ||
449 | 464 | >>> out = StringIO() |
450 | 465 | >>> exec_line(Parser('10 PRINT "HI";:PRINT "LO"').line(), {}, None, outfile=out) |
451 | 466 | >>> out.getvalue() |
452 | 467 | 'HILO\n' |
453 | 468 | |
454 | >>> from StringIO import StringIO | |
455 | 469 | >>> out = StringIO() |
456 | 470 | >>> exec_line(Parser('(0-50) PRINT "HI":GOTO 50').line(), {}, None, outfile=out) |
457 | 471 | ('goto', 50) |
458 | 472 | >>> out.getvalue() |
459 | 473 | 'HI\n' |
460 | 474 | |
461 | >>> from StringIO import StringIO | |
462 | 475 | >>> out = StringIO() |
463 | 476 | >>> exec_line(Parser('10 GOSUB 50:PRINT "HI"').line(), {}, None, outfile=out) |
464 | 477 | ('gosub', 50) |
465 | 478 | >>> out.getvalue() |
466 | 479 | '' |
467 | 480 | |
468 | >>> from StringIO import StringIO | |
469 | 481 | >>> out = StringIO() |
470 | 482 | >>> exec_line(Parser('1 LET A%(23)=5:PRINT A%(23)').line(), {}, None, outfile=out) |
471 | 483 | >>> out.getvalue() |
568 | 580 | self.largest = line_no |
569 | 581 | tmp.append((line_no, text_line, line)) |
570 | 582 | self.lines = sorted(tmp) |
571 | if False: | |
583 | if DEBUG: | |
572 | 584 | for (line_no, text_line, line) in self.lines: |
573 | print line_no, text_line, str(line.children[1])[:50] + '...' | |
585 | print(line_no, text_line, str(line.children[1])[:50] + '...') | |
574 | 586 | |
575 | 587 | def seek_line_number(self, line_number): |
576 | 588 | i = 0 |
621 | 633 | return |
622 | 634 | else: |
623 | 635 | raise NotImplementedError |
624 | if False: | |
625 | print "AT LINE %d" % line_number | |
636 | if DEBUG: | |
637 | print("AT LINE %d" % line_number) | |
626 | 638 | |
627 | 639 | |
628 | 640 | def main(argv): |
638 | 650 | import doctest |
639 | 651 | (fails, something) = doctest.testmod(verbose=True) |
640 | 652 | if fails == 0: |
641 | print "All tests passed." | |
653 | print("All tests passed.") | |
642 | 654 | sys.exit(0) |
643 | 655 | else: |
644 | 656 | sys.exit(1) |