git @ Cat's Eye Technologies SICKBAY / cb24379
Update code to run under both Python 2 and Python 3. Chris Pressey 3 years ago
2 changed file(s) with 26 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
66 import random
77 import re
88 import sys
9
10
11 DEBUG = False
912
1013
1114 class AST(object):
103106 if self.scan_pattern(r'.', 'unknown character'):
104107 return
105108 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)
107110
108111 def expect(self, token):
109112 if self.token == token:
311314 if rhs == 0:
312315 return 0
313316 else:
314 return lhs / rhs
317 return lhs // rhs
315318 else:
316319 raise NotImplementedError(e.value)
317320
347350
348351 def exec_stmt(stmt, store, ring, infile=sys.stdin, outfile=sys.stdout):
349352 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
351360 >>> out = StringIO()
352361 >>> exec_stmt(Parser('PRINT "HI"').stmt(), {'B%': {0: 88}}, None, outfile=out)
353362 >>> out.getvalue()
445454
446455 def exec_line(line, store, ring, infile=sys.stdin, outfile=sys.stdout):
447456 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
449464 >>> out = StringIO()
450465 >>> exec_line(Parser('10 PRINT "HI";:PRINT "LO"').line(), {}, None, outfile=out)
451466 >>> out.getvalue()
452467 'HILO\n'
453468
454 >>> from StringIO import StringIO
455469 >>> out = StringIO()
456470 >>> exec_line(Parser('(0-50) PRINT "HI":GOTO 50').line(), {}, None, outfile=out)
457471 ('goto', 50)
458472 >>> out.getvalue()
459473 'HI\n'
460474
461 >>> from StringIO import StringIO
462475 >>> out = StringIO()
463476 >>> exec_line(Parser('10 GOSUB 50:PRINT "HI"').line(), {}, None, outfile=out)
464477 ('gosub', 50)
465478 >>> out.getvalue()
466479 ''
467480
468 >>> from StringIO import StringIO
469481 >>> out = StringIO()
470482 >>> exec_line(Parser('1 LET A%(23)=5:PRINT A%(23)').line(), {}, None, outfile=out)
471483 >>> out.getvalue()
568580 self.largest = line_no
569581 tmp.append((line_no, text_line, line))
570582 self.lines = sorted(tmp)
571 if False:
583 if DEBUG:
572584 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] + '...')
574586
575587 def seek_line_number(self, line_number):
576588 i = 0
621633 return
622634 else:
623635 raise NotImplementedError
624 if False:
625 print "AT LINE %d" % line_number
636 if DEBUG:
637 print("AT LINE %d" % line_number)
626638
627639
628640 def main(argv):
638650 import doctest
639651 (fails, something) = doctest.testmod(verbose=True)
640652 if fails == 0:
641 print "All tests passed."
653 print("All tests passed.")
642654 sys.exit(0)
643655 else:
644656 sys.exit(1)
00 #!/bin/sh
11
2 script/SAWBONES -t
2 python2 script/SAWBONES -t || exit 1
3 python3 script/SAWBONES -t || exit 1