Skip to content

Commit 9ebdb14

Browse files
committed
Clarify lowering and inference helper names
1 parent a791a76 commit 9ebdb14

8 files changed

Lines changed: 93 additions & 81 deletions

R/r2f-arithmetic.R

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ r2f_handlers[["+"]] <- function(args, scope, ..., hoist = NULL) {
1818
hoist = hoist
1919
)
2020
.[left, right] <- promote_arith_pair(left, right, "+")
21-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
22-
Fortran(glue("({left} + {right})"), conform(left@value, right@value))
21+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
22+
Fortran(
23+
glue("({left} + {right})"),
24+
infer_result_variable(left@value, right@value)
25+
)
2326
}
2427
}
2528

@@ -38,24 +41,33 @@ r2f_handlers[["-"]] <- function(args, scope, ..., hoist = NULL) {
3841
hoist = hoist
3942
)
4043
.[left, right] <- promote_arith_pair(left, right, "-")
41-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
42-
Fortran(glue("({left} - {right})"), conform(left@value, right@value))
44+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
45+
Fortran(
46+
glue("({left} - {right})"),
47+
infer_result_variable(left@value, right@value)
48+
)
4349
}
4450
}
4551

4652
r2f_handlers[["*"]] <- function(args, scope = NULL, ..., hoist = NULL) {
4753
.[left, right] <- lower_elementwise_operands(args, scope, ..., hoist = hoist)
4854
.[left, right] <- promote_arith_pair(left, right, "*")
49-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
50-
Fortran(glue("({left} * {right})"), conform(left@value, right@value))
55+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
56+
Fortran(
57+
glue("({left} * {right})"),
58+
infer_result_variable(left@value, right@value)
59+
)
5160
}
5261

5362
r2f_handlers[["/"]] <- function(args, scope = NULL, ..., hoist = NULL) {
5463
.[left, right] <- lower_elementwise_operands(args, scope, ..., hoist = hoist)
5564
left <- maybe_cast_double(left)
5665
right <- maybe_cast_double(right)
57-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
58-
Fortran(glue("({left} / {right})"), conform(left@value, right@value))
66+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
67+
Fortran(
68+
glue("({left} / {right})"),
69+
infer_result_variable(left@value, right@value)
70+
)
5971
}
6072

6173
r2f_handlers[["^"]] <- function(args, scope, ..., hoist = NULL) {
@@ -68,15 +80,15 @@ r2f_handlers[["^"]] <- function(args, scope, ..., hoist = NULL) {
6880
if (identical(right@value@mode, "logical")) {
6981
right <- cast_to_mode(right, "integer", "^")
7082
}
71-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
83+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
7284
mode <- reduce_promoted_mode(left, right)
7385
if (!identical(mode, "complex")) {
7486
mode <- "double"
7587
}
7688
# Parenthesizing the exponent avoids non-standard `** -1_c_int`.
7789
Fortran(
7890
glue("({left} ** ({right}))"),
79-
conform(left@value, right@value, mode = mode)
91+
infer_result_variable(left@value, right@value, mode = mode)
8092
)
8193
}
8294

@@ -104,17 +116,17 @@ r2f_handlers[["%%"]] <- function(args, scope, ..., hoist = NULL) {
104116
}
105117
left <- cast_to_mode(left, mode, "%%")
106118
right <- cast_to_mode(right, mode, "%%")
107-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
108-
out_val <- conform(left@value, right@value)
119+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
120+
out_val <- infer_result_variable(left@value, right@value)
109121
# MODULO gives result with sign(right) - matches R %% behaviour
110122
Fortran(glue("modulo({left}, {right})"), out_val)
111123
}
112124

