예제 #1
0
class Definition(Item):

    value = None
    attr = None

    operations = None
    block = None

    is_method_call = False
    fn_call = None

    def __init__(self, item_list):
        super(Definition, self).__init__(item_list)

        # this is a simple definition like a = 3
        if len(self.items) == 3:
            self.value = PyToken(self.items[1], 1, args=self.items[1][1])
            self.attr = PyToken(self.items[2], 1, args=self.items[2][1])
        # a method based definition linke ctx = GetContext
        elif len(self.items) == 4:
            self.is_method_call = True
            self.attr = PyToken(self.items[-1], 1, args=self.items[-1][1])
            self.value = PyToken(Opcode(pyop.LOAD_CONST), 1, args=7)
            self.convert_class_call_to_block()

        elif len(self.items) == 5:
            if self.items[-1][0] == pyop.RETURN_VALUE:
                self.items = self.items[:3]
                self.value = PyToken(self.items[1], 1, args=self.items[1][1])
                self.attr = PyToken(self.items[2], 1, args=self.items[2][1])

        else:

            #            self.is_method_call=True
            self.attr = PyToken(self.items[-1], 1, args=self.items[-1][1])


#            print("SOMETHING ELSE!")
#            pdb.set_trace()

    def convert_class_call_to_block(self):

        from boa.code.block import Block

        opitems = self.items[1:]
        current_line_num = 1
        blockitems = []
        for i, (op, arg) in enumerate(opitems):

            token = PyToken(op, current_line_num, i, arg)
            blockitems.append(token)

        self.block = Block(blockitems)
        self.block.preprocess_method_calls(None)
        self.fn_call = self.block.oplist[0]
예제 #2
0
    def convert_class_call_to_block(self):

        from boa.code.block import Block

        opitems = self.items[1:]
        current_line_num = 1
        blockitems = []
        for i, (op, arg) in enumerate(opitems):

            token = PyToken(op, current_line_num, i, arg)
            blockitems.append(token)

        self.block = Block(blockitems)
        self.block.preprocess_method_calls(None)
        self.fn_call = self.block.oplist[0]
예제 #3
0
    def read_initial_tokens(self):
        """
        Take the initial set of tokens from the ``byteplay3`` code object and turn them into blocks.
        """

        self.blocks = []

        self.local_methods = collections.OrderedDict()

        self.local_stores = collections.OrderedDict()

        current_line_no = None

        block_group = None

        self.tokenizer = VMTokenizer(self)

        current_label = None

        current_loop_token = None

        total_lines = 0
        for i, (op, arg) in enumerate(self.code):

            # print("[%s] %s  ->  %s " % (i, op, arg))

            if type(op) is SetLinenoType:

                current_line_no = arg
                total_lines += 1

                if self.start_line_no is None:
                    self.start_line_no = current_line_no

                if block_group is not None:

                    self.blocks.append(Block(block_group))

                block_group = []

            elif type(op) is Label:

                current_label = op

            else:
                instance_type = None
                if op in [pyop.STORE_FAST, pyop.STORE_NAME, pyop.STORE_GLOBAL
                          ] and arg not in self.local_stores.keys():

                    self._check_for_type(arg, total_lines)
                    length = len(self.local_stores)
                    self.local_stores[arg] = length

                token = PyToken(op, current_line_no, i, arg)

                if op == pyop.SETUP_LOOP:
                    token.args = None
                    current_loop_token = token

                if op == pyop.BREAK_LOOP and current_loop_token is not None:
                    token.args = current_loop_token.args
                    current_loop_token = None

                if current_label is not None:
                    token.jump_label = current_label
                    current_label = None

                block_group.append(token)

        if len(block_group):
            self.blocks.append(Block(block_group))