From 6eb63bfed87c3512f91cc8ed2adc0e12cd9e1df0 Mon Sep 17 00:00:00 2001 From: msingletary Date: Sat, 21 May 2016 09:40:45 +0000 Subject: [PATCH] Add binary operators OR,AND,XOR,NOT --- lib/DDG/Goodie/BinaryLogic.pm | 8 +++++++- t/BinaryLogic.t | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/DDG/Goodie/BinaryLogic.pm b/lib/DDG/Goodie/BinaryLogic.pm index b3900ee26..0e718b978 100755 --- a/lib/DDG/Goodie/BinaryLogic.pm +++ b/lib/DDG/Goodie/BinaryLogic.pm @@ -10,8 +10,10 @@ use Marpa::R2; # Regexp triggers are used to find cases where the logical symbol # for 'not' is at the beginning of the query (e.g. the case '¬1') triggers query_raw => qr/.*\s+(and|or|xor)\s+.*/; +triggers query_raw => qr/.*\s+(AND|OR|XOR)\s+.*/; triggers query_raw => qr/.*\s*(⊕|∧|∨)\s*.*/; triggers query_raw => qr/not\s+.*/; +triggers query_raw => qr/NOT\s+.*/; triggers query_raw => qr/¬.*/; zci is_cached => 1; @@ -27,9 +29,13 @@ Term ::= ('(') Term (')') assoc => group action => ::first | Number | 'not' Term action => do_not + | 'NOT' Term action => do_not || Term 'xor' Term action => do_xor + || Term 'XOR' Term action => do_xor | Term 'and' Term action => do_and + | Term 'AND' Term action => do_and | Term 'or' Term action => do_or + | Term 'OR' Term action => do_or Number ::= HexNumber action => hex_number @@ -84,7 +90,7 @@ handle query_raw => sub { my $input = $_; my $testError = $input; - $testError =~ s/(?:0x|0b|[\d\s]|and|or|xor|not|\(|\)|⊕|∧|∨|¬)//ig; + $testError =~ s/(?:0x|0b|[\d\s]|and|or|xor|not|AND|OR|XOR|NOT|\(|\)|⊕|∧|∨|¬)//ig; return if length $testError != 0; my $grammar = Marpa::R2::Scanless::G->new({ source => \$rules }); diff --git a/t/BinaryLogic.t b/t/BinaryLogic.t index 9a9e89b58..76d1033f0 100755 --- a/t/BinaryLogic.t +++ b/t/BinaryLogic.t @@ -31,28 +31,34 @@ ddg_goodie_test( DDG::Goodie::BinaryLogic )], '4 xor 5' => test_zci(build_answer('1', sprintf "%b XOR %b", 4, 5)), + '4 XOR 5' => test_zci(build_answer('1', sprintf "%b XOR %b", 4, 5)), '4 ⊕ 5' => test_zci(build_answer('1', sprintf "%b XOR %b", 4, 5)), '4⊕5' => test_zci(build_answer('1', sprintf "%b XOR %b", 4, 5)), '9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985' => test_zci(build_answer('7378', sprintf "%b XOR %b XOR %b XOR %b XOR %b XOR %b XOR %b", 9489, 394, 9349, 39, 29, 4967, 3985)), '10 and 12' => test_zci(build_answer('8', sprintf "%b AND %b", 10, 12)), + '10 AND 12' => test_zci(build_answer('8', sprintf "%b AND %b", 10, 12)), '10 ∧ 12' => test_zci(build_answer('8', sprintf "%b AND %b", 10, 12)), '10∧12' => test_zci(build_answer('8', sprintf "%b AND %b", 10, 12)), '52 or 100' => test_zci(build_answer('116', sprintf "%b OR %b", 52, 100)), + '52 OR 100' => test_zci(build_answer('116', sprintf "%b OR %b", 52, 100)), '52 ∨ 100' => test_zci(build_answer('116', sprintf "%b OR %b", 52, 100)), '52∨100' => test_zci(build_answer('116', sprintf "%b OR %b", 52, 100)), '23 and (30 or 128)' => test_zci(build_answer('22', sprintf "%b AND (%b OR %b)", 23, 30, 128)), + '23 AND (30 OR 128)' => test_zci(build_answer('22', sprintf "%b AND (%b OR %b)", 23, 30, 128)), '23 ∧ (30 ∨ 128)' => test_zci(build_answer('22', sprintf "%b AND (%b OR %b)", 23, 30, 128)), '23∧(30∨128)' => test_zci(build_answer('22', sprintf "%b AND (%b OR %b)", 23, 30, 128)), '0x999 xor 0x589' => test_zci(build_answer('3088', sprintf "%b XOR %b", hex(999), hex(589))), '0x999 ⊕ 0x589' => test_zci(build_answer('3088', sprintf "%b XOR %b", hex(999), hex(589))), 'not 1' => test_zci(build_answer('18446744073709551614', sprintf "NOT %b", 1)), + 'NOT 1' => test_zci(build_answer('18446744073709551614', sprintf "NOT %b", 1)), '¬1' => test_zci(build_answer('18446744073709551614', sprintf "NOT %b", 1)), '3 and 2' => test_zci(build_answer('2', sprintf "%b AND %b", 3, 2)), '1 or 1234' => test_zci(build_answer('1235', sprintf "%b OR %b", 1, 1234)), '34 or 100' => test_zci(build_answer('102', sprintf "%b OR %b", 34, 100)), '10 and (30 or 128)' => test_zci(build_answer('10', sprintf "%b AND (%b OR %b)", 10, 30, 128)), '0x01 or not 0X100' => test_zci(build_answer('18446744073709551359', sprintf "%b OR NOT %b", hex(1), hex(100))), + '0x01 OR NOT 0X100' => test_zci(build_answer('18446744073709551359', sprintf "%b OR NOT %b", hex(1), hex(100))), '0x01 or 0x02' => test_zci(build_answer('3', sprintf "%b OR %b", hex(1), hex(2))), '0b01 or 0b10' => test_zci(build_answer('3', sprintf "01 OR 10")), '0B11 xor 0B10' => test_zci(build_answer('1', sprintf "11 XOR 10")),