diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2018-03-28 20:29:20 +0200 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2018-03-28 20:29:20 +0200 |
| commit | 7245bad9ebb58fc8ce5a322081fc6c39d2dc59c6 (patch) | |
| tree | 9019fdbab7696092551c3124391a4ec4df7bb262 /kconfiglib.py | |
| parent | a5ea0dcbc8b9343f6901654ad92de6ece156f954 (diff) | |
Parenthesize && expressions within || expressions
This is redundant from an evaluation perspective, as && has higher
precedence than ||, but is easier to read.
A && B || C && D is now rendered as "(A && B) || (C && D)".
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index cbe61c7..56ecd06 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -4122,11 +4122,14 @@ def expr_str(expr): return "!" + expr_str(expr[1]) # Symbol if expr[0] == AND: - return "{} && {}".format(_format_and_op(expr[1]), - _format_and_op(expr[2])) + return "{} && {}".format(_parenthesize(expr[1], OR), + _parenthesize(expr[2], OR)) if expr[0] == OR: - return "{} || {}".format(expr_str(expr[1]), expr_str(expr[2])) + # This turns A && B || C && D into "(A && B) || (C && D)", which is + # redundant, but more readable + return "{} || {}".format(_parenthesize(expr[1], AND), + _parenthesize(expr[2], AND)) # Relation return "{} {} {}".format(expr_str(expr[1]), @@ -4210,11 +4213,10 @@ def _make_depend_on(sym, expr): _internal_error("Internal error while fetching symbols from an " "expression with token stream {}.".format(expr)) -def _format_and_op(expr): - # expr_str() helper. Returns the string representation of 'expr', which is - # assumed to be an operand to AND, with parentheses added if needed. +def _parenthesize(expr, type_): + # expr_str() helper. Adds parentheses around expressions of type 'type_'. - if isinstance(expr, tuple) and expr[0] == OR: + if isinstance(expr, tuple) and expr[0] == type_: return "({})".format(expr_str(expr)) return expr_str(expr) |
