-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mrc-ide/mrc-5588
Translate old user calls to new parameter calls
- Loading branch information
Showing
10 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
parse_compat <- function(exprs, action, call) { | ||
## Things we can translate: | ||
## | ||
## user() -> parameter() | ||
## rbinom() -> Binomial() [etc] | ||
## dt -> drop | ||
## step -> time | ||
exprs <- lapply(exprs, parse_compat_fix_user, call) | ||
parse_compat_report(exprs, action, call) | ||
exprs | ||
} | ||
|
||
|
||
parse_compat_fix_user <- function(expr, call) { | ||
is_user_assignment <- | ||
rlang::is_call(expr$value, c("<-", "=")) && | ||
rlang::is_call(expr$value[[3]], "user") | ||
if (is_user_assignment) { | ||
res <- match_call( | ||
expr$value[[3]], | ||
function(default, integer, min, max, ...) NULL) | ||
if (!res$success) { | ||
odin_parse_error( | ||
"Failed to translate your 'user()' expression to use 'parameter()'", | ||
"E1016", expr, call = call) | ||
} | ||
for (arg in c("integer", "min", "max")) { | ||
if (!rlang::is_missing(res$value[[arg]])) { | ||
odin_parse_error( | ||
c("Can't yet translate 'user()' calls that use the '{arg}' argument", | ||
i = paste("We don't support this argument in parameter() yet,", | ||
"but once we do we will support translation")), | ||
"E0001", expr, call = call) | ||
} | ||
} | ||
|
||
original <- expr$value | ||
args <- list(as.name("parameter")) | ||
if (!rlang::is_missing(res$value$default)) { | ||
args <- c(args, list(res$value$default)) | ||
} | ||
expr$value[[3]] <- as.call(args) | ||
expr$compat <- list(type = "user", original = original) | ||
} | ||
expr | ||
} | ||
|
||
|
||
parse_compat_report <- function(exprs, action, call) { | ||
i <- !vlapply(exprs, function(x) is.null(x$compat)) | ||
if (action != "silent" && any(i)) { | ||
description <- c( | ||
user = "Replace calls to 'user()' with 'parameter()'") | ||
type <- vcapply(exprs[i], function(x) x$compat$type) | ||
|
||
detail <- NULL | ||
for (t in intersect(names(description), type)) { | ||
j <- i[type == t] | ||
## Getting line numbers here is really hard, so let's just not | ||
## try for now and do this on deparsed expressions. | ||
updated <- vcapply(exprs[j], function(x) deparse1(x$value)) | ||
original <- vcapply(exprs[j], function(x) deparse1(x$compat$original)) | ||
context_t <- set_names( | ||
c(rbind(updated, original, deparse.level = 0)), | ||
rep(c("x", "v"), length(updated))) | ||
detail <- c(detail, description[[t]], cli_nbsp(context_t)) | ||
} | ||
|
||
header <- "Found {sum(i)} compatibility issue{?s}" | ||
|
||
if (action == "error") { | ||
odin_parse_error(c(header, detail), "E1017", exprs[i], call) | ||
} else { | ||
cli::cli_warn(c(header, detail), call = call) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,8 @@ set_names <- function(x, nms) { | |
names(x) <- nms | ||
x | ||
} | ||
|
||
|
||
cli_nbsp <- function(x) { | ||
gsub(" ", "\u00a0", x) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
test_that("can translate user()", { | ||
d <- odin_parse({ | ||
a <- user(1) | ||
initial(x) <- 0 | ||
update(x) <- x + a | ||
}, compatibility = "silent") | ||
expect_equal(d$equations$a$src$value, | ||
quote(a <- parameter(1))) | ||
expect_equal(d$equations$a$src$compat, | ||
list(type = "user", original = quote(a <- user(1)))) | ||
}) | ||
|
||
|
||
test_that("can control severity of reporting", { | ||
code <- "a <- user(1)\ninitial(x) <- 0\nupdate(x) <- x + a" | ||
expect_silent(odin_parse(code, compatibility = "silent")) | ||
w <- expect_warning( | ||
odin_parse(code, compatibility = "warning"), | ||
"Found 1 compatibility issue") | ||
|
||
e <- expect_error( | ||
odin_parse(code, compatibility = "error"), | ||
"Found 1 compatibility issue") | ||
|
||
expect_true(startsWith(conditionMessage(e), conditionMessage(w))) | ||
}) | ||
|
||
|
||
test_that("can translate simple user calls", { | ||
expect_equal( | ||
parse_compat_fix_user(list(value = quote(a <- user()))), | ||
list(value = quote(a <- parameter()), | ||
compat = list(type = "user", original = quote(a <- user())))) | ||
expect_equal( | ||
parse_compat_fix_user(list(value = quote(a <- user(1)))), | ||
list(value = quote(a <- parameter(1)), | ||
compat = list(type = "user", original = quote(a <- user(1))))) | ||
expect_error( | ||
parse_compat_fix_user(list(value = quote(a <- user(integer = TRUE)))), | ||
"Can't yet translate 'user()' calls that use the 'integer' argument", | ||
fixed = TRUE, | ||
class = "odin_parse_error") | ||
expect_error( | ||
parse_compat_fix_user(list(value = quote(a <- user(min = 0)))), | ||
"Can't yet translate 'user()' calls that use the 'min' argument", | ||
fixed = TRUE, | ||
class = "odin_parse_error") | ||
expect_error( | ||
parse_compat_fix_user(list(value = quote(a <- user(max = 1)))), | ||
"Can't yet translate 'user()' calls that use the 'max' argument", | ||
fixed = TRUE, | ||
class = "odin_parse_error") | ||
}) | ||
|
||
|
||
test_that("handle errors that occur in translated code", { | ||
err <- expect_error( | ||
odin_parse({ | ||
a <- user(sqrt(b)) | ||
b <- 1 | ||
initial(x) <- 0 | ||
update(x) <- x + a | ||
}, compatibility = "silent",), | ||
"Invalid default argument to 'parameter()': sqrt(b)", | ||
fixed = TRUE) | ||
|
||
expect_match( | ||
conditionMessage(err), | ||
"The expression above has been translated") | ||
}) | ||
|
||
|
||
test_that("handle failure to pass a user call", { | ||
expect_error( | ||
odin_parse({ | ||
a <- user(min = 1, min = 1) | ||
initial(x) <- 0 | ||
update(x) <- x + a | ||
}), | ||
"Failed to translate your 'user()' expression to use 'parameter()'", | ||
fixed = TRUE) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters