Permalink
Browse files

Properly implement 'break' and 'continue' in C-style for loop.

Addresses issue #66.
  • Loading branch information...
Andy Chu
Andy Chu committed Jan 19, 2018
1 parent 40c0c79 commit c7da30c72a109e7b718f8a29702767db149db410
Showing with 30 additions and 9 deletions.
  1. +23 −7 core/cmd_exec.py
  2. +7 −2 spec/for-expr.test.sh
View
@@ -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)
View
@@ -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

Please sign in to comment.