use cypress::prelude::*;
#[derive(Debug, Clone, PartialEq)]
enum Instruction {
Left,
Right,
Increment,
Decrement,
Read,
Write,
Loop(Vec<Self>),
}
fn main() {
let parser = recursive(|expr| {
let instr = choice!(
select! {
'<' => Instruction::Left,
'>' => Instruction::Right,
'+' => Instruction::Increment,
'-' => Instruction::Decrement,
',' => Instruction::Read,
'.' => Instruction::Write,
},
expr.many()
.between(just('['), just(']'))
.map(Instruction::Loop)
);
Box::new(instr)
})
.many()
.until_end();
let input = b"+++++[>>+<<-]".into_input();
let expected_bf = vec![
Instruction::Increment,
Instruction::Increment,
Instruction::Increment,
Instruction::Increment,
Instruction::Increment,
Instruction::Loop(vec![
Instruction::Right,
Instruction::Right,
Instruction::Increment,
Instruction::Left,
Instruction::Left,
Instruction::Decrement,
]),
];
match parser.parse(input) {
Ok(PSuccess {
val: actual_bf,
rest: _,
}) => {
println!("{:?}", actual_bf);
assert_eq!(actual_bf, expected_bf)
}
Err(e) => println!("{}", e),
}
}