In Python, it is trivial to do this kind of AST walking for any valid python 
code that is provided as a string in a variable.
    
    
    import ast
    
    code_string = """
    def foo(x, y):
        z = x + y
        return f(z, g(z))
    """
    module_node = ast.parse(code_string)
    
    class FunctionVisitor(ast.NodeVisitor):
        def visit_FunctionDef(self, node):
            print(f"Found function declaration: {node.name}")
            for stmt in node.body:
                self.visit(stmt)
        
        def visit_Assign(self, node):
            print(f"Found assignment to: {node.targets[0].id}")
            self.visit(node.value)
        
        def visit_Call(self, node):
            print(f"Found function call: {node.func.id}")
            for arg in node.args:
                self.visit(arg)
        
        def visit_BinOp(self, node):
            print(f"Found binary operation: {node.op.__class__.__name__}")
            self.visit(node.left)
            self.visit(node.right)
    
    visitor = FunctionVisitor()
    visitor.visit(module_node)
    
    
    Run

I saw the macros section and found some relevant functionality such as 
parseExpr and parseStmt but I am not sure what to use/combine to achieve the 
same thing. Any help would be appreciated.

Reply via email to