Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sqrt primitive. #57

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ itertools = "0.8"
lrlex = "0.4"
lrpar = "0.4"
num-bigint = "0.2"
num-integer = "0.1"
num_enum = "0.3"
num-traits = "0.2"
natrob = { git="https://github.com/softdevteam/natrob", features=["abgc"] }
Expand Down
14 changes: 14 additions & 0 deletions lang_tests/double12.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"
VM:
status: success
stdout:
1.1
NaN
"

double12 = (
run = (
(1.21 sqrt) println.
(-1.1 sqrt) println.
)
)
16 changes: 16 additions & 0 deletions lang_tests/int26.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"
VM:
status: success
stdout:
5
1267650600228229401496703205376
1.7320508075688772
"

int26 = (
run = (
(25 sqrt) println.
((1 << 200) sqrt) println.
(3 sqrt) println.
)
)
11 changes: 11 additions & 0 deletions lang_tests/int27.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"
VM:
status: error
stderr: DomainError
"

int27 = (
run = (
-1 sqrt.
)
)
11 changes: 11 additions & 0 deletions lang_tests/int28.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"
VM:
status: error
stderr: DomainError
"

int28 = (
run = (
(-1 << 200) sqrt.
)
)
18 changes: 18 additions & 0 deletions lang_tests/int29.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"
VM:
status: success
stdout:
Integer
Integer
Double
Integer
"

int29 = (
run = (
(4 sqrt class) println.
((1 << 200) sqrt class) println.
(3 sqrt class) println.
((1 << 199) sqrt class) println.
)
)
1 change: 1 addition & 0 deletions lib/SOM/Double.som
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Double = (
>= argument = ( ^(self < argument) not )
<= argument = ( ^(self < argument) or: [ self = argument ] )
negative = ( ^self < 0.0 )
sqrt = primitive
asString = primitive
)
1 change: 1 addition & 0 deletions lib/SOM/Integer.som
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Integer = (
>= argument = primitive
<< argument = primitive
bitXor: argument = primitive
sqrt = primitive
asString = primitive

to: limit do: block = (
Expand Down
1 change: 1 addition & 0 deletions src/lib/compiler/ast_to_instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl<'a> Compiler<'a> {
requires_args(1)?;
Ok(cobjects::MethodBody::Primitive(Primitive::BitXor))
}
"sqrt" => Ok(cobjects::MethodBody::Primitive(Primitive::Sqrt)),
"asString" => Ok(cobjects::MethodBody::Primitive(Primitive::AsString)),
"class" => Ok(cobjects::MethodBody::Primitive(Primitive::Class)),
"concatenate:" => Ok(cobjects::MethodBody::Primitive(Primitive::Concatenate)),
Expand Down
1 change: 1 addition & 0 deletions src/lib/compiler/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub enum Primitive {
RefEquals,
Restart,
Shl,
Sqrt,
Sub,
/// Is this `value` (0), `value:` (1), or `value:with:` (2)?
Value(u8),
Expand Down
6 changes: 6 additions & 0 deletions src/lib/vm/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum VMError {
/// A value which can't be represented in an `usize`.
CantRepresentAsUsize,
DivisionByZero,
/// A value which is mathematically undefined.
DomainError,
/// The VM is trying to exit.
Exit,
/// Tried to perform a `Val::downcast` operation on a non-boxed `Val`. Note that `expected`
Expand Down Expand Up @@ -465,6 +467,10 @@ impl VM {
self.stack_push(stry!(rcv.shl(self, self.stack_pop())));
SendReturn::Val
}
Primitive::Sqrt => {
self.stack_push(stry!(rcv.sqrt(self)));
SendReturn::Val
}
Primitive::Sub => {
self.stack_push(stry!(rcv.sub(self, self.stack_pop())));
SendReturn::Val
Expand Down
4 changes: 4 additions & 0 deletions src/lib/vm/objects/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl Obj for Double {
}
}

fn sqrt(&self, vm: &VM) -> Result<Val, Box<VMError>> {
Ok(Double::new(vm, self.val.sqrt()))
}

fn ref_equals(&self, vm: &VM, other: Val) -> Result<Val, Box<VMError>> {
let b = if let Some(rhs) = other.try_downcast::<Double>(vm) {
self.val == rhs.double()
Expand Down
10 changes: 9 additions & 1 deletion src/lib/vm/objects/integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::convert::TryFrom;

use abgc_derive::GcLayout;
use num_bigint::BigInt;
use num_traits::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive, Zero};

use crate::vm::{
core::{VMError, VM},
Expand Down Expand Up @@ -155,6 +155,14 @@ impl Obj for ArbInt {
}
}

fn sqrt(&self, vm: &VM) -> Result<Val, Box<VMError>> {
if self.val < Zero::zero() {
ummarikar marked this conversation as resolved.
Show resolved Hide resolved
Err(Box::new(VMError::DomainError))
} else {
ArbInt::new(vm, self.val.sqrt())
}
}

fn ref_equals(&self, vm: &VM, other: Val) -> Result<Val, Box<VMError>> {
let b = if let Some(rhs) = other.try_downcast::<ArbInt>(vm) {
self.val == rhs.val
Expand Down
5 changes: 5 additions & 0 deletions src/lib/vm/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ pub trait Obj: std::fmt::Debug + abgc::GcLayout {
unimplemented!();
}

/// Produces a new `Val` which is the square root of this.
fn sqrt(&self, _: &VM) -> Result<Val, Box<VMError>> {
unimplemented!();
}

/// Is this `Val` reference equality equal to `other`? Only number types are likely to want to
/// override this.
fn ref_equals(&self, vm: &VM, other: Val) -> Result<Val, Box<VMError>> {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/vm/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,23 @@ impl Val {
self.tobj(vm).unwrap().shl(vm, other)
}

/// Produces a new `Val` which is the square root of this.
pub fn sqrt(&self, vm: &VM) -> Result<Val, Box<VMError>> {
if let Some(lhs) = self.as_isize(vm) {
if lhs < 0 {
ummarikar marked this conversation as resolved.
Show resolved Hide resolved
return Err(Box::new(VMError::DomainError));
} else {
let result = (lhs as f64).sqrt();
if result.round() == result {
return Val::from_isize(vm, result as isize);
} else {
return Ok(Double::new(vm, result));
}
}
}
self.tobj(vm).unwrap().sqrt(vm)
}

pub fn to_strval(&self, vm: &VM) -> Result<Val, Box<VMError>> {
debug_assert!(!self.is_illegal());
match self.valkind() {
Expand Down