|
| 1 | +import sys |
| 2 | +name = sys.argv[1].split('/')[-1].split('.')[0] |
| 3 | +with open(sys.argv[1]) as f: |
| 4 | + lines_correct = [l.strip('\n') for l in f.readlines()] |
| 5 | +lines_me = [l.strip('\n') for l in sys.stdin.readlines()] |
| 6 | +if len(lines_me) != len(lines_correct): |
| 7 | + if len(lines_me) == 0: |
| 8 | + print('{:<20}: no output'.format(name)) |
| 9 | + elif lines_me[0].find('syntax error') >= 0: |
| 10 | + print('{:<20}: syntax error'.format(name)) |
| 11 | + elif lines_me[0].find(' cannot be compiled') >= 0: |
| 12 | + print('{:<20}: compile error: {}'.format(name, lines_me[0])) |
| 13 | + else: |
| 14 | + print('{:<20}: mismatch in number of lines'.format(name)) |
| 15 | +else: |
| 16 | + total = len(lines_me) |
| 17 | + same = 0 |
| 18 | + bad_num_fields = 0 |
| 19 | + bad_2 = 0 |
| 20 | + bad_3 = 0 |
| 21 | + jump_op = ['JUMP_FORWARD', 'JUMP_ABSOLUTE', 'POP_JUMP_IF_FALSE', 'POP_JUMP_IF_TRUE', 'SETUP_LOOP'] |
| 22 | + jump_abs_op = ['JUMP_FORWARD', 'JUMP_ABSOLUTE'] |
| 23 | + for i in range(total): |
| 24 | + if lines_me[i] == lines_correct[i]: |
| 25 | + same += 1 |
| 26 | + else: |
| 27 | + # line is different |
| 28 | + line_me = lines_me[i].strip().split(' ', 2) |
| 29 | + line_correct = lines_correct[i].strip().split(' ', 2) |
| 30 | + allow = False |
| 31 | + if len(line_me) != len(line_correct): |
| 32 | + bad_num_fields += 1 |
| 33 | + elif len(line_me) == 2: |
| 34 | + if line_me[0] == line_correct[0] == 'stacksize': |
| 35 | + allow = True |
| 36 | + else: |
| 37 | + bad_2 += 1 |
| 38 | + else: |
| 39 | + assert(len(line_me) == 3) |
| 40 | + if line_me[0] == line_correct[0] and line_me[1] in jump_abs_op and line_correct[1] in jump_abs_op: |
| 41 | + allow = True |
| 42 | + elif line_me[0] == line_correct[0] and line_me[1] == line_correct[1] in jump_op: |
| 43 | + allow = True |
| 44 | + else: |
| 45 | + bad_3 += 1 |
| 46 | + #if not allow: |
| 47 | + # print(line_me, 'vs', line_correct) |
| 48 | + |
| 49 | + bad_str = '' |
| 50 | + if bad_num_fields > 0: |
| 51 | + bad_str += ', {} bad num fields'.format(bad_num_fields) |
| 52 | + if bad_2 > 0: |
| 53 | + bad_str += ', {} bad 2-field'.format(bad_2) |
| 54 | + if bad_3 > 0: |
| 55 | + bad_str += ', {} bad 3-field'.format(bad_3) |
| 56 | + print('{:<20}: {:>6} lines, {:>5.1f}% correct{}'.format(name, total, 100 * same / total, bad_str)) |
0 commit comments