flowr-analysis / flowr-r-adapter

Using flowR directly with R!
GNU General Public License v3.0
3 stars 0 forks source link

Allow for early termination of the visitor #18

Closed LukasPietzschmann closed 3 months ago

LukasPietzschmann commented 3 months ago

When visiting nodes in the AST, it could be handy if the visitor would allow for an early exit. This could work as follows:

visit_nodes(ast, function(node) {
  if(some check) {
    # We got what we want, so we do not have to visit more nodes
    return(FALSE)
  }
  # return nothing (or TRUE) to visit more nodes
})

If you (@Ellpeck and @EagleoutIce) are fine with this small addition, I already have the code ready :)

EagleoutIce commented 3 months ago

This mirrors flowR's behavior of allowing for an early exist, so please go ahead and open the PR :)

LukasPietzschmann commented 3 months ago

Maybe one small reiteration: I'm unsure of the right behavior when calling visit_nodes with a list of nodes. Should we instantly stop when the callback returns FALSE for a single node, or should we continue with the next node in the list?

EagleoutIce commented 3 months ago

I think the "intuitive" thing would be to stop early. We define a sequential traversal order through the vector of nodes passed. The R-Thing to do would be probably to return a vector greedly containing TRUE/FALSE for each node individually (i.e., vectorizing over the visitation order). But i think an early exit works perfectly fine.