upgrade marble value size to 32 bit

This commit is contained in:
Crispy 2024-09-30 21:07:24 +02:00
parent b9009021ec
commit 4102f5a351
3 changed files with 20 additions and 12 deletions

View file

@ -1,5 +1,5 @@
# marble "programming language" # 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 ## physics
Marbles (`o`) start out moving down (unless emitted by something). 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. - `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. - `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. - `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 - `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. - `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.

View file

@ -1,9 +1,9 @@
(print sum of 123 and 45) (print sum of 123 and 45)
o o
> 123 B > 1230 B
vo A-+ vo A-+
> 45 B| > 45898 B|
o --+ o --+
> * B > * B
o o

View file

@ -5,6 +5,8 @@ use owo_colors::OwoColorize;
mod grid; mod grid;
use grid::*; use grid::*;
type MarbleValue = u32;
#[derive(Debug)] #[derive(Debug)]
struct Machine { struct Machine {
grid: Grid<Tile>, grid: Grid<Tile>,
@ -24,7 +26,7 @@ enum Tile {
Comment(u8), Comment(u8),
Bag, Bag,
Marble { Marble {
value: u8, value: MarbleValue,
dir: Direction, dir: Direction,
}, },
Trigger, Trigger,
@ -230,12 +232,16 @@ impl Machine {
} }
print!(" :: "); print!(" :: ");
for (val, dir) in marbles_on_row { for (val, dir) in marbles_on_row {
print!("{}", format!("{}{:<3} ", dir.char(), val).magenta()) print!("{}", format!("{}{} ", dir.char(), val).magenta())
} }
println!(); println!();
} }
println!("output: {}", String::from_utf8_lossy(&self.output)); if !self.output.is_empty() {
println!("input: {}", String::from_utf8_lossy(&self.input)); 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); println!("steps: {}", self.steps);
} }
@ -271,7 +277,9 @@ impl Machine {
new_tile = Some(Tile::Blank); new_tile = Some(Tile::Blank);
} }
Tile::Digit(d) => { 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 { *target = Tile::Marble {
value: new_val, value: new_val,
dir, dir,
@ -378,7 +386,7 @@ impl Machine {
Tile::Print => { Tile::Print => {
let sample = self.grid.get(front_pos); let sample = self.grid.get(front_pos);
if let Tile::Marble { value, dir: _ } = sample { if let Tile::Marble { value, dir: _ } = sample {
self.output.push(value); self.output.push(value as u8);
} }
} }
Tile::Bag => { Tile::Bag => {
@ -389,7 +397,7 @@ impl Machine {
} }
Tile::Input => { Tile::Input => {
if self.input_index < self.input.len() && self.grid.get(front_pos).is_blank() { 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.grid.get_mut(front_pos) = Tile::Marble { value, dir };
self.marbles.push(front_pos); self.marbles.push(front_pos);
self.input_index += 1; self.input_index += 1;
@ -467,7 +475,7 @@ impl Machine {
} }
impl Tile { impl Tile {
fn read_value(&self) -> u8 { fn read_value(&self) -> MarbleValue {
if let Tile::Marble { value, dir: _ } = self { if let Tile::Marble { value, dir: _ } = self {
*value *value
} else { } else {