Skip to content

Commit

Permalink
Add sqrt primitive.
Browse files Browse the repository at this point in the history
This adds the sqrt primitive to yksom for Int, BigInt and Double.
  • Loading branch information
K1630549 authored and ummarikar committed Oct 23, 2019
1 parent c6177e5 commit d9d0f3e
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 1 deletion.
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.
)
)
18 changes: 18 additions & 0 deletions lang_tests/int26.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"
VM:
status: success
stdout:
5
1267650600228229401496703205376
NaN
NaN
"

int26 = (
run = (
(25 sqrt) println.
((1 << 200) sqrt) println.
(-1 sqrt) println.
((-1 << 200) sqrt) 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
4 changes: 4 additions & 0 deletions src/lib/vm/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,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() {
Ok(Double::new(vm, std::f64::NAN))
} 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
13 changes: 13 additions & 0 deletions src/lib/vm/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::{
use abgc::{self, Gc};
use num_bigint::BigInt;
use num_enum::{IntoPrimitive, UnsafeFromPrimitive};
use num_integer::Roots;
use num_traits::FromPrimitive;

use super::{
Expand Down Expand Up @@ -420,6 +421,18 @@ 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 {
return Ok(Double::new(vm, std::f64::NAN));
} else {
return Val::from_isize(vm, lhs.sqrt());
}
}
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

0 comments on commit d9d0f3e

Please sign in to comment.