diff options
Diffstat (limited to '')
-rw-r--r-- | matrix-std/Cargo.toml (renamed from matrix-stdlib/Cargo.toml) | 4 | ||||
-rw-r--r-- | matrix-std/src/core.rs (renamed from matrix-stdlib/src/core.rs) | 21 | ||||
-rw-r--r-- | matrix-std/src/io.rs (renamed from matrix-stdlib/src/io.rs) | 5 | ||||
-rw-r--r-- | matrix-std/src/iter.rs (renamed from matrix-stdlib/src/iter.rs) | 32 | ||||
-rw-r--r-- | matrix-std/src/lib.rs | 50 | ||||
-rw-r--r-- | matrix-std/src/math.rs (renamed from matrix-stdlib/src/math.rs) | 13 | ||||
-rw-r--r-- | matrix-std/src/sys.rs (renamed from matrix-stdlib/src/sys.rs) | 4 | ||||
-rw-r--r-- | matrix-stdlib/src/lib.rs | 32 |
8 files changed, 91 insertions, 70 deletions
diff --git a/matrix-stdlib/Cargo.toml b/matrix-std/Cargo.toml index 6256cfa..476272c 100644 --- a/matrix-stdlib/Cargo.toml +++ b/matrix-std/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "matrix-stdlib" +name = "matrix-std" version = "0.1.0" edition = "2021" @@ -7,7 +7,7 @@ edition = "2021" [dependencies] anyhow = "1" -matrix = { path = "../matrix" } +matrix-lang = { path = "../matrix-lang" } matrix-macros = { path = "../matrix-macros" } os_info = "3" rand = "0.8" diff --git a/matrix-stdlib/src/core.rs b/matrix-std/src/core.rs index 2c76497..69b6d97 100644 --- a/matrix-stdlib/src/core.rs +++ b/matrix-std/src/core.rs @@ -1,10 +1,8 @@ -use std::hash::{DefaultHasher, Hash, Hasher}; - -use matrix::{vm::Vm, value::Value, unpack_args, Result, unpack_varargs}; -use matrix_macros::native_func; +use std::hash::{DefaultHasher, Hasher}; use rand::Rng; -use crate::{VmArgs, next, error}; - +use matrix_lang::prelude::*; +use matrix_macros::native_func; +use crate::{VmArgs, next, error, unpack_args, unpack_varargs}; fn to_radix(r: i64, mut n: i64) -> String { let mut result = String::new(); @@ -123,11 +121,8 @@ fn remove(_: VmArgs, args: Vec<Value>) -> Result<Value> { #[native_func(1)] fn hash(_: VmArgs, args: Vec<Value>) -> Result<Value> { let [value] = unpack_args!(args); - if let Err(e) = value.can_hash() { - return Err(e) - }; let mut hasher = DefaultHasher::new(); - value.hash(&mut hasher); + value.try_hash(&mut hasher)?; let fin = hasher.finish(); Ok(Value::Int(fin as u32 as i64)) } @@ -201,8 +196,10 @@ fn sort(_: VmArgs, args: Vec<Value>) -> Result<Value> { let Value::List(mut list) = value else { return error!("sort requires a list") }; - let hi = list.len() - 1; - quicksort(list.as_mut(), 0, hi); + if list.len() > 1 { + let hi = list.len() - 1; + quicksort(list.as_mut(), 0, hi); + } Ok(Value::List(list)) } diff --git a/matrix-stdlib/src/io.rs b/matrix-std/src/io.rs index d72248c..19ff074 100644 --- a/matrix-stdlib/src/io.rs +++ b/matrix-std/src/io.rs @@ -1,8 +1,7 @@ use std::{io::{self, Read, Write}, cell::RefCell, fs::OpenOptions, rc::Rc}; - -use matrix::{value::Value, self, vm::Vm, Result, unpack_varargs, iter, unpack_args}; +use matrix_lang::prelude::*; use matrix_macros::native_func; -use crate::{error, VmArgs}; +use crate::{error, VmArgs, unpack_varargs, unpack_args}; #[native_func(0..)] fn print(_: VmArgs, args: Vec<Value>) -> Result<Value> { diff --git a/matrix-stdlib/src/iter.rs b/matrix-std/src/iter.rs index 630e52c..638755c 100644 --- a/matrix-stdlib/src/iter.rs +++ b/matrix-std/src/iter.rs @@ -1,7 +1,7 @@ use std::{cell::RefCell, rc::Rc}; -use matrix::{iter, vm::Vm, value::Value, Result, unpack_varargs, unpack_args}; +use matrix_lang::prelude::*; use matrix_macros::native_func; -use crate::{error, next, VmArgs}; +use crate::{error, next, VmArgs, unpack_args, unpack_varargs}; use Value as V; @@ -54,7 +54,7 @@ fn range(_: VmArgs, args: Vec<Value>) -> Result<Value> { Ok(iter!(move |_,_| { let curr = *(i.borrow()); *(i.borrow_mut()) = curr + step; - if curr >= max { + if (step >= 0 && curr >= max) || (step <= 0 && curr <= max) { return Ok(V::Nil) } else { return Ok(V::Int(curr)) @@ -235,7 +235,7 @@ fn lines(_: VmArgs, args: Vec<Value>) -> Result<Value> { let Value::String(str) = str else { return error!("lines arg must be a string") }; - let lines: Vec<Rc<str>> = str.split_inclusive("\n").map(|s| Rc::from(s)).collect(); + let lines: Vec<Rc<str>> = str.split("\n").map(|s| Rc::from(s)).collect(); let res = RefCell::new(lines.into_iter()); Ok(iter!(move |_,_| { match res.borrow_mut().next() { @@ -245,7 +245,7 @@ fn lines(_: VmArgs, args: Vec<Value>) -> Result<Value> { })) } -#[native_func(1)] +#[native_func(2)] fn skip((vm, frame): VmArgs, args: Vec<Value>) -> Result<Value> { let [count, iter] = unpack_args!(args); let iter = iter.into_iter_fn()?; @@ -291,25 +291,32 @@ fn alternate(_: VmArgs, args: Vec<Value>) -> Result<Value> { } #[native_func(2)] -fn intersperse(_: VmArgs, args: Vec<Value>) -> Result<Value> { +fn intersperse((vm, frame): VmArgs, args: Vec<Value>) -> Result<Value> { let [value, iter] = unpack_args!(args); let iter = iter.into_iter_fn()?; let flag = RefCell::new(Some(0)); + let next = RefCell::new(next!(vm, frame, iter)?); Ok(iter!(move |(vm, frame),_| { let f = *flag.borrow(); match f { Some(0) => { - let val = next!(vm, frame, iter)?; - if val == Value::Nil { + let val = next.borrow(); + if *val == Value::Nil { *flag.borrow_mut() = None; } else { *flag.borrow_mut() = Some(1); } - Ok(val) + Ok(val.clone()) }, Some(1) => { + *next.borrow_mut() = next!(vm, frame, iter)?; + if *next.borrow() == Value::Nil { + *flag.borrow_mut() = None; + return Ok(Value::Nil) + } else { + *flag.borrow_mut() = Some(0); + } let val = value.clone(); - *flag.borrow_mut() = Some(0); Ok(val) }, _ => Ok(Value::Nil) @@ -331,6 +338,7 @@ fn zip(_: VmArgs, args: Vec<Value>) -> Result<Value> { let vr = next!(vm, frame, ir)?; if vl == Value::Nil || vr == Value::Nil { *flag.borrow_mut() = false; + return Ok(Value::Nil) } Ok(Value::List(vec![vl, vr].into())) }, @@ -401,7 +409,7 @@ fn take((vm, frame): VmArgs, args: Vec<Value>) -> Result<Value> { Ok(Value::List(values.into())) } -#[native_func(2)] +#[native_func(1)] fn last((vm, frame): VmArgs, args: Vec<Value>) -> Result<Value> { let [iter] = unpack_args!(args); let iter = iter.into_iter_fn()?; @@ -414,7 +422,7 @@ fn last((vm, frame): VmArgs, args: Vec<Value>) -> Result<Value> { Ok(last) } -#[native_func(2)] +#[native_func(1)] fn next((vm, frame): VmArgs, args: Vec<Value>) -> Result<Value> { let [iter] = unpack_args!(args); let iter = iter.into_iter_fn()?; diff --git a/matrix-std/src/lib.rs b/matrix-std/src/lib.rs new file mode 100644 index 0000000..af4ecab --- /dev/null +++ b/matrix-std/src/lib.rs @@ -0,0 +1,50 @@ +mod core; +mod sys; +mod math; +mod io; +mod iter; + +use matrix_lang::prelude::*; +pub(crate) type VmArgs<'a, 'b> = (&'a mut Vm, &'b mut StackFrame); + +macro_rules! error { + ($($arg:tt)*) => { + Err(::matrix_lang::prelude::exception!(::matrix_lang::prelude::RUNTIME_EXCEPTION, $($arg)*)) + }; +} + +macro_rules! next { + ($vm:expr, $frame:expr, $iter:expr) => { + $vm.run_fn($frame, $iter.clone(), vec![]) + }; +} + +macro_rules! unpack_args { + ($e:expr) => { + $e.try_into().expect("bypassed arity check") + }; +} + +macro_rules! unpack_varargs { + ($e:expr) => {{ + let mut args = $e; + let matrix_lang::prelude::Value::List(varargs) = args.pop().expect("bypassed arity check") else { + panic!("bypassed arity check") + }; + let varargs = varargs.into_inner(); + (args.try_into().expect("bypassed arity check"), varargs) + }}; +} + +pub(crate) use error; +pub(crate) use next; +pub(crate) use unpack_args; +pub(crate) use unpack_varargs; + +pub fn load(vm: &mut Vm) { + core::load(vm); + sys::load(vm); + io::load(vm); + iter::load(vm); + math::load(vm); +} diff --git a/matrix-stdlib/src/math.rs b/matrix-std/src/math.rs index 3f33951..111544c 100644 --- a/matrix-stdlib/src/math.rs +++ b/matrix-std/src/math.rs @@ -1,9 +1,8 @@ use core::f64; use std::f64::{consts::{PI, E, TAU}, NAN, INFINITY}; - -use matrix::{vm::Vm, value::{Value, Matrix}, Result, unpack_args, Rational64, Complex64}; +use matrix_lang::prelude::*; use matrix_macros::native_func; -use crate::{error, VmArgs}; +use crate::{error, VmArgs, unpack_args}; #[native_func(1)] fn trans(_: VmArgs, args: Vec<Value>) -> Result<Value> { @@ -280,7 +279,7 @@ macro_rules! trig { fn $type(_: VmArgs, args: Vec<Value>) -> Result<Value> { use Value as V; let [value] = unpack_args!(args); - match value.promote_trig() { + match value.floaty() { V::Float(f) => Ok(V::Float(f.$type())), V::Complex(c) => Ok(V::Complex(c.$type())), v => error!("cannot compute {} on {v}", stringify!($type)) @@ -295,7 +294,7 @@ macro_rules! trigf { fn $str(_: VmArgs, args: Vec<Value>) -> Result<Value> { use Value as V; let [value] = unpack_args!(args); - match value.promote_trig() { + match value.floaty() { V::Float(f) => Ok(V::Float(f.$type())), v => error!("cannot compute {} on {v}", stringify!($str)) } @@ -307,7 +306,7 @@ macro_rules! trigf { fn log(_: VmArgs, args: Vec<Value>) -> Result<Value> { use Value as V; let [base, value] = unpack_args!(args); - match (base.promote_trig(), value.promote_trig()) { + match (base.floaty(), value.floaty()) { (V::Float(base), V::Float(arg)) => Ok(V::Float(arg.log(base))), (V::Float(base), V::Complex(arg)) => Ok(V::Complex(arg.log(base))), (V::Complex(base), V::Float(arg)) => Ok(V::Complex(arg.ln() / base.ln())), @@ -463,7 +462,7 @@ fn im(_: VmArgs, args: Vec<Value>) -> Result<Value> { #[native_func(1)] fn cis(_: VmArgs, args: Vec<Value>) -> Result<Value> { let [value] = unpack_args!(args); - match value.promote_trig() { + match value.floaty() { Value::Float(f) => Ok(Value::Complex(Complex64::cis(f))), Value::Complex(c) => Ok((Value::Complex(Complex64::cis(c.re)) * Value::Float((-c.im).exp()))?), _ => error!("cis can only take floats") diff --git a/matrix-stdlib/src/sys.rs b/matrix-std/src/sys.rs index d30226f..609e72d 100644 --- a/matrix-stdlib/src/sys.rs +++ b/matrix-std/src/sys.rs @@ -1,9 +1,9 @@ use std::{process::{exit, Command, Stdio, Child}, env, rc::Rc, io::{Read, self}, cell::RefCell, fs::{File, self}, os::fd::FromRawFd, sync::OnceLock, path::PathBuf}; -use matrix::{vm::Vm, value::{Value, ValueMap}, unpack_args, Result, gc::Gc}; +use matrix_lang::prelude::*; use matrix_macros::native_func; use os_info::Info; -use crate::{VmArgs, error}; +use crate::{VmArgs, error, unpack_args}; #[native_func(1)] fn sys_exit(_: VmArgs, args: Vec<Value>) -> Result<Value> { diff --git a/matrix-stdlib/src/lib.rs b/matrix-stdlib/src/lib.rs deleted file mode 100644 index b4ab658..0000000 --- a/matrix-stdlib/src/lib.rs +++ /dev/null @@ -1,32 +0,0 @@ -use matrix::vm::{Vm, StackFrame}; - -mod core; -mod sys; -mod math; -mod io; -mod iter; - -pub(crate) type VmArgs<'a, 'b> = (&'a mut Vm, &'b mut StackFrame); - -macro_rules! error { - ($($arg:tt)*) => { - Err(format!($($arg)*).into()) - }; -} - -macro_rules! next { - ($vm:expr, $frame:expr, $iter:expr) => { - $vm.run_fn($frame, $iter.clone(), vec![]) - }; -} - -pub(crate) use error; -pub(crate) use next; - -pub fn load(vm: &mut Vm) { - core::load(vm); - sys::load(vm); - io::load(vm); - iter::load(vm); - math::load(vm); -} |