Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
88e43ac0
Commit
88e43ac0
authored
Jan 17, 2017
by
Dylan Trotter
Committed by
GitHub
Jan 17, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix continue/break in else block behavior (#146)
Fixes:
https://github.com/google/grumpy/issues/123
parent
8f3ca94c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
1 deletion
+38
-1
compiler/stmt.py
compiler/stmt.py
+5
-1
compiler/stmt_test.py
compiler/stmt_test.py
+10
-0
testing/for_test.py
testing/for_test.py
+23
-0
No files found.
compiler/stmt.py
View file @
88e43ac0
...
...
@@ -173,6 +173,8 @@ class StatementVisitor(ast.NodeVisitor):
self
.
_tie_target
(
target
,
value
.
expr
)
def
visit_Break
(
self
,
node
):
if
not
self
.
block
.
loop_stack
:
raise
util
.
ParseError
(
node
,
"'break' not in loop"
)
self
.
_write_py_context
(
node
.
lineno
)
self
.
writer
.
write
(
'goto Label{}'
.
format
(
self
.
block
.
top_loop
().
end_label
))
...
...
@@ -242,6 +244,8 @@ class StatementVisitor(ast.NodeVisitor):
self
.
block
.
bind_var
(
self
.
writer
,
node
.
name
,
type_
.
expr
)
def
visit_Continue
(
self
,
node
):
if
not
self
.
block
.
loop_stack
:
raise
util
.
ParseError
(
node
,
"'continue' not in loop"
)
self
.
_write_py_context
(
node
.
lineno
)
self
.
writer
.
write
(
'goto Label{}'
.
format
(
self
.
block
.
top_loop
().
start_label
))
...
...
@@ -296,13 +300,13 @@ class StatementVisitor(ast.NodeVisitor):
self
.
_visit_each
(
node
.
body
)
self
.
writer
.
write
(
'goto Label{}'
.
format
(
loop
.
start_label
))
self
.
block
.
pop_loop
()
if
node
.
orelse
:
self
.
writer
.
write_label
(
orelse_label
)
self
.
_visit_each
(
node
.
orelse
)
# Avoid label "defined and not used" in case there's no break statements.
self
.
writer
.
write
(
'goto Label{}'
.
format
(
loop
.
end_label
))
self
.
writer
.
write_label
(
loop
.
end_label
)
self
.
block
.
pop_loop
()
def
visit_FunctionDef
(
self
,
node
):
self
.
_write_py_context
(
node
.
lineno
)
...
...
compiler/stmt_test.py
View file @
88e43ac0
...
...
@@ -202,6 +202,16 @@ class StatementVisitorTest(unittest.TestCase):
else:
print 'bar'"""
)))
def
testForElseBreakNotNested
(
self
):
self
.
assertRaisesRegexp
(
util
.
ParseError
,
"'continue' not in loop"
,
_ParseAndVisit
,
'for i in (1,):
\
n
pass
\
n
else:
\
n
continue'
)
def
testForElseContinueNotNested
(
self
):
self
.
assertRaisesRegexp
(
util
.
ParseError
,
"'continue' not in loop"
,
_ParseAndVisit
,
'for i in (1,):
\
n
pass
\
n
else:
\
n
continue'
)
def
testFunctionDef
(
self
):
self
.
assertEqual
((
0
,
'bar baz
\
n
'
),
_GrumpRun
(
textwrap
.
dedent
(
"""
\
def foo(a, b):
...
...
testing/for_test.py
View file @
88e43ac0
...
...
@@ -51,3 +51,26 @@ for i, j in [('a', 1), ('b', 2)]:
l
.
append
(
i
)
l
.
append
(
j
)
assert
l
==
[
'a'
,
1
,
'b'
,
2
]
# break and continue statements in an else clause applies to the outer loop.
# See: https://github.com/google/grumpy/issues/123
l
=
[]
for
i
in
range
(
2
):
l
.
append
(
i
)
for
j
in
range
(
10
,
12
):
l
.
append
(
j
)
else
:
l
.
append
(
12
)
continue
l
.
append
(
-
1
)
assert
l
==
[
0
,
10
,
11
,
12
,
1
,
10
,
11
,
12
]
l
=
[]
for
i
in
range
(
10
):
l
.
append
(
i
)
for
j
in
range
(
10
,
12
):
l
.
append
(
j
)
else
:
break
l
.
append
(
-
1
)
assert
l
==
[
0
,
10
,
11
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment