def main(argList=None): global debug, register_display, memory_display, visualize, \ test_case, test_case_is_exhaustive # argument handling: fname = 0 filename = "out.b" accepting_test_case = False if not argList: argList = [] for arg in argList: if fname: filename = arg fname = 0 continue elif accepting_test_case: if test_case: print("hmmmSimulator: more than one test case specified") sys.exit(1) test_case, test_case_is_exhaustive = eval(arg) if not test_case[1] and not test_case_is_exhaustive: # Vacuous test case: no expected output; because the # test case is not exhaustive, nothing that the # program could print could fail the test case; and # determining whether the input is consumed is # equivalent to the halting problem for a possibly # non-terminating program (which is what is generally # indicated by a non-exhaustive test). We can't do # anything useful, so just pass the test. print("Warning: vacuous test case.") print("[[ test case passed ]]") sys.exit(0) accepting_test_case = False elif arg == "--test-case": accepting_test_case = True debug = 2 elif arg[:2] == "-f": if arg[2:]: filename = arg[2:] else: fname = 1 elif arg == "-d" or arg == "--debug": debug = 1 elif arg == "-m" or arg == "-mr" or arg == "-rm" or arg == "--memory-display": memory_display = 1 elif arg == "-n" or arg == "--no-debug": debug = 2 elif arg == "-r" or arg == "-mr" or arg == "-rm" or arg == "--register-display": register_display = 1 else: if arg not in ("-h", "--help"): print("error: unrecognized argument: {}".format(arg)) print("hmmmSimulator.py") print( " Python program for simulating a Harvey Mudd Miniature Machine." ) print("Takes files compiled with hmmAssembler.py as input.") print(" Options:") print(" -d, --debug debugging mode") print( " -f filename use filename as the input file") print(" -h, --help print this help message") print( " -n, --no-debug do not prompt for debugging mode\n" ) print(" --test-case <test-case> run the provided test case") print( " Warning: --test-case is *not* validated, use hmmmgrader.py for validation" ) if arg in ("-h", "--help"): sys.exit(0) else: sys.exit(1) if filename == "": filename = input("Enter binary input file name: ") readfile(filename) # to read from stdin instead we would use: program = sys.stdin.readlines() if debug == 0: yn = input("Enter debugging mode? ") if re.findall(r'(^y[es]*)|(^indeed)|^t$|(^true)|(^affirmative)', yn): debug = 1 if debug == 2: debug = 0 if memory_display or register_display: import visualize if memory_display: visualize.mem_setup() if register_display: visualize.reg_setup() try: run() if test_case: inputs, outputs = test_case if input_index < len(inputs): remaining = inputs[input_index:] if len(remaining) > 1: error = ("inputs {} were not consumed by the program". format(remaining)) else: error = ( "input '{}' was not consumed by the program".format( remaining[0])) raise HMMMTestFailure(error) if output_index < len(outputs): remaining = outputs[output_index:] all_ellipses = True for output in remaining: if output is not Ellipsis: all_ellipses = False break if len(remaining) > 1: if all_ellipses: error = ( "expected at least {} more outputs from the program" .format(len(remaining))) else: error = ("expected outputs {} were not produced by the" " program".format(remaining)) else: if all_ellipses: error = "expected at least one more output from the program" else: error = ("expected output '{}' was not produced by the" " program".format(remaining[0])) raise HMMMTestFailure(error) except KeyboardInterrupt: print("\n\nInterrupted by user, halting program execution...\n") sys.exit(1) except EOFError: print("\n\nEnd of input, halting program execution...\n") sys.exit(1) except HMMMTestFailure as e: print("[[ test case failed: {} ]]".format(str(e))) sys.exit(1) except HMMMTestSuccess: pass if test_case: print("[[ test case passed ]]") sys.exit(0)
def main ( argList=None ) : global debug, register_display, memory_display, visualize, \ test_case, test_case_is_exhaustive # argument handling: fname = 0 filename = "out.b" accepting_test_case = False if not argList: argList = [] for arg in argList : if fname : filename = arg fname = 0 continue elif accepting_test_case: if test_case: print("hmmmSimulator: more than one test case specified") sys.exit(1) test_case, test_case_is_exhaustive = eval(arg) if not test_case[1] and not test_case_is_exhaustive: # Vacuous test case: no expected output; because the # test case is not exhaustive, nothing that the # program could print could fail the test case; and # determining whether the input is consumed is # equivalent to the halting problem for a possibly # non-terminating program (which is what is generally # indicated by a non-exhaustive test). We can't do # anything useful, so just pass the test. print("Warning: vacuous test case.") print("[[ test case passed ]]") sys.exit(0) accepting_test_case = False elif arg == "--test-case": accepting_test_case = True debug = 2 elif arg[:2] == "-f" : if arg[2:] : filename = arg[2:] else: fname = 1 elif arg == "-d" or arg == "--debug" : debug = 1 elif arg == "-m" or arg == "-mr" or arg == "-rm" or arg == "--memory-display" : memory_display = 1 elif arg == "-n" or arg == "--no-debug" : debug = 2 elif arg == "-r" or arg == "-mr" or arg == "-rm" or arg == "--register-display" : register_display = 1 else: if arg not in ("-h", "--help"): print("error: unrecognized argument: {}" .format(arg)) print("hmmmSimulator.py") print(" Python program for simulating a Harvey Mudd Miniature Machine.") print("Takes files compiled with hmmAssembler.py as input.") print(" Options:") print(" -d, --debug debugging mode") print(" -f filename use filename as the input file") print(" -h, --help print this help message") print(" -n, --no-debug do not prompt for debugging mode\n") print(" --test-case <test-case> run the provided test case") print(" Warning: --test-case is *not* validated, use hmmmgrader.py for validation") if arg in ("-h", "--help"): sys.exit(0) else: sys.exit(1) if filename == "" : filename = input("Enter binary input file name: ") readfile(filename) # to read from stdin instead we would use: program = sys.stdin.readlines() if debug == 0: yn = input("Enter debugging mode? ") if re.findall(r'(^y[es]*)|(^indeed)|^t$|(^true)|(^affirmative)', yn) : debug = 1 if debug == 2: debug = 0 if memory_display or register_display : import visualize if memory_display : visualize.mem_setup() if register_display : visualize.reg_setup() try : run() if test_case: inputs, outputs = test_case if input_index < len(inputs): remaining = inputs[input_index:] if len(remaining) > 1: error = ("inputs {} were not consumed by the program" .format(remaining)) else: error = ("input '{}' was not consumed by the program" .format(remaining[0])) raise HMMMTestFailure(error) if output_index < len(outputs): remaining = outputs[output_index:] all_ellipses = True for output in remaining: if output is not Ellipsis: all_ellipses = False break if len(remaining) > 1: if all_ellipses: error = ("expected at least {} more outputs from the program" .format(len(remaining))) else: error = ("expected outputs {} were not produced by the" " program" .format(remaining)) else: if all_ellipses: error = "expected at least one more output from the program" else: error = ("expected output '{}' was not produced by the" " program" .format(remaining[0])) raise HMMMTestFailure(error) except KeyboardInterrupt : print("\n\nInterrupted by user, halting program execution...\n") sys.exit(1) except EOFError : print("\n\nEnd of input, halting program execution...\n") sys.exit(1) except HMMMTestFailure as e: print("[[ test case failed: {} ]]".format(str(e))) sys.exit(1) except HMMMTestSuccess: pass if test_case: print("[[ test case passed ]]") sys.exit(0)
def main ( argList=None ) : global debug, register_display, memory_display, visualize # argument handling: fname = 0 filename = "out.b" if not argList: argList = [] for arg in argList : if fname : filename = arg fname = 0 continue elif arg[:2] == "-f" : if arg[2:] : filename = arg[2:] else: fname = 1 elif arg == "-d" or arg == "--debug" : debug = 1 elif arg == "-m" or arg == "-mr" or arg == "-rm" or arg == "--memory-display" : memory_display = 1 elif arg == "-n" or arg == "--no-debug" : debug = 2 elif arg == "-r" or arg == "-mr" or arg == "-rm" or arg == "--register-display" : register_display = 1 elif arg == "-h" or arg == "--help" : print "hmmmSimulator.py" print " Python program for simulating a Harvey Mudd Miniature Machine." print "Takes files compiled with hmmAssembler.py as input." print " Options:" print " -d, --debug debugging mode" print " -f filename use filename as the input file" print " -h, --help print this help message" print " -n, --no-debug do not prompt for debugging mode\n" sys.exit() if filename == "" : filename = raw_input("Enter binary input file name: ") readfile(filename) # to read from stdin instead we would use: program = sys.stdin.readlines() if debug == 0: yn = raw_input("Enter debugging mode? ") if re.findall(r'(^y[es]*)|(^indeed)|^t$|(^true)|(^affirmative)', yn) : debug = 1 if debug == 2: debug = 0 if memory_display or register_display : import visualize if memory_display : visualize.mem_setup() if register_display : visualize.reg_setup() try : run() except KeyboardInterrupt : print "\n\nInterrupted by user, halting program execution...\n" sys.exit() except EOFError : print "\n\nEnd of input, halting program execution...\n" sys.exit()