aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2012-02-23 11:26:29 +0800
committerMu Qiao <qiaomuf@gentoo.org>2012-02-23 11:27:18 +0800
commit49facd905dadf5bae1b33362686d1d02be80a5fe (patch)
treec514302c5a93a17638a11889ecdc16743a4b26dd
parentParser: respect operator precedence in keyword test (diff)
downloadlibbash-49facd905dadf5bae1b33362686d1d02be80a5fe.tar.gz
libbash-49facd905dadf5bae1b33362686d1d02be80a5fe.tar.bz2
libbash-49facd905dadf5bae1b33362686d1d02be80a5fe.zip
Walker: support shortcut in keyword test
-rw-r--r--bashast/libbashWalker.g16
-rw-r--r--scripts/test_expr.bash6
2 files changed, 20 insertions, 2 deletions
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index bfcb73b..e357642 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -763,8 +763,20 @@ common_condition returns[bool status]
|string_expr { $status = (!$string_expr.libbash_value.empty()); };
keyword_condition returns[bool status]
- :^(LOGICOR l=keyword_condition r=keyword_condition) { $status= l || r; }
- |^(LOGICAND l=keyword_condition r=keyword_condition) { $status= l && r; }
+ :^(LOGICOR l=keyword_condition {
+ if(l){
+ seek_to_next_tree(ctx);
+ SEEK(INDEX() + 1);
+ return true;
+ }
+ } r=keyword_condition) { $status= l || r; }
+ |^(LOGICAND l=keyword_condition {
+ if(!l){
+ seek_to_next_tree(ctx);
+ SEEK(INDEX() + 1);
+ return false;
+ }
+ } r=keyword_condition) { $status= l && r; }
|^(NEGATION l=keyword_condition) { $status = !l; }
|^(MATCH_REGULAR_EXPRESSION left_str=string_expr right_str=string_expr) {
boost::xpressive::sregex re = boost::xpressive::sregex::compile(right_str.libbash_value);
diff --git a/scripts/test_expr.bash b/scripts/test_expr.bash
index ccedb76..6ae7246 100644
--- a/scripts/test_expr.bash
+++ b/scripts/test_expr.bash
@@ -55,3 +55,9 @@ unset i
[[ =a <=b ]]
[[ =a >=b ]]
[[ a == a || c == b && a == b ]] && echo true
+i=1
+[[ a == b || $((i=0)) ]] && echo $i # i should be 0 now
+[[ a == a || $((i=1)) ]] && echo $i # i should still be 0
+[[ a == b && $((i=1)) ]] || echo $i # i should still be 0
+i=1
+[[ a == a && $((i=0)) ]] && echo $i # i should still be 0