From 942d7d942b2644faced06c4b5e66b47435a3242b Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Wed, 30 Oct 2024 14:10:31 +0100 Subject: [PATCH] Fix srcref handling with new evaluate Closes #49 --- R/parser.R | 2 +- tests/testthat/_snaps/parser-block.md | 110 ++++++++++++++++++++++++++ tests/testthat/test-parser-block.R | 4 - tests/testthat/test-parser-if.R | 1 - 4 files changed, 111 insertions(+), 6 deletions(-) diff --git a/R/parser.R b/R/parser.R index db1dcaa..9f55ac6 100644 --- a/R/parser.R +++ b/R/parser.R @@ -324,7 +324,7 @@ block_states <- function(block, counter, continue, last, return, info) { curr_refs <<- nested_refs } else { node_poke_cdr(prev_node, nested_node) - node_poke_cdr(prev_refs, nested_refs) + prev_refs %&&% node_poke_cdr(prev_refs, nested_refs) } node <<- nested_node diff --git a/tests/testthat/_snaps/parser-block.md b/tests/testthat/_snaps/parser-block.md index 494f49a..da24478 100644 --- a/tests/testthat/_snaps/parser-block.md +++ b/tests/testthat/_snaps/parser-block.md @@ -297,6 +297,51 @@ invisible(exhausted()) } +# `{` blocks - nested + + Code + generator_body(function() { + "before1" + "before2" + { + "before-inner" + yield(1L) + "after-inner" + } + "after1" + "after2" + }) + Output + { + if (exhausted) { + return(invisible(exhausted())) + } + repeat switch(state[[1L]], `1` = { + user({ + "before1" + "before2" + "before-inner" + 1L + }) + state[[1L]] <- 2L + suspend() + return(last_value()) + }, `2` = { + .last_value <- if (missing(arg)) NULL else arg + state[[1L]] <- 3L + }, `3` = { + user({ + "after-inner" + "after1" + "after2" + }) + exhausted <- TRUE + return(last_value()) + }) + exhausted <- TRUE + invisible(exhausted()) + } + # `{` blocks - nested and no past before pause Code @@ -377,6 +422,71 @@ invisible(exhausted()) } +# `{` blocks - complex nesting + + Code + generator_body(function() { + "before" + { + "before-inner" + yield(1L) + { + yield(2L) + yield(3L) + } + "after-inner" + } + "after" + }) + Output + { + if (exhausted) { + return(invisible(exhausted())) + } + repeat switch(state[[1L]], `1` = { + user({ + "before" + "before-inner" + 1L + }) + state[[1L]] <- 2L + suspend() + return(last_value()) + }, `2` = { + .last_value <- if (missing(arg)) NULL else arg + state[[1L]] <- 3L + }, `3` = { + user({ + 2L + }) + state[[1L]] <- 4L + suspend() + return(last_value()) + }, `4` = { + .last_value <- if (missing(arg)) NULL else arg + state[[1L]] <- 5L + }, `5` = { + user({ + 3L + }) + state[[1L]] <- 6L + suspend() + return(last_value()) + }, `6` = { + .last_value <- if (missing(arg)) NULL else arg + state[[1L]] <- 7L + }, `7` = { + user({ + "after-inner" + "after" + }) + exhausted <- TRUE + return(last_value()) + }) + exhausted <- TRUE + invisible(exhausted()) + } + # `{` blocks - simple nesting with various continuation states Code diff --git a/tests/testthat/test-parser-block.R b/tests/testthat/test-parser-block.R index 79272ce..86913e0 100644 --- a/tests/testthat/test-parser-block.R +++ b/tests/testthat/test-parser-block.R @@ -60,8 +60,6 @@ test_that("`{` blocks - no return value", { }) test_that("`{` blocks - nested", { - skip() - expect_snapshot0(generator_body(function() { "before1" "before2" @@ -99,8 +97,6 @@ test_that("`{` blocks - nested and goto after pause", { }) test_that("`{` blocks - complex nesting", { - skip() - expect_snapshot0(generator_body(function() { "before" { diff --git a/tests/testthat/test-parser-if.R b/tests/testthat/test-parser-if.R index 61a374d..c774808 100644 --- a/tests/testthat/test-parser-if.R +++ b/tests/testthat/test-parser-if.R @@ -27,7 +27,6 @@ test_that("`else` blocks - one pause", { }) test_that("`if` blocks - inner block", { - skip() expect_snapshot0(generator_body(function() { "before" if (TRUE) {