Пример #1
0
    def generate_instructions(self, instr_list=None, flags=None):
        """
        Generate code for a ``|`` operator if ``subtype == 'alt'``::

                split L1, L2
            L1: codes for e1
                jmp L3
            L2: codes for e2
            L3:

        If ``subtype != 'alt'``, then this node is reduced away during parsing.
        """
        instr_list = instr_list or []
        flags = flags or {}
        if self.subtype == 'alt':
            back_jump = len(instr_list)+1

            first_split = Instruction('split', back_jump, 'TEMP')
            instr_list.append(first_split)
            instr_list = self.children[0].generate_instructions(instr_list, flags)

            first_jump = Instruction('jmp', 'TEMP')
            instr_list.append(first_jump)
            first_split.arg2 = len(instr_list)

            instr_list = self.children[1].generate_instructions(instr_list, flags)
            first_jump.arg1 = len(instr_list)
            return instr_list
        return super(RegexNode, self).generate_instructions(instr_list, flags)
Пример #2
0
    def generate_instructions(self, instr_list=None, flags=None):
        """
        Generate code for a ``|`` operator if ``subtype == 'alt'``::

                split L1, L2
            L1: codes for e1
                jmp L3
            L2: codes for e2
            L3:

        If ``subtype != 'alt'``, then this node is reduced away during parsing.
        """
        instr_list = instr_list or []
        flags = flags or {}
        if self.subtype == 'alt':
            back_jump = len(instr_list) + 1

            first_split = Instruction('split', back_jump, 'TEMP')
            instr_list.append(first_split)
            instr_list = self.children[0].generate_instructions(
                instr_list, flags)

            first_jump = Instruction('jmp', 'TEMP')
            instr_list.append(first_jump)
            first_split.arg2 = len(instr_list)

            instr_list = self.children[1].generate_instructions(
                instr_list, flags)
            first_jump.arg1 = len(instr_list)
            return instr_list
        return super(RegexNode, self).generate_instructions(instr_list, flags)
Пример #3
0
    def generate_instructions(self, instr_list=None, flags=None):
        """
        Generate code for a +, *, or ? operator if ``subtype == 'dup'``.

        e+::

            L1: codes for e
                split L1, L3
            L3:

        e*::

            L1: split L2, L3
            L2: codes for e
                jmp L1
            L3:

        e?::

            split L1, L2
            L1: codes for e
            L2:

        """
        instr_list = instr_list or []
        flags = flags or {}
        if self.subtype == 'dup':
            lchild = self.children[0]
            if self.data == '+':
                back_jump = len(instr_list)
                instr_list = lchild.generate_instructions(instr_list, flags)
                forward_jump = len(instr_list) + 1
                instr_list.append(Instruction('split', back_jump,
                                              forward_jump))
                return instr_list
            if self.data == '*' or self.data == '?':
                back_jump = len(instr_list) + 1

                first_split = Instruction('split', back_jump, 'TEMP')
                instr_list.append(first_split)

                instr_list = lchild.generate_instructions(instr_list)
                if self.data == '*':
                    instr_list.append(Instruction('jmp', back_jump - 1))
                first_split.arg2 = len(instr_list)
                return instr_list
        else:
            return super(SimpleReNode,
                         self).generate_instructions(instr_list, flags)
Пример #4
0
    def generate_instructions(self, instr_list=None, flags=None):
        """
        Generate code for a +, *, or ? operator if ``subtype == 'dup'``.

        e+::

            L1: codes for e
                split L1, L3
            L3:

        e*::

            L1: split L2, L3
            L2: codes for e
                jmp L1
            L3:

        e?::

            split L1, L2
            L1: codes for e
            L2:

        """
        instr_list = instr_list or []
        flags = flags or {}
        if self.subtype == 'dup':
            lchild = self.children[0]
            if self.data == '+':
                back_jump = len(instr_list)
                instr_list = lchild.generate_instructions(instr_list, flags)
                forward_jump = len(instr_list) + 1
                instr_list.append(Instruction('split', back_jump, forward_jump))
                return instr_list
            if self.data == '*' or self.data == '?':
                back_jump = len(instr_list)+1

                first_split = Instruction('split', back_jump, 'TEMP')
                instr_list.append(first_split)

                instr_list = lchild.generate_instructions(instr_list)
                if self.data == '*':
                    instr_list.append(Instruction('jmp', back_jump-1))
                first_split.arg2 = len(instr_list)
                return instr_list
        else:
            return super(SimpleReNode, self).generate_instructions(instr_list, flags)