| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <geogram/basic/boolean_expression.h> | ||
| 41 | |||
| 42 | namespace GEO { | ||
| 43 | |||
| 44 | 118 | BooleanExpression::BooleanExpression( | |
| 45 | const std::string& expr | ||
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
|
118 | ) : expr_(expr) { |
| 47 | 118 | } | |
| 48 | |||
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 124246 times.
|
124246 | bool BooleanExpression::operator()(index_t x) const { |
| 50 | Context C(expr_, x); | ||
| 51 | 124246 | return parse_or(C); | |
| 52 | } | ||
| 53 | |||
| 54 | 124246 | bool BooleanExpression::parse_or(Context& C) const { | |
| 55 | 124246 | bool left = parse_and(C); | |
| 56 | 124246 | while( | |
| 57 |
1/2✓ Branch 0 taken 151710 times.
✗ Branch 1 not taken.
|
151710 | C.cur_char() == '|' || |
| 58 |
1/2✓ Branch 0 taken 151710 times.
✗ Branch 1 not taken.
|
151710 | C.cur_char() == '^' || |
| 59 |
2/4✓ Branch 0 taken 151710 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 151710 times.
|
303420 | C.cur_char() == '+' || |
| 60 | C.cur_char() == '-' | ||
| 61 | ) { | ||
| 62 | char op = C.cur_char(); | ||
| 63 | 151710 | C.next_char(); | |
| 64 | 151710 | bool right = parse_and(C); | |
| 65 |
1/2✓ Branch 0 taken 151710 times.
✗ Branch 1 not taken.
|
151710 | left = (op == '-') ? (left && !right) : |
| 66 | ✗ | (op == '^') ? (left ^ right) : | |
| 67 | ✗ | (left || right) ; | |
| 68 | } | ||
| 69 | 124246 | return left; | |
| 70 | } | ||
| 71 | |||
| 72 | 275956 | bool BooleanExpression::parse_and(Context& C) const { | |
| 73 | 275956 | bool left = parse_factor(C); | |
| 74 |
2/4✓ Branch 0 taken 151710 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151710 times.
✗ Branch 3 not taken.
|
579376 | while(C.cur_char() == '&' || C.cur_char() == '*') { |
| 75 | ✗ | C.next_char(); | |
| 76 | ✗ | bool right = parse_factor(C); | |
| 77 | ✗ | left = left && right; | |
| 78 | } | ||
| 79 | 275956 | return left; | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✓ Branch 0 taken 275956 times.
✗ Branch 1 not taken.
|
275956 | bool BooleanExpression::parse_factor(Context& C) const { |
| 83 |
3/6✓ Branch 0 taken 275956 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 275956 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 275956 times.
✗ Branch 5 not taken.
|
827868 | if(C.cur_char() == '!' || C.cur_char() == '~' || C.cur_char() == '-') { |
| 84 | ✗ | C.next_char(); | |
| 85 | ✗ | return !parse_factor(C); | |
| 86 | } | ||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275956 times.
|
275956 | if(C.cur_char() == '(') { |
| 88 | ✗ | C.next_char(); | |
| 89 | ✗ | bool result = parse_or(C); | |
| 90 | ✗ | if(C.cur_char() != ')') { | |
| 91 | throw std::logic_error( | ||
| 92 | ✗ | std::string("Unmatched parenthesis: ")+C.cur_char() | |
| 93 | ✗ | ); | |
| 94 | } | ||
| 95 | ✗ | C.next_char(); | |
| 96 | ✗ | return result; | |
| 97 | } | ||
| 98 |
2/2✓ Branch 0 taken 4960 times.
✓ Branch 1 taken 270996 times.
|
275956 | if((C.cur_char() == '*')) { |
| 99 | 4960 | C.next_char(); | |
| 100 | 4960 | return (C.x_ != 0); | |
| 101 | } | ||
| 102 |
3/6✓ Branch 0 taken 270996 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 270996 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 270996 times.
|
541992 | if((C.cur_char() >= 'A' && C.cur_char() <= 'Z') || C.cur_char() == 'x') { |
| 103 | 270996 | return parse_variable(C); | |
| 104 | } | ||
| 105 | ✗ | throw std::logic_error("Syntax error"); | |
| 106 | } | ||
| 107 | |||
| 108 |
1/2✓ Branch 0 taken 270996 times.
✗ Branch 1 not taken.
|
270996 | bool BooleanExpression::parse_variable(Context& C) const { |
| 109 | int bit = 0; | ||
| 110 |
2/4✓ Branch 0 taken 270996 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 270996 times.
✗ Branch 3 not taken.
|
270996 | if(C.cur_char() >= 'A' && C.cur_char() <= 'Z') { |
| 111 | ✗ | bit = int(C.cur_char()) - int('A'); | |
| 112 | ✗ | C.next_char(); | |
| 113 | } else { | ||
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 270996 times.
|
270996 | if(C.cur_char() != 'x') { |
| 115 | ✗ | throw std::logic_error("Syntax error in variable"); | |
| 116 | } | ||
| 117 | 270996 | C.next_char(); | |
| 118 |
3/4✓ Branch 0 taken 270996 times.
✓ Branch 1 taken 151710 times.
✓ Branch 2 taken 270996 times.
✗ Branch 3 not taken.
|
693702 | while(C.cur_char() >= '0' && C.cur_char() <= '9') { |
| 119 | 270996 | bit = bit * 10 + (int(C.cur_char()) - '0'); | |
| 120 | 270996 | C.next_char(); | |
| 121 | } | ||
| 122 | } | ||
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 270996 times.
|
270996 | if(bit > 31) { |
| 124 | ✗ | throw std::logic_error("Bit larger than 31"); | |
| 125 | } | ||
| 126 | 270996 | return ((C.x_ & (index_t(1u) << bit)) != 0); | |
| 127 | } | ||
| 128 | } | ||
| 129 |