Compare commits
3 Commits
a6094bbed4
...
8516575d9a
Author | SHA1 | Date | |
---|---|---|---|
8516575d9a | |||
0cbe372b98 | |||
2e6369fb0c |
@ -3,31 +3,31 @@ use std::ops::Not;
|
||||
use crate::push::state::{Gene, PushState};
|
||||
|
||||
/// 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] {
|
||||
Gene::Block(_) => true,
|
||||
_ => 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.
|
||||
fn _is_singular(vals: Vec<Gene>) -> Option<bool> {
|
||||
Some(_is_block(vals)?.not())
|
||||
fn _is_singular(vals: &Vec<&Gene>) -> Option<bool> {
|
||||
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
|
||||
fn _length(vals: Vec<Gene>) -> Option<i128> {
|
||||
fn _length(vals: &Vec<&Gene>) -> Option<i128> {
|
||||
Some(match &vals[0] {
|
||||
Gene::Block(x) => x.len() as i128,
|
||||
_ => 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
|
||||
fn _first(vals: Vec<Gene>) -> Option<Gene> {
|
||||
fn _first(vals: &Vec<&Gene>) -> Option<Gene> {
|
||||
match &vals[0] {
|
||||
Gene::Block(x) => {
|
||||
if x.len() > 1 {
|
||||
@ -39,10 +39,10 @@ fn _first(vals: Vec<Gene>) -> Option<Gene> {
|
||||
_ => 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
|
||||
fn _last(vals: Vec<Gene>) -> Option<Gene> {
|
||||
fn _last(vals: &Vec<&Gene>) -> Option<Gene> {
|
||||
match &vals[0] {
|
||||
Gene::Block(x) => {
|
||||
if x.len() > 1 {
|
||||
@ -54,10 +54,10 @@ fn _last(vals: Vec<Gene>) -> Option<Gene> {
|
||||
_ => 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
|
||||
fn _rest(vals: Vec<Gene>) -> Option<Gene> {
|
||||
fn _rest(vals: &Vec<&Gene>) -> Option<Gene> {
|
||||
match &vals[0] {
|
||||
Gene::Block(x) => {
|
||||
if x.len() > 1 {
|
||||
@ -69,10 +69,10 @@ fn _rest(vals: Vec<Gene>) -> Option<Gene> {
|
||||
_ => 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
|
||||
fn _but_last(vals: Vec<Gene>) -> Option<Gene> {
|
||||
fn _but_last(vals: &Vec<&Gene>) -> Option<Gene> {
|
||||
match &vals[0] {
|
||||
Gene::Block(x) => {
|
||||
let x_len = x.len();
|
||||
@ -85,13 +85,13 @@ fn _but_last(vals: Vec<Gene>) -> Option<Gene> {
|
||||
_ => 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
|
||||
fn _wrap_block(vals: Vec<Gene>) -> Option<Gene> {
|
||||
Some(Gene::Block(Box::new(vals)))
|
||||
fn _wrap_block(vals: &Vec<&Gene>) -> Option<Gene> {
|
||||
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.
|
||||
/// If the second gene is a block and the first one isn't,
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user