Ejemplo n.º 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)
Ejemplo n.º 2
0
 def visit_stmt_trailer(self, node, name):
     first = node.first_child
     store = expr_context.Store
     if first.type == sym.arglist:
         args = self.visit(first)
         call = AST.Call(name.value, args, name.context)
         return AST.ExprStmt(call, name.context)
     elif first.value == '[':
         index = self.visit(node.children[1])  # Load
         value = self.visit(node.children[-1])  # Load
         subscript = AST.Subscript(name.value, index, store, node.context)
         return AST.Assign(subscript, value, name.context)
     else:
         assert first.value == '=', first
         value = self.visit(node.children[-1])  # Load
         target = AST.Name(name.value, store, name.context)
         return AST.Assign(target, value, name.context)
Ejemplo n.º 3
0
 def visit_atom(self, node, context):
     first = node.first_child
     if first.type == sym.NAME:
         if len(node.children) == 1:
             # single name
             return AST.Name(first.value, context, first.context)
         # name with trailer: visit_trailer
         trailer = node.children[1]
         return self.visit(trailer, first.value, context)
     if first.type == sym.NUMBER:
         return AST.Num(int(first.value), first.context)
     if first.type == sym.CHAR:
         return AST.Char(first.value, first.context)
     else:
         assert first.value == '('
         # parenthesis expr
         # visit_expr
         value = self.visit(node.children[1], context)
         return AST.ParenExpr(value, first.context)
Ejemplo n.º 4
0
 def visit_read_stmt(self, node):
     names = [AST.Name(c.value, expr_context.Store, node.context)
              for c in node.children[2:-1:2]]
     return AST.Read(names, node.context)