From 588e64ba35abd2379d3892045eb88dcb81d5afdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Fri, 29 Mar 2024 13:01:21 +0100 Subject: [PATCH 1/6] apply clippy suggestions --- ic10emu/build.rs | 27 ++++----- ic10emu/src/grammar.rs | 113 +++++++++++++++++------------------ ic10emu/src/interpreter.rs | 42 +++++++------ ic10emu/src/lib.rs | 62 +++++++------------ ic10emu/src/rand_mscorlib.rs | 5 +- ic10emu/src/tokens.rs | 4 +- 6 files changed, 114 insertions(+), 139 deletions(-) diff --git a/ic10emu/build.rs b/ic10emu/build.rs index cbc70ee..f24e9e0 100644 --- a/ic10emu/build.rs +++ b/ic10emu/build.rs @@ -68,13 +68,13 @@ fn write_repr_enum( } else { "".to_string() }; - write!( + writeln!( writer, - " #[strum({serialize_str}{props_str})] {variant_name},\n" + " #[strum({serialize_str}{props_str})] {variant_name}," ) .unwrap(); } - write!(writer, "}}\n").unwrap(); + writeln!(writer, "}}").unwrap(); } fn write_logictypes() { @@ -192,9 +192,8 @@ fn write_enums() { let e_contents = fs::read_to_string(e_infile).unwrap(); for line in e_contents.lines().filter(|l| !l.trim().is_empty()) { - let mut it = line.splitn(2, ' '); - let name = it.next().unwrap(); - let val_str = it.next().unwrap(); + let (name, val_str) = line.split_once(' ').unwrap(); + let val: Option = val_str.parse().ok(); if !check_set.contains(name) { @@ -206,9 +205,9 @@ fn write_enums() { } } - write!( + writeln!( &mut writer, - "pub(crate) const ENUM_LOOKUP: phf::Map<&'static str, u8> = {};\n", + "pub(crate) const ENUM_LOOKUP: phf::Map<&'static str, u8> = {};", enums_lookup_map_builder.build() ) .unwrap(); @@ -327,9 +326,9 @@ fn write_constants() { constants_lookup_map_builder.entry(name, constant); } - write!( + writeln!( &mut writer, - "pub(crate) const CONSTANTS_LOOKUP: phf::Map<&'static str, f64> = {};\n", + "pub(crate) const CONSTANTS_LOOKUP: phf::Map<&'static str, f64> = {};", constants_lookup_map_builder.build() ) .unwrap(); @@ -361,12 +360,12 @@ fn write_instructions_enum() { ) .unwrap(); - write!(&mut writer, " Nop,\n").unwrap(); + writeln!(&mut writer, " Nop,").unwrap(); for typ in &instructions { - write!(&mut writer, " {},\n", typ.to_case(Case::Pascal)).unwrap(); + writeln!(&mut writer, " {},", typ.to_case(Case::Pascal)).unwrap(); } - write!(&mut writer, "}}\n").unwrap(); + writeln!(&mut writer, "}}").unwrap(); write!( &mut writer, @@ -380,7 +379,7 @@ fn write_instructions_enum() { for typ in &instructions { let name = typ.to_case(Case::Pascal); - write!(&mut writer, " \"{typ}\" => Ok(Self::{name}),\n").unwrap(); + writeln!(&mut writer, " \"{typ}\" => Ok(Self::{name}),").unwrap(); } write!( &mut writer, diff --git a/ic10emu/src/grammar.rs b/ic10emu/src/grammar.rs index f22d5c1..cca7dac 100644 --- a/ic10emu/src/grammar.rs +++ b/ic10emu/src/grammar.rs @@ -9,6 +9,7 @@ use strum::EnumProperty; pub mod generated { use super::ParseError; use crate::interpreter::ICError; + use serde::{Deserialize, Serialize}; use std::str::FromStr; use strum::AsRefStr; use strum::Display; @@ -16,7 +17,6 @@ pub mod generated { use strum::EnumProperty; use strum::EnumString; use strum::IntoEnumIterator; - use serde::{Deserialize, Serialize}; include!(concat!(env!("OUT_DIR"), "/instructions.rs")); include!(concat!(env!("OUT_DIR"), "/logictypes.rs")); @@ -156,10 +156,10 @@ pub struct Line { impl FromStr for Line { type Err = ParseError; fn from_str(s: &str) -> Result { - let mut parts = s.splitn(2, "#"); + let mut parts = s.splitn(2, '#'); let code = parts .next() - .map(|s| { + .and_then(|s| { let s = s.trim_end(); if s.is_empty() { None @@ -167,7 +167,6 @@ impl FromStr for Line { Some(s.parse::()) } }) - .flatten() .transpose()?; let comment = parts.next().map(|s| s.parse()).transpose()?; Ok(Line { code, comment }) @@ -231,11 +230,11 @@ impl FromStr for Instruction { line: 0, start: 0, end: 0, - msg: format!("Missing instruction"), + msg: "Missing instruction".to_string(), }) } }?; - + let operands = get_operand_tokens(s, tokens_iter) .iter() .map(|(index, token)| { @@ -251,7 +250,10 @@ impl FromStr for Instruction { } } -fn get_operand_tokens<'a>(s: &'a str, tokens_iter: SplitConsecutiveWithIndices<'a>) -> Vec<(usize, &'a str)> { +fn get_operand_tokens<'a>( + s: &'a str, + tokens_iter: SplitConsecutiveWithIndices<'a>, +) -> Vec<(usize, &'a str)> { let mut operand_tokens = Vec::with_capacity(8); let mut string_start = None; for (index, token) in tokens_iter { @@ -298,29 +300,29 @@ pub enum Operand { impl Operand { pub fn get_value(&self, ic: &interpreter::IC) -> Result { match &self { - &Operand::RegisterSpec { + Operand::RegisterSpec { indirection, target, } => ic.get_register(*indirection, *target), - &Operand::Number(num) => Ok(num.value()), - &Operand::LogicType(lt) => lt + Operand::Number(num) => Ok(num.value()), + Operand::LogicType(lt) => lt .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::SlotLogicType(slt) => slt + Operand::SlotLogicType(slt) => slt .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::BatchMode(bm) => bm + Operand::BatchMode(bm) => bm .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::ReagentMode(rm) => rm + Operand::ReagentMode(rm) => rm .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::Identifier(ident) => ic.get_ident_value(&ident.name), - &Operand::DeviceSpec { .. } => Err(interpreter::ICError::DeviceNotValue), + Operand::Identifier(ident) => ic.get_ident_value(&ident.name), + Operand::DeviceSpec { .. } => Err(interpreter::ICError::DeviceNotValue), } } @@ -330,9 +332,9 @@ impl Operand { signed: bool, ) -> Result { let val = self.get_value(ic)?; - if val < -9.223372036854776E+18 { + if val < -9.223_372_036_854_776E18 { Err(interpreter::ICError::ShiftUnderflowI64) - } else if val <= 9.223372036854776E+18 { + } else if val <= 9.223_372_036_854_776E18 { Ok(interpreter::f64_to_i64(val, signed)) } else { Err(interpreter::ICError::ShiftOverflowI64) @@ -355,7 +357,7 @@ impl Operand { ic: &interpreter::IC, ) -> Result<(Option, Option), interpreter::ICError> { match &self { - &Operand::DeviceSpec { device, connection } => match device { + Operand::DeviceSpec { device, connection } => match device { Device::Db => Ok((Some(ic.device), *connection)), Device::Numbered(p) => { let dp = ic @@ -378,7 +380,7 @@ impl Operand { Ok((dp, *connection)) } }, - &Operand::Identifier(id) => ic.get_ident_device_id(&id.name), + Operand::Identifier(id) => ic.get_ident_device_id(&id.name), _ => Err(interpreter::ICError::ValueNotDevice), } } @@ -402,7 +404,7 @@ impl FromStr for Operand { let mut rest_iter = rest.iter(); let indirection = rest_iter.take_while_ref(|c| *c == &'r').count(); let target = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::() .parse::() .ok(); @@ -419,7 +421,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid register specifier"), + msg: "Invalid register specifier".to_string(), }) } ['d', rest @ ..] => match rest { @@ -428,7 +430,7 @@ impl FromStr for Operand { connection: None, }), ['b', ':', chan @ ..] => { - if chan.into_iter().all(|c| c.is_digit(10)) { + if chan.iter().all(|c| c.is_ascii_digit()) { Ok(Operand::DeviceSpec { device: Device::Db, connection: Some(String::from_iter(chan).parse().unwrap()), @@ -438,15 +440,15 @@ impl FromStr for Operand { line: 0, start: 3, end: 3, - msg: format!("Invalid device connection specifier"), + msg: "Invalid device connection specifier".to_string(), }) } } ['r', rest @ ..] => { - let mut rest_iter = rest.into_iter().peekable(); + let mut rest_iter = rest.iter().peekable(); let indirection = rest_iter.take_while_ref(|c| *c == &'r').count(); let target_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let target = target_str.parse::().ok(); let connection = { @@ -454,11 +456,11 @@ impl FromStr for Operand { // take off ':' rest_iter.next(); let connection_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let connection = connection_str.parse::().unwrap(); let trailing = rest_iter.clone().collect::>(); - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Some(connection)) } else { let start = @@ -467,7 +469,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid device connection specifier"), + msg: "Invalid device connection specifier".to_string(), }) } } else { @@ -476,30 +478,30 @@ impl FromStr for Operand { }?; let trailing = rest_iter.collect::>(); if let Some(target) = target { - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Operand::DeviceSpec { device: Device::Indirect { indirection: indirection as u32, target, }, - connection: connection, + connection, }) } else { Err(ParseError { line: 0, start: 0, end: 0, - msg: format!("Invalid register specifier"), + msg: "Invalid register specifier".to_string(), }) } } else { Ok(Operand::Identifier(s.parse::()?)) } } - [rest @ ..] => { - let mut rest_iter = rest.into_iter().peekable(); + rest => { + let mut rest_iter = rest.iter().peekable(); let target_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let target = target_str.parse::().ok(); let connection = { @@ -507,11 +509,11 @@ impl FromStr for Operand { // take off ':' rest_iter.next(); let connection_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let connection = connection_str.parse::().unwrap(); let trailing = rest_iter.clone().collect::>(); - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Some(connection)) } else { let start = 1 + target_str.len() + 1 + connection_str.len(); @@ -519,7 +521,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid device connection specifier"), + msg: "Invalid device connection specifier".to_string(), }) } } else { @@ -528,17 +530,17 @@ impl FromStr for Operand { }?; let trailing = rest_iter.collect::>(); if let Some(target) = target { - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Operand::DeviceSpec { device: Device::Numbered(target), - connection: connection, + connection, }) } else { Err(ParseError { line: 0, start: 0, end: 0, - msg: format!("Invalid device specifier"), + msg: "Invalid device specifier".to_string(), }) } } else { @@ -554,14 +556,14 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid hash string: Can not contain '\"'"), + msg: "Invalid hash string: Can not contain '\"'".to_string(), }) } } ['$', rest @ ..] => { - let mut rest_iter = rest.into_iter(); + let mut rest_iter = rest.iter(); let num_str = rest_iter - .take_while_ref(|c| c.is_digit(16)) + .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(); @@ -572,12 +574,12 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid Hexadecimal Number"), + msg: "Invalid Hexadecimal Number".to_string(), }) } } ['%', rest @ ..] => { - let mut rest_iter = rest.into_iter(); + let mut rest_iter = rest.iter(); let num_str = rest_iter .take_while_ref(|c| c.is_digit(2)) .collect::(); @@ -590,24 +592,24 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid Binary Number"), + msg: "Invalid Binary Number".to_string(), }) } } - [rest @ ..] => { - let mut rest_iter = rest.into_iter().peekable(); + rest => { + let mut rest_iter = rest.iter().peekable(); let float_str = if rest_iter.peek() == Some(&&'-') { format!("{}", rest_iter.next().unwrap()) } else { "".to_string() } + &rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); if !float_str.is_empty() { if rest_iter.peek() == Some(&&'.') { rest_iter.next(); let decimal_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); if !decimal_str.is_empty() { let float_str = float_str + "." + &decimal_str; @@ -619,7 +621,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid Decimal Number"), + msg: "Invalid Decimal Number".to_string(), }) } } else { @@ -633,7 +635,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid Integer Number"), + msg: "Invalid Integer Number".to_string(), }) } } @@ -703,10 +705,7 @@ impl FromStr for Identifier { fn from_str(s: &str) -> Result { let mut iter = s.chars(); if let Some(c) = iter.next() { - if match c { - 'a'..='z' | 'A'..='Z' | '_' | '.' => true, - _ => false, - } { + if matches!(c, 'a'..='z' | 'A'..='Z' | '_' | '.') { for (index, cc) in iter.enumerate() { match cc { 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' => continue, @@ -734,7 +733,7 @@ impl FromStr for Identifier { line: 0, start: 0, end: 0, - msg: format!("Empty Identifier"), + msg: "Empty Identifier".to_string(), }) } } diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index 1b2ca0c..8f07c8e 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -127,7 +127,7 @@ impl Display for ICState { ICState::Yield => "Ic has yielded, Resume on next tick".to_string(), ICState::Sleep(then, sleep_for) => { let format = format_description::parse("[hour]:[minute]:[second]").unwrap(); - let resume = then.clone() + time::Duration::new(*sleep_for as i64, 0); + let resume = *then + time::Duration::new(*sleep_for as i64, 0); format!( "Sleeping for {sleep_for} seconds, will resume at {}", resume.format(&format).unwrap() @@ -169,7 +169,6 @@ impl Default for Program { } impl Program { - pub fn new() -> Self { Program { instructions: Vec::new(), @@ -178,7 +177,7 @@ impl Program { } pub fn try_from_code(code: &str) -> Result { - let parse_tree = grammar::parse(&code)?; + let parse_tree = grammar::parse(code)?; let mut labels_set = HashSet::new(); let mut labels = HashMap::new(); let instructions = parse_tree @@ -220,7 +219,6 @@ impl Program { } impl IC { - pub fn new(id: u16, device: u16) -> Self { IC { device, @@ -352,7 +350,7 @@ impl IC { pub fn poke(&mut self, address: f64, val: f64) -> Result { let sp = address as i32; - if sp < 0 || sp >= 512 { + if !(0..512).contains(&sp) { Err(ICError::StackIndexOutOfRange(address)) } else { let last = self.stack[sp as usize]; @@ -426,7 +424,7 @@ impl IC { }), }, // TODO Yield => { - if operands.len() != 0 { + if !operands.is_empty() { Err(TooManyOperands { provided: operands.len() as u32, desired: 0, @@ -479,14 +477,14 @@ impl IC { }); }; let alias = match &device_reg { - &Operand::RegisterSpec { + Operand::RegisterSpec { indirection, target, } => Operand::RegisterSpec { indirection: *indirection, target: *target, }, - &Operand::DeviceSpec { device, connection } => Operand::DeviceSpec { + Operand::DeviceSpec { device, connection } => Operand::DeviceSpec { device: *device, connection: *connection, }, @@ -3110,7 +3108,7 @@ impl IC { Some(device) => match device.borrow().ic.as_ref() { Some(ic_id) => { let addr = addr.get_value(self)?; - let ic = vm.ics.get(&ic_id).unwrap().borrow(); + let ic = vm.ics.get(ic_id).unwrap().borrow(); let val = ic.peek_addr(addr)?; self.set_register(indirection, target, val)?; Ok(()) @@ -3149,7 +3147,7 @@ impl IC { Some(device) => match device.borrow().ic.as_ref() { Some(ic_id) => { let addr = addr.get_value(self)?; - let ic = vm.ics.get(&ic_id).unwrap().borrow(); + let ic = vm.ics.get(ic_id).unwrap().borrow(); let val = ic.peek_addr(addr)?; self.set_register(indirection, target, val)?; Ok(()) @@ -3173,13 +3171,13 @@ impl IC { let (Some(device_id), _connection) = dev_id.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => match device.borrow().ic.as_ref() { Some(ic_id) => { let addr = addr.get_value(self)?; let val = val.get_value(self)?; - let mut ic = vm.ics.get(&ic_id).unwrap().borrow_mut(); + let mut ic = vm.ics.get(ic_id).unwrap().borrow_mut(); ic.poke(addr, val)?; Ok(()) } @@ -3209,7 +3207,7 @@ impl IC { Some(ic_id) => { let addr = addr.get_value(self)?; let val = val.get_value(self)?; - let mut ic = vm.ics.get(&ic_id).unwrap().borrow_mut(); + let mut ic = vm.ics.get(ic_id).unwrap().borrow_mut(); ic.poke(addr, val)?; Ok(()) } @@ -3240,14 +3238,14 @@ impl IC { break 'inst Err(MissingConnecitonSpecifier); }; let network_id = vm - .get_device_same_network(self.device, device_id as u16) + .get_device_same_network(self.device, device_id) .map(|device| device.borrow().get_network_id(connection as usize)) .unwrap_or(Err(UnknownDeviceID(device_id as f64)))?; let val = val.get_value(self)?; vm.set_network_channel(network_id as usize, channel, val)?; return Ok(()); } - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let val = val.get_value(self)?; @@ -3280,7 +3278,7 @@ impl IC { device.borrow_mut().set_field(lt, val)?; Ok(()) } - None => Err(UnknownDeviceID(device_id as f64)), + None => Err(UnknownDeviceID(device_id)), } } oprs => Err(TooManyOperands { @@ -3297,7 +3295,7 @@ impl IC { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let index = index.get_value(self)?; @@ -3394,14 +3392,14 @@ impl IC { break 'inst Err(MissingConnecitonSpecifier); }; let network_id = vm - .get_device_same_network(self.device, device_id as u16) + .get_device_same_network(self.device, device_id) .map(|device| device.borrow().get_network_id(connection as usize)) .unwrap_or(Err(UnknownDeviceID(device_id as f64)))?; let val = vm.get_network_channel(network_id as usize, channel)?; self.set_register(indirection, target, val)?; return Ok(()); } - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let val = device.borrow().get_field(lt)?; @@ -3444,7 +3442,7 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - None => Err(UnknownDeviceID(device_id as f64)), + None => Err(UnknownDeviceID(device_id)), } } oprs => Err(TooManyOperands { @@ -3471,7 +3469,7 @@ impl IC { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let index = index.get_value(self)?; @@ -3507,7 +3505,7 @@ impl IC { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let rm = ReagentMode::try_from(rm.get_value(self)?)?; diff --git a/ic10emu/src/lib.rs b/ic10emu/src/lib.rs index 45389e3..b82e089 100644 --- a/ic10emu/src/lib.rs +++ b/ic10emu/src/lib.rs @@ -76,17 +76,11 @@ pub struct Network { pub channels: [f64; 8], } -#[derive(Debug)] +#[derive(Debug, Default)] struct IdSequenceGenerator { next: u16, } -impl Default for IdSequenceGenerator { - fn default() -> Self { - IdSequenceGenerator { next: 0 } - } -} - impl IdSequenceGenerator { pub fn next(&mut self) -> u16 { let val = self.next; @@ -201,16 +195,14 @@ impl Device { pub fn get_network_id(&self, connection: usize) -> Result { if connection >= 8 { Err(ICError::ConnecitonIndexOutOFRange(connection as u32)) - } else { - if let Connection::CableNetwork(network_id) = self.connections[connection] { - if let Some(network_id) = network_id { - Ok(network_id) - } else { - Err(ICError::NetworkNotConnected(connection as u32)) - } + } else if let Connection::CableNetwork(network_id) = self.connections[connection] { + if let Some(network_id) = network_id { + Ok(network_id) } else { - Err(ICError::NotDataConnection(connection as u32)) + Err(ICError::NetworkNotConnected(connection as u32)) } + } else { + Err(ICError::NotDataConnection(connection as u32)) } } @@ -291,6 +283,12 @@ impl Device { } } +impl Default for VM { + fn default() -> Self { + Self::new() + } +} + impl VM { pub fn new() -> Self { let id_gen = IdSequenceGenerator::default(); @@ -656,17 +654,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device.borrow_mut().get_field(typ).map(|val| Some(val)) + device.borrow_mut().get_field(typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), @@ -706,17 +701,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device.borrow().get_field(typ).map(|val| Some(val)) + device.borrow().get_field(typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), @@ -757,20 +749,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device - .borrow() - .get_slot_field(index, typ) - .map(|val| Some(val)) + device.borrow().get_slot_field(index, typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), @@ -809,20 +795,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device - .borrow() - .get_slot_field(index, typ) - .map(|val| Some(val)) + device.borrow().get_slot_field(index, typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), diff --git a/ic10emu/src/rand_mscorlib.rs b/ic10emu/src/rand_mscorlib.rs index e449969..ac887aa 100644 --- a/ic10emu/src/rand_mscorlib.rs +++ b/ic10emu/src/rand_mscorlib.rs @@ -73,8 +73,7 @@ impl Random { inextp = 1; } - let mut retval = - self.seed_array[inext as usize].wrapping_sub(self.seed_array[inextp as usize]); + let mut retval = self.seed_array[inext].wrapping_sub(self.seed_array[inextp]); if retval == i32::MAX { retval -= 1; @@ -82,7 +81,7 @@ impl Random { if retval < 0 { retval = retval.wrapping_add(i32::MAX); } - self.seed_array[inext as usize] = retval; + self.seed_array[inext] = retval; self.inext = inext; self.inextp = inextp; diff --git a/ic10emu/src/tokens.rs b/ic10emu/src/tokens.rs index b33cb22..7db62b1 100644 --- a/ic10emu/src/tokens.rs +++ b/ic10emu/src/tokens.rs @@ -21,7 +21,7 @@ impl<'a> Iterator for SplitConsecutiveWithIndices<'a> { + start + 'find_end: { let mut last = start; - for (index, c) in (&tail[start..]).chars().enumerate() { + for (index, c) in tail[start..].chars().enumerate() { if !self.chars.contains(&c) { break 'find_end index; } @@ -71,7 +71,7 @@ pub trait SplitConsecutiveIndicesExt: ) -> SplitConsecutiveWithIndices<'p> { SplitConsecutiveWithIndices { haystack: &self[..], - chars: chars, + chars, start: 0, } } 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 2/6] 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)) } } } From 3254e4ad30b50291f141bc6952ceacf70a20012c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Fri, 29 Mar 2024 14:26:41 +0100 Subject: [PATCH 3/6] fix typos --- ic10emu/build.rs | 32 ++++++++++++++++---------------- ic10emu/src/interpreter.rs | 8 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ic10emu/build.rs b/ic10emu/build.rs index 1a02092..38acd5d 100644 --- a/ic10emu/build.rs +++ b/ic10emu/build.rs @@ -29,7 +29,7 @@ where { pub aliases: Vec, pub value: Option

, - pub depricated: bool, + pub deprecated: bool, } fn write_repr_enum( @@ -58,13 +58,13 @@ fn write_repr_enum( .map(|s| format!("serialize = \"{s}\"")) .collect::>() .join(", "); - let depricated_str = if variant.depricated { - ", depricated = \"true\"".to_string() + let deprecated_str = if variant.deprecated { + ", deprecated = \"true\"".to_string() } else { "".to_string() }; let props_str = if let Some(val) = &variant.value { - format!(", props( value = \"{val}\"{depricated_str})") + format!(", props( value = \"{val}\"{deprecated_str})") } else { "".to_string() }; @@ -94,7 +94,7 @@ fn write_logictypes() { let val_str = it.next().unwrap(); let val: Option = val_str.parse().ok(); let docs = it.next(); - let depricated = docs + let deprecated = docs .map(|docs| docs.trim().to_uppercase() == "DEPRECATED") .unwrap_or(false); @@ -104,14 +104,14 @@ fn write_logictypes() { .find(|(_, variant)| variant.value == Some(val)) { variant.aliases.push(name.to_string()); - variant.depricated = depricated; + variant.deprecated = deprecated; } else { logictypes.insert( name.to_string(), EnumVariant { aliases: Vec::new(), value: Some(val), - depricated, + deprecated, }, ); } @@ -121,7 +121,7 @@ fn write_logictypes() { EnumVariant { aliases: Vec::new(), value: val, - depricated, + deprecated, }, ); } @@ -137,7 +137,7 @@ fn write_logictypes() { let val_str = it.next().unwrap(); let val: Option = val_str.parse().ok(); let docs = it.next(); - let depricated = docs + let deprecated = docs .map(|docs| docs.trim().to_uppercase() == "DEPRECATED") .unwrap_or(false); @@ -147,14 +147,14 @@ fn write_logictypes() { .find(|(_, variant)| variant.value == Some(val)) { variant.aliases.push(name.to_string()); - variant.depricated = depricated; + variant.deprecated = deprecated; } else { slotlogictypes.insert( name.to_string(), EnumVariant { aliases: Vec::new(), value: Some(val), - depricated, + deprecated, }, ); } @@ -164,7 +164,7 @@ fn write_logictypes() { EnumVariant { aliases: Vec::new(), value: val, - depricated, + deprecated, }, ); } @@ -244,7 +244,7 @@ fn write_modes() { EnumVariant { aliases: Vec::new(), value: Some(val), - depricated: false, + deprecated: false, }, ); } @@ -254,7 +254,7 @@ fn write_modes() { EnumVariant { aliases: Vec::new(), value: val, - depricated: false, + deprecated: false, }, ); } @@ -282,7 +282,7 @@ fn write_modes() { EnumVariant { aliases: Vec::new(), value: Some(val), - depricated: false, + deprecated: false, }, ); } @@ -292,7 +292,7 @@ fn write_modes() { EnumVariant { aliases: Vec::new(), value: val, - depricated: false, + deprecated: false, }, ); } diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index 6e1d8cf..d1b6d12 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -31,7 +31,7 @@ impl Error for LineError {} #[derive(Debug, Error, Clone, Serialize, Deserialize)] pub enum ICError { - #[error("Error Compileing Code: {0}")] + #[error("Error Compiling Code: {0}")] ParseError(#[from] ParseError), #[error("Duplicate label {0}")] DuplicateLabel(String), @@ -100,7 +100,7 @@ pub enum ICError { #[error("Connection index out of range: '{0}'")] ConnecitonIndexOutOFRange(u32), #[error("Connection specifier missing")] - MissingConnecitonSpecifier, + MissingConnectionSpecifier, #[error("No data network on connection '{0}'")] NotDataConnection(u32), #[error("Network not connected on connection '{0}'")] @@ -3235,7 +3235,7 @@ impl IC { if CHANNEL_LOGIC_TYPES.contains(<) { let channel = lt.as_channel().unwrap(); let Some(connection) = connection else { - break 'inst Err(MissingConnecitonSpecifier); + break 'inst Err(MissingConnectionSpecifier); }; let network_id = vm .get_device_same_network(self.device, device_id) @@ -3389,7 +3389,7 @@ impl IC { if CHANNEL_LOGIC_TYPES.contains(<) { let channel = lt.as_channel().unwrap(); let Some(connection) = connection else { - break 'inst Err(MissingConnecitonSpecifier); + break 'inst Err(MissingConnectionSpecifier); }; let network_id = vm .get_device_same_network(self.device, device_id) From f69073c1b233d1ffffe8ca425e3d71c7f7b0b50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Fri, 29 Mar 2024 14:56:09 +0100 Subject: [PATCH 4/6] simplify mismatch operand error handling also fixes an issue where for example `sleep` would report too many operands --- ic10emu/src/interpreter.rs | 1232 +++++------------------------------- 1 file changed, 162 insertions(+), 1070 deletions(-) diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index d1b6d12..910a257 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -109,6 +109,30 @@ pub enum ICError { BadNetworkId(u32), } +impl ICError { + pub const fn too_few_operands(provided: usize, desired: u32) -> Self { + ICError::TooFewOperands { + provided: provided as u32, + desired, + } + } + + pub const fn too_many_operands(provided: usize, desired: u32) -> Self { + ICError::TooManyOperands { + provided: provided as u32, + desired, + } + } + + pub const fn mismatch_operands(provided: usize, desired: u32) -> Self { + if provided < desired as usize { + ICError::too_few_operands(provided, desired) + } else { + ICError::too_many_operands(provided, desired) + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub enum ICState { Start, @@ -418,27 +442,17 @@ impl IC { self.state = ICState::Sleep(now, a); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 1)), }, // TODO Yield => { if !operands.is_empty() { - Err(TooManyOperands { - provided: operands.len() as u32, - desired: 0, - }) + Err(ICError::too_many_operands(operands.len(), 0)) } else { self.state = ICState::Yield; Ok(()) } } Define => match &operands[..] { - [_op1] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [name, number] => { let &Operand::Identifier(ident) = &name else { break 'inst Err(IncorrectOperandType { @@ -459,16 +473,9 @@ impl IC { Ok(()) } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Alias => match &operands[..] { - [_op1] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [name, device_reg] => { let &Operand::Identifier(ident) = &name else { break 'inst Err(IncorrectOperandType { @@ -498,16 +505,9 @@ impl IC { self.aliases.insert(ident.name.clone(), alias); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Move => match &operands[..] { - [_op1] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [reg, val] => { let &Operand::RegisterSpec { indirection, @@ -524,17 +524,10 @@ impl IC { self.set_register(*indirection, *target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Beq => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -542,16 +535,9 @@ impl IC { next_ip = if a == b { c as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Beqal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -560,16 +546,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Breq => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -581,32 +560,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Beqz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a == 0.0 { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Beqzal => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -614,16 +579,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Breqz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -634,16 +592,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bne => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -651,16 +602,9 @@ impl IC { next_ip = if a != b { c as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bneal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -669,16 +613,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brne => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -690,32 +627,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bnez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a != 0.0 { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bnezal => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -723,16 +646,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brnez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -743,16 +659,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Blt => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -760,16 +669,9 @@ impl IC { next_ip = if a < b { c as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bltal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -778,16 +680,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brlt => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -799,16 +694,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Ble => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -816,16 +704,9 @@ impl IC { next_ip = if a <= b { c as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bleal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -834,16 +715,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brle => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -855,32 +729,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Blez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a <= 0.0 { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Blezal => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -888,16 +748,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brlez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -908,32 +761,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bltz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a < 0.0 { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bltzal => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -941,16 +780,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brltz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -961,16 +793,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bgt => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -978,16 +803,9 @@ impl IC { next_ip = if a > b { c as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bgtal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -996,16 +814,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brgt => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1017,32 +828,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bgtz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a > 0.0 { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bgtzal => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1050,16 +847,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brgtz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1070,16 +860,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bge => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1087,16 +870,9 @@ impl IC { next_ip = if a >= b { c as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bgeal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1105,16 +881,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brge => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1126,32 +895,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bgez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a >= 0.0 { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bgezal => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1159,16 +914,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brgez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1179,16 +927,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bap => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [a, b, c, d] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1203,16 +944,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Bapal => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [a, b, c, d] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1228,16 +962,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Brap => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [a, b, c, d] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1253,16 +980,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Bapz => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1274,16 +994,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bapzal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1296,16 +1009,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brapz => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1317,16 +1023,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bna => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [a, b, c, d] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1341,16 +1040,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Bnaal => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [a, b, c, d] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1366,16 +1058,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Brna => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [a, b, c, d] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1390,16 +1075,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Bnaz => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1411,16 +1089,9 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bnazal => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1433,16 +1104,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Brnaz => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [a, b, c] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1454,32 +1118,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Bdse => match &operands[..] { - [_] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [d, a] => { let (device, _connection) = d.get_device_id(self)?; let a = a.get_value(self)?; next_ip = if device.is_some() { a as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bdseal => match &operands[..] { - [_] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [d, a] => { let (device, _connection) = d.get_device_id(self)?; let a = a.get_value(self)?; @@ -1487,16 +1137,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brdse => match &operands[..] { - [_] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [d, a] => { let (device, _connection) = d.get_device_id(self)?; let a = a.get_value(self)?; @@ -1507,32 +1150,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bdns => match &operands[..] { - [_] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [d, a] => { let (device, _connection) = d.get_device_id(self)?; let a = a.get_value(self)?; next_ip = if device.is_none() { a as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bdnsal => match &operands[..] { - [_] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [d, a] => { let (device, _connection) = d.get_device_id(self)?; let a = a.get_value(self)?; @@ -1540,16 +1169,9 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brdns => match &operands[..] { - [_] => Err(TooFewOperands { - provided: 1, - desired: 2, - }), [d, a] => { let (device, _connection) = d.get_device_id(self)?; let a = a.get_value(self)?; @@ -1560,32 +1182,18 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Bnan => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; next_ip = if a.is_nan() { b as u32 } else { next_ip }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Brnan => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; @@ -1596,10 +1204,7 @@ impl IC { }; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, J => match &operands[..] { @@ -1608,10 +1213,7 @@ impl IC { next_ip = a as u32; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Jal => match &operands[..] { [a] => { @@ -1620,10 +1222,7 @@ impl IC { self.al(); Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Jr => match &operands[..] { [a] => { @@ -1631,17 +1230,10 @@ impl IC { next_ip = (self.ip as f64 + a) as u32; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Seq => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -1658,16 +1250,9 @@ impl IC { self.set_register(indirection, target, if a == b { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Seqz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -1683,16 +1268,9 @@ impl IC { self.set_register(indirection, target, if a == 0.0 { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sne => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -1709,16 +1287,9 @@ impl IC { self.set_register(indirection, target, if a != b { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Snez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -1734,16 +1305,9 @@ impl IC { self.set_register(indirection, target, if a != 0.0 { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Slt => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -1760,16 +1324,9 @@ impl IC { self.set_register(indirection, target, if a < b { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sltz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -1785,16 +1342,9 @@ impl IC { self.set_register(indirection, target, if a < 0.0 { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sle => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -1811,16 +1361,9 @@ impl IC { self.set_register(indirection, target, if a <= b { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Slez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -1836,16 +1379,9 @@ impl IC { self.set_register(indirection, target, if a <= 0.0 { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sgt => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -1862,16 +1398,9 @@ impl IC { self.set_register(indirection, target, if a > b { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sgtz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -1887,16 +1416,9 @@ impl IC { self.set_register(indirection, target, if a > 0.0 { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sge => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -1913,16 +1435,9 @@ impl IC { self.set_register(indirection, target, if a >= b { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sgez => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -1938,16 +1453,9 @@ impl IC { self.set_register(indirection, target, if a >= 0.0 { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sap => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b, c] => { let &Operand::RegisterSpec { indirection, @@ -1975,16 +1483,9 @@ impl IC { )?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sapz => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2009,16 +1510,9 @@ impl IC { )?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sna => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [reg, a, b, c] => { let &Operand::RegisterSpec { indirection, @@ -2046,16 +1540,9 @@ impl IC { )?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Snaz => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2080,16 +1567,9 @@ impl IC { )?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sdse => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, device] => { let &Operand::RegisterSpec { indirection, @@ -2109,16 +1589,9 @@ impl IC { )?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Sdns => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, device] => { let &Operand::RegisterSpec { indirection, @@ -2138,16 +1611,9 @@ impl IC { )?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Snan => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2163,16 +1629,9 @@ impl IC { self.set_register(indirection, target, if a.is_nan() { 1.0 } else { 0.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Snanz => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2188,17 +1647,10 @@ impl IC { self.set_register(indirection, target, if a.is_nan() { 0.0 } else { 1.0 })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Select => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [reg, a, b, c] => { let &Operand::RegisterSpec { indirection, @@ -2216,17 +1668,10 @@ impl IC { self.set_register(indirection, target, if a != 0.0 { b } else { c })?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Add => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2243,16 +1688,9 @@ impl IC { self.set_register(indirection, target, a + b)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sub => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2269,16 +1707,9 @@ impl IC { self.set_register(indirection, target, a - b)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Mul => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2295,16 +1726,9 @@ impl IC { self.set_register(indirection, target, a * b)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Div => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2321,16 +1745,9 @@ impl IC { self.set_register(indirection, target, a / b)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Mod => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2347,16 +1764,9 @@ impl IC { self.set_register(indirection, target, ((a % b) + b) % b)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Exp => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2372,16 +1782,9 @@ impl IC { self.set_register(indirection, target, f64::exp(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Log => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2397,16 +1800,9 @@ impl IC { self.set_register(indirection, target, f64::ln(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Sqrt => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2422,17 +1818,10 @@ impl IC { self.set_register(indirection, target, f64::sqrt(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Max => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2449,16 +1838,9 @@ impl IC { self.set_register(indirection, target, f64::max(a, b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Min => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2475,16 +1857,9 @@ impl IC { self.set_register(indirection, target, f64::min(a, b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Ceil => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2500,16 +1875,9 @@ impl IC { self.set_register(indirection, target, f64::ceil(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Floor => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2525,16 +1893,9 @@ impl IC { self.set_register(indirection, target, f64::floor(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Abs => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2550,16 +1911,9 @@ impl IC { self.set_register(indirection, target, f64::abs(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Round => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2575,16 +1929,9 @@ impl IC { self.set_register(indirection, target, f64::round(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Trunc => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2600,10 +1947,7 @@ impl IC { self.set_register(indirection, target, f64::trunc(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Rand => match &operands[..] { @@ -2622,17 +1966,10 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Sin => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2648,16 +1985,9 @@ impl IC { self.set_register(indirection, target, f64::sin(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Cos => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2673,16 +2003,9 @@ impl IC { self.set_register(indirection, target, f64::cos(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Tan => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2698,16 +2021,9 @@ impl IC { self.set_register(indirection, target, f64::tan(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Asin => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2723,16 +2039,9 @@ impl IC { self.set_register(indirection, target, f64::asin(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Acos => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2748,16 +2057,9 @@ impl IC { self.set_register(indirection, target, f64::acos(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Atan => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -2773,16 +2075,9 @@ impl IC { self.set_register(indirection, target, f64::atan(a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Atan2 => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2799,17 +2094,10 @@ impl IC { self.set_register(indirection, target, f64::atan2(a, b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sll | Sla => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2826,16 +2114,9 @@ impl IC { self.set_register(indirection, target, i64_to_f64(a << b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Srl => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2852,16 +2133,9 @@ impl IC { self.set_register(indirection, target, i64_to_f64((a as u64 >> b) as i64))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sra => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2878,17 +2152,10 @@ impl IC { self.set_register(indirection, target, i64_to_f64(a >> b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, And => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2905,16 +2172,9 @@ impl IC { self.set_register(indirection, target, i64_to_f64(a & b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Or => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2931,16 +2191,9 @@ impl IC { self.set_register(indirection, target, i64_to_f64(a | b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Xor => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2957,16 +2210,9 @@ impl IC { self.set_register(indirection, target, i64_to_f64(a ^ b))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Nor => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, a, b] => { let &Operand::RegisterSpec { indirection, @@ -2983,16 +2229,9 @@ impl IC { self.set_register(indirection, target, i64_to_f64(!(a | b)))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Not => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [reg, a] => { let &Operand::RegisterSpec { indirection, @@ -3008,10 +2247,7 @@ impl IC { self.set_register(indirection, target, i64_to_f64(!a))?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Push => match &operands[..] { @@ -3020,10 +2256,7 @@ impl IC { self.push(a)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Pop => match &operands[..] { [reg] => { @@ -3041,26 +2274,16 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Poke => match &operands[..] { - oprs @ [_] => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 2, - }), [a, b] => { let a = a.get_value(self)?; let b = b.get_value(self)?; self.poke(a, b)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 2, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 2)), }, Peek => match &operands[..] { [reg] => { @@ -3078,17 +2301,10 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 1, - }), + oprs => Err(ICError::too_many_operands(oprs.len(), 1)), }, Get => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, dev_id, addr] => { let &Operand::RegisterSpec { indirection, @@ -3118,16 +2334,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Getd => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, dev_id, addr] => { let &Operand::RegisterSpec { indirection, @@ -3157,16 +2366,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Put => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [dev_id, addr, val] => { let (Some(device_id), _connection) = dev_id.get_device_id(self)? else { break 'inst Err(DeviceNotSet); @@ -3186,16 +2388,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Putd => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [dev_id, addr, val] => { let device_id = dev_id.get_value(self)?; if device_id >= u16::MAX as f64 || device_id < u16::MIN as f64 { @@ -3216,17 +2411,10 @@ impl IC { None => Err(UnknownDeviceID(device_id)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, S => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [dev, lt, val] => { let (Some(device_id), connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); @@ -3255,16 +2443,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sd => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [dev, lt, val] => { let device_id = dev.get_value(self)?; if device_id >= u16::MAX as f64 || device_id < u16::MIN as f64 { @@ -3281,16 +2462,9 @@ impl IC { None => Err(UnknownDeviceID(device_id)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Ss => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [dev, index, lt, val] => { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); @@ -3307,16 +2481,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Sb => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [prefab, lt, val] => { let prefab = prefab.get_value(self)?; let lt = LogicType::try_from(lt.get_value(self)?)?; @@ -3324,16 +2491,9 @@ impl IC { vm.set_batch_device_field(self.device, prefab, lt, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Sbs => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [prefab, index, lt, val] => { let prefab = prefab.get_value(self)?; let index = index.get_value(self)?; @@ -3342,16 +2502,9 @@ impl IC { vm.set_batch_device_slot_field(self.device, prefab, index, lt, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Sbn => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [prefab, name, lt, val] => { let prefab = prefab.get_value(self)?; let name = name.get_value(self)?; @@ -3360,17 +2513,10 @@ impl IC { vm.set_batch_name_device_field(self.device, prefab, name, lt, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, L => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, dev, lt] => { let &Operand::RegisterSpec { indirection, @@ -3409,16 +2555,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Ld => match &operands[..] { - oprs @ ([_] | [_, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 3, - }), [reg, dev, lt] => { let &Operand::RegisterSpec { indirection, @@ -3445,16 +2584,9 @@ impl IC { None => Err(UnknownDeviceID(device_id)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 3, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 3)), }, Ls => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [reg, dev, index, lt] => { let &Operand::RegisterSpec { indirection, @@ -3481,16 +2613,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Lr => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [reg, dev, rm, name] => { let &Operand::RegisterSpec { indirection, @@ -3517,16 +2642,9 @@ impl IC { None => Err(UnknownDeviceID(device_id as f64)), } } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Lb => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 4, - }), [reg, prefab, lt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3545,16 +2663,9 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 4, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 4)), }, Lbn => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _] | [_, _, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 5, - }), [reg, prefab, name, lt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3575,18 +2686,9 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 5, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 5)), }, Lbns => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _] | [_, _, _, _] | [_, _, _, _, _]) => { - Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 6, - }) - } [reg, prefab, name, index, slt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3614,16 +2716,9 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 6, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 6)), }, Lbs => match &operands[..] { - oprs @ ([_] | [_, _] | [_, _, _] | [_, _, _, _]) => Err(TooFewOperands { - provided: oprs.len() as u32, - desired: 5, - }), [reg, prefab, index, slt, bm] => { let &Operand::RegisterSpec { indirection, @@ -3644,10 +2739,7 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - oprs => Err(TooManyOperands { - provided: oprs.len() as u32, - desired: 5, - }), + oprs => Err(ICError::mismatch_operands(oprs.len(), 5)), }, } }; From 5dc021f9537a7d6dda991d6126fd4b913c9a5594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Fri, 29 Mar 2024 15:10:06 +0100 Subject: [PATCH 5/6] make yield look more like the other instructions syntactically --- ic10emu/src/interpreter.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index 910a257..1a8de6f 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -444,14 +444,13 @@ impl IC { } oprs => Err(ICError::mismatch_operands(oprs.len(), 1)), }, // TODO - Yield => { - if !operands.is_empty() { - Err(ICError::too_many_operands(operands.len(), 0)) - } else { + Yield => match &operands[..] { + [] => { self.state = ICState::Yield; Ok(()) } - } + oprs => Err(ICError::mismatch_operands(oprs.len(), 0)), + }, Define => match &operands[..] { [name, number] => { let &Operand::Identifier(ident) = &name else { From 5e3ba95a83b69d39a9264c10e6772dcd3103f9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Fri, 29 Mar 2024 15:19:41 +0100 Subject: [PATCH 6/6] use `to_owned` for lit -> String --- ic10emu/build.rs | 6 +- ic10emu/src/grammar.rs | 52 ++++++------- ic10emu/src/interpreter.rs | 146 ++++++++++++++++++------------------- 3 files changed, 102 insertions(+), 102 deletions(-) diff --git a/ic10emu/build.rs b/ic10emu/build.rs index 38acd5d..2a99838 100644 --- a/ic10emu/build.rs +++ b/ic10emu/build.rs @@ -59,14 +59,14 @@ fn write_repr_enum( .collect::>() .join(", "); let deprecated_str = if variant.deprecated { - ", deprecated = \"true\"".to_string() + ", deprecated = \"true\"".to_owned() } else { - "".to_string() + "".to_owned() }; let props_str = if let Some(val) = &variant.value { format!(", props( value = \"{val}\"{deprecated_str})") } else { - "".to_string() + "".to_owned() }; writeln!( writer, diff --git a/ic10emu/src/grammar.rs b/ic10emu/src/grammar.rs index cc9b5a4..a887211 100644 --- a/ic10emu/src/grammar.rs +++ b/ic10emu/src/grammar.rs @@ -230,7 +230,7 @@ impl FromStr for Instruction { line: 0, start: 0, end: 0, - msg: "Missing instruction".to_string(), + msg: "Missing instruction".to_owned(), }) } }?; @@ -420,7 +420,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: "Invalid register specifier".to_string(), + msg: "Invalid register specifier".to_owned(), }) } ['d', rest @ ..] => match rest { @@ -439,7 +439,7 @@ impl FromStr for Operand { line: 0, start: 3, end: 3, - msg: "Invalid device connection specifier".to_string(), + msg: "Invalid device connection specifier".to_owned(), }) } } @@ -467,7 +467,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: "Invalid device connection specifier".to_string(), + msg: "Invalid device connection specifier".to_owned(), }) } } else { @@ -488,7 +488,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: "Invalid register specifier".to_string(), + msg: "Invalid register specifier".to_owned(), }) } } else { @@ -517,7 +517,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: "Invalid device connection specifier".to_string(), + msg: "Invalid device connection specifier".to_owned(), }) } } else { @@ -535,7 +535,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: "Invalid device specifier".to_string(), + msg: "Invalid device specifier".to_owned(), }) } } else { @@ -551,7 +551,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: "Invalid hash string: Can not contain '\"'".to_string(), + msg: "Invalid hash string: Can not contain '\"'".to_owned(), }) } } @@ -568,7 +568,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: "Invalid Hexadecimal Number".to_string(), + msg: "Invalid Hexadecimal Number".to_owned(), }) } } @@ -585,7 +585,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: "Invalid Binary Number".to_string(), + msg: "Invalid Binary Number".to_owned(), }) } } @@ -594,7 +594,7 @@ impl FromStr for Operand { let float_str = if rest_iter.peek() == Some(&&'-') { format!("{}", rest_iter.next().unwrap()) } else { - "".to_string() + "".to_owned() } + &rest_iter .take_while_ref(|c| c.is_ascii_digit()) .collect::(); @@ -614,7 +614,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: "Invalid Decimal Number".to_string(), + msg: "Invalid Decimal Number".to_owned(), }) } } else if rest_iter.next().is_none() { @@ -626,7 +626,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: "Invalid Integer Number".to_string(), + msg: "Invalid Integer Number".to_owned(), }) } } else if let Some(val) = CONSTANTS_LOOKUP.get(s) { @@ -672,13 +672,13 @@ impl FromStr for Label { line: 0, start: index, end: index, - msg: "Missing ':' at end of label".to_string(), + msg: "Missing ':' at end of label".to_owned(), }), None => Err(ParseError { line: 0, start: 0, end: 0, - msg: "empty string for label? parse miscalled".to_string(), + msg: "empty string for label? parse miscalled".to_owned(), }), } } @@ -723,7 +723,7 @@ impl FromStr for Identifier { line: 0, start: 0, end: 0, - msg: "Empty Identifier".to_string(), + msg: "Empty Identifier".to_owned(), }) } } @@ -775,7 +775,7 @@ mod tests { ], },),), comment: Some(Comment { - comment: " This is a comment".to_string(), + comment: " This is a comment".to_owned(), },), },], ); @@ -827,7 +827,7 @@ mod tests { Line { code: None, comment: Some(Comment { - comment: " This is a comment".to_string(), + comment: " This is a comment".to_owned(), },), }, Line { @@ -835,7 +835,7 @@ mod tests { instruction: InstructionOp::Define, operands: vec![ Operand::Identifier(Identifier { - name: "a_def".to_string(), + name: "a_def".to_owned(), },), Operand::Number(Number::Float(10.0,),), ], @@ -847,9 +847,9 @@ mod tests { instruction: InstructionOp::Define, operands: vec![ Operand::Identifier(Identifier { - name: "a_hash".to_string(), + name: "a_hash".to_owned(), },), - Operand::Number(Number::String("This is a String".to_string()),), + Operand::Number(Number::String("This is a String".to_owned()),), ], },),), comment: None, @@ -859,7 +859,7 @@ mod tests { instruction: InstructionOp::Alias, operands: vec![ Operand::Identifier(Identifier { - name: "a_var".to_string(), + name: "a_var".to_owned(), },), Operand::RegisterSpec { indirection: 0, @@ -874,7 +874,7 @@ mod tests { instruction: InstructionOp::Alias, operands: vec![ Operand::Identifier(Identifier { - name: "a_device".to_string(), + name: "a_device".to_owned(), },), Operand::DeviceSpec { device: Device::Numbered(0), @@ -927,7 +927,7 @@ mod tests { Line { code: Some(Code::Label(Label { id: Identifier { - name: "main".to_string(), + name: "main".to_owned(), }, },),), comment: None, @@ -964,7 +964,7 @@ mod tests { indirection: 0, target: 0, }, - Operand::Number(Number::String("AccessCardBlack".to_string()),), + Operand::Number(Number::String("AccessCardBlack".to_owned()),), ], },),), comment: None, @@ -1032,7 +1032,7 @@ mod tests { code: Some(Code::Instruction(Instruction { instruction: InstructionOp::J, operands: vec![Operand::Identifier(Identifier { - name: "main".to_string(), + name: "main".to_owned(), },),], },),), comment: None, diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index 1a8de6f..580ff63 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -146,9 +146,9 @@ pub enum ICState { impl Display for ICState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let out = match self { - ICState::Start => "Not Run".to_string(), - ICState::Running => "Running".to_string(), - ICState::Yield => "Ic has yielded, Resume on next tick".to_string(), + ICState::Start => "Not Run".to_owned(), + ICState::Running => "Running".to_owned(), + ICState::Yield => "Ic has yielded, Resume on next tick".to_owned(), ICState::Sleep(then, sleep_for) => { let format = format_description::parse("[hour]:[minute]:[second]").unwrap(); let resume = *then + time::Duration::new(*sleep_for as i64, 0); @@ -158,7 +158,7 @@ impl Display for ICState { ) } ICState::Error(err) => format!("{err}"), - ICState::HasCaughtFire => "IC has caught fire! this is not a joke!".to_string(), + ICState::HasCaughtFire => "IC has caught fire! this is not a joke!".to_owned(), }; write!(f, "{out}") } @@ -456,13 +456,13 @@ impl IC { let &Operand::Identifier(ident) = &name else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Name".to_string(), + desired: "Name".to_owned(), }); }; let &Operand::Number(num) = &number else { break 'inst Err(IncorrectOperandType { index: 2, - desired: "Number".to_string(), + desired: "Number".to_owned(), }); }; if self.defines.contains_key(&ident.name) { @@ -479,7 +479,7 @@ impl IC { let &Operand::Identifier(ident) = &name else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Name".to_string(), + desired: "Name".to_owned(), }); }; let alias = match &device_reg { @@ -497,7 +497,7 @@ impl IC { _ => { break 'inst Err(IncorrectOperandType { index: 2, - desired: "Device Or Register".to_string(), + desired: "Device Or Register".to_owned(), }) } }; @@ -515,7 +515,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; @@ -1241,7 +1241,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1260,7 +1260,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1278,7 +1278,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1297,7 +1297,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1315,7 +1315,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1334,7 +1334,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1352,7 +1352,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1371,7 +1371,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1389,7 +1389,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1408,7 +1408,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1426,7 +1426,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1445,7 +1445,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1463,7 +1463,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1493,7 +1493,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1520,7 +1520,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1550,7 +1550,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1577,7 +1577,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (device, _connection) = device.get_device_id(self)?; @@ -1599,7 +1599,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (device, _connection) = device.get_device_id(self)?; @@ -1621,7 +1621,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1639,7 +1639,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1658,7 +1658,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1679,7 +1679,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1698,7 +1698,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1717,7 +1717,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1736,7 +1736,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1755,7 +1755,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1774,7 +1774,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1792,7 +1792,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1810,7 +1810,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1829,7 +1829,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1848,7 +1848,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1867,7 +1867,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1885,7 +1885,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1903,7 +1903,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1921,7 +1921,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1939,7 +1939,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1958,7 +1958,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let val = vm.random.clone().borrow_mut().next_f64(); @@ -1977,7 +1977,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -1995,7 +1995,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -2013,7 +2013,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -2031,7 +2031,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -2049,7 +2049,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -2067,7 +2067,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -2085,7 +2085,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value(self)?; @@ -2105,7 +2105,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2124,7 +2124,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, false)?; @@ -2143,7 +2143,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2163,7 +2163,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2182,7 +2182,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2201,7 +2201,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2220,7 +2220,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2239,7 +2239,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let a = a.get_value_i64(self, true)?; @@ -2266,7 +2266,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let val = self.pop()?; @@ -2293,7 +2293,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let val = self.peek()?; @@ -2312,7 +2312,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (Some(device_id), _connection) = dev_id.get_device_id(self)? else { @@ -2344,7 +2344,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (Some(device_id), _connection) = dev_id.get_device_id(self)? else { @@ -2524,7 +2524,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (Some(device_id), connection) = dev.get_device_id(self)? else { @@ -2565,7 +2565,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let device_id = dev.get_value(self)?; @@ -2594,7 +2594,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (Some(device_id), _connection) = dev.get_device_id(self)? else { @@ -2623,7 +2623,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let (Some(device_id), _connection) = dev.get_device_id(self)? else { @@ -2652,7 +2652,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let prefab = prefab.get_value(self)?; @@ -2673,7 +2673,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let prefab = prefab.get_value(self)?; @@ -2696,7 +2696,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let prefab = prefab.get_value(self)?; @@ -2726,7 +2726,7 @@ impl IC { else { break 'inst Err(IncorrectOperandType { index: 1, - desired: "Register".to_string(), + desired: "Register".to_owned(), }); }; let prefab = prefab.get_value(self)?;