Compare commits

...

4 Commits

2 changed files with 199 additions and 48 deletions

View File

@ -7,3 +7,7 @@ a better language to develop this in rather than Haskell.
This is truly following the test-driven development model This is truly following the test-driven development model
so each of the files should have its own tests module. so each of the files should have its own tests module.
## Link for later
https://miroslavtushev.medium.com/does-my-sample-have-to-be-normally-distributed-for-a-t-test-7ee91aaaca2a

View File

@ -207,7 +207,7 @@ pub fn code_do_count(state: &mut PushState) {
Gene::GeneInt(count - 1), Gene::GeneInt(count - 1),
Gene::StateFunc(code_from_exec), Gene::StateFunc(code_from_exec),
code, code,
Gene::StateFunc(code_do_range) Gene::StateFunc(code_do_range),
]))); ])));
} }
@ -226,7 +226,7 @@ pub fn exec_do_count(state: &mut PushState) {
Gene::GeneInt(0), Gene::GeneInt(0),
Gene::GeneInt(count - 1), Gene::GeneInt(count - 1),
Gene::StateFunc(exec_do_range), Gene::StateFunc(exec_do_range),
code code,
]))); ])));
} }
@ -241,10 +241,7 @@ pub fn code_do_times(state: &mut PushState) {
} }
let code = state.code.pop().unwrap(); let code = state.code.pop().unwrap();
let times = state.int.pop().unwrap(); let times = state.int.pop().unwrap();
let nested_block = Gene::Block(Box::new(vec![ let nested_block = Gene::Block(Box::new(vec![Gene::StateFunc(int_pop), code]));
Gene::StateFunc(int_pop),
code,
]));
state.exec.push(Gene::Block(Box::new(vec![ state.exec.push(Gene::Block(Box::new(vec![
Gene::GeneInt(0), Gene::GeneInt(0),
Gene::GeneInt(times - 1), Gene::GeneInt(times - 1),
@ -264,10 +261,7 @@ pub fn exec_do_times(state: &mut PushState) {
} }
let code = state.exec.pop().unwrap(); let code = state.exec.pop().unwrap();
let times = state.int.pop().unwrap(); let times = state.int.pop().unwrap();
let nested_block = Gene::Block(Box::new(vec![ let nested_block = Gene::Block(Box::new(vec![Gene::StateFunc(int_pop), code]));
Gene::StateFunc(int_pop),
code,
]));
state.exec.push(Gene::Block(Box::new(vec![ state.exec.push(Gene::Block(Box::new(vec![
Gene::GeneInt(0), Gene::GeneInt(0),
Gene::GeneInt(times - 1), Gene::GeneInt(times - 1),
@ -321,11 +315,7 @@ pub fn code_map(state: &mut PushState) {
let mut contents = Vec::new(); let mut contents = Vec::new();
for item in c_vec.clone().into_iter() { for item in c_vec.clone().into_iter() {
let code_block = vec![ let code_block = vec![Gene::StateFunc(code_from_exec), item, e.clone()];
Gene::StateFunc(code_from_exec),
item,
e.clone(),
];
contents.push(Gene::Block(Box::new(code_block))); contents.push(Gene::Block(Box::new(code_block)));
} }
@ -341,7 +331,11 @@ pub fn code_map(state: &mut PushState) {
/// If top bool is true, execute top element of code/exec stack and skip the second. /// If top bool is true, execute top element of code/exec stack and skip the second.
/// If false, execute second element and skip the top. /// If false, execute second element and skip the top.
pub fn _if(vals: Vec<Gene>, auxs: Vec<bool>) -> Option<Gene> { pub fn _if(vals: Vec<Gene>, auxs: Vec<bool>) -> Option<Gene> {
Some(if auxs[0] { vals[0].clone() } else { vals[1].clone() }) Some(if auxs[0] {
vals[0].clone()
} else {
vals[1].clone()
})
} }
make_instruction_aux!(code, exec, _if, Gene, 2, boolean, 1, bool); make_instruction_aux!(code, exec, _if, Gene, 2, boolean, 1, bool);
make_instruction_aux!(exec, exec, _if, Gene, 2, boolean, 1, bool); make_instruction_aux!(exec, exec, _if, Gene, 2, boolean, 1, bool);
@ -380,6 +374,71 @@ pub fn _member(vals: Vec<Gene>) -> Option<bool> {
} }
make_instruction_clone!(code, boolean, _member, Gene, 2); make_instruction_clone!(code, boolean, _member, Gene, 2);
/// Pushes the nth item of the top element of the code stack.
/// If top code item isn't a block, wrap one around it.
pub fn _nth(vals: Vec<Gene>, auxs: Vec<i128>) -> Option<Gene> {
let gene_vec = match vals[0].clone() {
Gene::Block(val) => *val,
val => vec![val],
};
let gene_vec_len = gene_vec.len();
if gene_vec_len == 0 {
return None;
}
let ndx = auxs[0].abs() as usize % gene_vec_len;
Some(gene_vec[ndx].clone())
}
make_instruction_aux!(code, code, _nth, Gene, 1, int, 1, i128);
/// Pushes an empty block to the top of a stack.
pub fn _make_empty_block<T>(_: Vec<T>) -> Option<Gene> {
Some(Gene::Block(Box::new(vec![])))
}
make_instruction_clone!(code, code, _make_empty_block, Gene, 0);
make_instruction_clone!(exec, exec, _make_empty_block, Gene, 0);
/// Checks to see if the top item on the code/exec stack is an empty block.
/// True if is, False if not.
pub fn _is_empty_block(vals: Vec<Gene>) -> Option<bool> {
Some(match vals[0].clone() {
Gene::Block(val) => {
if val.is_empty() {
true
} else {
false
}
}
_ => false,
})
}
make_instruction_clone!(code, boolean, _is_empty_block, Gene, 1);
make_instruction_clone!(exec, boolean, _is_empty_block, Gene, 1);
/// Returns the size of the top item on the code/exec stack.
pub fn _size(vals: Vec<Gene>) -> Option<i128> {
Some(match vals[0].clone() {
Gene::Block(val) => val.len() as i128,
_ => 1,
})
}
make_instruction_clone!(code, int, _size, Gene, 1);
make_instruction_clone!(exec, int, _size, Gene, 1);
/// Returns a nested element inside a block based on an int.
pub fn _extract(vals: Vec<Gene>, auxs: Vec<i128>) -> Option<Gene> {
Some(match vals[0].clone() {
Gene::Block(val) => {
if *val.len() == 0 {
return None;
} else {
let ndx = (auxs[0] % *val.len()).abs();
// @TODO: Finish this later!
}
}
val => val,
})
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -728,10 +787,7 @@ mod tests {
test_state.boolean = vec![false, true, false, true, true, true]; test_state.boolean = vec![false, true, false, true, true, true];
test_state.int = vec![1, 1, 1, 1]; test_state.int = vec![1, 1, 1, 1];
test_state.exec = vec![ test_state.exec = vec![Gene::StateFunc(int_add), Gene::StateFunc(exec_while)];
Gene::StateFunc(int_add),
Gene::StateFunc(exec_while),
];
interpret_program(&mut test_state, STEP_LIMIT, MAX_STACK_SIZE); interpret_program(&mut test_state, STEP_LIMIT, MAX_STACK_SIZE);
assert_eq!(vec![4], test_state.int); assert_eq!(vec![4], test_state.int);
assert_eq!(vec![false, true], test_state.boolean); assert_eq!(vec![false, true], test_state.boolean);
@ -740,10 +796,7 @@ mod tests {
test_state.boolean = vec![false, true, false, true, true, false]; test_state.boolean = vec![false, true, false, true, true, false];
test_state.int = vec![1, 1, 1, 1]; test_state.int = vec![1, 1, 1, 1];
test_state.exec = vec![ test_state.exec = vec![Gene::StateFunc(int_add), Gene::StateFunc(exec_while)];
Gene::StateFunc(int_add),
Gene::StateFunc(exec_while),
];
interpret_program(&mut test_state, STEP_LIMIT, MAX_STACK_SIZE); interpret_program(&mut test_state, STEP_LIMIT, MAX_STACK_SIZE);
assert_eq!(vec![1, 1, 1, 1], test_state.int); assert_eq!(vec![1, 1, 1, 1], test_state.int);
} }
@ -754,10 +807,7 @@ mod tests {
test_state.boolean = vec![false, true, false, true, true, false]; test_state.boolean = vec![false, true, false, true, true, false];
test_state.int = vec![1, 1, 1, 1]; test_state.int = vec![1, 1, 1, 1];
test_state.exec = vec![ test_state.exec = vec![Gene::StateFunc(int_add), Gene::StateFunc(exec_do_while)];
Gene::StateFunc(int_add),
Gene::StateFunc(exec_do_while),
];
interpret_program(&mut test_state, STEP_LIMIT, MAX_STACK_SIZE); interpret_program(&mut test_state, STEP_LIMIT, MAX_STACK_SIZE);
assert_eq!(vec![1, 1, 2], test_state.int); assert_eq!(vec![1, 1, 2], test_state.int);
} }
@ -770,16 +820,12 @@ mod tests {
test_state.code = vec![Gene::GeneInt(5)]; test_state.code = vec![Gene::GeneInt(5)];
test_state.exec = vec![Gene::GeneInt(-1)]; test_state.exec = vec![Gene::GeneInt(-1)];
code_map(&mut test_state); code_map(&mut test_state);
test_state.exec = vec![ test_state.exec = vec![Gene::Block(Box::new(vec![Gene::Block(Box::new(vec![
Gene::Block(Box::new(vec![
Gene::Block(Box::new(vec![
Gene::StateFunc(code_from_exec), Gene::StateFunc(code_from_exec),
Gene::GeneInt(5), Gene::GeneInt(5),
Gene::GeneInt(-1), Gene::GeneInt(-1),
Gene::StateFunc(code_wrap_block) Gene::StateFunc(code_wrap_block),
])) ]))]))]
]))
]
} }
#[test] #[test]
@ -850,23 +896,29 @@ mod tests {
} }
#[test] #[test]
fn member_test() { fn code_exec_member_test() {
let mut test_state = EMPTY_STATE; let mut test_state = EMPTY_STATE;
test_state.code = vec![Gene::GeneInt(0), Gene::Block(Box::new(vec![ test_state.code = vec![
Gene::GeneInt(0),
Gene::Block(Box::new(vec![
Gene::GeneInt(0), Gene::GeneInt(0),
Gene::GeneInt(4), Gene::GeneInt(4),
Gene::StateFunc(exec_do_range), Gene::StateFunc(exec_do_range),
]))]; ])),
];
code_member(&mut test_state); code_member(&mut test_state);
assert_eq!(vec![true], test_state.boolean); assert_eq!(vec![true], test_state.boolean);
test_state.boolean.clear(); test_state.boolean.clear();
test_state.code = vec![Gene::GeneInt(0), Gene::Block(Box::new(vec![ test_state.code = vec![
Gene::GeneInt(0),
Gene::Block(Box::new(vec![
Gene::GeneInt(5), Gene::GeneInt(5),
Gene::GeneInt(4), Gene::GeneInt(4),
Gene::StateFunc(exec_do_range), Gene::StateFunc(exec_do_range),
]))]; ])),
];
code_member(&mut test_state); code_member(&mut test_state);
assert_eq!(vec![false], test_state.boolean); assert_eq!(vec![false], test_state.boolean);
test_state.boolean.clear(); test_state.boolean.clear();
@ -880,4 +932,99 @@ mod tests {
code_member(&mut test_state); code_member(&mut test_state);
assert_eq!(vec![false], test_state.boolean); assert_eq!(vec![false], test_state.boolean);
} }
#[test]
fn code_exec_nth_test() {
let mut test_state = EMPTY_STATE;
test_state.code = vec![Gene::Block(Box::new(vec![
Gene::GeneInt(0),
Gene::GeneInt(1),
Gene::GeneInt(2),
]))];
test_state.int = vec![1];
code_nth(&mut test_state);
assert_eq!(vec![Gene::GeneInt(1)], test_state.code);
test_state.code = vec![Gene::GeneInt(1)];
test_state.int = vec![0];
code_nth(&mut test_state);
assert_eq!(vec![Gene::GeneInt(1)], test_state.code);
test_state.code = vec![Gene::Block(Box::new(vec![
Gene::GeneInt(0),
Gene::GeneInt(1),
Gene::GeneInt(2),
]))];
test_state.int = vec![4];
code_nth(&mut test_state);
assert_eq!(vec![Gene::GeneInt(1)], test_state.code);
}
#[test]
fn code_exec_make_empty_block_test() {
let mut test_state = EMPTY_STATE;
test_state.code = vec![Gene::GeneInt(0)];
code_make_empty_block(&mut test_state);
let empty_vec: Vec<Gene> = Vec::new();
assert_eq!(
vec![Gene::GeneInt(0), Gene::Block(Box::new(empty_vec.clone()))],
test_state.code
);
test_state.code.clear();
exec_make_empty_block(&mut test_state);
assert_eq!(empty_vec, test_state.code);
}
#[test]
fn code_exec_is_empty_block_test() {
let mut test_state = EMPTY_STATE;
// code
test_state.code = vec![Gene::GeneInt(0)];
code_is_empty_block(&mut test_state);
assert_eq!(vec![false], test_state.boolean);
test_state.boolean.clear();
let empty_vec: Vec<Gene> = Vec::new();
test_state.code = vec![Gene::Block(Box::new(empty_vec.clone()))];
code_is_empty_block(&mut test_state);
assert_eq!(vec![true], test_state.boolean);
test_state.boolean.clear();
test_state.code = vec![Gene::Block(Box::new(vec![Gene::GeneBoolean(false)]))];
code_is_empty_block(&mut test_state);
assert_eq!(vec![false], test_state.boolean);
test_state.boolean.clear();
// exec
test_state.exec = vec![Gene::GeneInt(0)];
exec_is_empty_block(&mut test_state);
assert_eq!(vec![false], test_state.boolean);
test_state.boolean.clear();
let empty_vec: Vec<Gene> = Vec::new();
test_state.exec = vec![Gene::Block(Box::new(empty_vec.clone()))];
exec_is_empty_block(&mut test_state);
assert_eq!(vec![true], test_state.boolean);
}
#[test]
fn code_exec_size_test() {
let mut test_state = EMPTY_STATE;
test_state.code = vec![Gene::GeneInt(1)];
code_size(&mut test_state);
assert_eq!(vec![1], test_state.int);
test_state.int.clear();
test_state.code = vec![Gene::Block(Box::new(vec![
Gene::GeneBoolean(false),
Gene::GeneInt(42),
]))];
code_size(&mut test_state);
assert_eq!(vec![2], test_state.int);
}
} }