Rename bitwise operators for readability (#5937)

This commit is contained in:
JT 2022-07-03 10:05:02 +12:00 committed by GitHub
parent b82dccf0bd
commit a48616697a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 29 deletions

View File

@ -440,19 +440,19 @@ pub fn eval_expression(
}
Operator::BitOr => {
let rhs = eval_expression(engine_state, stack, rhs)?;
lhs.bor(op_span, &rhs, expr.span)
lhs.bit_or(op_span, &rhs, expr.span)
}
Operator::BitAnd => {
let rhs = eval_expression(engine_state, stack, rhs)?;
lhs.band(op_span, &rhs, expr.span)
lhs.bit_and(op_span, &rhs, expr.span)
}
Operator::ShiftRight => {
let rhs = eval_expression(engine_state, stack, rhs)?;
lhs.bshr(op_span, &rhs, expr.span)
lhs.bit_shr(op_span, &rhs, expr.span)
}
Operator::ShiftLeft => {
let rhs = eval_expression(engine_state, stack, rhs)?;
lhs.bshl(op_span, &rhs, expr.span)
lhs.bit_shl(op_span, &rhs, expr.span)
}
}
}

View File

@ -4106,10 +4106,10 @@ 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"bit-or" => Operator::BitOr,
b"bit-and" => Operator::BitAnd,
b"bit-shl" => Operator::ShiftLeft,
b"bit-shr" => Operator::ShiftRight,
b"starts-with" => Operator::StartsWith,
b"ends-with" => Operator::EndsWith,
b"&&" | b"and" => Operator::And,

View File

@ -52,10 +52,10 @@ impl Display for Operator {
Operator::And => write!(f, "&&"),
Operator::Or => write!(f, "||"),
Operator::Pow => write!(f, "**"),
Operator::BitOr => write!(f, "bor"),
Operator::BitAnd => write!(f, "band"),
Operator::ShiftLeft => write!(f, "bshl"),
Operator::ShiftRight => write!(f, "bshr"),
Operator::BitOr => write!(f, "bit-or"),
Operator::BitAnd => write!(f, "bit-and"),
Operator::ShiftLeft => write!(f, "bit-shl"),
Operator::ShiftRight => write!(f, "bit-shr"),
Operator::LessThanOrEqual => write!(f, "<="),
Operator::GreaterThanOrEqual => write!(f, ">="),
Operator::StartsWith => write!(f, "starts-with"),

View File

@ -2240,7 +2240,7 @@ impl Value {
}
}
pub fn bshl(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
pub fn bit_shl(&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,
@ -2259,7 +2259,7 @@ impl Value {
}
}
pub fn bshr(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
pub fn bit_shr(&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,
@ -2278,7 +2278,7 @@ impl Value {
}
}
pub fn bor(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
pub fn bit_or(&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,
@ -2297,7 +2297,7 @@ impl Value {
}
}
pub fn band(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
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 {
span,

View File

@ -26,23 +26,23 @@ fn modulo2() -> TestResult {
}
#[test]
fn bshr() -> TestResult {
run_test("16 bshr 1", "8")
fn bit_shr() -> TestResult {
run_test("16 bit-shr 1", "8")
}
#[test]
fn bshl() -> TestResult {
run_test("5 bshl 1", "10")
fn bit_shl() -> TestResult {
run_test("5 bit-shl 1", "10")
}
#[test]
fn bshl_add() -> TestResult {
run_test("2 bshl 1 + 2", "16")
fn bit_shl_add() -> TestResult {
run_test("2 bit-shl 1 + 2", "16")
}
#[test]
fn sub_bshr() -> TestResult {
run_test("10 - 2 bshr 2", "2")
fn sub_bit_shr() -> TestResult {
run_test("10 - 2 bit-shr 2", "2")
}
#[test]
@ -56,18 +56,18 @@ fn or() -> TestResult {
}
#[test]
fn band() -> TestResult {
run_test("2 band 4", "0")
fn bit_and() -> TestResult {
run_test("2 bit-and 4", "0")
}
#[test]
fn bor() -> TestResult {
run_test("2 bor 4", "6")
fn bit_or() -> TestResult {
run_test("2 bit-or 4", "6")
}
#[test]
fn bit_and_or() -> TestResult {
run_test("2 bor 4 band 1 + 2", "2")
run_test("2 bit-or 4 bit-and 1 + 2", "2")
}
#[test]