From 4102f5a35177da0f861daffff116b0aa7188085a Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 30 Sep 2024 21:07:24 +0200 Subject: [PATCH] upgrade marble value size to 32 bit --- README.md | 4 ++-- programs/adder.mbl | 4 ++-- src/main.rs | 24 ++++++++++++++++-------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5d26841..91f3a80 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # marble "programming language" -Source code is ascii art that describes a marble track. Marbles carry one byte of data each and move around interacting with parts of the board. Marbles can be created and destroyed arbitrarily by the various operators. +Marbles carry one unsigned 32 bit integer each and move around interacting with tiles on the board. Source code is ascii art that describes a marble machine. Marbles can be created and destroyed arbitrarily by the various operators. ## physics Marbles (`o`) start out moving down (unless emitted by something). @@ -25,7 +25,7 @@ P P - `B` Bag: consumes marbles that move into it, and emits an empty (0) marble when powered. - `ASMDR` Math: Add/Subtract/Multiply/Divide/Remainder of the marble on the left and the right, relative to the power direction. Outputs a marble forward if at least one input exists. - `LG=!` Logic gates: (Less/Greater/Equal/Not Equal) compares the left and right input (both treated as 0 if absent) when powered, and powers forward if the result is true. -- `P` Print: Prints the value of the front marble when powered +- `P` Print: Prints the value of the front marble when powered, truncated to a single byte. - `I` Input: Outputs a marble with the next byte from the program input stream, if there are any - `0123456789` Digits: decimal digits are added to a marbles value after multiplying the marble with 10, this means that a marble passing over `123` would contain the number 123 afterwards. Digits are consumed by marbles passing over them. diff --git a/programs/adder.mbl b/programs/adder.mbl index d01f5c4..4657b31 100644 --- a/programs/adder.mbl +++ b/programs/adder.mbl @@ -1,9 +1,9 @@ (print sum of 123 and 45) o - > 123 B + > 1230 B vo A-+ - > 45 B| + > 45898 B| o --+ > * B o diff --git a/src/main.rs b/src/main.rs index 7514e56..3a633e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use owo_colors::OwoColorize; mod grid; use grid::*; +type MarbleValue = u32; + #[derive(Debug)] struct Machine { grid: Grid, @@ -24,7 +26,7 @@ enum Tile { Comment(u8), Bag, Marble { - value: u8, + value: MarbleValue, dir: Direction, }, Trigger, @@ -230,12 +232,16 @@ impl Machine { } print!(" :: "); for (val, dir) in marbles_on_row { - print!("{}", format!("{}{:<3} ", dir.char(), val).magenta()) + print!("{}", format!("{}{} ", dir.char(), val).magenta()) } println!(); } - println!("output: {}", String::from_utf8_lossy(&self.output)); - println!("input: {}", String::from_utf8_lossy(&self.input)); + if !self.output.is_empty() { + println!("output: '{}'", String::from_utf8_lossy(&self.output)); + } + if !self.input.is_empty() { + println!("input: '{}'", String::from_utf8_lossy(&self.input)); + } println!("steps: {}", self.steps); } @@ -271,7 +277,9 @@ impl Machine { new_tile = Some(Tile::Blank); } Tile::Digit(d) => { - let new_val = value.wrapping_mul(10).wrapping_add(*d - b'0'); + let new_val = value + .wrapping_mul(10) + .wrapping_add((*d - b'0') as MarbleValue); *target = Tile::Marble { value: new_val, dir, @@ -378,7 +386,7 @@ impl Machine { Tile::Print => { let sample = self.grid.get(front_pos); if let Tile::Marble { value, dir: _ } = sample { - self.output.push(value); + self.output.push(value as u8); } } Tile::Bag => { @@ -389,7 +397,7 @@ impl Machine { } Tile::Input => { if self.input_index < self.input.len() && self.grid.get(front_pos).is_blank() { - let value = self.input[self.input_index]; + let value = self.input[self.input_index] as MarbleValue; *self.grid.get_mut(front_pos) = Tile::Marble { value, dir }; self.marbles.push(front_pos); self.input_index += 1; @@ -467,7 +475,7 @@ impl Machine { } impl Tile { - fn read_value(&self) -> u8 { + fn read_value(&self) -> MarbleValue { if let Tile::Marble { value, dir: _ } = self { *value } else {