Add bit operator: bit-xor (#5940)

This commit is contained in:
Justin Ma
2022-07-03 19:45:20 +08:00
committed by GitHub
parent 3a38fb94f0
commit 4e90b478b7
8 changed files with 59 additions and 20 deletions

View File

@ -49,7 +49,7 @@ impl Expression {
| Operator::In
| Operator::NotIn => 80,
Operator::BitAnd => 75,
// Operator::BitXor => 70,
Operator::BitXor => 70,
Operator::BitOr => 60,
Operator::And => 50,
Operator::Or => 40,

View File

@ -27,6 +27,7 @@ pub enum Operator {
StartsWith,
EndsWith,
BitOr,
BitXor,
BitAnd,
ShiftLeft,
ShiftRight,
@ -53,6 +54,7 @@ impl Display for Operator {
Operator::Or => write!(f, "||"),
Operator::Pow => write!(f, "**"),
Operator::BitOr => write!(f, "bit-or"),
Operator::BitXor => write!(f, "bit-xor"),
Operator::BitAnd => write!(f, "bit-and"),
Operator::ShiftLeft => write!(f, "bit-shl"),
Operator::ShiftRight => write!(f, "bit-shr"),

View File

@ -2297,6 +2297,25 @@ impl Value {
}
}
pub fn bit_xor(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) {
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
span,
val: *lhs ^ rhs,
}),
(Value::CustomValue { val: lhs, span }, rhs) => {
lhs.operation(*span, Operator::BitXor, op, rhs)
}
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),
lhs_span: self.span()?,
rhs_ty: rhs.get_type(),
rhs_span: rhs.span()?,
}),
}
}
pub fn bit_and(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) {
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {