Esempio n. 1
0
 def test_arithmetic(self):
     """Test that arithmetic operations result in integers."""
     for test in [
         TypeTest(sir.BinOpCode(name = 'OP_ADD', left = sir.Int(5), right = sir.Int(6)), SymbolType.Integer),
         TypeTest(sir.BinOpCode(name = 'OP_ADD', left = sir.Bytes('05'), right = sir.Bytes('06')), SymbolType.Integer),
     ]:
         self._test(test)
Esempio n. 2
0
 def test_function(self):
     for node, expected in [
         (sir.Function(name='foo', args=[sir.Symbol('a')],
                 body=[sir.BinOpCode(name='OP_ADD', left=sir.Symbol('a'), right=sir.Int(2))]), 'func foo(a) {a + 2;}'),
         (sir.Function(name='foo', args=[sir.Symbol('a')],
                 body=[sir.BinOpCode(name='OP_ADD', left=sir.Symbol('a'), right=sir.Int(2)), sir.Int(5)]), 'func foo(a) {a + 2; 5;}'),
         (sir.Function(name='foo', args=[sir.Symbol('a'), sir.Symbol('b')],
                 body=[sir.BinOpCode(name='OP_ADD', left=sir.Symbol('a'), right=sir.Symbol('b')), sir.Int(5)]), 'func foo(a, b) {a + b; 5;}'),
     ]:
         self.assertEqual(expected, SInstructions.format_op(node))
Esempio n. 3
0
    def visit_Compare(self, node):
        node.left = self.visit(node.left)
        node.comparators[0] = self.visit(node.comparators[0])

        # Special case for NotEq (!=).
        if node.ops[0].__class__.__name__ == 'NotEq':
            binop = types.BinOpCode(name='OP_EQUAL',
                                    left=node.left,
                                    right=node.comparators[0])
            return types.UnaryOpCode(name='OP_NOT', operand=binop)

        # Assume one op and one comparator.
        return types.BinOpCode(name=self.get_op_name(node.ops[0]),
                               left=node.left,
                               right=node.comparators[0])
Esempio n. 4
0
    def visit_op_function_call(self, node):
        """Transform a function call into its corresponding OpCode."""
        op_func = get_op_func(node.func.id)
        if not op_func:
            raise ParsingNameError('No function "%s" exists.' % node.func.id)
        # Ensure args have been visited.
        node.args = self.map_visit(node.args)
        # Ensure the number of args is correct.
        if op_func.nargs != -1 and len(node.args) != op_func.nargs:
            raise ParsingError('%s() requires %d arguments (got %d)' %
                               (op_func.name, op_func.nargs, len(node.args)))

        # Unary opcode.
        if op_func.nargs == 1:
            return types.UnaryOpCode(name=op_func.op_name,
                                     operand=node.args[0])
        # Binary opcode.
        elif op_func.nargs == 2:
            return types.BinOpCode(name=op_func.op_name,
                                   left=node.args[0],
                                   right=node.args[1])
        # Variable arguments.
        else:
            return types.VariableArgsOpCode(name=op_func.op_name,
                                            operands=list(node.args))
Esempio n. 5
0
    def visit_BoolOp(self, node):
        node.values = self.map_visit(node.values)

        name = self.get_op_name(node.op)
        # Create nested boolean ops.
        op = _reduce(
            lambda left, right: types.BinOpCode(
                name=name, left=left, right=right), node.values)
        return op
Esempio n. 6
0
    def test_binary_op(self):
        for name, expected in [
            ('OP_ADD', '05 + 06'),
            ('OP_SUB', '05 - 06'),
            ('OP_MUL', '05 * 06'),
            ('OP_DIV', '05 / 06'),
            ('OP_MOD', '05 % 06'),
            ('OP_LSHIFT', '05 << 06'),
            ('OP_RSHIFT', '05 >> 06'),

            ('OP_BOOLAND', '05 and 06'),
            ('OP_BOOLOR', '05 or 06'),

            ('OP_NUMEQUAL', '05 == 06'),
            ('OP_NUMNOTEQUAL', '05 != 06'),
            ('OP_LESSTHAN', '05 < 06'),
            ('OP_GREATERTHAN', '05 > 06'),
            ('OP_LESSTHANOREQUAL', '05 <= 06'),
            ('OP_GREATERTHANOREQUAL', '05 >= 06'),
        ]:
            op = sir.BinOpCode(name = name, left = sir.Bytes('05'), right = sir.Bytes('06'))
            self.assertEqual(expected, SInstructions.format_op(op))
Esempio n. 7
0
    def visit_BinOp(self, node):
        node.left, node.right = self.map_visit([node.left, node.right])

        return types.BinOpCode(name=self.get_op_name(node.op),
                               left=node.left,
                               right=node.right)