|
1 | 1 | # r2f-conditionals.R |
2 | 2 | # Handlers for vectorized conditionals: ifelse |
3 | 3 |
|
| 4 | +# --- Local Helpers --- |
| 5 | + |
| 6 | +ifelse_branch_shape_msg <- paste0( |
| 7 | + "ifelse() `yes` and `no` must be scalars or match the shape of `test`; ", |
| 8 | + "R-style recycling is not supported" |
| 9 | +) |
| 10 | + |
| 11 | +# Three-valued conformability verdict for one axis of an ifelse() branch |
| 12 | +# against `test`: ok+known (no guard), not-ok+known (compile error), or |
| 13 | +# unknown (runtime guard). NA dims are always unknown: two unknown lengths |
| 14 | +# are not the same quantity. |
| 15 | +ifelse_axis_verdict <- function(test_dim, branch_dim) { |
| 16 | + if (is_wholenumber(test_dim) && is_wholenumber(branch_dim)) { |
| 17 | + return(list( |
| 18 | + ok = identical(as.integer(test_dim), as.integer(branch_dim)), |
| 19 | + unknown = FALSE |
| 20 | + )) |
| 21 | + } |
| 22 | + if (!is_scalar_na(test_dim) && !is_scalar_na(branch_dim)) { |
| 23 | + test_norm <- fortranize_expr_symbols(test_dim) |
| 24 | + branch_norm <- fortranize_expr_symbols(branch_dim) |
| 25 | + if (identical(test_norm, branch_norm)) { |
| 26 | + return(list(ok = TRUE, unknown = FALSE)) |
| 27 | + } |
| 28 | + } |
| 29 | + list(ok = TRUE, unknown = TRUE) |
| 30 | +} |
| 31 | + |
| 32 | +# Enforce the shape contract for one ifelse() branch: scalars broadcast |
| 33 | +# natively; a non-scalar branch must match `test`'s shape, because |
| 34 | +# merge() requires conformable arguments and a runtime mismatch would |
| 35 | +# read past the shorter branch. Statically unequal dims are a compile |
| 36 | +# error; symbolic dims get a statement-level runtime size guard, emitted into |
| 37 | +# `hoist` -- always a live hoist context, since r2f() substitutes a fresh one |
| 38 | +# before dispatching to any handler. |
| 39 | +check_ifelse_branch_shape <- function(branch, mask, hoist, scope) { |
| 40 | + if (passes_as_scalar(branch@value)) { |
| 41 | + return(invisible()) |
| 42 | + } |
| 43 | + if (branch@value@rank != mask@value@rank) { |
| 44 | + stop(ifelse_branch_shape_msg, call. = FALSE) |
| 45 | + } |
| 46 | + unknown_axes <- integer() |
| 47 | + for (axis in seq_len(mask@value@rank)) { |
| 48 | + verdict <- ifelse_axis_verdict( |
| 49 | + dim_or_one(mask, axis), |
| 50 | + dim_or_one(branch, axis) |
| 51 | + ) |
| 52 | + if (!verdict$ok) { |
| 53 | + stop(ifelse_branch_shape_msg, call. = FALSE) |
| 54 | + } |
| 55 | + if (verdict$unknown) { |
| 56 | + unknown_axes <- c(unknown_axes, axis) |
| 57 | + } |
| 58 | + } |
| 59 | + if (!length(unknown_axes)) { |
| 60 | + return(invisible()) |
| 61 | + } |
| 62 | + # size() is an inquiry, so applying it to operand expression text does |
| 63 | + # not evaluate the operands. |
| 64 | + condition <- str_flatten( |
| 65 | + map_chr( |
| 66 | + unknown_axes, |
| 67 | + function(axis) glue("size({branch}, {axis}) /= size({mask}, {axis})") |
| 68 | + ), |
| 69 | + " .or. " |
| 70 | + ) |
| 71 | + emit_quickr_error_if(condition, ifelse_branch_shape_msg, hoist, scope) |
| 72 | + invisible() |
| 73 | +} |
| 74 | + |
4 | 75 | # --- Handlers --- |
5 | 76 |
|
6 | | -r2f_handlers[["ifelse"]] <- function(args, scope, ...) { |
7 | | - .[mask, tsource, fsource] <- lapply(args, r2f, scope, ...) |
| 77 | +r2f_handlers[["ifelse"]] <- function(args, scope, ..., hoist = NULL) { |
| 78 | + .[mask, tsource, fsource] <- lapply(args, r2f, scope, ..., hoist = hoist) |
| 79 | + |
| 80 | + # R: the result is shaped like `test` (branches only contribute values). |
| 81 | + # A scalar test with array branches is not representable with merge(). |
| 82 | + if ( |
| 83 | + passes_as_scalar(mask@value) && |
| 84 | + !(passes_as_scalar(tsource@value) && passes_as_scalar(fsource@value)) |
| 85 | + ) { |
| 86 | + stop( |
| 87 | + "ifelse() result takes the shape of `test`; ", |
| 88 | + "array-valued yes/no with scalar test is not supported", |
| 89 | + call. = FALSE |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + # Checked before casts so guards splice the bare operand text. |
| 94 | + check_ifelse_branch_shape(tsource, mask, hoist, scope) |
| 95 | + check_ifelse_branch_shape(fsource, mask, hoist, scope) |
| 96 | + |
8 | 97 | mask <- booleanize_logical_as_int(mask) |
9 | | - # (tsource, fsource, mask) |
10 | | - mode <- tsource@value@mode |
11 | | - dims <- conform(mask@value, tsource@value, fsource@value)@dims |
12 | | - Fortran(glue("merge({tsource}, {fsource}, {mask})"), Variable(mode, dims)) |
| 98 | + |
| 99 | + # merge() requires same-typed branches; promote both to their common mode. |
| 100 | + promoted <- promote_operands(list(tsource, fsource), context = "ifelse()") |
| 101 | + .[tsource, fsource] <- promoted$args |
| 102 | + mode <- promoted$mode |
| 103 | + |
| 104 | + Fortran( |
| 105 | + glue("merge({tsource}, {fsource}, {mask})"), |
| 106 | + Variable(mode, mask@value@dims) |
| 107 | + ) |
13 | 108 | } |
0 commit comments