| 1 |
|
| 2 |
expr <- function(expr, envir = parent.frame()) { |
| 3 |
renv_expr_impl(substitute(expr), envir) |
| 4 |
} |
| 5 |
|
| 6 |
renv_expr_impl <- function(expr, envir) { |
| 7 |
|
| 8 |
# repair parse trees |
| 9 |
expr <- renv_expr_repair(expr) |
| 10 |
|
| 11 |
# check for inject calls |
| 12 |
if (is.call(expr) && identical(expr[[1L]], as.symbol("!"))) { |
| 13 |
inner <- expr[[2L]] |
| 14 |
if (is.call(inner) && identical(inner[[1L]], as.symbol("!"))) { |
| 15 |
value <- eval(inner[[2L]], envir = envir) |
| 16 |
return(value) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
# recurse where possible |
| 21 |
if (is.recursive(expr)) { |
| 22 |
for (i in seq_along(expr)) { |
| 23 |
if (!identical(expr[[i]], quote(expr = ))) { |
| 24 |
if (!is.null(expr[[i]])) { |
| 25 |
expr[[i]] <- renv_expr_impl(expr[[i]], envir) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
expr |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
renv_expr_extract <- function(expr) { |
| 36 |
|
| 37 |
if (is.call(expr) && identical(expr[[1L]], as.symbol("!"))) { |
| 38 |
inner <- expr[[2L]] |
| 39 |
if (is.call(inner) && identical(inner[[1L]], as.symbol("!"))) { |
| 40 |
return(inner[[2L]]) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
# TODO: Doesn't properly handle precedence for multiple injections, |
| 47 |
# e.g. in '!!a + !!b + !!c'. |
| 48 |
renv_expr_repair <- function(expr) { |
| 49 |
|
| 50 |
lhs <- renv_expr_extract(expr) |
| 51 |
if (is.null(lhs)) |
| 52 |
return(expr) |
| 53 |
|
| 54 |
check <- is.call(lhs) && length(lhs) == 3L |
| 55 |
if (!check) |
| 56 |
return(expr) |
| 57 |
|
| 58 |
rhs <- renv_expr_extract(lhs[[3L]]) |
| 59 |
if (is.null(rhs)) |
| 60 |
return(expr) |
| 61 |
|
| 62 |
parts <- list( |
| 63 |
lhs[[1L]], |
| 64 |
call("!", call("!", lhs[[2L]])), |
| 65 |
call("!", call("!", rhs)) |
| 66 |
) |
| 67 |
|
| 68 |
as.call(parts) |
| 69 |
|
| 70 |
} |