From 1d51ee0486018a1dff39608d78ef2be842c7a202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Fri, 29 Mar 2024 14:22:20 +0100 Subject: [PATCH] some more lints and small changes --- ic10emu/build.rs | 4 +- ic10emu/src/grammar.rs | 74 ++++++------ ic10emu/src/interpreter.rs | 217 +++++++++++++++++------------------ ic10emu/src/lib.rs | 40 +++---- ic10emu/src/rand_mscorlib.rs | 2 +- ic10emu/src/tokens.rs | 76 ++++++------ 6 files changed, 194 insertions(+), 219 deletions(-) diff --git a/ic10emu/build.rs b/ic10emu/build.rs index f24e9e0..1a02092 100644 --- a/ic10emu/build.rs +++ b/ic10emu/build.rs @@ -49,7 +49,7 @@ fn write_repr_enum( pub enum {name} {{\n" ) .unwrap(); - for (name, variant) in variants.into_iter() { + for (name, variant) in variants { let variant_name = name.to_case(Case::Pascal); let mut serialize = vec![name.clone()]; serialize.extend(variant.aliases.iter().cloned()); @@ -354,7 +354,7 @@ fn write_instructions_enum() { write!( &mut writer, - "#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]\n\ + "#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]\n\ pub enum InstructionOp {{\n\ " ) diff --git a/ic10emu/src/grammar.rs b/ic10emu/src/grammar.rs index cca7dac..cc9b5a4 100644 --- a/ic10emu/src/grammar.rs +++ b/ic10emu/src/grammar.rs @@ -110,32 +110,32 @@ impl Error for ParseError {} impl ParseError { /// Offset the ParseError in it's line, adding the passed values to it's `start` and `end` + #[must_use] pub fn offset(self, offset: usize) -> Self { ParseError { - line: self.line, start: self.start + offset, end: self.end + offset, - msg: self.msg, + ..self } } /// Offset the ParseError line, adding the passed value to it's `line` + #[must_use] pub fn offset_line(self, offset: usize) -> Self { ParseError { line: self.line + offset, start: self.start, - end: self.end, - msg: self.msg, + ..self } } /// Mark the parse error as extending 'length' bytes from `start` + #[must_use] pub fn span(self, length: usize) -> Self { ParseError { - line: self.line, start: self.start, end: self.start + length, - msg: self.msg, + ..self } } } @@ -195,7 +195,7 @@ impl FromStr for Code { } } -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Eq, Debug)] pub struct Comment { pub comment: String, } @@ -272,7 +272,7 @@ fn get_operand_tokens<'a>( operand_tokens } -#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)] pub enum Device { Db, Numbered(u32), @@ -408,9 +408,8 @@ impl FromStr for Operand { .collect::() .parse::() .ok(); - let trailing = rest_iter.count(); if let Some(target) = target { - if trailing == 0 { + if rest_iter.next().is_none() { return Ok(Operand::RegisterSpec { indirection: indirection as u32, target, @@ -459,8 +458,7 @@ impl FromStr for Operand { .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let connection = connection_str.parse::().unwrap(); - let trailing = rest_iter.clone().collect::>(); - if trailing.is_empty() { + if rest_iter.next().is_none() { Ok(Some(connection)) } else { let start = @@ -476,9 +474,8 @@ impl FromStr for Operand { Ok(None) } }?; - let trailing = rest_iter.collect::>(); if let Some(target) = target { - if trailing.is_empty() { + if rest_iter.next().is_none() { Ok(Operand::DeviceSpec { device: Device::Indirect { indirection: indirection as u32, @@ -512,8 +509,7 @@ impl FromStr for Operand { .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let connection = connection_str.parse::().unwrap(); - let trailing = rest_iter.clone().collect::>(); - if trailing.is_empty() { + if rest_iter.next().is_none() { Ok(Some(connection)) } else { let start = 1 + target_str.len() + 1 + connection_str.len(); @@ -528,9 +524,8 @@ impl FromStr for Operand { Ok(None) } }?; - let trailing = rest_iter.collect::>(); if let Some(target) = target { - if trailing.is_empty() { + if rest_iter.next().is_none() { Ok(Operand::DeviceSpec { device: Device::Numbered(target), connection, @@ -566,8 +561,7 @@ impl FromStr for Operand { .take_while_ref(|c| c.is_ascii_hexdigit()) .collect::(); let num = i64::from_str_radix(&num_str, 16).unwrap() as f64; - let trailing = rest_iter.count(); - if trailing == 0 { + if rest_iter.next().is_none() { Ok(Operand::Number(Number::Hexadecimal(num))) } else { Err(ParseError { @@ -584,8 +578,7 @@ impl FromStr for Operand { .take_while_ref(|c| c.is_digit(2)) .collect::(); let num = i64::from_str_radix(&num_str, 2).unwrap() as f64; - let trailing = rest_iter.count(); - if trailing == 0 { + if rest_iter.next().is_none() { Ok(Operand::Number(Number::Binary(num))) } else { Err(ParseError { @@ -624,20 +617,17 @@ impl FromStr for Operand { msg: "Invalid Decimal Number".to_string(), }) } + } else if rest_iter.next().is_none() { + let num = f64::from_str(&float_str).unwrap(); + Ok(Operand::Number(Number::Float(num))) } else { - let trailing = rest_iter.count(); - if trailing == 0 { - let num = f64::from_str(&float_str).unwrap(); - Ok(Operand::Number(Number::Float(num))) - } else { - let start = float_str.len(); - Err(ParseError { - line: 0, - start, - end: start, - msg: "Invalid Integer Number".to_string(), - }) - } + let start = float_str.len(); + Err(ParseError { + line: 0, + start, + end: start, + msg: "Invalid Integer Number".to_string(), + }) } } else if let Some(val) = CONSTANTS_LOOKUP.get(s) { Ok(Operand::Number(Number::Constant(*val))) @@ -665,7 +655,7 @@ impl FromStr for Operand { // pub value: f64, // } -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Eq, Debug)] pub struct Label { pub id: Identifier, // #[rust_sitter::leaf(text = r":")] pub ()); @@ -694,7 +684,7 @@ impl FromStr for Label { } } -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct Identifier { // #[rust_sitter::leaf(pattern = r"[a-zA-Z_.][\w\d.]*", transform = |id| id.to_string())] pub name: String, @@ -752,11 +742,11 @@ pub enum Number { impl Number { pub fn value(&self) -> f64 { match self { - Number::Enum(val) => *val, - Number::Float(val) => *val, - Number::Binary(val) => *val, - Number::Constant(val) => *val, - Number::Hexadecimal(val) => *val, + Number::Enum(val) + | Number::Float(val) + | Number::Binary(val) + | Number::Constant(val) + | Number::Hexadecimal(val) => *val, Number::String(s) => const_crc32::crc32(s.as_bytes()) as i32 as f64, } } diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index 8f07c8e..6e1d8cf 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -191,10 +191,10 @@ impl Program { Some(code) => match code { grammar::Code::Label(label) => { if labels_set.contains(&label.id.name) { - Err(ICError::DuplicateLabel(label.id.name.clone())) + Err(ICError::DuplicateLabel(label.id.name)) } else { labels_set.insert(label.id.name.clone()); - labels.insert(label.id.name.clone(), line_number as u32); + labels.insert(label.id.name, line_number as u32); Ok(grammar::Instruction { instruction: grammar::InstructionOp::Nop, operands: vec![], @@ -414,7 +414,7 @@ impl IC { [a] => { let a = a.get_value(self)?; let now = time::OffsetDateTime::now_local() - .unwrap_or(time::OffsetDateTime::now_utc()); + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()); self.state = ICState::Sleep(now, a); Ok(()) } @@ -531,7 +531,7 @@ impl IC { }, Beq => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -548,7 +548,7 @@ impl IC { }), }, Beqal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -566,7 +566,7 @@ impl IC { }), }, Breq => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -640,7 +640,7 @@ impl IC { }), }, Bne => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -657,7 +657,7 @@ impl IC { }), }, Bneal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -675,7 +675,7 @@ impl IC { }), }, Brne => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -749,7 +749,7 @@ impl IC { }), }, Blt => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -766,7 +766,7 @@ impl IC { }), }, Bltal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -784,7 +784,7 @@ impl IC { }), }, Brlt => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -805,7 +805,7 @@ impl IC { }), }, Ble => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -822,7 +822,7 @@ impl IC { }), }, Bleal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -840,7 +840,7 @@ impl IC { }), }, Brle => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -967,7 +967,7 @@ impl IC { }), }, Bgt => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -984,7 +984,7 @@ impl IC { }), }, Bgtal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1002,7 +1002,7 @@ impl IC { }), }, Brgt => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1076,7 +1076,7 @@ impl IC { }), }, Bge => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1093,7 +1093,7 @@ impl IC { }), }, Bgeal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1111,7 +1111,7 @@ impl IC { }), }, Brge => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1185,7 +1185,7 @@ impl IC { }), }, Bap => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -1209,7 +1209,7 @@ impl IC { }), }, Bapal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -1234,7 +1234,7 @@ impl IC { }), }, Brap => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -1259,7 +1259,7 @@ impl IC { }), }, Bapz => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1280,7 +1280,7 @@ impl IC { }), }, Bapzal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1302,7 +1302,7 @@ impl IC { }), }, Brapz => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1323,7 +1323,7 @@ impl IC { }), }, Bna => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -1347,7 +1347,7 @@ impl IC { }), }, Bnaal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -1372,7 +1372,7 @@ impl IC { }), }, Brna => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -1396,7 +1396,7 @@ impl IC { }), }, Bnaz => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1417,7 +1417,7 @@ impl IC { }), }, Bnazal => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1439,7 +1439,7 @@ impl IC { }), }, Brnaz => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1638,7 +1638,7 @@ impl IC { }, Seq => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1689,7 +1689,7 @@ impl IC { }), }, Sne => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1740,7 +1740,7 @@ impl IC { }), }, Slt => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1791,7 +1791,7 @@ impl IC { }), }, Sle => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1842,7 +1842,7 @@ impl IC { }), }, Sgt => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1893,7 +1893,7 @@ impl IC { }), }, Sge => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1944,7 +1944,7 @@ impl IC { }), }, Sap => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -1981,7 +1981,7 @@ impl IC { }), }, Sapz => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2015,7 +2015,7 @@ impl IC { }), }, Sna => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -2052,7 +2052,7 @@ impl IC { }), }, Snaz => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2195,7 +2195,7 @@ impl IC { }, Select => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -2223,7 +2223,7 @@ impl IC { }, Add => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2249,7 +2249,7 @@ impl IC { }), }, Sub => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2275,7 +2275,7 @@ impl IC { }), }, Mul => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2301,7 +2301,7 @@ impl IC { }), }, Div => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2327,7 +2327,7 @@ impl IC { }), }, Mod => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2429,7 +2429,7 @@ impl IC { }, Max => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2455,7 +2455,7 @@ impl IC { }), }, Min => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2779,7 +2779,7 @@ impl IC { }), }, Atan2 => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2806,7 +2806,7 @@ impl IC { }, Sll | Sla => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2832,7 +2832,7 @@ impl IC { }), }, Srl => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2858,7 +2858,7 @@ impl IC { }), }, Sra => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2885,7 +2885,7 @@ impl IC { }, And => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2911,7 +2911,7 @@ impl IC { }), }, Or => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2937,7 +2937,7 @@ impl IC { }), }, Xor => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -2963,7 +2963,7 @@ impl IC { }), }, Nor => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3085,7 +3085,7 @@ impl IC { }, Get => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3124,7 +3124,7 @@ impl IC { }), }, Getd => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3163,7 +3163,7 @@ impl IC { }), }, Put => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3192,7 +3192,7 @@ impl IC { }), }, Putd => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3223,7 +3223,7 @@ impl IC { }, S => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3233,7 +3233,7 @@ impl IC { }; let lt = LogicType::try_from(lt.get_value(self)?)?; if CHANNEL_LOGIC_TYPES.contains(<) { - let channel = channel_logic_to_channel(lt).unwrap(); + let channel = lt.as_channel().unwrap(); let Some(connection) = connection else { break 'inst Err(MissingConnecitonSpecifier); }; @@ -3261,7 +3261,7 @@ impl IC { }), }, Sd => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3287,7 +3287,7 @@ impl IC { }), }, Ss => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -3313,7 +3313,7 @@ impl IC { }), }, Sb => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3330,7 +3330,7 @@ impl IC { }), }, Sbs => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -3348,7 +3348,7 @@ impl IC { }), }, Sbn => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -3367,7 +3367,7 @@ impl IC { }, L => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3387,7 +3387,7 @@ impl IC { }; let lt = LogicType::try_from(lt.get_value(self)?)?; if CHANNEL_LOGIC_TYPES.contains(<) { - let channel = channel_logic_to_channel(lt).unwrap(); + let channel = lt.as_channel().unwrap(); let Some(connection) = connection else { break 'inst Err(MissingConnecitonSpecifier); }; @@ -3415,7 +3415,7 @@ impl IC { }), }, Ld => match &operands[..] { - oprs @ [_] | oprs @ [_, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 3, }), @@ -3451,7 +3451,7 @@ impl IC { }), }, Ls => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -3487,7 +3487,7 @@ impl IC { }), }, Lr => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -3523,7 +3523,7 @@ impl IC { }), }, Lb => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] => Err(TooFewOperands { + oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { provided: oprs.len() as u32, desired: 4, }), @@ -3551,12 +3551,10 @@ impl IC { }), }, Lbn => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] | oprs @ [_, _, _, _] => { - Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 5, - }) - } + oprs @ ([_] | [_, _] | [_, _, _] | [_, _, _, _]) => Err(TooFewOperands { + provided: oprs.len() as u32, + desired: 5, + }), [reg, prefab, name, lt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3583,14 +3581,12 @@ impl IC { }), }, Lbns => match &operands[..] { - oprs @ [_] - | oprs @ [_, _] - | oprs @ [_, _, _] - | oprs @ [_, _, _, _] - | oprs @ [_, _, _, _, _] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 6, - }), + oprs @ ([_] | [_, _] | [_, _, _] | [_, _, _, _] | [_, _, _, _, _]) => { + Err(TooFewOperands { + provided: oprs.len() as u32, + desired: 6, + }) + } [reg, prefab, name, index, slt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3624,12 +3620,10 @@ impl IC { }), }, Lbs => match &operands[..] { - oprs @ [_] | oprs @ [_, _] | oprs @ [_, _, _] | oprs @ [_, _, _, _] => { - Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 5, - }) - } + oprs @ ([_] | [_, _] | [_, _, _] | [_, _, _, _]) => Err(TooFewOperands { + provided: oprs.len() as u32, + desired: 5, + }), [reg, prefab, index, slt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3675,24 +3669,29 @@ const CHANNEL_LOGIC_TYPES: [grammar::LogicType; 8] = [ grammar::LogicType::Channel7, ]; -fn channel_logic_to_channel(lt: grammar::LogicType) -> Option { - match lt { - grammar::LogicType::Channel0 => Some(0), - grammar::LogicType::Channel1 => Some(1), - grammar::LogicType::Channel2 => Some(2), - grammar::LogicType::Channel3 => Some(3), - grammar::LogicType::Channel4 => Some(4), - grammar::LogicType::Channel5 => Some(5), - grammar::LogicType::Channel6 => Some(6), - grammar::LogicType::Channel7 => Some(7), - _ => None, +trait LogicTypeExt { + fn as_channel(&self) -> Option; +} +impl LogicTypeExt for grammar::LogicType { + fn as_channel(&self) -> Option { + match self { + grammar::LogicType::Channel0 => Some(0), + grammar::LogicType::Channel1 => Some(1), + grammar::LogicType::Channel2 => Some(2), + grammar::LogicType::Channel3 => Some(3), + grammar::LogicType::Channel4 => Some(4), + grammar::LogicType::Channel5 => Some(5), + grammar::LogicType::Channel6 => Some(6), + grammar::LogicType::Channel7 => Some(7), + _ => None, + } } } pub fn f64_to_i64(f: f64, signed: bool) -> i64 { let mut num: i64 = (f % 9007199254740992.0) as i64; if !signed { - num &= 18014398509481983_i64 + num &= 18014398509481983_i64; } num } @@ -3701,7 +3700,7 @@ pub fn i64_to_f64(i: i64) -> f64 { let flag: bool = (i & 9007199254740992_i64) != 0; let mut i = i & 9007199254740991_i64; if flag { - i &= -9007199254740992_i64 + i &= -9007199254740992_i64; } i as f64 } diff --git a/ic10emu/src/lib.rs b/ic10emu/src/lib.rs index b82e089..910b66e 100644 --- a/ic10emu/src/lib.rs +++ b/ic10emu/src/lib.rs @@ -329,18 +329,13 @@ impl VM { } } let mut device = self.new_device(); - if let Some(first_network) = device - .connections - .iter_mut() - .filter_map(|c| { - if let Connection::CableNetwork(c) = c { - Some(c) - } else { - None - } - }) - .next() - { + if let Some(first_network) = device.connections.iter_mut().find_map(|c| { + if let Connection::CableNetwork(c) = c { + Some(c) + } else { + None + } + }) { first_network.replace(if let Some(network) = network { network } else { @@ -367,18 +362,13 @@ impl VM { } } let (mut device, ic) = self.new_ic(); - if let Some(first_network) = device - .connections - .iter_mut() - .filter_map(|c| { - if let Connection::CableNetwork(c) = c { - Some(c) - } else { - None - } - }) - .next() - { + if let Some(first_network) = device.connections.iter_mut().find_map(|c| { + if let Connection::CableNetwork(c) = c { + Some(c) + } else { + None + } + }) { first_network.replace(if let Some(network) = network { network } else { @@ -522,7 +512,7 @@ impl VM { } pub fn devices_on_same_network(&self, ids: &[u16]) -> bool { - for (_id, net) in self.networks.iter() { + for net in self.networks.values() { if net.borrow().contains(ids) { return true; } diff --git a/ic10emu/src/rand_mscorlib.rs b/ic10emu/src/rand_mscorlib.rs index ac887aa..add6cbd 100644 --- a/ic10emu/src/rand_mscorlib.rs +++ b/ic10emu/src/rand_mscorlib.rs @@ -11,7 +11,7 @@ pub struct Random { } /// Partial implementation of mscorlib System.Random -/// https://github.com/microsoft/referencesource/blob/master/mscorlib/system/random.cs#L94 +/// impl Random { pub fn new() -> Self { Self::with_seed(rand::random::()) diff --git a/ic10emu/src/tokens.rs b/ic10emu/src/tokens.rs index 7db62b1..ac680e1 100644 --- a/ic10emu/src/tokens.rs +++ b/ic10emu/src/tokens.rs @@ -15,49 +15,45 @@ impl<'a> Iterator for SplitConsecutiveWithIndices<'a> { let tail = &self.haystack[self.start..]; - match tail.find(self.chars) { - Some(start) => { - let end = self.start - + start - + 'find_end: { - let mut last = start; - for (index, c) in tail[start..].chars().enumerate() { - if !self.chars.contains(&c) { - break 'find_end index; - } - last = index + c.len_utf8(); - } - last - }; - let start = self.start + start; - - if self.start == start { - //consecutive delim matches, skip to next match - let start = end; - let end = match &self.haystack[start..].find(self.chars) { - Some(i) => start + i, - None => self.haystack.len(), - }; - let s = &self.haystack[start..end]; - self.start = end; - if s.is_empty() { - None - } else { - Some((start, s)) + let Some(start) = tail.find(self.chars) else { + let s = &self.haystack[self.start..]; + let index = self.start; + self.start = self.haystack.len(); + return Some((index, s)); + }; + let end = self.start + + start + + 'find_end: { + let mut last = start; + for (index, c) in tail[start..].chars().enumerate() { + if !self.chars.contains(&c) { + break 'find_end index; } - } else { - let s = &self.haystack[self.start..start]; - let index = self.start; - self.start = start; - Some((index, s)) + last = index + c.len_utf8(); } + last + }; + let start = self.start + start; + + if self.start == start { + //consecutive delim matches, skip to next match + let start = end; + let end = match &self.haystack[start..].find(self.chars) { + Some(i) => start + i, + None => self.haystack.len(), + }; + let s = &self.haystack[start..end]; + self.start = end; + if s.is_empty() { + None + } else { + Some((start, s)) } - None => { - let s = &self.haystack[self.start..]; - let index = self.start; - self.start = self.haystack.len(); - Some((index, s)) - } + } else { + let s = &self.haystack[self.start..start]; + let index = self.start; + self.start = start; + Some((index, s)) } } }