113125
r2f_handlers[["%/%"]] <- function(args, scope, ..., hoist = NULL) {
114126
.[left, right] <- lower_elementwise_operands(args, scope, ..., hoist = hoist)
115127
.[left, right] <- promote_arith_pair(left, right, "%/%")
116-
.[left, right] <- maybe_reshape_vector_matrix(left, right, hoist, scope)
117-
out_val <- conform(left@value, right@value)
128+
.[left, right] <- conform_elementwise_operands(left, right, hoist, scope)
129+
out_val <- infer_result_variable(left@value, right@value)
118130

119131
expr <- switch(
120132
out_val@mode,

R/r2f-constructors.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ is_fill_constructor_call <- function(e) {
2222
# `a:b` sequences, and symbols bound to a known literal vector; anything
2323
# else falls through to r2dims().
2424
# Used by: array()
25-
array_dim_to_dims <- function(dim_arg, scope) {
25+
parse_array_dims <- function(dim_arg, scope) {
2626
if (
2727
is.atomic(dim_arg) &&
2828
typeof(dim_arg) %in% c("integer", "double")
@@ -80,7 +80,7 @@ array_dim_to_dims <- function(dim_arg, scope) {
8080
(is.language(var@r) || is.atomic(var@r)) &&
8181
!identical(var@r, dim_arg)
8282
) {
83-
return(array_dim_to_dims(var@r, scope))
83+
return(parse_array_dims(var@r, scope))
8484
}
8585
}
8686

@@ -90,7 +90,7 @@ array_dim_to_dims <- function(dim_arg, scope) {
9090
# Product of a dims list when every dim is a known whole number, NA_real_
9191
# otherwise (in double to dodge integer overflow on large dims).
9292
# Used by: array()
93-
known_dims_prod <- function(dims) {
93+
known_dims_product <- function(dims) {
9494
if (is.null(dims) || !length(dims)) {
9595
return(1)
9696
}
@@ -389,7 +389,7 @@ r2f_handlers[["array"]] <- function(args, scope = NULL, ..., hoist = NULL) {
389389
}
390390

391391
out <- r2f(args$data, scope, ..., hoist = hoist)
392-
target_dims <- array_dim_to_dims(args$dim, scope)
392+
target_dims <- parse_array_dims(args$dim, scope)
393393
if (!length(target_dims)) {
394394
stop("array(dim=) must not be empty", call. = FALSE)
395395
}
@@ -447,8 +447,8 @@ r2f_handlers[["array"]] <- function(args, scope = NULL, ..., hoist = NULL) {
447447
i <- scope_unique_var(scope, "integer")
448448
glue("[({out}, {i}=1, int({n_expr}))]")
449449
} else {
450-
n_target <- known_dims_prod(target_dims)
451-
n_source <- known_dims_prod(out@value@dims)
450+
n_target <- known_dims_product(target_dims)
451+
n_source <- known_dims_product(out@value@dims)
452452
if (!is.na(n_target) && !is.na(n_source) && n_target > n_source) {
453453
stop(
454454
"array() reshape does not support recycling: prod(dim)=",

R/r2f-logical.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ lower_comparison_operands <- function(args, scope, op, ..., hoist = NULL) {
4444
stop("invalid comparison with complex values", call. = FALSE)
4545
}
4646
.[left, right] <- promote_arith_pair(left, right, "comparison")
47-
maybe_reshape_vector_matrix(
47+
conform_elementwise_operands(
4848
left,
4949
right,
5050
hoist,
@@ -61,7 +61,7 @@ r2f_handlers[["<"]] <- function(args, scope, ..., hoist = NULL) {
6161
...,
6262
hoist = hoist
6363
)
64-
value <- conform(left@value, right@value)
64+
value <- infer_result_variable(left@value, right@value)
6565
value@mode <- "logical"
6666
Fortran(glue("({left} < {right})"), value)
6767
}
@@ -74,7 +74,7 @@ r2f_handlers[["<="]] <- function(args, scope, ..., hoist = NULL) {
7474
...,
7575
hoist = hoist
7676
)
77-
value <- conform(left@value, right@value)
77+
value <- infer_result_variable(left@value, right@value)
7878
value@mode <- "logical"
7979
Fortran(glue("({left} <= {right})"), value)
8080
}
@@ -87,7 +87,7 @@ r2f_handlers[[">"]] <- function(args, scope, ..., hoist = NULL) {
8787
...,
8888
hoist = hoist
8989
)
90-
value <- conform(left@value, right@value)
90+
value <- infer_result_variable(left@value, right@value)
9191
value@mode <- "logical"
9292
Fortran(glue("({left} > {right})"), value)
9393
}
@@ -100,7 +100,7 @@ r2f_handlers[[">="]] <- function(args, scope, ..., hoist = NULL) {
100100
...,
101101
hoist = hoist
102102
)
103-
value <- conform(left@value, right@value)
103+
value <- infer_result_variable(left@value, right@value)
104104
value@mode <- "logical"
105105
Fortran(glue("({left} >= {right})"), value)
106106
}
@@ -113,7 +113,7 @@ r2f_handlers[["=="]] <- function(args, scope, ..., hoist = NULL) {
113113
...,
114114
hoist = hoist
115115
)
116-
value <- conform(left@value, right@value)
116+
value <- infer_result_variable(left@value, right@value)
117117
value@mode <- "logical"
118118
Fortran(glue("({left} == {right})"), value)
119119
}
@@ -126,7 +126,7 @@ r2f_handlers[["!="]] <- function(args, scope, ..., hoist = NULL) {
126126
...,
127127
hoist = hoist
128128
)
129-
value <- conform(left@value, right@value)
129+
value <- infer_result_variable(left@value, right@value)
130130
value@mode <- "logical"
131131
Fortran(glue("({left} /= {right})"), value)
132132
}
@@ -140,7 +140,7 @@ lower_logical_operands <- function(args, scope, op, ..., hoist = NULL) {
140140
}
141141
left <- booleanize_logical_as_int(left)
142142
right <- booleanize_logical_as_int(right)
143-
.[left, right] <- maybe_reshape_vector_matrix(
143+
.[left, right] <- conform_elementwise_operands(
144144
left,
145145
right,
146146
hoist,
@@ -158,7 +158,7 @@ r2f_handlers[["&"]] <- function(args, scope, ..., hoist = NULL) {
158158
...,
159159
hoist = hoist
160160
)
161-
value <- conform(left@value, right@value)
161+
value <- infer_result_variable(left@value, right@value)
162162
value@mode <- "logical"
163163
Fortran(glue("{left} .and. {right}"), value)
164164
}
@@ -171,7 +171,7 @@ r2f_handlers[["|"]] <- function(args, scope, ..., hoist = NULL) {
171171
...,
172172
hoist = hoist
173173
)
174-
value <- conform(left@value, right@value)
174+
value <- infer_result_variable(left@value, right@value)
175175
value@mode <- "logical"
176176
Fortran(glue("{left} .or. {right}"), value)
177177
}

0 commit comments

Comments
 (0)