Compare commits
3 Commits
748e96639a
...
5298941bb9
Author | SHA1 | Date | |
---|---|---|---|
5298941bb9 | |||
af9f3d1754 | |||
708a66c5cc |
@ -63,7 +63,7 @@ fn _rest(vals: Vec<Gene>) -> Option<Gene> {
|
||||
match &vals[0] {
|
||||
Gene::Block(x) => {
|
||||
if x.len() > 1 {
|
||||
Some(Gene::Block(Box::new(x[1..].to_vec())))
|
||||
Some(Gene::Block(x[1..].to_vec()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -79,7 +79,7 @@ fn _but_last(vals: Vec<Gene>) -> Option<Gene> {
|
||||
Gene::Block(x) => {
|
||||
let x_len = x.len();
|
||||
if x_len > 1 {
|
||||
Some(Gene::Block(Box::new(x[..x_len - 1].to_vec())))
|
||||
Some(Gene::Block(x[..x_len - 1].to_vec()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -91,7 +91,7 @@ make_instruction_clone!(code, code, _but_last, Gene, 1);
|
||||
|
||||
/// Returns all the vals wrapped in a code block
|
||||
fn _wrap_block(vals: Vec<Gene>) -> Option<Gene> {
|
||||
Some(Gene::Block(Box::new(vals)))
|
||||
Some(Gene::Block(vals))
|
||||
}
|
||||
make_instruction_clone!(code, code, _wrap_block, Gene, 1);
|
||||
|
||||
@ -116,7 +116,7 @@ fn _combine(vals: Vec<Gene>) -> Option<Gene> {
|
||||
y_clone.push(x.clone());
|
||||
Some(Gene::Block(y_clone))
|
||||
}
|
||||
(x, y) => Some(Gene::Block(Box::new(vec![x.clone(), y.clone()]))),
|
||||
(x, y) => Some(Gene::Block(vec![x.clone(), y.clone()])),
|
||||
}
|
||||
}
|
||||
make_instruction_clone!(code, code, _combine, Gene, 2);
|
||||
@ -150,13 +150,13 @@ pub fn code_do_range(state: &mut PushState) {
|
||||
}
|
||||
|
||||
if increment != 0 {
|
||||
state.exec.push(Gene::Block(Box::new(vec![
|
||||
state.exec.push(Gene::Block(vec![
|
||||
Gene::GeneInt(current_idx + increment),
|
||||
Gene::GeneInt(dest_idx),
|
||||
Gene::StateFunc(code_from_exec),
|
||||
to_do.clone(),
|
||||
Gene::StateFunc(code_do_range),
|
||||
])));
|
||||
]));
|
||||
}
|
||||
state.int.push(current_idx);
|
||||
state.exec.push(to_do);
|
||||
@ -180,12 +180,12 @@ pub fn exec_do_range(state: &mut PushState) {
|
||||
}
|
||||
|
||||
if increment != 0 {
|
||||
state.exec.push(Gene::Block(Box::new(vec![
|
||||
state.exec.push(Gene::Block(vec![
|
||||
Gene::GeneInt(current_idx + increment),
|
||||
Gene::GeneInt(dest_idx),
|
||||
Gene::StateFunc(exec_do_range),
|
||||
to_do.clone(),
|
||||
])));
|
||||
]));
|
||||
}
|
||||
state.int.push(current_idx);
|
||||
state.exec.push(to_do);
|
||||
@ -202,13 +202,13 @@ pub fn code_do_count(state: &mut PushState) {
|
||||
}
|
||||
let code = state.code.pop().unwrap();
|
||||
let count = state.int.pop().unwrap();
|
||||
state.exec.push(Gene::Block(Box::new(vec![
|
||||
state.exec.push(Gene::Block(vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::GeneInt(count - 1),
|
||||
Gene::StateFunc(code_from_exec),
|
||||
code,
|
||||
Gene::StateFunc(code_do_range),
|
||||
])));
|
||||
]));
|
||||
}
|
||||
|
||||
/// Evaluates the top item on the exec stack n times. N pulled from top
|
||||
@ -222,12 +222,12 @@ pub fn exec_do_count(state: &mut PushState) {
|
||||
}
|
||||
let code = state.exec.pop().unwrap();
|
||||
let count = state.int.pop().unwrap();
|
||||
state.exec.push(Gene::Block(Box::new(vec![
|
||||
state.exec.push(Gene::Block(vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::GeneInt(count - 1),
|
||||
Gene::StateFunc(exec_do_range),
|
||||
code,
|
||||
])));
|
||||
]));
|
||||
}
|
||||
|
||||
/// Evaluates the top item on the code stack n times but differently that
|
||||
@ -241,17 +241,17 @@ pub fn code_do_times(state: &mut PushState) {
|
||||
}
|
||||
let code = state.code.pop().unwrap();
|
||||
let times = state.int.pop().unwrap();
|
||||
let nested_block = Gene::Block(Box::new(vec![Gene::StateFunc(int_pop), code]));
|
||||
state.exec.push(Gene::Block(Box::new(vec![
|
||||
let nested_block = Gene::Block(vec![Gene::StateFunc(int_pop), code]);
|
||||
state.exec.push(Gene::Block(vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::GeneInt(times - 1),
|
||||
Gene::StateFunc(code_from_exec),
|
||||
nested_block,
|
||||
Gene::StateFunc(code_do_range),
|
||||
])));
|
||||
]));
|
||||
}
|
||||
|
||||
/// Evalutes the top item on the code stack n times. Also different :shrug:
|
||||
/// Evaluates the top item on the code stack n times, also different :shrug:
|
||||
pub fn exec_do_times(state: &mut PushState) {
|
||||
if state.exec.is_empty() || state.int.is_empty() {
|
||||
return;
|
||||
@ -261,13 +261,13 @@ pub fn exec_do_times(state: &mut PushState) {
|
||||
}
|
||||
let code = state.exec.pop().unwrap();
|
||||
let times = state.int.pop().unwrap();
|
||||
let nested_block = Gene::Block(Box::new(vec![Gene::StateFunc(int_pop), code]));
|
||||
state.exec.push(Gene::Block(Box::new(vec![
|
||||
let nested_block = Gene::Block(vec![Gene::StateFunc(int_pop), code]);
|
||||
state.exec.push(Gene::Block(vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::GeneInt(times - 1),
|
||||
Gene::StateFunc(exec_do_range),
|
||||
nested_block,
|
||||
])));
|
||||
]));
|
||||
}
|
||||
|
||||
/// Evaluates the top item on the exec stack until the top bool isn't true
|
||||
@ -308,7 +308,7 @@ pub fn code_map(state: &mut PushState) {
|
||||
let e = state.exec.pop().unwrap();
|
||||
let c = state.code.pop().unwrap();
|
||||
let c_vec = match c {
|
||||
Gene::Block(val) => *val,
|
||||
Gene::Block(val) => val,
|
||||
val => vec![val],
|
||||
};
|
||||
|
||||
@ -316,7 +316,7 @@ pub fn code_map(state: &mut PushState) {
|
||||
|
||||
for item in c_vec.clone().into_iter() {
|
||||
let code_block = vec![Gene::StateFunc(code_from_exec), item, e.clone()];
|
||||
contents.push(Gene::Block(Box::new(code_block)));
|
||||
contents.push(Gene::Block(code_block));
|
||||
}
|
||||
|
||||
contents.push(Gene::StateFunc(code_wrap_block));
|
||||
@ -325,7 +325,7 @@ pub fn code_map(state: &mut PushState) {
|
||||
contents.push(Gene::StateFunc(code_combine));
|
||||
}
|
||||
|
||||
state.exec.push(Gene::Block(Box::new(contents)));
|
||||
state.exec.push(Gene::Block(contents));
|
||||
}
|
||||
|
||||
/// If top bool is true, execute top element of code/exec stack and skip the second.
|
||||
@ -366,7 +366,7 @@ pub fn exec_when(state: &mut PushState) {
|
||||
/// If the first item isn't a block, coerced into one.
|
||||
pub fn _member(vals: Vec<Gene>) -> Option<bool> {
|
||||
let block = match vals[0].clone() {
|
||||
Gene::Block(val) => *val,
|
||||
Gene::Block(val) => val,
|
||||
val => vec![val],
|
||||
};
|
||||
|
||||
@ -378,7 +378,7 @@ make_instruction_clone!(code, boolean, _member, Gene, 2);
|
||||
/// 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,
|
||||
Gene::Block(val) => val,
|
||||
val => vec![val],
|
||||
};
|
||||
let gene_vec_len = gene_vec.len();
|
||||
@ -392,7 +392,7 @@ 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![])))
|
||||
Some(Gene::Block(vec![]))
|
||||
}
|
||||
make_instruction_clone!(code, code, _make_empty_block, Gene, 0);
|
||||
make_instruction_clone!(exec, exec, _make_empty_block, Gene, 0);
|
||||
@ -430,7 +430,7 @@ pub fn _extract(vals: Vec<Gene>, auxs: Vec<i128>) -> Option<Gene> {
|
||||
block @ Gene::Block(_) => {
|
||||
let block_len = block.rec_len();
|
||||
if block_len == 0 {
|
||||
return None;
|
||||
None
|
||||
} else {
|
||||
let ndx = (auxs[0] % block_len as i128).abs() as usize;
|
||||
Some(vals[0].clone().code_at_point(ndx)?)
|
||||
@ -447,7 +447,7 @@ make_instruction_aux!(code, code, _extract, Gene, 1, int, 1, i128);
|
||||
pub fn _insert(vals: Vec<Gene>, auxs: Vec<i128>) -> Option<Gene> {
|
||||
let mut block = match vals[0].clone() {
|
||||
iblock @ Gene::Block(_) => iblock,
|
||||
val => Gene::Block(Box::new(vec![val])),
|
||||
val => Gene::Block(vec![val]),
|
||||
};
|
||||
if block.rec_len() == 0 {
|
||||
return _combine(vec![block, vals[1].clone()]);
|
||||
@ -514,12 +514,12 @@ mod tests {
|
||||
fn is_block_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![]))];
|
||||
test_state.code = vec![Gene::Block(vec![])];
|
||||
code_is_block(&mut test_state);
|
||||
assert_eq!(vec![true], test_state.boolean);
|
||||
test_state.boolean.clear();
|
||||
|
||||
test_state.code = vec![(Gene::GeneInt(1))];
|
||||
test_state.code = vec![Gene::GeneInt(1)];
|
||||
code_is_block(&mut test_state);
|
||||
assert_eq!(vec![false], test_state.boolean);
|
||||
}
|
||||
@ -528,12 +528,12 @@ mod tests {
|
||||
fn is_singular_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![]))];
|
||||
test_state.code = vec![Gene::Block(vec![])];
|
||||
code_is_singular(&mut test_state);
|
||||
assert_eq!(vec![false], test_state.boolean);
|
||||
test_state.boolean.clear();
|
||||
|
||||
test_state.code = vec![(Gene::GeneInt(1))];
|
||||
test_state.code = vec![Gene::GeneInt(1)];
|
||||
code_is_singular(&mut test_state);
|
||||
assert_eq!(vec![true], test_state.boolean);
|
||||
}
|
||||
@ -542,15 +542,15 @@ mod tests {
|
||||
fn length_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
]))];
|
||||
])];
|
||||
code_length(&mut test_state);
|
||||
assert_eq!(vec![2], test_state.int);
|
||||
test_state.int.clear();
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![]))];
|
||||
test_state.code = vec![Gene::Block(vec![])];
|
||||
code_length(&mut test_state);
|
||||
assert_eq!(vec![0], test_state.int);
|
||||
test_state.int.clear();
|
||||
@ -564,10 +564,10 @@ mod tests {
|
||||
fn first_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
]))];
|
||||
])];
|
||||
code_first(&mut test_state);
|
||||
assert_eq!(vec![Gene::GeneInt(1)], test_state.code);
|
||||
|
||||
@ -586,10 +586,10 @@ mod tests {
|
||||
fn last_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
]))];
|
||||
])];
|
||||
code_last(&mut test_state);
|
||||
assert_eq!(vec![Gene::GeneFloat(dec!(3.8))], test_state.code);
|
||||
|
||||
@ -608,17 +608,17 @@ mod tests {
|
||||
fn rest_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
Gene::GeneBoolean(true),
|
||||
]))];
|
||||
])];
|
||||
code_rest(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
Gene::GeneBoolean(true)
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
|
||||
@ -637,17 +637,17 @@ mod tests {
|
||||
fn but_last_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
Gene::GeneBoolean(true),
|
||||
]))];
|
||||
])];
|
||||
code_but_last(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
|
||||
@ -668,58 +668,49 @@ mod tests {
|
||||
|
||||
test_state.code = vec![Gene::GeneInt(1)];
|
||||
code_wrap_block(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![Gene::GeneInt(1)]))],
|
||||
test_state.code
|
||||
);
|
||||
assert_eq!(vec![Gene::Block(vec![Gene::GeneInt(1)])], test_state.code);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combine_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state
|
||||
.code
|
||||
.push(Gene::Block(Box::new(vec![Gene::GeneInt(1)])));
|
||||
test_state.code.push(Gene::Block(Box::new(vec![
|
||||
test_state.code.push(Gene::Block(vec![Gene::GeneInt(1)]));
|
||||
test_state.code.push(Gene::Block(vec![
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
Gene::GeneBoolean(true),
|
||||
])));
|
||||
]));
|
||||
code_combine(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(3.8)),
|
||||
Gene::GeneBoolean(true),
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
test_state.code.clear();
|
||||
|
||||
test_state
|
||||
.code
|
||||
.push(Gene::Block(Box::new(vec![Gene::GeneInt(1)])));
|
||||
test_state.code.push(Gene::Block(vec![Gene::GeneInt(1)]));
|
||||
test_state.code.push(Gene::GeneFloat(dec!(4.0)));
|
||||
code_combine(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(4.0)),
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
test_state.code.clear();
|
||||
|
||||
test_state.code.push(Gene::GeneFloat(dec!(4.0)));
|
||||
test_state
|
||||
.code
|
||||
.push(Gene::Block(Box::new(vec![Gene::GeneInt(1)])));
|
||||
test_state.code.push(Gene::Block(vec![Gene::GeneInt(1)]));
|
||||
code_combine(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(4.0)),
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
test_state.code.clear();
|
||||
@ -728,10 +719,10 @@ mod tests {
|
||||
test_state.code.push(Gene::GeneChar('z'));
|
||||
code_combine(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneChar('z'),
|
||||
Gene::GeneFloat(dec!(4.0)),
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
}
|
||||
@ -879,12 +870,12 @@ mod tests {
|
||||
test_state.code = vec![Gene::GeneInt(5)];
|
||||
test_state.exec = vec![Gene::GeneInt(-1)];
|
||||
code_map(&mut test_state);
|
||||
test_state.exec = vec![Gene::Block(Box::new(vec![Gene::Block(Box::new(vec![
|
||||
test_state.exec = vec![Gene::Block(vec![Gene::Block(vec![
|
||||
Gene::StateFunc(code_from_exec),
|
||||
Gene::GeneInt(5),
|
||||
Gene::GeneInt(-1),
|
||||
Gene::StateFunc(code_wrap_block),
|
||||
]))]))]
|
||||
])])]
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -960,11 +951,11 @@ mod tests {
|
||||
|
||||
test_state.code = vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::GeneInt(4),
|
||||
Gene::StateFunc(exec_do_range),
|
||||
])),
|
||||
]),
|
||||
];
|
||||
code_member(&mut test_state);
|
||||
assert_eq!(vec![true], test_state.boolean);
|
||||
@ -972,11 +963,11 @@ mod tests {
|
||||
|
||||
test_state.code = vec![
|
||||
Gene::GeneInt(0),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(5),
|
||||
Gene::GeneInt(4),
|
||||
Gene::StateFunc(exec_do_range),
|
||||
])),
|
||||
]),
|
||||
];
|
||||
code_member(&mut test_state);
|
||||
assert_eq!(vec![false], test_state.boolean);
|
||||
@ -996,11 +987,11 @@ mod tests {
|
||||
fn code_exec_nth_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(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);
|
||||
@ -1010,11 +1001,11 @@ mod tests {
|
||||
code_nth(&mut test_state);
|
||||
assert_eq!(vec![Gene::GeneInt(1)], test_state.code);
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(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);
|
||||
@ -1028,7 +1019,7 @@ mod tests {
|
||||
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()))],
|
||||
vec![Gene::GeneInt(0), Gene::Block(empty_vec.clone())],
|
||||
test_state.code
|
||||
);
|
||||
test_state.code.clear();
|
||||
@ -1048,12 +1039,12 @@ mod tests {
|
||||
test_state.boolean.clear();
|
||||
|
||||
let empty_vec: Vec<Gene> = Vec::new();
|
||||
test_state.code = vec![Gene::Block(Box::new(empty_vec.clone()))];
|
||||
test_state.code = vec![Gene::Block(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)]))];
|
||||
test_state.code = vec![Gene::Block(vec![Gene::GeneBoolean(false)])];
|
||||
code_is_empty_block(&mut test_state);
|
||||
assert_eq!(vec![false], test_state.boolean);
|
||||
test_state.boolean.clear();
|
||||
@ -1065,7 +1056,7 @@ mod tests {
|
||||
test_state.boolean.clear();
|
||||
|
||||
let empty_vec: Vec<Gene> = Vec::new();
|
||||
test_state.exec = vec![Gene::Block(Box::new(empty_vec.clone()))];
|
||||
test_state.exec = vec![Gene::Block(empty_vec.clone())];
|
||||
exec_is_empty_block(&mut test_state);
|
||||
assert_eq!(vec![true], test_state.boolean);
|
||||
}
|
||||
@ -1079,10 +1070,10 @@ mod tests {
|
||||
assert_eq!(vec![1], test_state.int);
|
||||
test_state.int.clear();
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneBoolean(false),
|
||||
Gene::GeneInt(42),
|
||||
]))];
|
||||
])];
|
||||
code_size(&mut test_state);
|
||||
assert_eq!(vec![2], test_state.int);
|
||||
}
|
||||
@ -1091,17 +1082,17 @@ mod tests {
|
||||
fn extract_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
let test_code = vec![Gene::Block(Box::new(vec![
|
||||
let test_code = vec![Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(4),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
Gene::Block(Box::new(vec![Gene::GeneString(vec!['t'])])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneString(vec!['t'])]),
|
||||
]),
|
||||
Gene::GeneInt(10),
|
||||
Gene::Block(Box::new(vec![Gene::GeneBoolean(false)])),
|
||||
]))];
|
||||
Gene::Block(vec![Gene::GeneBoolean(false)]),
|
||||
])];
|
||||
|
||||
test_state.code = test_code.clone();
|
||||
test_state.int = vec![0];
|
||||
@ -1122,7 +1113,7 @@ mod tests {
|
||||
test_state.int = vec![9];
|
||||
code_extract(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![Gene::GeneBoolean(false)]))],
|
||||
vec![Gene::Block(vec![Gene::GeneBoolean(false)])],
|
||||
test_state.code
|
||||
);
|
||||
}
|
||||
@ -1133,47 +1124,47 @@ mod tests {
|
||||
|
||||
test_state.code = vec![
|
||||
Gene::GeneInt(20),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![Gene::GeneInt(1), Gene::GeneInt(1)])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneInt(1), Gene::GeneInt(1)]),
|
||||
]),
|
||||
];
|
||||
test_state.int = vec![1];
|
||||
let inserted_block = vec![Gene::Block(Box::new(vec![
|
||||
let inserted_block = vec![Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneInt(20),
|
||||
Gene::Block(Box::new(vec![Gene::GeneInt(1), Gene::GeneInt(1)])),
|
||||
]))];
|
||||
Gene::Block(vec![Gene::GeneInt(1), Gene::GeneInt(1)]),
|
||||
])];
|
||||
code_insert(&mut test_state);
|
||||
assert_eq!(inserted_block, test_state.code);
|
||||
|
||||
test_state.code = vec![
|
||||
Gene::GeneInt(20),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(4),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
Gene::Block(Box::new(vec![Gene::GeneString(vec!['t'])])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneString(vec!['t'])]),
|
||||
]),
|
||||
Gene::GeneInt(10),
|
||||
Gene::Block(Box::new(vec![Gene::GeneBoolean(false)])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneBoolean(false)]),
|
||||
]),
|
||||
];
|
||||
test_state.int = vec![4];
|
||||
let inserted_block = vec![Gene::Block(Box::new(vec![
|
||||
let inserted_block = vec![Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(4),
|
||||
Gene::GeneInt(20),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
Gene::Block(Box::new(vec![Gene::GeneString(vec!['t'])])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneString(vec!['t'])]),
|
||||
]),
|
||||
Gene::GeneInt(10),
|
||||
Gene::Block(Box::new(vec![Gene::GeneBoolean(false)])),
|
||||
]))];
|
||||
Gene::Block(vec![Gene::GeneBoolean(false)]),
|
||||
])];
|
||||
code_insert(&mut test_state);
|
||||
assert_eq!(inserted_block, test_state.code);
|
||||
}
|
||||
@ -1184,12 +1175,12 @@ mod tests {
|
||||
|
||||
test_state.code = vec![
|
||||
Gene::GeneInt(20),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneInt(20),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
])),
|
||||
]),
|
||||
];
|
||||
code_first_position(&mut test_state);
|
||||
assert_eq!(vec![2], test_state.int);
|
||||
@ -1207,12 +1198,12 @@ mod tests {
|
||||
|
||||
test_state.code = vec![
|
||||
Gene::GeneInt(-6),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneInt(20),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
])),
|
||||
]),
|
||||
];
|
||||
code_first_position(&mut test_state);
|
||||
assert_eq!(vec![-1], test_state.int);
|
||||
@ -1227,18 +1218,18 @@ mod tests {
|
||||
code_reverse(&mut test_state);
|
||||
assert_eq!(vec![Gene::GeneInt(1)], test_state.code);
|
||||
|
||||
test_state.code = vec![Gene::Block(Box::new(vec![
|
||||
test_state.code = vec![Gene::Block(vec![
|
||||
Gene::GeneBoolean(false),
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneInt(20),
|
||||
]))];
|
||||
])];
|
||||
code_reverse(&mut test_state);
|
||||
assert_eq!(
|
||||
vec![Gene::Block(Box::new(vec![
|
||||
vec![Gene::Block(vec![
|
||||
Gene::GeneInt(20),
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneBoolean(false),
|
||||
]))],
|
||||
])],
|
||||
test_state.code
|
||||
);
|
||||
}
|
||||
|
@ -9,6 +9,12 @@ fn _noop<T>(_: Vec<T>) -> Option<T> {
|
||||
make_instruction_clone!(code, code, _noop, Gene, 0);
|
||||
make_instruction_clone!(exec, exec, _noop, Gene, 0);
|
||||
|
||||
fn _noop_block<T>(_: Vec<T>) -> Option<T> {
|
||||
None
|
||||
}
|
||||
make_instruction_clone!(code, code, _noop_block, Gene, 0);
|
||||
make_instruction_clone!(exec, exec, _noop_block, Gene, 0);
|
||||
|
||||
/// Pops the top value from the stack
|
||||
fn _pop<T>(vals: Vec<T>) -> Option<T>
|
||||
where
|
||||
|
@ -76,19 +76,6 @@ where
|
||||
}
|
||||
make_instruction_out!(float, boolean, _from_float, Decimal, 1);
|
||||
|
||||
pub fn boolean_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vec![
|
||||
boolean_and,
|
||||
boolean_or,
|
||||
boolean_not,
|
||||
boolean_xor,
|
||||
boolean_invert_first_then_and,
|
||||
boolean_invert_second_then_and,
|
||||
boolean_from_int,
|
||||
boolean_from_float,
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -1,3 +1,10 @@
|
||||
use crate::instructions::code::*;
|
||||
use crate::instructions::common::*;
|
||||
use crate::instructions::logical::*;
|
||||
use crate::instructions::numeric::*;
|
||||
use crate::instructions::vector::*;
|
||||
use crate::push::state::PushState;
|
||||
|
||||
#[macro_use]
|
||||
pub mod macros {
|
||||
/// A macro that makes a push instruction given: the name of the input stack to use,
|
||||
@ -82,7 +89,7 @@ pub mod macros {
|
||||
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.
|
||||
#[allow(clippy::reversed_empty_ranges)]
|
||||
#[allow(clippy::reversed_empty_ranges, unused_comparisons)]
|
||||
pub fn [< $in_stack $fn_name >] (state: &mut PushState) {
|
||||
let in_stack_len = state.$in_stack.len();
|
||||
if in_stack_len < $fn_arity {
|
||||
@ -137,6 +144,7 @@ pub mod macros {
|
||||
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.
|
||||
#[allow(unused_comparisons)]
|
||||
pub fn [< $in_stack $fn_name >] (state: &mut PushState) {
|
||||
let in_stack_len = state.$in_stack.len();
|
||||
if in_stack_len < $fn_arity {
|
||||
@ -264,3 +272,359 @@ pub mod logical;
|
||||
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<fn(&mut PushState)> {
|
||||
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<fn(&mut PushState)> {
|
||||
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<fn(&mut PushState)> {
|
||||
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_length,
|
||||
string_reverse,
|
||||
string_push_all,
|
||||
string_make_empty,
|
||||
string_is_empty,
|
||||
string_contains,
|
||||
string_index_of,
|
||||
string_occurrences_of,
|
||||
string_set_nth,
|
||||
string_replace,
|
||||
// common.rs
|
||||
string_pop,
|
||||
]
|
||||
}
|
||||
pub fn boolean_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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<fn(&mut PushState)> {
|
||||
vec![
|
||||
// common.rs
|
||||
char_pop,
|
||||
]
|
||||
}
|
||||
pub fn vector_int_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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_length,
|
||||
vector_int_reverse,
|
||||
vector_int_push_all,
|
||||
vector_int_make_empty,
|
||||
vector_int_is_empty,
|
||||
vector_int_contains,
|
||||
vector_int_index_of,
|
||||
vector_int_occurrences_of,
|
||||
vector_int_set_nth,
|
||||
vector_int_replace,
|
||||
// common.rs
|
||||
vector_int_pop,
|
||||
]
|
||||
}
|
||||
pub fn vector_float_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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_length,
|
||||
vector_float_reverse,
|
||||
vector_float_push_all,
|
||||
vector_float_make_empty,
|
||||
vector_float_is_empty,
|
||||
vector_float_contains,
|
||||
vector_float_index_of,
|
||||
vector_float_occurrences_of,
|
||||
vector_float_set_nth,
|
||||
vector_float_replace,
|
||||
// common.rs
|
||||
vector_float_pop,
|
||||
]
|
||||
}
|
||||
pub fn vector_string_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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_length,
|
||||
vector_string_reverse,
|
||||
vector_string_make_empty,
|
||||
vector_string_is_empty,
|
||||
vector_string_contains,
|
||||
vector_string_index_of,
|
||||
vector_string_occurrences_of,
|
||||
vector_string_set_nth,
|
||||
vector_string_replace,
|
||||
// common.rs
|
||||
vector_string_pop,
|
||||
]
|
||||
}
|
||||
pub fn vector_boolean_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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_length,
|
||||
vector_boolean_reverse,
|
||||
vector_boolean_push_all,
|
||||
vector_boolean_make_empty,
|
||||
vector_boolean_is_empty,
|
||||
vector_boolean_contains,
|
||||
vector_boolean_index_of,
|
||||
vector_boolean_occurrences_of,
|
||||
vector_boolean_set_nth,
|
||||
vector_boolean_replace,
|
||||
// common.rs
|
||||
vector_boolean_pop,
|
||||
]
|
||||
}
|
||||
pub fn vector_char_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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_length,
|
||||
vector_char_reverse,
|
||||
vector_char_push_all,
|
||||
vector_char_make_empty,
|
||||
vector_char_is_empty,
|
||||
vector_char_contains,
|
||||
vector_char_index_of,
|
||||
vector_char_occurrences_of,
|
||||
vector_char_set_nth,
|
||||
vector_char_replace,
|
||||
// common.rs
|
||||
vector_char_pop,
|
||||
]
|
||||
}
|
||||
pub fn exec_instructions() -> Vec<fn(&mut PushState)> {
|
||||
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<fn(&mut PushState)> {
|
||||
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,
|
||||
]
|
||||
}
|
||||
|
@ -303,75 +303,6 @@ where
|
||||
make_instruction!(int, int, _square, i128, 1);
|
||||
make_instruction!(float, float, _square, Decimal, 1);
|
||||
|
||||
/// A list of all of the defined int functions in this file.
|
||||
/// Must manually register functions in this list if added.
|
||||
pub fn int_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vec![
|
||||
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,
|
||||
]
|
||||
}
|
||||
|
||||
/// All of the float instructions declared in this file.
|
||||
pub fn float_instructions() -> Vec<fn(&mut PushState)> {
|
||||
vec![
|
||||
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,
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -1326,7 +1326,6 @@ mod tests {
|
||||
#[test]
|
||||
fn contains_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
let empty_vec: Vec<i128> = vec![];
|
||||
|
||||
test_state.vector_int = vec![vec![0, 1, 2, 3, 4, 5]];
|
||||
test_state.int = vec![2];
|
||||
|
69
src/main.rs
69
src/main.rs
@ -1,57 +1,24 @@
|
||||
use crate::instructions::vector::_replace;
|
||||
use crate::push::state::{EMPTY_STATE, PushState};
|
||||
use instructions::utils::NumericTrait;
|
||||
use rust_decimal::MathematicalOps;
|
||||
use rust_decimal::prelude::*;
|
||||
use crate::instructions::*;
|
||||
use crate::push::interpreter::interpret_program;
|
||||
use crate::push::state::EMPTY_STATE;
|
||||
|
||||
mod instructions;
|
||||
mod push;
|
||||
|
||||
fn test_func() {}
|
||||
fn another_test_func() {}
|
||||
|
||||
fn main() {
|
||||
// let sixth_pi = Decimal::PI / dec!(6.0);
|
||||
// let result = dec!(1).sin();
|
||||
// let result = Decimal::PI.sin().checked_div(Decimal::PI.cos());
|
||||
// let result = dec!(1.0) / Decimal::HALF_PI.sin();
|
||||
// let result = sixth_pi.sin();
|
||||
// let result = Decimal::HALF_PI.cos();
|
||||
// let result = Decimal::PI.sin();
|
||||
// let result = Decimal::PI.tan();
|
||||
// let result = dec!(1.0) / Decimal::QUARTER_PI.tan();
|
||||
// let result = dec!(1.0) / Decimal::QUARTER_PI.cos();
|
||||
// let result = dec!(1.2).checked_exp();
|
||||
// let result = dec!(2).log10();
|
||||
/*let result = vec![0, 1, 2];
|
||||
let r_len = result.len();
|
||||
let fin_result = &result[..r_len - 1];
|
||||
println!("{fin_result:?}");*/
|
||||
|
||||
// println!("{result:?}");
|
||||
// println!("{sixth_pi}");
|
||||
|
||||
// casting a function call to a usize is a way to
|
||||
// test for function equality.
|
||||
// let test_func_result = test_func as usize == test_func as usize;
|
||||
// println!("{test_func_result}");
|
||||
|
||||
//let temp_vec = vec![0, 1, 2, 3];
|
||||
//temp_vec[0..9].to_vec();
|
||||
|
||||
//let res = 3 % 2;
|
||||
//println!("res is {res}");
|
||||
|
||||
//let mut test_vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
//test_vec.drain(..15);
|
||||
//println!("{:?}", test_vec);
|
||||
|
||||
//let test_state = EMPTY_STATE;
|
||||
//println!("{}", test_state.int == test_state.boolean);
|
||||
/*println!(
|
||||
"{}",
|
||||
std::any::type_name::<PushState>() == std::any::type_name::<PushState>()
|
||||
);*/
|
||||
|
||||
let temp: Option<Vec<i128>> = _replace(vec![vec![1, 2, 3]], vec![1, 2]);
|
||||
// These need to stay so linter doesn't go crazy.
|
||||
int_instructions();
|
||||
float_instructions();
|
||||
string_instructions();
|
||||
boolean_instructions();
|
||||
char_instructions();
|
||||
vector_int_instructions();
|
||||
vector_float_instructions();
|
||||
vector_string_instructions();
|
||||
vector_boolean_instructions();
|
||||
vector_char_instructions();
|
||||
exec_instructions();
|
||||
code_instructions();
|
||||
let mut empty_state = EMPTY_STATE;
|
||||
interpret_program(&mut empty_state, 1000, 1000);
|
||||
}
|
||||
|
@ -26,15 +26,50 @@ pub fn gene_to_stack(state: &mut PushState, gene: Gene) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensures the stacks don't go over a set size. Removes the oldest values first.
|
||||
/// Unsure how to make this function prettier yet.
|
||||
macro_rules! ensure_boundaries {
|
||||
($state:expr, $max_stack_size:expr, $stack:ident) => {
|
||||
if $state.$stack.len() > $max_stack_size {
|
||||
$state
|
||||
.$stack
|
||||
.drain(0..($state.$stack.len() - $max_stack_size));
|
||||
}
|
||||
};
|
||||
($state:expr, $max_stack_size:expr, $stack:ident, $($other_stacks:ident), +) => {
|
||||
if $state.$stack.len() > $max_stack_size {
|
||||
$state
|
||||
.$stack
|
||||
.drain(0..($state.$stack.len() - $max_stack_size));
|
||||
}
|
||||
ensure_boundaries!($state, $max_stack_size, $($other_stacks), +);
|
||||
};
|
||||
}
|
||||
|
||||
/// Where a push program's exec stack is interpreted to completion.
|
||||
/// TODO: Decide where to place loading in a push program.
|
||||
pub fn interpret_program(state: &mut PushState, step_limit: usize, max_stack_size: usize) {
|
||||
let mut steps: usize = 0;
|
||||
while state.exec.len() > 0 && steps < step_limit {
|
||||
while !state.exec.is_empty() && steps < step_limit {
|
||||
if let Some(gene) = state.exec.pop() {
|
||||
gene_to_stack(state, gene);
|
||||
steps += 1;
|
||||
}
|
||||
// If adding any more stacks in the future, must be added to this list
|
||||
ensure_boundaries!(
|
||||
state,
|
||||
max_stack_size,
|
||||
int,
|
||||
float,
|
||||
string,
|
||||
boolean,
|
||||
char,
|
||||
vector_int,
|
||||
vector_float,
|
||||
vector_string,
|
||||
vector_boolean,
|
||||
vector_char
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,20 +161,14 @@ mod tests {
|
||||
);
|
||||
test_state.vector_char.clear();
|
||||
|
||||
let test_block: Gene = Gene::Block(Box::new(vec![
|
||||
let test_block: Gene = Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneFloat(dec!(2.3)),
|
||||
Gene::StateFunc(int_add),
|
||||
]));
|
||||
]);
|
||||
test_state.exec.push(Gene::GeneInt(2));
|
||||
gene_to_stack(&mut test_state, test_block);
|
||||
assert_eq!(
|
||||
//vec![
|
||||
// Gene::GeneInt(2),
|
||||
// Gene::GeneInt(1),
|
||||
// Gene::GeneFloat(dec!(2.3)),
|
||||
// Gene::StateFunc(int_add)
|
||||
//],
|
||||
vec![
|
||||
Gene::GeneInt(2),
|
||||
Gene::StateFunc(int_add),
|
||||
@ -166,4 +195,15 @@ mod tests {
|
||||
interpret_program(&mut test_state, 1000, 1000);
|
||||
assert_eq!(vec![9], test_state.int);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boundary_test() {
|
||||
let mut test_state = EMPTY_STATE;
|
||||
|
||||
test_state.int = vec![1, 2, 3, 4, 5];
|
||||
test_state.char = vec!['a', 'b', 'c', 'd', 'f', 'z', 'g'];
|
||||
ensure_boundaries!(test_state, 3, int, char);
|
||||
assert_eq!(vec!['f', 'z', 'g'], test_state.char);
|
||||
assert_eq!(vec![3, 4, 5], test_state.int);
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ pub const EMPTY_STATE: PushState = PushState {
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
#[allow(dead_code)] // remove this later. Is here bc Close, Skip, CrossoverPadding
|
||||
pub enum Gene {
|
||||
GeneInt(i128),
|
||||
GeneFloat(Decimal),
|
||||
@ -51,7 +52,7 @@ pub enum Gene {
|
||||
Close,
|
||||
Open(u8),
|
||||
Skip,
|
||||
Block(Box<Vec<Gene>>),
|
||||
Block(Vec<Gene>),
|
||||
CrossoverPadding,
|
||||
}
|
||||
|
||||
@ -157,66 +158,66 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn rec_len_test() {
|
||||
let block = Gene::Block(Box::new(vec![
|
||||
let block = Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![Gene::GeneInt(1), Gene::GeneInt(1)])),
|
||||
]));
|
||||
Gene::Block(vec![Gene::GeneInt(1), Gene::GeneInt(1)]),
|
||||
]);
|
||||
assert_eq!(4, block.rec_len());
|
||||
|
||||
let block = Gene::Block(Box::new(vec![
|
||||
let block = Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(4),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
Gene::Block(Box::new(vec![Gene::GeneString(vec!['t'])])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneString(vec!['t'])]),
|
||||
]),
|
||||
Gene::GeneInt(10),
|
||||
Gene::Block(Box::new(vec![Gene::GeneBoolean(false)])),
|
||||
]));
|
||||
Gene::Block(vec![Gene::GeneBoolean(false)]),
|
||||
]);
|
||||
assert_eq!(10, block.rec_len());
|
||||
|
||||
let block = Gene::Block(Box::new(vec![]));
|
||||
let block = Gene::Block(vec![]);
|
||||
assert_eq!(0, block.rec_len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_test() {
|
||||
let mut block = Gene::Block(Box::new(vec![
|
||||
let mut block = Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![Gene::GeneInt(1), Gene::GeneInt(1)])),
|
||||
]));
|
||||
let inserted_block = Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![Gene::GeneInt(1), Gene::GeneInt(1)]),
|
||||
]);
|
||||
let inserted_block = Gene::Block(vec![
|
||||
Gene::GeneInt(1),
|
||||
Gene::GeneInt(20),
|
||||
Gene::Block(Box::new(vec![Gene::GeneInt(1), Gene::GeneInt(1)])),
|
||||
]));
|
||||
Gene::Block(vec![Gene::GeneInt(1), Gene::GeneInt(1)]),
|
||||
]);
|
||||
block.with_code_inserted_at_point(Gene::GeneInt(20), 1);
|
||||
assert_eq!(inserted_block, block);
|
||||
|
||||
let mut block = Gene::Block(Box::new(vec![
|
||||
let mut block = Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(4),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
Gene::Block(Box::new(vec![Gene::GeneString(vec!['t'])])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneString(vec!['t'])]),
|
||||
]),
|
||||
Gene::GeneInt(10),
|
||||
Gene::Block(Box::new(vec![Gene::GeneBoolean(false)])),
|
||||
]));
|
||||
let inserted_block = Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![Gene::GeneBoolean(false)]),
|
||||
]);
|
||||
let inserted_block = Gene::Block(vec![
|
||||
Gene::GeneBoolean(true),
|
||||
Gene::GeneInt(1),
|
||||
Gene::Block(Box::new(vec![
|
||||
Gene::Block(vec![
|
||||
Gene::GeneInt(4),
|
||||
Gene::GeneInt(20),
|
||||
Gene::GeneFloat(dec!(6.0)),
|
||||
Gene::Block(Box::new(vec![Gene::GeneString(vec!['t'])])),
|
||||
])),
|
||||
Gene::Block(vec![Gene::GeneString(vec!['t'])]),
|
||||
]),
|
||||
Gene::GeneInt(10),
|
||||
Gene::Block(Box::new(vec![Gene::GeneBoolean(false)])),
|
||||
]));
|
||||
Gene::Block(vec![Gene::GeneBoolean(false)]),
|
||||
]);
|
||||
block.with_code_inserted_at_point(Gene::GeneInt(20), 4);
|
||||
assert_eq!(inserted_block, block);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user