Skip to content

Commit

Permalink
Merge #150
Browse files Browse the repository at this point in the history
150: Update Val::div to the same format as other numeric operations. r=vext01 a=ltratt

This had not been modernised, which meant that it didn't handle the conversions of some types correctly.

Co-authored-by: Laurence Tratt <[email protected]>
  • Loading branch information
bors[bot] and ltratt authored Jun 15, 2020
2 parents ea79ae4 + b52f65c commit 3951251
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lang_tests/double4.som
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ VM:
1236106187891530871397840632111672689764312345256368613097472
5356460147529967823014489092130522184788338220858498848129024
8034690221294951377709810461705813012611014968913964176506880
-803469022129495137770981046170581301261101496891396417650688
-803469022129495137770981046170581301261101496891396417650688
"

double4 = (
Expand All @@ -20,5 +22,7 @@ double4 = (
((1 << 200) / 1.3) println.
((1 << 200) / 0.3) println.
((1 << 200) / 0.2) println.
((-1 << 200) / 2) println.
((1 << 200) / -2) println.
)
)
4 changes: 4 additions & 0 deletions lang_tests/int2.som
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ VM:
1
20
4
-2
-2
"

int2 = (
Expand All @@ -18,5 +20,7 @@ int2 = (
(1 - (1 - 1)) println.
(2 + 3 * 4) println.
(20 / 5) println.
(4 / -2) println.
(-4 / 2) println.
)
)
28 changes: 20 additions & 8 deletions src/lib/vm/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,27 @@ impl Val {

/// Produce a new `Val` which divides `other` from this.
pub fn div(&self, vm: &mut VM, other: Val) -> Result<Val, Box<VMError>> {
debug_assert_eq!(ValKind::INT as usize, 0);
if self.valkind() == ValKind::INT && other.valkind() == ValKind::INT {
if other.val != 0 {
return Ok(Val {
val: (self.val / other.val) * (1 << TAG_BITSIZE),
});
} else {
return Err(VMError::new(vm, VMErrorKind::DivisionByZero));
if let Some(lhs) = self.as_isize(vm) {
if let Some(rhs) = other.as_isize(vm) {
if rhs == 0 {
return Err(VMError::new(vm, VMErrorKind::DivisionByZero));
} else {
return Ok(Val::from_isize(vm, lhs / rhs));
}
} else if let Some(rhs) = other.try_downcast::<ArbInt>(vm) {
return Ok(ArbInt::new(
vm,
BigInt::from_isize(lhs).unwrap() / rhs.bigint(),
));
} else if let Some(rhs) = other.try_downcast::<Double>(vm) {
if rhs.double() == 0f64 {
return Err(VMError::new(vm, VMErrorKind::DivisionByZero));
} else {
return Ok(Val::from_isize(vm, ((lhs as f64) / rhs.double()) as isize));
}
}
let got = other.dyn_objtype(vm);
return Err(VMError::new(vm, VMErrorKind::NotANumber { got }));
}
self.tobj(vm).unwrap().div(vm, other)
}
Expand Down

0 comments on commit 3951251

Please sign in to comment.