add logical not operator

This commit is contained in:
Crispy 2023-12-16 23:16:23 +01:00
parent e70580785f
commit 4b0444e979
2 changed files with 18 additions and 2 deletions

View file

@ -8,6 +8,7 @@
## commands ## commands
``` ```
+- pop 2 values, push sum/difference (uses the order they are popped, so `0-` negates the top of the stack) +- pop 2 values, push sum/difference (uses the order they are popped, so `0-` negates the top of the stack)
~ logical not (0 becomes 1, nonzero becomes 0)
><^v change direction ><^v change direction
0..9 push number to stack 0..9 push number to stack
/\ pop stack, reflect to the side if not zero /\ pop stack, reflect to the side if not zero

View file

@ -18,6 +18,7 @@ struct SandWormInterpreter {
input_index: usize, input_index: usize,
output: Vec<u8>, output: Vec<u8>,
state: State, state: State,
steps: usize,
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -76,7 +77,7 @@ fn main() {
match action.as_slice() { match action.as_slice() {
[] | ["step"] => interpreter.step_once(), [] | ["step"] => interpreter.step_once(),
["step", num] => _ = num.parse().map(|n| interpreter.step(n)), ["step", num] => _ = num.parse().map(|n| interpreter.step(n)),
// ["run"] => interpreter.run(), ["run"] => interpreter.run(),
["q" | "exit" | "quit"] => break, ["q" | "exit" | "quit"] => break,
_ => println!("{}", "unrecognised command".red()), _ => println!("{}", "unrecognised command".red()),
@ -101,6 +102,13 @@ impl SandWormInterpreter {
state: State::default(), state: State::default(),
direction: Direction::default(), direction: Direction::default(),
input_index: 0, input_index: 0,
steps: 0,
}
}
fn run(&mut self) {
while self.state == State::Running {
self.step_once();
} }
} }
@ -114,7 +122,8 @@ impl SandWormInterpreter {
} }
fn show(&self) { fn show(&self) {
dbg!(&self); // dbg!(&self);
print!("\x1B[2J"); // clear screen
println!( println!(
"{:?}", "{:?}",
self.worm.iter().map(|p| self.get(*p)).collect::<Vec<_>>() self.worm.iter().map(|p| self.get(*p)).collect::<Vec<_>>()
@ -145,6 +154,7 @@ impl SandWormInterpreter {
} }
println!("output: {}", String::from_utf8_lossy(&self.output)); println!("output: {}", String::from_utf8_lossy(&self.output));
println!("input: {}", String::from_utf8_lossy(&self.input)); println!("input: {}", String::from_utf8_lossy(&self.input));
println!("steps: {}", self.steps);
} }
fn step_once(&mut self) { fn step_once(&mut self) {
@ -156,6 +166,7 @@ impl SandWormInterpreter {
self.state = State::EndOfProgram; self.state = State::EndOfProgram;
return; return;
} }
self.steps += 1;
let instruction = self.get(front); let instruction = self.get(front);
let mut dont_push_instruction = false; let mut dont_push_instruction = false;
@ -202,6 +213,10 @@ impl SandWormInterpreter {
let last_val = self.worm.last().map(|&p| self.get(p)).unwrap_or_default(); let last_val = self.worm.last().map(|&p| self.get(p)).unwrap_or_default();
self.worm_in.push(last_val); self.worm_in.push(last_val);
} }
b'~' => {
let last_val = self.shrink();
self.worm_in.push((last_val == 0) as u8);
}
b'\\' => { b'\\' => {
let val = self.shrink(); let val = self.shrink();
if val != 0 { if val != 0 {