def start_interactive_shell(): try: if command_line_arguments[1] == "-m" and command_line_arguments[ 2].isdecimal(): memory_size = int(command_line_arguments[2]) elif command_line_arguments[1].split( "=")[0] == "--memory-size" and command_line_arguments[1].split( "=")[1].isdecimal(): memory_size = int(command_line_arguments[1].split("=")[1]) else: raise IndexError() if memory_size > 100: proceed_or_not = input( f"""You are going to allocate more than {memory_size} nodes or {4 * memory_size} cells. Are you sure? ("n" to exit) """ ) if proceed_or_not == "n": print("Execution terminated.") return interpreter = Interpreter(memory_size) while True: cmd = input(">>> ") try: interpreter.execute_command(cmd) except Exception as err: print(type(err), err) except IndexError: print( "ERROR: Memory options not specified correctly. Use -m [memory size] or --memory-size=[memory size]." )
def execute_file(): try: if command_line_arguments[1] == "-m" and command_line_arguments[ 2].isdecimal(): try: file_path = command_line_arguments[3] except IndexError: raise FileNotFoundError() memory_size = int(command_line_arguments[2]) elif command_line_arguments[1].split( "=")[0] == "--memory-size" and command_line_arguments[1].split( "=")[1].isdecimal(): try: file_path = command_line_arguments[2] except IndexError: raise FileNotFoundError() memory_size = int(command_line_arguments[1].split("=")[1]) else: raise IndexError() if memory_size > 100: proceed_or_not = input(f""" You are going to allocate more than {memory_size} nodes or {4 * memory_size} cells. Are you sure? (y/n) """ ) if proceed_or_not == "n": print("Execution terminated.") return file = open(file_path, "r") lines = file.readlines() file.close() interpreter = Interpreter(memory_size) line_number = 1 for line in lines: try: interpreter.execute_command(line[:-1]) # removing \n line_number += 1 except Exception as err: print(f"Line number #{line_number}", type(err), err) except IndexError: print( "ERROR: Memory options not specified correctly. Use -m [memory size] or --memory-size=[memory size]." ) except FileNotFoundError: print("ERROR: Module not found!")
def test_print_command(self): i = Interpreter(100) i.execute_command("L1 = ((a)(b((c)d))ef(g(h)))") i.execute_command("Print L1")
def test_invalid_list_expression_raising_syntax_error(self): i = Interpreter(100) self.assertRaises( SyntaxError, i.execute_command, "salam = ((*)(*))(8))") # Invalid list expression raises error
def test_garbage_collect(self): i = Interpreter(100) i.execute_command("a = ((((Z)Y)X)TM(A(B(C))))") i.execute_command("b = ((V)PQ(R(S)))") i.execute_command("c = (123(8)45(6(7)))") i.execute_command("Print c") i.execute_command("Make (ABC) Child of b at (***(** Without Root") i.execute_command("Print b") i.execute_command("Make b Child of a at (*** Without Root") i.execute_command("Print a") i.execute_command("b = (D(E)(F(H))(G))") i.execute_command("Print b") i.execute_command("c = (B)") i.execute_command( "Garbage-Collect" ) # We can see that list c was garbage collected and returned to memory
def test_different_variable_name(self): i = Interpreter(100) i.execute_command("d222_good_var3 = (((((A(C)B)))))" ) # testing variable names with numbers i.execute_command("Print d222_good_var3")
def test_make_child_with_root_command(self): i = Interpreter(100) i.execute_command("b = ((V)PQ(R(S)))") i.execute_command("Make (ABC) Child of b at (***(** With Root") i.execute_command("Print b")
def test_make_child_command(self): i = Interpreter(100) i.execute_command("a = ((((Z)Y)X)TM(A(B(C))))") i.execute_command("b = ((V)PQ(R(S)))") i.execute_command("Make (ABC) Child of b at (***(** Without Root") i.execute_command("Print b") i.execute_command("Make b Child of a at (*** Without Root") i.execute_command("Print a")
def test_empty_list_command(self): i = Interpreter(100) i.execute_command("salam = ()") i.execute_command("Print salam")
def test_delete_command(self): i = Interpreter(100) i.execute_command("salam = ((a)(b((c)d))ef(g(h)))") i.execute_command("Delete salam from (**") i.execute_command("Print salam")