while let Some(op) = ops.pop_front() {
let lhs = match vals.pop_front().expect("No value remaining") {
Value::Parsed(n) => n,
Value::Unparsed(s) => s.parse::<i32>().unwrap_or(*self.variables.get(s).expect("Unrecognized variable"))
};
match op {
"->" => {
let rhs = vals.pop_front().unwrap();
match rhs {
Value::Unparsed(s) => {
*self.variables.entry(s).or_default() = lhs;
},
_ => unreachable!()
}
},
op2 if matches!(op2, "AND" | "OR" | "LSHIFT" | "RSHIFT") => {
let rhs = match vals.pop_front().expect("No value remaining") {
Value::Parsed(n) => n,
Value::Unparsed(s) => s.parse::<i32>().unwrap_or(*self.variables.get(s).expect("Unrecognized variable"))
};
match op2 {
"AND" => vals.push_front(Value::Parsed(lhs & rhs)),
"OR" => vals.push_front(Value::Parsed(lhs | rhs)),
"LSHIFT" => vals.push_front(Value::Parsed(lhs << rhs)),
"RSHIFT" => vals.push_front(Value::Parsed(lhs >> rhs)),
_ => unreachable!() // <-- Why should be any other case here?
}
}
"NOT" => {
let arg = vals.pop_front().unwrap();
vals.push_front(Value::Parsed(match arg {
Value::Parsed(n) => !n,
Value::Unparsed(s) => !s.parse::<i32>().unwrap_or(*self.variables.get(s).expect("Unrecognized variable"))
}));
},
_ => unreachable!()
}
In the code above I had matched op2 against a known set of patterns, why do I need to specify an extra case?