示例#1
0
    def test_list_of_list_constant(self):
        input = [[1, 2], [3, 4, 5, 6], [7]]
        expected_output = (
            # list[2]
            Opcode.INITSSLOT + b'\x01' + Opcode.PUSH7  # 7
            + Opcode.PUSH1  # list length
            + Opcode.PACK
            # list[1]
            + Opcode.PUSH6  # 6
            + Opcode.PUSH5  # 5
            + Opcode.PUSH4  # 4
            + Opcode.PUSH3  # 3
            + Opcode.PUSH4  # list length
            + Opcode.PACK
            # list[0]
            + Opcode.PUSH2  # 2
            + Opcode.PUSH1  # 1
            + Opcode.PUSH2  # list length
            + Opcode.PACK + Opcode.PUSH3  # list length
            + Opcode.PACK + Opcode.DROP + Opcode.RET)

        analyser = Analyser(ast.parse(str(input)))
        analyser.symbol_table['x'] = Variable(Type.any)
        output = CodeGenerator.generate_code(analyser)

        self.assertEqual(expected_output, output)
示例#2
0
    def test_tuple_of_tuple_constant(self):
        input = ((1, 2), (3, 4, 5, 6), (7, ))
        expected_output = (
            Opcode.INITSSLOT + b'\x01'
            # tuple[2]
            + Opcode.PUSH7  # 7
            + Opcode.PUSH1  # tuple length
            + Opcode.PACK
            # tuple[1]
            + Opcode.PUSH6  # 6
            + Opcode.PUSH5  # 7
            + Opcode.PUSH4  # 4
            + Opcode.PUSH3  # 3
            + Opcode.PUSH4  # tuple length
            + Opcode.PACK
            # tuple[0]
            + Opcode.PUSH2  # 2
            + Opcode.PUSH1  # 1
            + Opcode.PUSH2  # tuple length
            + Opcode.PACK + Opcode.PUSH3  # tuple length
            + Opcode.PACK + Opcode.DROP + Opcode.RET)

        analyser = Analyser(ast.parse(str(input)))
        analyser.symbol_table['x'] = Variable(Type.any)
        output = CodeGenerator.generate_code(analyser)

        self.assertEqual(expected_output, output)
示例#3
0
    def _compile(self) -> bytes:
        """
        Compile the analysed Python file.

        :return: the compiled file as a bytecode.
        :raise NotLoadedException: raised if none file were analysed
        """
        if not self._analyser.is_analysed:
            raise NotLoadedException
        return CodeGenerator.generate_code(self._analyser)
示例#4
0
    def test_integer_list_constant(self):
        input = [1, 2, 3]
        expected_output = (
            Opcode.INITSSLOT + b'\x01' + Opcode.PUSH3  # 3
            + Opcode.PUSH2  # 2
            + Opcode.PUSH1  # 1
            + Opcode.PUSH3  # list length
            + Opcode.PACK + Opcode.DROP + Opcode.RET)

        analyser = Analyser(ast.parse(str(input)))
        analyser.symbol_table['x'] = Variable(Type.any)
        output = CodeGenerator.generate_code(analyser)

        self.assertEqual(expected_output, output)
示例#5
0
    def test_any_list_constant(self):
        input = [1, '2', False]
        byte_input1 = String(input[1]).to_bytes()

        expected_output = (
            Opcode.INITSSLOT + b'\x01' + Opcode.PUSH0  # False
            + Opcode.PUSHDATA1  # '2'
            + Integer(len(byte_input1)).to_byte_array() + byte_input1 +
            Opcode.PUSH1  # 1
            + Opcode.PUSH3  # list length
            + Opcode.PACK + Opcode.DROP + Opcode.RET)

        analyser = Analyser(ast.parse(str(input)))
        analyser.symbol_table['x'] = Variable(Type.any)
        output = CodeGenerator.generate_code(analyser)

        self.assertEqual(expected_output, output)
示例#6
0
    def _compile(self) -> bytes:
        """
        Compile the analysed Python file.

        :return: the compiled file as a bytecode.
        :raise NotLoadedException: raised if none file were analysed
        """
        if not self._analyser.is_analysed:
            raise NotLoadedException
        analyser = self._analyser.copy()
        result = CodeGenerator.generate_code(analyser)
        if len(result) == 0:
            raise NotLoadedException(empty_script=True)

        if constants.INITIALIZE_METHOD_ID in analyser.symbol_table:
            self._analyser.symbol_table[constants.INITIALIZE_METHOD_ID] = analyser.symbol_table[constants.INITIALIZE_METHOD_ID]
        if constants.DEPLOY_METHOD_ID in analyser.symbol_table:
            self._analyser.symbol_table[constants.DEPLOY_METHOD_ID] = analyser.symbol_table[constants.DEPLOY_METHOD_ID]

        return result
示例#7
0
    def test_string_tuple_constant(self):
        input = ('1', '2', '3')
        byte_input0 = String(input[0]).to_bytes()
        byte_input1 = String(input[1]).to_bytes()
        byte_input2 = String(input[2]).to_bytes()

        expected_output = (
            Opcode.INITSSLOT + b'\x01' + Opcode.PUSHDATA1  # '3'
            + Integer(len(byte_input2)).to_byte_array() + byte_input2 +
            Opcode.PUSHDATA1  # '2'
            + Integer(len(byte_input1)).to_byte_array() + byte_input1 +
            Opcode.PUSHDATA1  # '1'
            + Integer(len(byte_input0)).to_byte_array() + byte_input0 +
            Opcode.PUSH3  # tuple length
            + Opcode.PACK + Opcode.DROP + Opcode.RET)

        analyser = Analyser(ast.parse(str(input)))
        analyser.symbol_table['x'] = Variable(Type.any)
        output = CodeGenerator.generate_code(analyser)

        self.assertEqual(expected_output, output)