what_to_execute = { "instructions": [("LOAD_VALUE", 0), # the first number ("LOAD_VALUE", 1), # the second number ("ADD_TWO_VALUES", None), ("PRINT_ANSWER", None)], "numbers": [7, 5] }
对于指令集的解释器
Python的动态方法查找
1 2 3 4 5 6 7 8 9 10
defexecute(self, what_to_execute): instructions = what_to_execute["instructions"] for each_step in instructions: instruction, argument = each_step argument = self.parse_argument(instruction, argument, what_to_execute) bytecode_method = getattr(self, instruction) if argument isNone: bytecode_method() else: bytecode_method(argument)
classVirtualMachine(object): def__init__(self): self.frames = [] # The call stack of frames. self.frame = None# The current frame. self.return_value = None self.last_exception = None
defrun_code(self, code, global_names=None, local_names=None): """ An entry point to execute code using the virtual machine.""" frame = self.make_frame(code, global_names=global_names, local_names=local_names) self.run_frame(frame)
defparse_byte_and_args(self): f = self.frame opoffset = f.last_instruction byteCode = f.code_obj.co_code[opoffset] f.last_instruction += 1 byte_name = dis.opname[byteCode] if byteCode >= dis.HAVE_ARGUMENT: # index into the bytecode arg = f.code_obj.co_code[f.last_instruction:f.last_instruction+2] f.last_instruction += 2# advance the instruction pointer arg_val = arg[0] + (arg[1] * 256) if byteCode in dis.hasconst: # Look up a constant arg = f.code_obj.co_consts[arg_val] elif byteCode in dis.hasname: # Look up a name arg = f.code_obj.co_names[arg_val] elif byteCode in dis.haslocal: # Look up a local name arg = f.code_obj.co_varnames[arg_val] elif byteCode in dis.hasjrel: # Calculate a relative jump arg = f.last_instruction + arg_val else: arg = arg_val argument = [arg] else: argument = []