|
| 1 | +# r2f-arithmetic.R |
| 2 | +# Handlers for arithmetic operators: +, -, *, /, ^, %%, %/% |
| 3 | + |
| 4 | +# --- Handlers --- |
| 5 | + |
| 6 | +r2f_handlers[["+"]] <- function(args, scope, ...) { |
| 7 | + # Support both binary and unary plus |
| 8 | + if (length(args) == 1L) { |
| 9 | + x <- r2f(args[[1L]], scope, ...) |
| 10 | + Fortran(glue("(+{x})"), Variable(x@value@mode, x@value@dims)) |
| 11 | + } else { |
| 12 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 13 | + reshaped <- maybe_reshape_vector_matrix(left, right) |
| 14 | + left <- reshaped$left |
| 15 | + right <- reshaped$right |
| 16 | + Fortran(glue("({left} + {right})"), conform(left@value, right@value)) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +r2f_handlers[["-"]] <- function(args, scope, ...) { |
| 21 | + # Support both binary and unary minus |
| 22 | + if (length(args) == 1L) { |
| 23 | + x <- r2f(args[[1L]], scope, ...) |
| 24 | + Fortran(glue("(-{x})"), Variable(x@value@mode, x@value@dims)) |
| 25 | + } else { |
| 26 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 27 | + reshaped <- maybe_reshape_vector_matrix(left, right) |
| 28 | + left <- reshaped$left |
| 29 | + right <- reshaped$right |
| 30 | + Fortran(glue("({left} - {right})"), conform(left@value, right@value)) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +r2f_handlers[["*"]] <- function(args, scope = NULL, ...) { |
| 35 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 36 | + reshaped <- maybe_reshape_vector_matrix(left, right) |
| 37 | + left <- reshaped$left |
| 38 | + right <- reshaped$right |
| 39 | + Fortran(glue("({left} * {right})"), conform(left@value, right@value)) |
| 40 | +} |
| 41 | + |
| 42 | +r2f_handlers[["/"]] <- function(args, scope = NULL, ...) { |
| 43 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 44 | + left <- maybe_cast_double(left) |
| 45 | + right <- maybe_cast_double(right) |
| 46 | + reshaped <- maybe_reshape_vector_matrix(left, right) |
| 47 | + left <- reshaped$left |
| 48 | + right <- reshaped$right |
| 49 | + Fortran(glue("({left} / {right})"), conform(left@value, right@value)) |
| 50 | +} |
| 51 | + |
| 52 | +r2f_handlers[["^"]] <- function(args, scope, ...) { |
| 53 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 54 | + reshaped <- maybe_reshape_vector_matrix(left, right) |
| 55 | + left <- reshaped$left |
| 56 | + right <- reshaped$right |
| 57 | + Fortran(glue("({left} ** {right})"), conform(left@value, right@value)) |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +# ---- remainder (%%) and integer division (%/%) ---- |
| 62 | +# |
| 63 | +# R semantics: |
| 64 | +# x %% y == r where r has the sign of y (divisor) |
| 65 | +# x %/% y == q where q = floor(x / y) |
| 66 | +# and x == r + y * q (within rounding error) |
| 67 | +# |
| 68 | +# Fortran intrinsics: |
| 69 | +# - MODULO(a,p) : remainder with sign(p) |
| 70 | +# - FLOOR(x) : greatest integer <= x (real) |
| 71 | +# - AINT(x) : truncation toward 0 (real) |
| 72 | + |
| 73 | +r2f_handlers[["%%"]] <- function(args, scope, ...) { |
| 74 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 75 | + out_val <- conform(left@value, right@value) |
| 76 | + # MODULO gives result with sign(right) - matches R %% behaviour |
| 77 | + Fortran(glue("modulo({left}, {right})"), out_val) |
| 78 | +} |
| 79 | + |
| 80 | +r2f_handlers[["%/%"]] <- function(args, scope, ...) { |
| 81 | + .[left, right] <- lapply(args, r2f, scope, ...) |
| 82 | + out_val <- conform(left@value, right@value) |
| 83 | + |
| 84 | + expr <- switch( |
| 85 | + out_val@mode, |
| 86 | + integer = glue("int(floor(real({left}) / real({right})))"), |
| 87 | + double = glue("floor({left} / {right})"), |
| 88 | + stop("%/% only implemented for numeric types") |
| 89 | + ) |
| 90 | + |
| 91 | + Fortran(expr, out_val) |
| 92 | +} |
0 commit comments