From d4b82007dfc8db396b847a746abc26517e067883 Mon Sep 17 00:00:00 2001 From: janczer Date: Fri, 8 Feb 2019 20:34:33 +0100 Subject: [PATCH 1/3] Add floats.{__truediv__, __mul__} --- tests/snippets/floats.py | 3 +++ vm/src/obj/objfloat.rs | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/tests/snippets/floats.py b/tests/snippets/floats.py index cf6a9a0c0..b2b2cad8c 100644 --- a/tests/snippets/floats.py +++ b/tests/snippets/floats.py @@ -15,3 +15,6 @@ assert b >= a assert c >= a assert not a >= b +assert a + b == 2.5 +assert a - c == 0 +assert a / c == 1 diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 1bb57e572..4e4b09807 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -266,6 +266,46 @@ fn float_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } } +fn float_truediv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [(i, Some(vm.ctx.float_type())), (i2, None)] + ); + let v1 = get_value(i); + if objtype::isinstance(i2, &vm.ctx.float_type) { + Ok(vm + .ctx + .new_float(v1 / get_value(i2))) + } else if objtype::isinstance(i2, &vm.ctx.int_type) { + Ok(vm + .ctx + .new_float(v1 / objint::get_value(i2).to_f64().unwrap())) + } else { + Err(vm.new_type_error(format!("Cannot divide {} and {}", i.borrow(), i2.borrow()))) + } +} + +fn float_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [(i, Some(vm.ctx.float_type())), (i2, None)] + ); + let v1 = get_value(i); + if objtype::isinstance(i2, &vm.ctx.float_type) { + Ok(vm + .ctx + .new_float(v1 * get_value(i2))) + } else if objtype::isinstance(i2, &vm.ctx.int_type) { + Ok(vm + .ctx + .new_float(v1 * objint::get_value(i2).to_f64().unwrap())) + } else { + Err(vm.new_type_error(format!("Cannot divide {} and {}", i.borrow(), i2.borrow()))) + } +} + pub fn init(context: &PyContext) { let float_type = &context.float_type; @@ -299,4 +339,6 @@ pub fn init(context: &PyContext) { "__doc__", context.new_str(float_doc.to_string()), ); + context.set_attr(&float_type, "__truediv__", context.new_rustfunc(float_truediv)); + context.set_attr(&float_type, "__mul__", context.new_rustfunc(float_mul)); } From 59706538f4071b2552f926d7bc7e0c8e303e0b52 Mon Sep 17 00:00:00 2001 From: janczer Date: Fri, 8 Feb 2019 21:56:07 +0100 Subject: [PATCH 2/3] Fix typo and fix fmt errors --- vm/src/obj/objfloat.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 4e4b09807..4067f395c 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -274,9 +274,7 @@ fn float_truediv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let v1 = get_value(i); if objtype::isinstance(i2, &vm.ctx.float_type) { - Ok(vm - .ctx - .new_float(v1 / get_value(i2))) + Ok(vm.ctx.new_float(v1 / get_value(i2))) } else if objtype::isinstance(i2, &vm.ctx.int_type) { Ok(vm .ctx @@ -294,15 +292,13 @@ fn float_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let v1 = get_value(i); if objtype::isinstance(i2, &vm.ctx.float_type) { - Ok(vm - .ctx - .new_float(v1 * get_value(i2))) + Ok(vm.ctx.new_float(v1 * get_value(i2))) } else if objtype::isinstance(i2, &vm.ctx.int_type) { Ok(vm .ctx .new_float(v1 * objint::get_value(i2).to_f64().unwrap())) } else { - Err(vm.new_type_error(format!("Cannot divide {} and {}", i.borrow(), i2.borrow()))) + Err(vm.new_type_error(format!("Cannot multiply {} and {}", i.borrow(), i2.borrow()))) } } @@ -339,6 +335,10 @@ pub fn init(context: &PyContext) { "__doc__", context.new_str(float_doc.to_string()), ); - context.set_attr(&float_type, "__truediv__", context.new_rustfunc(float_truediv)); + context.set_attr( + &float_type, + "__truediv__", + context.new_rustfunc(float_truediv), + ); context.set_attr(&float_type, "__mul__", context.new_rustfunc(float_mul)); } From ba19732fbb3b05eb5910e7ed5168bb3751e64053 Mon Sep 17 00:00:00 2001 From: janczer Date: Fri, 8 Feb 2019 22:09:42 +0100 Subject: [PATCH 3/3] Fix the fmt error --- vm/src/obj/objfloat.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 4067f395c..6d8c2ff73 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -298,7 +298,11 @@ fn float_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .ctx .new_float(v1 * objint::get_value(i2).to_f64().unwrap())) } else { - Err(vm.new_type_error(format!("Cannot multiply {} and {}", i.borrow(), i2.borrow()))) + Err(vm.new_type_error(format!( + "Cannot multiply {} and {}", + i.borrow(), + i2.borrow() + ))) } }