From c750a47de9133e35d34ffc80f9b81344427167fd Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 16 Aug 2025 08:57:40 +0900 Subject: [PATCH] mruby-complex: refactor arithmetic operations Refactor the C implementation of arithmetic operations (+, -, *) to reduce code duplication. A new static helper function `complex_op` is introduced to handle the common logic of the operations. Co-authored-by: Gemini --- mrbgems/mruby-complex/src/complex.c | 80 ++++++++++++----------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index 046b8e236..379853d61 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -246,24 +246,41 @@ complex_eq(mrb_state *mrb, mrb_value x) return mrb_bool_value(mrb_complex_eq(mrb, x, y)); } +static mrb_value +complex_op(mrb_state *mrb, mrb_value x, mrb_value y, char op) +{ + struct mrb_complex *p1 = complex_ptr(mrb, x); + mrb_float r, i; + + switch (mrb_type(y)) { + case MRB_TT_COMPLEX: { + struct mrb_complex *p2 = complex_ptr(mrb, y); + r = p2->real; + i = p2->imaginary; + break; + } + default: { + r = mrb_as_float(mrb, y); + i = 0; + break; + } + } + + switch (op) { + case '+': + return mrb_complex_new(mrb, p1->real + r, p1->imaginary + i); + case '-': + return mrb_complex_new(mrb, p1->real - r, p1->imaginary - i); + case '*': + return mrb_complex_new(mrb, p1->real * r - p1->imaginary * i, p1->real * i + p1->imaginary * r); + } + return mrb_nil_value(); /* should not happen */ +} + mrb_value mrb_complex_add(mrb_state *mrb, mrb_value x, mrb_value y) { - struct mrb_complex *p1 = complex_ptr(mrb, x); - - switch (mrb_type(y)) { - case MRB_TT_COMPLEX: - { - struct mrb_complex *p2 = complex_ptr(mrb, y); - return mrb_complex_new(mrb, p1->real+p2->real, p1->imaginary+p2->imaginary); - } - - default: - { - mrb_float z = mrb_as_float(mrb, y); - return mrb_complex_new(mrb, p1->real+z, p1->imaginary); - } - } + return complex_op(mrb, x, y, '+'); } /* @@ -287,21 +304,7 @@ complex_add(mrb_state *mrb, mrb_value x) mrb_value mrb_complex_sub(mrb_state *mrb, mrb_value x, mrb_value y) { - struct mrb_complex *p1 = complex_ptr(mrb, x); - - switch (mrb_type(y)) { - case MRB_TT_COMPLEX: - { - struct mrb_complex *p2 = complex_ptr(mrb, y); - return mrb_complex_new(mrb, p1->real-p2->real, p1->imaginary-p2->imaginary); - } - - default: - { - mrb_float z = mrb_as_float(mrb, y); - return mrb_complex_new(mrb, p1->real-z, p1->imaginary); - } - } + return complex_op(mrb, x, y, '-'); } /* @@ -325,22 +328,7 @@ complex_sub(mrb_state *mrb, mrb_value x) mrb_value mrb_complex_mul(mrb_state *mrb, mrb_value x, mrb_value y) { - struct mrb_complex *p1 = complex_ptr(mrb, x); - - switch (mrb_type(y)) { - case MRB_TT_COMPLEX: - { - struct mrb_complex *p2 = complex_ptr(mrb, y); - return mrb_complex_new(mrb, p1->real*p2->real - p1->imaginary*p2->imaginary, - p1->real*p2->imaginary + p2->real*p1->imaginary); - } - - default: - { - mrb_float z = mrb_as_float(mrb, y); - return mrb_complex_new(mrb, p1->real*z, p1->imaginary*z); - } - } + return complex_op(mrb, x, y, '*'); } /*