Permalink
Browse files
Properly implement 'break' and 'continue' in C-style for loop.
- Loading branch information...
Showing
with
30 additions
and
9 deletions.
-
+23
−7
core/cmd_exec.py
-
+7
−2
spec/for-expr.test.sh
|
|
@@ -964,7 +964,6 @@ def _Dispatch(self, node, fork_external): |
|
|
break
|
|
|
elif e.IsContinue():
|
|
|
status = 0
|
|
|
continue
|
|
|
else: # return needs to pop up more
|
|
|
raise
|
|
|
finally:
|
|
|
@@ -973,13 +972,30 @@ def _Dispatch(self, node, fork_external): |
|
|
elif node.tag == command_e.ForExpr:
|
|
|
status = 0
|
|
|
self.arith_ev.Eval(node.init)
|
|
|
while True:
|
|
|
b = self.arith_ev.Eval(node.cond)
|
|
|
if not b:
|
|
|
break
|
|
|
|
|
|
status = self._Execute(node.body)
|
|
|
self.arith_ev.Eval(node.update)
|
|
|
self.loop_level += 1
|
|
|
try:
|
|
|
while True:
|
|
|
b = self.arith_ev.Eval(node.cond)
|
|
|
if not b:
|
|
|
break
|
|
|
|
|
|
do_continue = False
|
|
|
try:
|
|
|
status = self._Execute(node.body)
|
|
|
except _ControlFlow as e:
|
|
|
if e.IsBreak():
|
|
|
status = 0
|
|
|
break
|
|
|
elif e.IsContinue():
|
|
|
status = 0
|
|
|
else: # return needs to pop up more
|
|
|
raise
|
|
|
|
|
|
self.arith_ev.Eval(node.update)
|
|
|
|
|
|
finally:
|
|
|
self.loop_level -= 1
|
|
|
|
|
|
elif node.tag == command_e.DoGroup:
|
|
|
status = self._ExecuteList(node.children)
|
|
|
|
|
|
@@ -4,16 +4,21 @@ |
|
|
# mksh implements most too.
|
|
|
|
|
|
### C-style for loop
|
|
|
n=5
|
|
|
n=10
|
|
|
for ((a=1; a <= n ; a++)) # Double parentheses, and naked 'n'
|
|
|
do
|
|
|
if test $a = 3; then
|
|
|
continue
|
|
|
fi
|
|
|
if test $a = 6; then
|
|
|
break
|
|
|
fi
|
|
|
echo $a
|
|
|
done # A construct borrowed from ksh93.
|
|
|
## status: 0
|
|
|
## STDOUT:
|
|
|
1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
## N-I mksh status: 1
|
|
|
|
0 comments on commit
c7da30c