unroller.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:TerpreT 作者: 51alg 项目源码 文件源码
def subs(root, **kwargs):
    '''Substitute ast.Name nodes for numbers in root using the mapping in
    kwargs. Returns a new copy of root.

    '''
    root = copy.deepcopy(root)

    class Transformer(ast.NodeTransformer):
        def visit_FunctionDef(self, node):
            return node

        def visit_Name(self, node):
            if node.id in kwargs and not isinstance(node.ctx, ast.Store):
                replacement = kwargs[node.id]
                if isinstance(replacement, int):
                    return ast.copy_location(ast.Num(n=replacement), node)
                else:
                    return copy.copy(replacement)
            else:
                return node

    return Transformer().visit(root)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号