Performs one common subexpression elimination pass. Carefully examine the results after running this function!

opt_common_subexpr(texts, n_values = 2, in_fun_call = FALSE)

Arguments

texts

A list of character vectors with the code to optimize.

n_values

A numeric indicating the minimum number of values to consider a subexpression.

in_fun_call

A logical indicating whether it should propagate in function calls. Note: this could change the semantics of the program.

Examples

code <- paste( "a <- b * c + g", "d = b * c * e", sep = "\n" ) cat(opt_common_subexpr(list(code))$codes[[1]])
#> cs_1 <- b * c #> a <- cs_1 + g #> d = cs_1 * e
heron_formula <- paste( "area <- (a/2 + b/2 +c/2) * (a/2 + b/2 + c/2 - a) * (a/2 + b/2 + c/2 - b) *", " (a/2 + b/2 + c/2 - c)", "area <- sqrt(area)", sep = '\n' ) cat(opt_common_subexpr(list(heron_formula))$codes[[1]])
#> cs_1 <- a/2 + b/2 +c/2 #> area <- (cs_1) * (cs_1 - a) * (cs_1 - b) * #> (cs_1 - c) #> area <- sqrt(area)