diff --git a/src/instructions/vector.rs b/src/instructions/vector.rs index 655e3b2..78e952d 100644 --- a/src/instructions/vector.rs +++ b/src/instructions/vector.rs @@ -10,695 +10,210 @@ fn bounded_idx(num: i128, length: usize) -> usize { } /// Concats two vectors together. -pub fn _concat(vals: Vec>) -> Option> +fn _concat(a: Vec, b: Vec) -> Option> where T: Clone, { - let mut concat_vec = vals[0].clone(); - concat_vec.extend(vals[1].clone().into_iter()); + let mut concat_vec = a; + concat_vec.extend(b.into_iter()); Some(concat_vec) } -make_instruction_clone!(vector_int, vector_int, _concat, Vec, 2); -make_instruction_clone!(vector_float, vector_float, _concat, Vec, 2); -make_instruction_clone!(vector_string, vector_string, _concat, Vec>, 2); -make_instruction_clone!(vector_boolean, vector_boolean, _concat, Vec, 2); -make_instruction_clone!(vector_char, vector_char, _concat, Vec, 2); -make_instruction_clone!(string, string, _concat, Vec, 2); /// Prepends a primitive value to a vector. -pub fn _conj(vec_vals: Vec>, prim_vals: Vec) -> Option> -where - T: Clone, -{ - let mut t_vec = vec_vals[0].clone(); - t_vec.insert(0, prim_vals[0].clone()); +fn _conj(vect: Vec, prim: T) -> Option> { + let mut t_vec = vect; + t_vec.insert(0, prim); Some(t_vec) } -make_instruction_aux!(vector_int, vector_int, _conj, Vec, 1, int, 1, i128); -make_instruction_aux!( - vector_float, - vector_float, - _conj, - Vec, - 1, - float, - 1, - Decimal -); -make_instruction_aux!( - vector_string, - vector_string, - _conj, - Vec>, - 1, - string, - 1, - Vec -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _conj, - Vec, - 1, - boolean, - 1, - bool -); -make_instruction_aux!(vector_char, vector_char, _conj, Vec, 1, char, 1, char); -make_instruction_aux!(string, string, _conj, Vec, 1, char, 1, char); /// Appends a primitive value to a vector. -pub fn _conj_end(vec_vals: Vec>, prim_vals: Vec) -> Option> -where - T: Clone, -{ - let mut t_vec = vec_vals[0].clone(); - t_vec.push(prim_vals[0].clone()); - Some(t_vec) +fn _conj_end(mut vals: Vec, prim: T) -> Option> { + vals.push(prim); + Some(vals) } -make_instruction_aux!( - vector_int, - vector_int, - _conj_end, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_float, - vector_float, - _conj_end, - Vec, - 1, - float, - 1, - Decimal -); -make_instruction_aux!( - vector_string, - vector_string, - _conj_end, - Vec>, - 1, - string, - 1, - Vec -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _conj_end, - Vec, - 1, - boolean, - 1, - bool -); -make_instruction_aux!( - vector_char, - vector_char, - _conj_end, - Vec, - 1, - char, - 1, - char -); -make_instruction_aux!(string, string, _conj_end, Vec, 1, char, 1, char); /// Takes the first N items from a vector. N based on an int. -pub fn _take_n(vals: Vec>, auxs: Vec) -> Option> +fn _take_n(vals: Vec, amt: i128) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(ret_vec[0..bounded_idx(auxs[0], ret_vec.len())].to_vec()) + Some(vals[0..bounded_idx(amt, vals.len())].to_vec()) } -make_instruction_aux!(vector_int, vector_int, _take_n, Vec, 1, int, 1, i128); -make_instruction_aux!( - vector_float, - vector_float, - _take_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_string, - vector_string, - _take_n, - Vec>, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _take_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_char, - vector_char, - _take_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!(string, string, _take_n, Vec, 1, int, 1, i128); /// Takes the first N items from a vector. N based on an int. -pub fn _take_last_n(vals: Vec>, auxs: Vec) -> Option> +fn _take_last_n(vals: Vec, amt: i128) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - let ret_vec_len = ret_vec.len(); - Some(ret_vec[ret_vec_len - bounded_idx(auxs[0], ret_vec_len)..ret_vec_len].to_vec()) + let vals_len = vals.len(); + Some(vals[vals_len - bounded_idx(amt, vals_len)..vals_len].to_vec()) } -make_instruction_aux!( - vector_int, - vector_int, - _take_last_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_float, - vector_float, - _take_last_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_string, - vector_string, - _take_last_n, - Vec>, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _take_last_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_char, - vector_char, - _take_last_n, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!(string, string, _take_last_n, Vec, 1, int, 1, i128); /// Takes a sublist of a vector based on two ints. -pub fn _sub(vals: Vec>, auxs: Vec) -> Option> +fn _sub(vals: Vec, idx0: i128, idx1: i128) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - let (mut start, mut end): (usize, usize) = ( - auxs[0].unsigned_abs() as usize, - auxs[1].unsigned_abs() as usize, - ); + let (mut start, mut end): (usize, usize) = + (idx0.unsigned_abs() as usize, idx1.unsigned_abs() as usize); if start > end { (start, end) = (end, start) } - let fin_start = start.min(ret_vec.len()); - let fin_end = end.min(ret_vec.len()); - Some(ret_vec[fin_start..fin_end].to_vec()) + let fin_start = start.min(vals.len()); + let fin_end = end.min(vals.len()); + Some(vals[fin_start..fin_end].to_vec()) } -make_instruction_aux!(vector_int, vector_int, _sub, Vec, 1, int, 2, i128); -make_instruction_aux!( - vector_float, - vector_float, - _sub, - Vec, - 1, - int, - 2, - i128 -); -make_instruction_aux!( - vector_string, - vector_string, - _sub, - Vec>, - 1, - int, - 2, - i128 -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _sub, - Vec, - 1, - int, - 2, - i128 -); -make_instruction_aux!(vector_char, vector_char, _sub, Vec, 1, int, 2, i128); -make_instruction_aux!(string, string, _sub, Vec, 1, int, 2, i128); /// Takes the first item from a vector. -pub fn _first(vals: Vec>) -> Option +fn _first(vals: Vec) -> Option where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(vals[0][0].clone()) + Some(vals[0].clone()) } -make_instruction_clone!(vector_int, int, _first, Vec, 1); -make_instruction_clone!(vector_float, float, _first, Vec, 1); -make_instruction_clone!(vector_string, string, _first, Vec>, 1); -make_instruction_clone!(vector_boolean, boolean, _first, Vec, 1); -make_instruction_clone!(vector_char, char, _first, Vec, 1); -make_instruction_clone!(string, char, _first, Vec, 1); /// Takes the first item from a vector, wraps it into a vector, and pushes it back /// to the same stack. -pub fn _from_first_prim(vals: Vec>) -> Option> +fn _from_first_prim(vals: Vec) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(vec![vals[0][0].clone()]) -} -make_instruction_clone!(vector_int, vector_int, _from_first_prim, Vec, 1); -make_instruction_clone!( - vector_float, - vector_float, - _from_first_prim, - Vec, - 1 -); -make_instruction_clone!( - vector_string, - vector_string, - _from_first_prim, - Vec>, - 1 -); -make_instruction_clone!( - vector_boolean, - vector_boolean, - _from_first_prim, - Vec, - 1 -); -make_instruction_clone!(vector_char, vector_char, _from_first_prim, Vec, 1); -make_instruction_clone!(string, string, _from_first_prim, Vec, 1); - -/// Places the top of a primitive type into a vector -pub fn _from_prim(vals: Vec) -> Option> -where - T: Clone, -{ Some(vec![vals[0].clone()]) } -make_instruction_out!(int, vector_int, _from_prim, i128, 1); -make_instruction_out!(float, vector_float, _from_prim, Decimal, 1); -make_instruction_out!(string, vector_string, _from_prim, Vec, 1); -make_instruction_out!(boolean, vector_boolean, _from_prim, bool, 1); -make_instruction_out!(char, vector_char, _from_prim, char, 1); -make_instruction_out!(char, string, _from_prim, char, 1); + +/// Places the top of a primitive type into a vector +fn _from_prim(prim: T) -> Option> { + Some(vec![prim]) +} /// Takes the last item from a vector. -pub fn _last(vals: Vec>) -> Option +fn _last(vals: Vec) -> Option where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(vals[0][ret_vec.len() - 1].clone()) + Some(vals[vals.len() - 1].clone()) } -make_instruction_clone!(vector_int, int, _last, Vec, 1); -make_instruction_clone!(vector_float, float, _last, Vec, 1); -make_instruction_clone!(vector_string, string, _last, Vec>, 1); -make_instruction_clone!(vector_boolean, boolean, _last, Vec, 1); -make_instruction_clone!(vector_char, char, _last, Vec, 1); -make_instruction_clone!(string, char, _last, Vec, 1); /// Takes the last item from a vector, wraps it into a vector, and pushes it back /// to the same stack. -pub fn _from_last_prim(vals: Vec>) -> Option> +fn _from_last_prim(vals: Vec) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(vec![vals[0][ret_vec.len() - 1].clone()]) + Some(vec![vals[vals.len() - 1].clone()]) } -make_instruction_clone!(vector_int, vector_int, _from_last_prim, Vec, 1); -make_instruction_clone!(vector_float, vector_float, _from_last_prim, Vec, 1); -make_instruction_clone!( - vector_string, - vector_string, - _from_last_prim, - Vec>, - 1 -); -make_instruction_clone!( - vector_boolean, - vector_boolean, - _from_last_prim, - Vec, - 1 -); -make_instruction_clone!(vector_char, vector_char, _from_last_prim, Vec, 1); -make_instruction_clone!(string, string, _from_last_prim, Vec, 1); /// Takes the nth item from a vector. N from int stack. -pub fn _nth(vals: Vec>, auxs: Vec) -> Option +fn _nth(vals: Vec, idx: i128) -> Option where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(vals[0][bounded_idx(auxs[0], ret_vec.len())].clone()) + Some(vals[bounded_idx(idx, vals.len())].clone()) } -make_instruction_aux!(vector_int, int, _nth, Vec, 1, int, 1, i128); -make_instruction_aux!(vector_float, float, _nth, Vec, 1, int, 1, i128); -make_instruction_aux!(vector_string, string, _nth, Vec>, 1, int, 1, i128); -make_instruction_aux!(vector_boolean, boolean, _nth, Vec, 1, int, 1, i128); -make_instruction_aux!(vector_char, char, _nth, Vec, 1, int, 1, i128); -make_instruction_aux!(string, char, _nth, Vec, 1, int, 1, i128); /// Takes the nth item from a vector, wraps it into a vector, and pushes it back /// to the same stack. N from int stack -pub fn _from_nth_prim(vals: Vec>, auxs: Vec) -> Option> +fn _from_nth_prim(vals: Vec, idx: i128) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(vec![vals[0][bounded_idx(auxs[0], ret_vec.len())].clone()]) + Some(vec![vals[bounded_idx(idx, vals.len())].clone()]) } -make_instruction_aux!( - vector_int, - vector_int, - _from_nth_prim, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_float, - vector_float, - _from_nth_prim, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_string, - vector_string, - _from_nth_prim, - Vec>, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _from_nth_prim, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_char, - vector_char, - _from_nth_prim, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - string, - vector_char, - _from_nth_prim, - Vec, - 1, - int, - 1, - i128 -); /// Takes a vector and removes the first element. -pub fn _rest(vals: Vec>) -> Option> +fn _rest(vals: Vec) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(ret_vec[1..].to_vec()) + Some(vals[1..].to_vec()) } -make_instruction_clone!(vector_int, vector_int, _rest, Vec, 1); -make_instruction_clone!(vector_float, vector_float, _rest, Vec, 1); -make_instruction_clone!(vector_string, vector_string, _rest, Vec>, 1); -make_instruction_clone!(vector_boolean, vector_boolean, _rest, Vec, 1); -make_instruction_clone!(vector_char, vector_char, _rest, Vec, 1); -make_instruction_clone!(string, string, _rest, Vec, 1); /// Takes a vector and removes the last element. -pub fn _but_last(vals: Vec>) -> Option> +fn _but_last(vals: Vec) -> Option> where T: Clone, { - let ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - Some(ret_vec[0..ret_vec.len() - 1].to_vec()) + Some(vals[0..vals.len() - 1].to_vec()) } -make_instruction_clone!(vector_int, vector_int, _but_last, Vec, 1); -make_instruction_clone!(vector_float, vector_float, _but_last, Vec, 1); -make_instruction_clone!(vector_string, vector_string, _but_last, Vec>, 1); -make_instruction_clone!(vector_boolean, vector_boolean, _but_last, Vec, 1); -make_instruction_clone!(vector_char, vector_char, _but_last, Vec, 1); -make_instruction_clone!(string, string, _but_last, Vec, 1); /// Removes the first n items from a vector. n from the int stack. -pub fn _drop(vals: Vec>, auxs: Vec) -> Option> +fn _drop(mut vals: Vec, idx: i128) -> Option> where T: Clone, { - let mut ret_vec = vals[0].clone(); - if ret_vec.is_empty() { + if vals.is_empty() { return None; } - ret_vec.drain(0..auxs[0].abs().min(ret_vec.len() as i128) as usize); - Some(ret_vec) + vals.drain(0..idx.abs().min(vals.len() as i128) as usize); + Some(vals) } -make_instruction_aux!(vector_int, vector_int, _drop, Vec, 1, int, 1, i128); -make_instruction_aux!( - vector_float, - vector_float, - _drop, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_string, - vector_string, - _drop, - Vec>, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _drop, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!(vector_char, vector_char, _drop, Vec, 1, int, 1, i128); -make_instruction_aux!(string, string, _drop, Vec, 1, int, 1, i128); -pub fn _drop_last(vals: Vec>, auxs: Vec) -> Option> +fn _drop_last(mut vals: Vec, idx: i128) -> Option> where T: Clone, { - let mut ret_vec = vals[0].clone(); - let rvlen = ret_vec.len(); //Ret_Vec Len - if ret_vec.is_empty() { + let valslen = vals.len(); //Ret_Vec Len + if vals.is_empty() { return None; } - ret_vec.drain((rvlen - (auxs[0].abs().min(rvlen as i128) as usize))..rvlen); - Some(ret_vec) + vals.drain((valslen - (idx.abs().min(valslen as i128) as usize))..valslen); + Some(vals) } -make_instruction_aux!( - vector_int, - vector_int, - _drop_last, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_float, - vector_float, - _drop_last, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_string, - vector_string, - _drop_last, - Vec>, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_boolean, - vector_boolean, - _drop_last, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!( - vector_char, - vector_char, - _drop_last, - Vec, - 1, - int, - 1, - i128 -); -make_instruction_aux!(string, string, _drop_last, Vec, 1, int, 1, i128); /// Takes the length of a vector. -pub fn _length(vals: Vec>) -> Option { - Some(vals[0].len() as i128) +fn _length(vals: Vec) -> Option { + Some(vals.len() as i128) } -make_instruction_clone!(vector_int, int, _length, Vec, 1); -make_instruction_clone!(vector_float, int, _length, Vec, 1); -make_instruction_clone!(vector_string, int, _length, Vec>, 1); -make_instruction_clone!(vector_boolean, int, _length, Vec, 1); -make_instruction_clone!(vector_char, int, _length, Vec, 1); -make_instruction_clone!(string, int, _length, Vec, 1); /// Reverses a vector -pub fn _reverse(vals: Vec>) -> Option> +fn _reverse(mut vals: Vec) -> Option> where T: Clone, { - let mut rev_vec = vals[0].clone(); - rev_vec.reverse(); - Some(rev_vec) + vals.reverse(); + Some(vals) } -make_instruction_clone!(vector_int, vector_int, _reverse, Vec, 1); -make_instruction_clone!(vector_float, vector_float, _reverse, Vec, 1); -make_instruction_clone!(vector_string, vector_string, _reverse, Vec>, 1); -make_instruction_clone!(vector_boolean, vector_boolean, _reverse, Vec, 1); -make_instruction_clone!(vector_char, vector_char, _reverse, Vec, 1); -make_instruction_clone!(string, string, _reverse, Vec, 1); /// Pushes all values of a vector into a primitive stack -pub fn _push_all(vals: Vec>) -> Option> -where - T: Clone, -{ - Some(vals[0].clone()) +fn _push_all(vals: Vec) -> Option> { + Some(vals) } -make_instruction_mult!(vector_int, int, _push_all, Vec, 1); -make_instruction_mult!(vector_float, float, _push_all, Vec, 1); -// make_instruction_mult!(vector_string, string, _push_all, Vec>, 1); // Optional -make_instruction_mult!(vector_boolean, boolean, _push_all, Vec, 1); -make_instruction_mult!(vector_char, char, _push_all, Vec, 1); -make_instruction_mult!(string, char, _push_all, Vec, 1); /// Creates an empty vector -pub fn _make_empty(_: Vec>) -> Option> { +fn _make_empty(_: Vec>) -> Option> { let empty_vec: Vec = Vec::new(); Some(empty_vec) } @@ -710,7 +225,7 @@ make_instruction_clone!(vector_char, vector_char, _make_empty, Vec, 0); make_instruction_clone!(string, string, _make_empty, Vec, 0); /// Checks if a vector is empty. Pushes true if is, false otherwise -pub fn _is_empty(vals: Vec>) -> Option { +fn _is_empty(vals: Vec>) -> Option { Some(vals[0].is_empty()) } make_instruction_clone!(vector_int, boolean, _is_empty, Vec, 1); @@ -721,7 +236,7 @@ make_instruction_clone!(vector_char, boolean, _is_empty, Vec, 1); make_instruction_clone!(string, boolean, _is_empty, Vec, 1); /// Checks if a vector contains a primitive. True if does, false otherwise -pub fn _contains(vals: Vec>, auxs: Vec) -> Option +fn _contains(vals: Vec>, auxs: Vec) -> Option where T: Eq, { @@ -762,7 +277,7 @@ make_instruction_aux!(vector_char, boolean, _contains, Vec, 1, char, 1, ch make_instruction_aux!(string, boolean, _contains, Vec, 1, char, 1, char); /// Checks if a vector contains another vector in no order. True if does, false otherwise -pub fn _contains_vector_non_contiguous(vals: Vec>) -> Option +fn _contains_vector_non_contiguous(vals: Vec>) -> Option where T: Eq + Hash, { @@ -813,7 +328,7 @@ make_instruction_clone!( ); /// Checks if a vector contains another contiguous vector. True if does, false otherwise -pub fn _contains_vector_contiguous(vals: Vec>) -> Option +fn _contains_vector_contiguous(vals: Vec>) -> Option where T: Eq, { @@ -860,7 +375,7 @@ make_instruction_clone!( make_instruction_clone!(string, boolean, _contains_vector_contiguous, Vec, 2); /// Returns the index of a primitive in a vector, pushes result to int stack -pub fn _index_of(vals: Vec>, auxs: Vec) -> Option +fn _index_of(vals: Vec>, auxs: Vec) -> Option where T: Clone + Eq, { @@ -906,7 +421,7 @@ make_instruction_aux!(vector_char, int, _index_of, Vec, 1, char, 1, char); make_instruction_aux!(string, int, _index_of, Vec, 1, char, 1, char); /// Finds the index of the start of one vector in another. Searches in contiguous space. -pub fn _index_of_vector(vals: Vec>) -> Option +fn _index_of_vector(vals: Vec>) -> Option where T: Eq, { @@ -926,7 +441,7 @@ make_instruction_clone!(vector_char, int, _index_of_vector, Vec, 2); make_instruction_clone!(string, int, _index_of_vector, Vec, 2); /// Counts the amount of a primitive in a vector -pub fn _occurrences_of(vals: Vec>, auxs: Vec) -> Option +fn _occurrences_of(vals: Vec>, auxs: Vec) -> Option where T: Clone + Eq, { @@ -982,7 +497,7 @@ make_instruction_aux!( make_instruction_aux!(string, int, _occurrences_of, Vec, 1, char, 1, char); /// Counts the amount of continuous occurrences one vector appears in another. -pub fn _occurrences_of_vector(vals: Vec>) -> Option +fn _occurrences_of_vector(vals: Vec>) -> Option where T: Eq, { @@ -1011,7 +526,7 @@ make_instruction_clone!(string, int, _occurrences_of_vector, Vec, 2); /// Pushes the values inside a vector separated into individual vectors back to /// the stack. -pub fn _parse_to_prim(vals: Vec>) -> Option>> +fn _parse_to_prim(vals: Vec>) -> Option>> where T: Clone, Vec: FromIterator, @@ -1032,7 +547,7 @@ make_instruction_mult!(vector_char, vector_char, _parse_to_prim, Vec, 1); make_instruction_mult!(string, string, _parse_to_prim, Vec, 1); /// Sets the nth index in a vector. N from the int stack. -pub fn _set_nth(vals: Vec>, aux0: Vec, aux1: Vec) -> Option> +fn _set_nth(vals: Vec>, aux0: Vec, aux1: Vec) -> Option> where T: Clone, { @@ -1121,7 +636,7 @@ make_instruction_aux2!( ); /// Splits a vector based the first occurence of a primitive -pub fn _split_on(vals: Vec>, auxs: Vec) -> Option>> +fn _split_on(vals: Vec>, auxs: Vec) -> Option>> where T: Clone + Eq, Vec: FromIterator, @@ -1194,7 +709,7 @@ make_instruction_mult_aux!( make_instruction_mult_aux!(string, string, _split_on, Vec, 1, char, 1, char); /*/// Splits a vector based the first occurence of a primitive -pub fn _split_on_vector(vals: Vec>) -> Option>> +fn _split_on_vector(vals: Vec>) -> Option>> where T: Clone + Eq, { @@ -1243,7 +758,7 @@ make_instruction_mult!(string, string, _split_on_vector, Vec, 1);*/ /// Replaces all values in a vector with respect to two primitives. The first primitive is /// the search value and the second value is the one to replace. -pub fn _replace(mut vals: Vec>, auxs: Vec) -> Option> +fn _replace(mut vals: Vec>, auxs: Vec) -> Option> where T: Clone, for<'a> &'a T: Eq, @@ -1306,7 +821,7 @@ make_instruction_aux!( make_instruction_aux!(string, string, _replace, Vec, 1, char, 2, char); /// Removes all values in a vector with respect to a primitives. If is equal, remove it. -pub fn _remove(vals: Vec>, auxs: Vec) -> Option> +fn _remove(vals: Vec>, auxs: Vec) -> Option> where T: Clone, for<'a> &'a T: Eq, @@ -1398,7 +913,7 @@ make_iterate!(vector_char, char, GeneVectorChar); //make_iterate!(string, string, GeneString); /// Sorts a vector -pub fn _sort(mut vals: Vec>) -> Option> +fn _sort(mut vals: Vec>) -> Option> where T: NumericTrait + Clone, { @@ -1409,7 +924,7 @@ make_instruction_clone!(vector_int, vector_int, _sort, Vec, 1); make_instruction_clone!(vector_float, vector_float, _sort, Vec, 1); /// Sorts a vector and reverses it -pub fn _sort_reverse(mut vals: Vec>) -> Option> +fn _sort_reverse(mut vals: Vec>) -> Option> where T: NumericTrait + Clone, { @@ -1421,7 +936,7 @@ make_instruction_clone!(vector_int, vector_int, _sort_reverse, Vec, 1); make_instruction_clone!(vector_float, vector_float, _sort_reverse, Vec, 1); /// Inserts a primitive into a vector at a given point from the int stack -pub fn _insert(mut vals: Vec>, auxs: Vec, auxs2: Vec) -> Option> +fn _insert(mut vals: Vec>, auxs: Vec, auxs2: Vec) -> Option> where T: Clone, { @@ -1509,7 +1024,7 @@ make_instruction_aux2!( ); /// Inserts one vector into another based on an index. -pub fn _insert_vector(mut vals: Vec>, auxs: Vec) -> Option> +fn _insert_vector(mut vals: Vec>, auxs: Vec) -> Option> where T: Clone, { @@ -1572,7 +1087,7 @@ make_instruction_aux!( make_instruction_aux!(string, string, _insert_vector, Vec, 2, int, 1, i128); /// Takes the mean of a vector -pub fn _mean(vals: Vec>) -> Option { +fn _mean(vals: Vec>) -> Option { if vals[0].is_empty() { return Some(T::zero()); } @@ -1586,7 +1101,7 @@ make_instruction_clone!(vector_int, int, _mean, Vec, 1); make_instruction_clone!(vector_float, float, _mean, Vec, 1); /// Takes the max of a vector -pub fn _maximum(vals: Vec>) -> Option { +fn _maximum(vals: Vec>) -> Option { if vals[0].is_empty() { return Some(T::zero()); } @@ -1596,7 +1111,7 @@ make_instruction_clone!(vector_int, int, _maximum, Vec, 1); make_instruction_clone!(vector_float, float, _maximum, Vec, 1); /// Takes the min of a vector -pub fn _minimum(vals: Vec>) -> Option { +fn _minimum(vals: Vec>) -> Option { if vals[0].is_empty() { return Some(T::zero()); } @@ -1606,7 +1121,7 @@ make_instruction_clone!(vector_int, int, _minimum, Vec, 1); make_instruction_clone!(vector_float, float, _minimum, Vec, 1); /// Takes the sum of a vector -pub fn _sum(vals: Vec>) -> Option { +fn _sum(vals: Vec>) -> Option { if vals[0].is_empty() { return Some(T::zero()); } @@ -1620,7 +1135,7 @@ make_instruction_clone!(vector_int, int, _sum, Vec, 1); make_instruction_clone!(vector_float, float, _sum, Vec, 1); /// Takes the mode of a vector -pub fn _mode(vals: Vec>) -> Option { +fn _mode(vals: Vec>) -> Option { if vals[0].is_empty() { return Some(T::zero()); } @@ -1638,7 +1153,7 @@ make_instruction_clone!(vector_int, int, _mode, Vec, 1); make_instruction_clone!(vector_float, float, _mode, Vec, 1); /// Adds the squares of all values in a vector and then takes the square root -pub fn _two_norm(vals: Vec>) -> Option { +fn _two_norm(vals: Vec>) -> Option { if vals[0].is_empty() { return Some(T::zero()); } @@ -1652,7 +1167,7 @@ make_instruction_clone!(vector_int, int, _two_norm, Vec, 1); make_instruction_clone!(vector_float, float, _two_norm, Vec, 1); /// Takes the cumulative sum of a vector -pub fn _cumulative_sum(vals: Vec>) -> Option> { +fn _cumulative_sum(vals: Vec>) -> Option> { if vals[0].is_empty() { return Some(vec![]); } @@ -1668,7 +1183,7 @@ make_instruction_clone!(vector_int, vector_int, _cumulative_sum, Vec, 1); make_instruction_clone!(vector_float, vector_float, _cumulative_sum, Vec, 1); /* /// Takes the cumulative mean of a vector -pub fn _cumulative_mean(vals: Vec>) -> Option> { +fn _cumulative_mean(vals: Vec>) -> Option> { if vals[0].is_empty() { return Some(vec![]); } @@ -1694,6 +1209,51 @@ make_instruction_clone!( 1 );*/ +macro_rules! make_vector_instructions { + ($stack:ident, $prim_stack:ident) => { + make_instruction_new!(_concat, $stack, $stack, $stack, $stack); + make_instruction_new!(_conj, $stack, $stack, $stack, $prim_stack); + make_instruction_new!(_conj_end, $stack, $stack, $stack, $prim_stack); + make_instruction_new!(_take_n, $stack, $stack, $stack, int); + make_instruction_new!(_take_last_n, $stack, $stack, $stack, int); + make_instruction_new!(_sub, $stack, $stack, $stack, int, int); + make_instruction_new!(_first, $stack, $prim_stack, $stack); + make_instruction_new!(_from_first_prim, $stack, $stack, $stack); + make_instruction_new!(_from_prim, $stack, $stack, $prim_stack); + make_instruction_new!(_last, $stack, $prim_stack, $stack); + make_instruction_new!(_from_last_prim, $stack, $stack, $stack); + make_instruction_new!(_nth, $stack, $prim_stack, $stack, int); + make_instruction_new!(_from_nth_prim, $stack, $stack, $stack, int); + make_instruction_new!(_rest, $stack, $stack, $stack); + make_instruction_new!(_but_last, $stack, $stack, $stack); + make_instruction_new!(_drop, $stack, $stack, $stack, int); + make_instruction_new!(_drop_last, $stack, $stack, $stack, int); + make_instruction_new!(_length, $stack, int, $stack); + make_instruction_new!(_reverse, $stack, $stack, $stack); + //make_instruction_new_aux!(_push_all, $stack, $prim_stack, $stack); + //make_instruction_empty!(_make_empty, $stack, $stack, $stack); + }; +} + +macro_rules! all_vector_instructions { + () => { + make_vector_instructions!(vector_int, int); + make_vector_instructions!(vector_float, float); + make_vector_instructions!(vector_string, string); + make_vector_instructions!(vector_boolean, boolean); + make_vector_instructions!(vector_char, char); + make_vector_instructions!(string, char); + + // Instructions that don't work on every stack + make_instruction_new_aux!(_push_all, vector_int, int, vector_int); + make_instruction_new_aux!(_push_all, vector_float, float, vector_float); + make_instruction_new_aux!(_push_all, vector_boolean, boolean, vector_boolean); + make_instruction_new_aux!(_push_all, vector_char, char, vector_char); + make_instruction_new_aux!(_push_all, string, char, string); + }; +} +all_vector_instructions!(); + #[cfg(test)] mod tests { use super::*; @@ -2069,6 +1629,15 @@ mod tests { assert_eq!(vec![vec![5, 4, 3, 2, 1, 0]], test_state.vector_int); } + #[test] + fn push_all_test() { + let mut test_state = EMPTY_STATE; + + test_state.vector_int = vec![vec![1, 2, 3]]; + vector_int_push_all(&mut test_state); + assert_eq!(vec![1, 2, 3], test_state.int); + } + #[test] fn make_empty_vec_test() { let mut test_state = EMPTY_STATE; diff --git a/tests/instruction_test.rs b/tests/instruction_test.rs index 901c64a..f1d5b61 100644 --- a/tests/instruction_test.rs +++ b/tests/instruction_test.rs @@ -13,6 +13,10 @@ fn two_stacks(x: i128, y: i128, cond: bool) -> Option { if cond { Some(x + y) } else { Some(x - y) } } +fn aux_char(ch: Vec) -> Option> { + Some(ch) +} + #[test] fn run_extract_test() { let mut test_state = EMPTY_STATE; @@ -34,4 +38,8 @@ fn run_extract_test() { test_state.int = vec![1, 2]; test_state.boolean = vec![true]; run_instruction!(two_stacks, int, test_state, int, int, boolean); + + test_state.vector_char = vec![vec!['a', 'b']]; + run_instruction!(aux_char, char, test_state, vector_char;); + assert_eq!(vec!['a', 'b'], test_state.char); }