Skip to content

Commit 76b0dd8

Browse files
committed
create a wrapper that changes the code into a list of calls with dependency attributes
1 parent e4fdcd4 commit 76b0dd8

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

R/qenv-eval_code.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
7979
}
8080

8181
attr(current_code, "id") <- sample.int(.Machine$integer.max, size = 1)
82-
83-
pd <- utils::getParseData(current_call)
84-
pd <- normalize_pd(pd)
85-
call_pd <- extract_calls(pd)[[1]]
86-
87-
attr(current_code, "dependency") <- c(extract_side_effects(call_pd), extract_occurrence(call_pd))
82+
attr(current_code, "dependency") <- extract_dependency(current_call)
8883
object@code <- c(object@code, list(current_code))
8984
}
9085

R/utils-get_code_dependency.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,37 @@ split_code <- function(code) {
462462
# semicolon is treated by R parser as a separate call.
463463
gsub("^([[:space:]])*;(.+)$", "\\1\\2", new_code, perl = TRUE)
464464
}
465+
466+
467+
#' Reshape code to the list
468+
#'
469+
#' List will be divided by the calls. Each element of the list contains `id` and `dependency` attributes.
470+
#'
471+
#' @param code `character` with the code.
472+
#'
473+
#' @return list of `character`s of the length equal to the number of calls in `code`.
474+
#'
475+
#' @keywords internal
476+
#' @noRd
477+
code2list <- function(code) {
478+
checkmate::assert_character(code, null.ok = TRUE)
479+
if (length(code)) {
480+
lapply(split_code(code), function(current_code) {
481+
attr(current_code, "id") <- sample.int(.Machine$integer.max, 1)
482+
parsed_code <- parse(text = trimws(current_code), keep.source = TRUE)
483+
attr(current_code, "dependency") <- extract_dependency(parsed_code)
484+
current_code
485+
})
486+
} else {
487+
list(character(0))
488+
}
489+
}
490+
491+
#' @param parsed_code results of `parse(text = code, keep.source = TRUE` (parsed text)
492+
#' @keywords internal
493+
#' @noRd
494+
extract_dependency <- function(parsed_code) {
495+
pd <- normalize_pd(utils::getParseData(parsed_code))
496+
call_pd <- extract_calls(pd)[[1]]
497+
c(extract_side_effects(call_pd), extract_occurrence(call_pd))
498+
}

0 commit comments

Comments
 (0)