def test_logical_binop(logical_binop, left, right): root = ast.BinaryOp(logical_binop, left, right) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == BuiltinTypes.BOOL
def parse_binop_rhs(self, left, min_precedence=0): while True: precedence = _get_precedence(self.curr_token) if precedence < min_precedence: return left op = self.curr_token self.advance() if op.kind == 'AS': right = self.parse_type() else: right = self.parse_unary_expr() next_precedence = _get_precedence(self.curr_token) if precedence < next_precedence: right = self.parse_binop_rhs(right, precedence + 1) if op.kind == 'AS': left = ast.Cast(left, right, loc=op.loc) elif op.kind in ASSIGNMENT_TOKENS: left = ast.Assignment(left, right, op=AUGMENT_ASSIGNMENT_OP[op.kind], loc=op.loc) else: left = ast.BinaryOp(TOKEN_TO_BINOP[op.kind], left, right, loc=op.loc)
def test_shift_binop(shift_binop, left, right, expected_ty): root = ast.BinaryOp(shift_binop, left, right) tp = TypePass() actual_ty = tp.visit(root) assert actual_ty == expected_ty
def visit_Assignment(self, node): ctx = self.ctx if node.op is not None: tmp = ast.BinaryOp(node.op, node.lvalue, node.rvalue, ty=node.ty, loc=node.loc) right = self.visit_BinaryOp(tmp) else: right = self.visit(node.rvalue) ptr = ctx.symbol_table.get(node.lvalue.name) result = ctx.builder.store(right, ptr, align=4) return result
def visit_Assignment(self, node): lvalue_ty = self.visit(node.lvalue) rvalue_ty = self.visit(node.rvalue) node.ty = lvalue_ty _validate_assignment(node, lvalue_ty, rvalue_ty) if node.op is not None: expr = ast.BinaryOp(node.op, node.lvalue, node.rvalue, loc=node.loc) self.visit(expr) if lvalue_ty == rvalue_ty: return lvalue_ty promote_to = _builtin_type_promotion(rvalue_ty, lvalue_ty) if not promote_to: msg = 'cannot implicitly cast %r to %r' % ( rvalue_ty.name, lvalue_ty.name) raise DumbTypeError(msg, loc=node.loc) node.rvalue = ast.Cast(node.rvalue, lvalue_ty, rvalue_ty) return lvalue_ty
def test_augmented_assignment(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.IntegerConstant(1), Operator.ADD) ]) tp = TypePass() tp.visit(root) @pytest.mark.parametrize('cond', [ ast.IntegerConstant(1), ast.FloatConstant(1.0), ast.BinaryOp(Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(0)), ]) def test_if_bad_cond(cond): root = ast.If(cond, ast.Block([])) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root) @pytest.mark.parametrize('cond', [ ast.BooleanConstant(True), ast.BinaryOp(Operator.LT, ast.IntegerConstant(0), ast.IntegerConstant(0)), ast.BinaryOp(Operator.LE, ast.IntegerConstant(0), ast.IntegerConstant(0)), ast.BinaryOp(Operator.GT, ast.IntegerConstant(0), ast.IntegerConstant(0)), ast.BinaryOp(Operator.GE, ast.IntegerConstant(0), ast.IntegerConstant(0)),
raise RuntimeError('Unexpected call.') def visit_Expression(self, node): raise RuntimeError('Unexpected call.') class StubDeclVisitor(DeclVisitor): def visit_Function(self, node): raise RuntimeError('Unexpected call.') def visit_TranslationUnit(self, node): raise RuntimeError('Unexpected call.') @pytest.mark.parametrize('node', [ ast.BinaryOp(None, None, None), ast.Assignment(None, None), ast.UnaryOp(None, None), ast.Cast(None, None), ast.IntegerConstant(None), ast.FloatConstant(None), ast.BooleanConstant(None), ast.StringConstant(None), ast.Identifier(None), ast.FuncCall(None, None) ]) def test_expr_visitor(node): visitor = StubExprVisitor() method_name = 'visit_' + type(node).__name__ with mock.patch.object(visitor, method_name): visitor.visit(node)
if isinstance(tree, list): assert len(tree) == len(expected) for _tree, _expected in zip(tree, expected): assert_tree(_tree, _expected) elif isinstance(tree, ast.Node): for attr_name, expected_value in vars(expected).items(): assert_tree(getattr(tree, attr_name), expected_value) else: assert tree == expected @pytest.mark.parametrize('with_dead_code,without_dead_code', [ ([], []), ([ ast.BinaryOp(ast.Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(1)), ast.Return(), ast.Break() ], [ ast.BinaryOp(ast.Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(1)), ast.Return() ]), ([ ast.BinaryOp(ast.Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(1)), ast.Break(),