From 4b0444e979b983fe164941102db2cb13bdb9acbd Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 16 Dec 2023 23:16:23 +0100 Subject: [PATCH 1/2] add logical not operator --- README.md | 1 + src/main.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 286bb54..4bfada8 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ ## commands ``` +- 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 0..9 push number to stack /\ pop stack, reflect to the side if not zero diff --git a/src/main.rs b/src/main.rs index 259d4fc..b14e917 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ struct SandWormInterpreter { input_index: usize, output: Vec, state: State, + steps: usize, } #[derive(Debug, Default)] @@ -76,7 +77,7 @@ fn main() { match action.as_slice() { [] | ["step"] => interpreter.step_once(), ["step", num] => _ = num.parse().map(|n| interpreter.step(n)), - // ["run"] => interpreter.run(), + ["run"] => interpreter.run(), ["q" | "exit" | "quit"] => break, _ => println!("{}", "unrecognised command".red()), @@ -101,6 +102,13 @@ impl SandWormInterpreter { state: State::default(), direction: Direction::default(), 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) { - dbg!(&self); + // dbg!(&self); + print!("\x1B[2J"); // clear screen println!( "{:?}", self.worm.iter().map(|p| self.get(*p)).collect::>() @@ -145,6 +154,7 @@ impl SandWormInterpreter { } println!("output: {}", String::from_utf8_lossy(&self.output)); println!("input: {}", String::from_utf8_lossy(&self.input)); + println!("steps: {}", self.steps); } fn step_once(&mut self) { @@ -156,6 +166,7 @@ impl SandWormInterpreter { self.state = State::EndOfProgram; return; } + self.steps += 1; let instruction = self.get(front); 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(); self.worm_in.push(last_val); } + b'~' => { + let last_val = self.shrink(); + self.worm_in.push((last_val == 0) as u8); + } b'\\' => { let val = self.shrink(); if val != 0 { From dc2e3bc106e0fc8232e5da5b0ac669afc57aa7af Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 16 Dec 2023 23:17:01 +0100 Subject: [PATCH 2/2] add 99 bottles of beer example program --- programs/99_bottles_of_beer.worm | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 programs/99_bottles_of_beer.worm diff --git a/programs/99_bottles_of_beer.worm b/programs/99_bottles_of_beer.worm new file mode 100644 index 0000000..1f4c88f --- /dev/null +++ b/programs/99_bottles_of_beer.worm @@ -0,0 +1,5 @@ +v00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000@< +>6=+=+=+=+3+v>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^ +v <^!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_of_beer_on_the_wall=+55-:9_bottle_of_beer_on_the_wall+55-:9_bottle_of_beer+55Take_one_down,_pass_it_around+55No_bottles_of_beer_on_the_wall< +> 1v +\11!!!!!!!!!!!!!!!!!!!!!!!s_of_beer_on_the_wall=+55/~+-01=!!!!!!!_bottle"=+-01!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_bottles_of_beer+55Take_one_down,_pass_it_around+55"=!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_bottles_of_beer_on_the_wall+55"=/