Add logical xor operator (#7242)

We already have the binary `bit-xor` and the shortcircuiting logical
`or`(`||`) and `and`(`&&`).
This introduces `xor` as a compact form for both brevity and clarity.
You can express the operation through `not`/`and`/`or` with a slight
risk of introducing bugs through typos.

Operator precedence

`and` > `xor` > `or`

Added logic and precedence tests.
This commit is contained in:
Stefan Holderbach
2022-11-26 17:02:37 +01:00
committed by GitHub
parent 3e76ed9122
commit 2ccb91dc6a
9 changed files with 57 additions and 1 deletions

View File

@ -246,6 +246,11 @@ fn shortcircuiting_or() -> TestResult {
run_test(r#"true || (5 / 0; false)"#, "true")
}
#[test]
fn nonshortcircuiting_xor() -> TestResult {
run_test(r#"true xor (print "hello"; false) | ignore"#, "hello")
}
#[test]
fn open_ended_range() -> TestResult {
run_test(r#"1.. | first 100000 | length"#, "100000")

View File

@ -55,6 +55,16 @@ fn or() -> TestResult {
run_test("true || false", "true")
}
#[test]
fn xor_1() -> TestResult {
run_test("false xor true", "true")
}
#[test]
fn xor_2() -> TestResult {
run_test("true xor true", "false")
}
#[test]
fn bit_xor() -> TestResult {
run_test("4 bit-xor 4", "0")

View File

@ -430,3 +430,15 @@ fn date_literal() -> TestResult {
fn and_and_or() -> TestResult {
run_test(r#"true and false or true"#, "true")
}
#[test]
fn and_and_xor() -> TestResult {
// Assumes the precedence NOT > AND > XOR > OR
run_test(r#"true and true xor true and false"#, "true")
}
#[test]
fn or_and_xor() -> TestResult {
// Assumes the precedence NOT > AND > XOR > OR
run_test(r#"true or false xor true or false"#, "true")
}