From: Jacob Casper Date: Mon, 15 Jan 2024 18:06:21 +0000 (-0600) Subject: Kinda implement field specifications X-Git-Url: https://git.jacobcasper.com/?a=commitdiff_plain;h=5a2d304f69c7956f398fb165f7c90aaa6d5107d0;p=mix.git Kinda implement field specifications Field specifications non-optimally and with no appropriate bounds checking. As long as I only call this with good data it should be fine... right? --- diff --git a/src/main.rs b/src/main.rs index 12a5f53..0fedeee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,18 @@ +#[derive(Debug, Default, Copy, Clone)] +struct MixBit { + v: u8, +} + +impl MixBit { + // Clamps a u8 to a u6 + fn value(&self) -> u8 { + return self.v % 64; + } + + fn as_value(&self) -> MixBit { + MixBit { v: self.value() } + } +} // > Each byte contains an unspecified amount of information but it must be capable of holding at // > least 64 distinct values. // > A computer word consists of five bytes and a sign. The sign portion has only two possible @@ -6,11 +21,26 @@ #[derive(Debug, Default, Copy, Clone)] struct Word { sign: bool, - bytes: [u8; 5], + bytes: [MixBit; 5], } impl Word { - fn new(sign: bool, bytes: [u8; 5]) -> Word { + fn new(sign: bool, bytes: [MixBit; 5]) -> Word { + Word { sign, bytes } + } + // The allowable fields are those that are adjacent in a computer word, and they are + // represented by (L:R) where L is the number of the left hand part and R is the number of the + // right-hand part of the field. + fn field_specification(&self, l: usize, r: usize) -> Word { + let mut sign = false; + if l == 0 { + sign = self.sign; + } + let r_clamp = r % 5; + let mut bytes = [MixBit::default(); 5]; + for n in l..r_clamp { + bytes[n] = self.bytes[n]; + } Word { sign, bytes } } } @@ -69,5 +99,12 @@ impl Machine { } fn main() { - println!("{:?}", Machine::default()); + println!( + "{:?}", + Word { + sign: true, + bytes: [MixBit { v: 255 }; 5] + } + .field_specification(0, 2) + ); }