summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2018-03-28 20:29:20 +0200
committerUlf Magnusson <ulfalizer@gmail.com>2018-03-28 20:29:20 +0200
commit7245bad9ebb58fc8ce5a322081fc6c39d2dc59c6 (patch)
tree9019fdbab7696092551c3124391a4ec4df7bb262
parenta5ea0dcbc8b9343f6901654ad92de6ece156f954 (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)".
-rw-r--r--kconfiglib.py16
-rw-r--r--tests/Kstr4
-rw-r--r--testsuite.py4
3 files changed, 14 insertions, 10 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)
diff --git a/tests/Kstr b/tests/Kstr
index 2498a74..547da9c 100644
--- a/tests/Kstr
+++ b/tests/Kstr
@@ -30,7 +30,7 @@ config ADVANCED
prompt "prompt 2"
menuconfig ADVANCED
- prompt "prompt 3" if DEP2 && (DEP3 || !DEP4 || !(DEP5 && DEP6))
+ prompt "prompt 3"
if DEP3
@@ -40,6 +40,8 @@ menu "foo"
config ADVANCED
help
second help text
+ depends on A || !B || (C && D) || !(E && F) || G = H || \
+ (I && !J && (K || L) && !(M || N) && O = P)
endmenu
diff --git a/testsuite.py b/testsuite.py
index 9177efd..5d8780e 100644
--- a/testsuite.py
+++ b/testsuite.py
@@ -528,10 +528,10 @@ config ADVANCED
prompt "prompt 2"
menuconfig ADVANCED
- prompt "prompt 3" if DEP2 && (DEP3 || !DEP4 || !(DEP5 && DEP6))
+ prompt "prompt 3"
config ADVANCED
- depends on DEP4 && DEP3
+ depends on (A || !B || (C && D) || !(E && F) || G = H || (I && !J && (K || L) && !(M || N) && O = P)) && DEP4 && DEP3
help
second help text
""")