Add band and bor operator for bit operations (#5936)

* Add `band` and `bor` Operator

* Add tests
This commit is contained in:
Justin Ma
2022-07-03 02:03:36 +08:00
committed by GitHub
parent 84caf8859f
commit b82dccf0bd
8 changed files with 91 additions and 17 deletions

View File

@ -4106,6 +4106,8 @@ pub fn parse_operator(
b"in" => Operator::In,
b"not-in" => Operator::NotIn,
b"mod" => Operator::Modulo,
b"bor" => Operator::BitOr,
b"band" => Operator::BitAnd,
b"bshl" => Operator::ShiftLeft,
b"bshr" => Operator::ShiftRight,
b"starts-with" => Operator::StartsWith,

View File

@ -492,25 +492,27 @@ pub fn math_result_type(
)
}
},
Operator::ShiftLeft | Operator::ShiftRight => match (&lhs.ty, &rhs.ty) {
(Type::Int, Type::Int) => (Type::Int, None),
Operator::ShiftLeft | Operator::ShiftRight | Operator::BitAnd | Operator::BitOr => {
match (&lhs.ty, &rhs.ty) {
(Type::Int, Type::Int) => (Type::Int, None),
(Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None),
_ => {
*op = Expression::garbage(op.span);
(
Type::Any,
Some(ParseError::UnsupportedOperation(
op.span,
lhs.span,
lhs.ty.clone(),
rhs.span,
rhs.ty.clone(),
)),
)
(Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None),
_ => {
*op = Expression::garbage(op.span);
(
Type::Any,
Some(ParseError::UnsupportedOperation(
op.span,
lhs.span,
lhs.ty.clone(),
rhs.span,
rhs.ty.clone(),
)),
)
}
}
},
}
},
_ => {
*op = Expression::garbage(op.span);