Пример #1
0
    def visit_for_stmt(self, node):
        # initial: stmt
        name, _, expr = node.children[2:5]
        initial = AST.Assign(
            AST.Name(name.value, expr_context.Store, name.context),
            self.visit(expr),
            name.context)

        condition = self.visit(node.children[6])

        # step: stmt
        target, _, name, op, num = node.children[8:13]
        name_ = AST.Name(name.value, expr_context.Load, name.context)
        op = AST.string2operator[op.value]
        # this is a NUMBER
        assert num.type == sym.NUMBER
        num = AST.Num(int(num.value), num.context)

        next = AST.BinOp(name_, op, num, name.context)
        step = AST.Assign(
            AST.Name(target.value, expr_context.Store, target.context),
            next, target.context)

        stmt = list(self.visit(node.children[-1]))
        return AST.For(initial, condition, step, stmt, node.context)
Пример #2
0
    def visit_binop(self, node, context):
        # this function must call visit_expr() instead of visit()!
        result = self.visit_expr(node.first_child, context)
        nops = (len(node.children) - 1) // 2

        for i in range(nops):
            next_oper = node.children[i * 2 + 1]
            op = AST.string2operator[next_oper.value]
            tmp = self.visit_expr(node.children[i * 2 + 2], context)
            tmp_result = AST.BinOp(result, op, tmp, next_oper.context)
            result = tmp_result
        return result