Compare commits

...

3 Commits

Author SHA1 Message Date
8516575d9a this is broken 2025-04-07 16:13:26 -05:00
0cbe372b98 reference macros 2025-04-07 15:42:08 -05:00
2e6369fb0c comment out doc comment 2025-04-07 15:41:32 -05:00
3 changed files with 76 additions and 19 deletions

View File

@ -3,31 +3,31 @@ use std::ops::Not;
use crate::push::state::{Gene, PushState}; use crate::push::state::{Gene, PushState};
/// Checks to see if a single gene is a block. /// Checks to see if a single gene is a block.
fn _is_block(vals: Vec<Gene>) -> Option<bool> { fn _is_block(vals: &Vec<&Gene>) -> Option<bool> {
Some(match vals[0] { Some(match vals[0] {
Gene::Block(_) => true, Gene::Block(_) => true,
_ => false, _ => false,
}) })
} }
make_instruction_clone!(code, boolean, _is_block, Gene, 1); make_instruction_ref!(code, boolean, _is_block, Gene, 1);
/// Checks to see if a single gene is not a block. /// Checks to see if a single gene is not a block.
fn _is_singular(vals: Vec<Gene>) -> Option<bool> { fn _is_singular(vals: &Vec<&Gene>) -> Option<bool> {
Some(_is_block(vals)?.not()) Some(_is_block(&vals)?.not())
} }
make_instruction_clone!(code, boolean, _is_singular, Gene, 1); make_instruction_ref!(code, boolean, _is_singular, Gene, 1);
/// Returns the length of a block, else 1 if not a block /// Returns the length of a block, else 1 if not a block
fn _length(vals: Vec<Gene>) -> Option<i128> { fn _length(vals: &Vec<&Gene>) -> Option<i128> {
Some(match &vals[0] { Some(match &vals[0] {
Gene::Block(x) => x.len() as i128, Gene::Block(x) => x.len() as i128,
_ => 1, _ => 1,
}) })
} }
make_instruction_clone!(code, int, _length, Gene, 1); make_instruction_ref!(code, int, _length, Gene, 1);
/// Returns the first item in a block if doable, else None /// Returns the first item in a block if doable, else None
fn _first(vals: Vec<Gene>) -> Option<Gene> { fn _first(vals: &Vec<&Gene>) -> Option<Gene> {
match &vals[0] { match &vals[0] {
Gene::Block(x) => { Gene::Block(x) => {
if x.len() > 1 { if x.len() > 1 {
@ -39,10 +39,10 @@ fn _first(vals: Vec<Gene>) -> Option<Gene> {
_ => None, _ => None,
} }
} }
make_instruction_clone!(code, code, _first, Gene, 1); make_instruction_ref!(code, code, _first, Gene, 1);
/// Returns the first item in a block if applicable, else None /// Returns the first item in a block if applicable, else None
fn _last(vals: Vec<Gene>) -> Option<Gene> { fn _last(vals: &Vec<&Gene>) -> Option<Gene> {
match &vals[0] { match &vals[0] {
Gene::Block(x) => { Gene::Block(x) => {
if x.len() > 1 { if x.len() > 1 {
@ -54,10 +54,10 @@ fn _last(vals: Vec<Gene>) -> Option<Gene> {
_ => None, _ => None,
} }
} }
make_instruction_clone!(code, code, _last, Gene, 1); make_instruction_ref!(code, code, _last, Gene, 1);
/// Returns all but the first code item in a block if applicable, else None /// Returns all but the first code item in a block if applicable, else None
fn _rest(vals: Vec<Gene>) -> Option<Gene> { fn _rest(vals: &Vec<&Gene>) -> Option<Gene> {
match &vals[0] { match &vals[0] {
Gene::Block(x) => { Gene::Block(x) => {
if x.len() > 1 { if x.len() > 1 {
@ -69,10 +69,10 @@ fn _rest(vals: Vec<Gene>) -> Option<Gene> {
_ => None, _ => None,
} }
} }
make_instruction_clone!(code, code, _rest, Gene, 1); make_instruction_ref!(code, code, _rest, Gene, 1);
/// Returns all but the first code item in a block if applicable, else None /// Returns all but the first code item in a block if applicable, else None
fn _but_last(vals: Vec<Gene>) -> Option<Gene> { fn _but_last(vals: &Vec<&Gene>) -> Option<Gene> {
match &vals[0] { match &vals[0] {
Gene::Block(x) => { Gene::Block(x) => {
let x_len = x.len(); let x_len = x.len();
@ -85,13 +85,13 @@ fn _but_last(vals: Vec<Gene>) -> Option<Gene> {
_ => None, _ => None,
} }
} }
make_instruction_clone!(code, code, _but_last, Gene, 1); make_instruction_ref!(code, code, _but_last, Gene, 1);
/// Returns all of the vals wrapped in a code block /// Returns all of the vals wrapped in a code block
fn _wrap_block(vals: Vec<Gene>) -> Option<Gene> { fn _wrap_block(vals: &Vec<&Gene>) -> Option<Gene> {
Some(Gene::Block(Box::new(vals))) Some(Gene::Block(Box::new(*vals)))
} }
make_instruction_clone!(code, code, _wrap_block, Gene, 1); make_instruction_ref!(code, code, _wrap_block, Gene, 1);
/// Combines two genes into one. Accounts for blocks. /// Combines two genes into one. Accounts for blocks.
/// If the second gene is a block and the first one isn't, /// If the second gene is a block and the first one isn't,

View File

@ -100,6 +100,63 @@ pub mod macros {
} }
}; };
} }
/// Attempt to make this function using a reference in the internal
/// function call rather than the inputs being moved into the function.
#[macro_export]
macro_rules! make_instruction_ref {
($in_stack:ident, $out_stack:ident, $fn_name:ident, $fn_type:ty, $fn_arity:stmt) => {
paste::item! {
/// Runs the $fn_name function on the top $fn_arity items from
/// the $in_stack and places the calculated value on the $out_stack.
pub fn [< $in_stack $fn_name >] (state: &mut PushState) {
let in_stack_len = state.$in_stack.len();
if in_stack_len < $fn_arity {
return;
}
let inputs: &mut Vec<&$fn_type> = &mut Vec::with_capacity($fn_arity);
for n in 1..=$fn_arity {
inputs.push(&state.$in_stack[in_stack_len - n]);
}
if let Some(result) = $fn_name(inputs) {
for _ in 0..$fn_arity {
state.$in_stack.pop();
}
state.$out_stack.push(result);
}
}
}
};
}
/// Attempt to make this function using a reference in the internal
/// function call rather than the inputs being moved into the function.
/// Names the function with the out_stack rather than the in_stack.
#[macro_export]
macro_rules! make_instruction_ref_out {
($in_stack:ident, $out_stack:ident, $fn_name:ident, $fn_type:ty, $fn_arity:stmt) => {
paste::item! {
/// Runs the $fn_name function on the top $fn_arity items from
/// the $in_stack and places the calculated value on the $out_stack.
pub fn [< $out_stack $fn_name >] (state: &mut PushState) {
let in_stack_len = state.$in_stack.len();
if in_stack_len < $fn_arity {
return;
}
let mut inputs: Vec<$fn_type> = Vec::with_capacity($fn_arity);
for n in 1..=$fn_arity {
inputs.push(state.$in_stack[in_stack_len - n]);
}
if let Some(result) = $fn_name(&inputs) {
for _ in 0..$fn_arity {
state.$in_stack.pop();
}
state.$out_stack.push(result);
}
}
}
};
}
} }
pub mod code; pub mod code;