From 2f0500c2d2730205dc60a4bdaceaa7e6e1ba0618 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 18 Apr 2025 01:21:43 -0500 Subject: [PATCH] optionify the code --- rush_macro/src/lib.rs | 6 +- rush_macro/src/utils/instruction.rs | 10 +- src/instructions/mod.rs | 450 +--------------------------- src/instructions/numeric.rs | 7 +- tests/instruction_test.rs | 8 +- 5 files changed, 22 insertions(+), 459 deletions(-) diff --git a/rush_macro/src/lib.rs b/rush_macro/src/lib.rs index 45b963f..8bf6864 100644 --- a/rush_macro/src/lib.rs +++ b/rush_macro/src/lib.rs @@ -24,10 +24,10 @@ mod utils; /// If there are not enough items in the stack to run the function, it /// will not be called. /// -/// An function with multiple outputs, for example this one: +/// A function with multiple outputs, for example this one: /// ``` -/// fn aux_iadd(x: i128, y: i128) -> Vec { -/// vec![x + y, x - y] +/// fn aux_iadd(x: i128, y: i128) -> Option> { +/// Some(vec![x + y, x - y]) /// } /// /// run_instruction!(aux_iadd, int, state, int, int;); diff --git a/rush_macro/src/utils/instruction.rs b/rush_macro/src/utils/instruction.rs index 1ffe818..e7ea2e0 100644 --- a/rush_macro/src/utils/instruction.rs +++ b/rush_macro/src/utils/instruction.rs @@ -76,12 +76,14 @@ impl ToTokens for Extract { let aux_run = match aux { true => quote! { - let result = #inner_func(#(#values),*); - #inner_state.#inner_out_stack.extend(result.iter()); + if let Some(result) = #inner_func(#(#values),*) { + #inner_state.#inner_out_stack.extend(result.iter()); + } }, false => quote! { - let result = #inner_func(#(#values),*); - #inner_state.#inner_out_stack.push(result); + if let Some(result) = #inner_func(#(#values),*) { + #inner_state.#inner_out_stack.push(result); + } }, }; diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index 67604bc..e8c6851 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -310,7 +310,7 @@ pub mod macros { ($func:ident, $prefix:ident, $out_stack:ident, $($stacks:ident), *) => { paste::item! { pub fn [< $prefix $func >] (state: &mut PushState) { - run_instruction!($func, $out_stack, state, $($stacks), *); + rush_macro::run_instruction!($func, $out_stack, state, $($stacks), *); } } }; @@ -322,7 +322,7 @@ pub mod macros { ($func:ident, $prefix:ident, $out_stack:ident, $($stacks:ident), *) => { paste::item! { pub fn [< $prefix $func >] (state: &mut PushState) { - run_instruction!($func, $out_stack, state, $($stacks), *, ;); + rush_macro::run_instruction!($func, $out_stack, state, $($stacks), *, ;); } } }; @@ -336,444 +336,6 @@ pub mod numeric; pub mod utils; pub mod vector; -// unsure how to procedurally read a file and put all functions -// into a vector. Probably need to use procedural macros, but I'm not there yet. -pub fn int_instructions() -> Vec { - vec![ - // numeric.rs - int_add, - int_sub, - int_mult, - int_div, - int_rem, - int_max, - int_min, - int_inc, - int_dec, - int_lt, - int_gt, - int_lte, - int_gte, - int_sin, - int_arcsin, - int_cos, - int_arccos, - int_tan, - int_arctan, - int_from_float, - int_from_boolean, - int_log, - int_exp, - int_sqrt, - int_inv, - int_abs, - int_sign_reverse, - int_square, - // common.rs - int_pop, - ] -} -pub fn float_instructions() -> Vec { - vec![ - // numeric - float_add, - float_sub, - float_mult, - float_div, - float_rem, - float_max, - float_min, - float_inc, - float_dec, - float_lt, - float_gt, - float_lte, - float_gte, - float_sin, - float_arcsin, - float_cos, - float_arccos, - float_tan, - float_arctan, - float_from_int, - float_from_boolean, - float_log, - float_exp, - float_sqrt, - float_inv, - float_abs, - float_sign_reverse, - float_square, - // common.rs - float_pop, - ] -} -pub fn string_instructions() -> Vec { - vec![ - // numeric.rs - string_concat, - string_conj, - string_conj_end, - string_take_n, - string_take_last_n, - string_sub, - string_first, - string_from_first_prim, - string_from_prim, - string_last, - string_from_last_prim, - string_nth, - string_from_nth_prim, - string_rest, - string_but_last, - string_drop, - string_drop_last, - string_length, - string_reverse, - string_push_all, - string_make_empty, - string_is_empty, - string_contains, - string_contains_vector_non_contiguous, - string_contains_vector_contiguous, - string_index_of, - string_index_of_vector, - string_occurrences_of, - string_occurrences_of_vector, - string_parse_to_prim, - string_set_nth, - string_split_on, - string_replace, - string_remove, - string_insert, - string_insert_vector, - // common.rs - string_pop, - ] -} -pub fn boolean_instructions() -> Vec { - vec![ - // logical.rs - boolean_and, - boolean_or, - boolean_not, - boolean_xor, - boolean_invert_first_then_and, - boolean_invert_second_then_and, - boolean_from_int, - boolean_from_float, - // common.rs - boolean_pop, - ] -} -pub fn char_instructions() -> Vec { - vec![ - // common.rs - char_pop, - ] -} -pub fn vector_int_instructions() -> Vec { - vec![ - // vector.rs - vector_int_concat, - vector_int_conj, - vector_int_conj_end, - vector_int_take_n, - vector_int_take_last_n, - vector_int_sub, - vector_int_first, - vector_int_from_first_prim, - vector_int_from_prim, - vector_int_last, - vector_int_from_last_prim, - vector_int_nth, - vector_int_from_nth_prim, - vector_int_rest, - vector_int_but_last, - vector_int_drop, - vector_int_drop_last, - vector_int_length, - vector_int_reverse, - vector_int_push_all, - vector_int_make_empty, - vector_int_is_empty, - vector_int_contains, - vector_int_contains_vector_non_contiguous, - vector_int_contains_vector_contiguous, - vector_int_index_of, - vector_int_index_of_vector, - vector_int_occurrences_of, - vector_int_occurrences_of_vector, - vector_int_parse_to_prim, - vector_int_set_nth, - vector_int_split_on, - vector_int_replace, - vector_int_remove, - vector_int_iterate, - vector_int_sort, - vector_int_sort_reverse, - vector_int_insert, - vector_int_insert_vector, - vector_int_mean, - vector_int_maximum, - vector_int_minimum, - vector_int_sum, - vector_int_mode, - vector_int_two_norm, - vector_int_cumulative_sum, - // common.rs - vector_int_pop, - ] -} -pub fn vector_float_instructions() -> Vec { - vec![ - // vector.rs - vector_float_concat, - vector_float_conj, - vector_float_conj_end, - vector_float_take_n, - vector_float_take_last_n, - vector_float_sub, - vector_float_first, - vector_float_from_first_prim, - vector_float_from_prim, - vector_float_last, - vector_float_from_last_prim, - vector_float_nth, - vector_float_from_nth_prim, - vector_float_rest, - vector_float_but_last, - vector_float_drop, - vector_float_drop_last, - vector_float_length, - vector_float_reverse, - vector_float_push_all, - vector_float_make_empty, - vector_float_is_empty, - vector_float_contains, - vector_float_contains_vector_non_contiguous, - vector_float_contains_vector_contiguous, - vector_float_index_of, - vector_float_index_of_vector, - vector_float_occurrences_of, - vector_float_occurrences_of_vector, - vector_float_parse_to_prim, - vector_float_set_nth, - vector_float_split_on, - vector_float_replace, - vector_float_remove, - vector_float_iterate, - vector_float_sort, - vector_float_sort_reverse, - vector_float_insert, - vector_float_insert_vector, - vector_float_mean, - vector_float_maximum, - vector_float_minimum, - vector_float_sum, - vector_float_mode, - vector_float_two_norm, - vector_float_cumulative_sum, - // common.rs - vector_float_pop, - ] -} -pub fn vector_string_instructions() -> Vec { - vec![ - // vector.rs - vector_string_concat, - vector_string_conj, - vector_string_conj_end, - vector_string_take_n, - vector_string_take_last_n, - vector_string_sub, - vector_string_first, - vector_string_from_first_prim, - vector_string_from_prim, - vector_string_last, - vector_string_from_last_prim, - vector_string_nth, - vector_string_from_nth_prim, - vector_string_rest, - vector_string_but_last, - vector_string_drop, - vector_string_drop_last, - vector_string_length, - vector_string_reverse, - vector_string_make_empty, - vector_string_is_empty, - vector_string_contains, - vector_string_contains_vector_non_contiguous, - vector_string_contains_vector_contiguous, - vector_string_index_of, - vector_string_index_of_vector, - vector_string_occurrences_of, - vector_string_occurrences_of_vector, - vector_string_parse_to_prim, - vector_string_set_nth, - vector_string_split_on, - vector_string_replace, - vector_string_remove, - vector_string_insert, - vector_string_insert_vector, - // common.rs - vector_string_pop, - ] -} -pub fn vector_boolean_instructions() -> Vec { - vec![ - // vector.rs - vector_boolean_concat, - vector_boolean_conj, - vector_boolean_conj_end, - vector_boolean_take_n, - vector_boolean_take_last_n, - vector_boolean_sub, - vector_boolean_first, - vector_boolean_from_first_prim, - vector_boolean_from_prim, - vector_boolean_last, - vector_boolean_from_last_prim, - vector_boolean_nth, - vector_boolean_from_nth_prim, - vector_boolean_rest, - vector_boolean_but_last, - vector_boolean_drop, - vector_boolean_drop_last, - vector_boolean_length, - vector_boolean_reverse, - vector_boolean_push_all, - vector_boolean_make_empty, - vector_boolean_is_empty, - vector_boolean_contains, - vector_boolean_contains_vector_non_contiguous, - vector_boolean_contains_vector_contiguous, - vector_boolean_index_of, - vector_boolean_index_of_vector, - vector_boolean_occurrences_of, - vector_boolean_occurrences_of_vector, - vector_boolean_parse_to_prim, - vector_boolean_set_nth, - vector_boolean_split_on, - vector_boolean_replace, - vector_boolean_remove, - vector_boolean_iterate, - vector_boolean_insert, - vector_boolean_insert_vector, - // common.rs - vector_boolean_pop, - ] -} -pub fn vector_char_instructions() -> Vec { - vec![ - // vector.rs - vector_char_concat, - vector_char_conj, - vector_char_conj_end, - vector_char_take_n, - vector_char_take_last_n, - vector_char_sub, - vector_char_first, - vector_char_from_first_prim, - vector_char_from_prim, - vector_char_last, - vector_char_from_last_prim, - vector_char_nth, - vector_char_from_nth_prim, - vector_char_rest, - vector_char_but_last, - vector_char_drop, - vector_char_drop_last, - vector_char_length, - vector_char_reverse, - vector_char_push_all, - vector_char_make_empty, - vector_char_is_empty, - vector_char_contains, - vector_char_contains_vector_non_contiguous, - vector_char_contains_vector_contiguous, - vector_char_index_of, - vector_char_index_of_vector, - vector_char_occurrences_of, - vector_char_occurrences_of_vector, - vector_char_parse_to_prim, - vector_char_set_nth, - vector_char_split_on, - vector_char_replace, - vector_char_remove, - vector_char_iterate, - vector_char_insert, - vector_char_insert_vector, - // common.rs - vector_char_pop, - ] -} -pub fn exec_instructions() -> Vec { - vec![ - // code.rs - exec_do_range, - exec_do_count, - exec_do_times, - exec_while, - exec_do_while, - exec_if, - exec_when, - exec_make_empty_block, - exec_is_empty_block, - exec_size, - // common.rs - exec_noop, - exec_noop_block, - exec_pop, - ] -} -pub fn code_instructions() -> Vec { - vec![ - // code.rs - code_is_block, - code_is_singular, - code_length, - code_first, - code_last, - code_rest, - code_but_last, - code_wrap_block, - code_combine, - code_do_then_pop, - code_do_range, - code_do_count, - code_do_times, - code_map, - code_if, - code_when, - code_member, - code_nth, - code_make_empty_block, - code_is_empty_block, - code_size, - code_extract, - code_insert, - code_insert, - code_first_position, - code_reverse, - // common.rs - code_noop, - code_noop_block, - code_pop, - code_from_int, - code_from_float, - code_from_string, - code_from_boolean, - code_from_char, - code_from_vector_int, - code_from_vector_float, - code_from_vector_string, - code_from_vector_boolean, - code_from_vector_char, - code_from_exec, - ] -} - #[cfg(test)] mod tests { use super::*; @@ -781,12 +343,12 @@ mod tests { #[test] fn make_instruction_new_test() { - fn _test_func(x: i128, y: i128) -> i128 { - x + y + fn _test_func(x: i128, y: i128) -> Option { + Some(x + y) } - fn _aux_test_func(x: i128, y: i128) -> Vec { - vec![x + y, x - y] + fn _aux_test_func(x: i128, y: i128) -> Option> { + Some(vec![x + y, x - y]) } let mut test_state = EMPTY_STATE; diff --git a/src/instructions/numeric.rs b/src/instructions/numeric.rs index 535ddfb..10a787c 100644 --- a/src/instructions/numeric.rs +++ b/src/instructions/numeric.rs @@ -13,14 +13,13 @@ use std::ops::{Add, Div, Mul, Sub}; use super::utils::{CastingTrait, NumericTrait}; /// Adds two addable values together. -fn _add(vals: Vec) -> Option +fn _add(b: T, a: T) -> Option where T: Add + Copy, { - Some(vals[1] + vals[0]) + Some(b + a) } -make_instruction!(int, int, _add, i128, 2); -make_instruction!(float, float, _add, Decimal, 2); +make_instruction_new!(_add, int, int, int, int); /// Subtracts two subtractable values from each other. fn _sub(vals: Vec) -> Option diff --git a/tests/instruction_test.rs b/tests/instruction_test.rs index 1eefdb4..86bdca9 100644 --- a/tests/instruction_test.rs +++ b/tests/instruction_test.rs @@ -1,12 +1,12 @@ use rush::push::state::EMPTY_STATE; use rush_macro::run_instruction; -fn iadd(x: i128, y: i128) -> i128 { - x + y +fn iadd(x: i128, y: i128) -> Option { + Some(x + y) } -fn aux_iadd(x: i128, y: i128) -> Vec { - vec![x + y, x - y] +fn aux_iadd(x: i128, y: i128) -> Option> { + Some(vec![x + y, x - y]) } #[test]