Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #879 from BPYap/#876-fix
Browse files Browse the repository at this point in the history
Fixed Incompatible Stack Height caused by expression statement
  • Loading branch information
freakboy3742 authored Aug 2, 2018
2 parents edbfc47 + 2355a05 commit f1243c6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions tests/structures/test_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def test_for_over_range(self):
print(i, total)
""")

# Test expression statement
self.assertCodeExecution("""
for i in range(10):
[1, 2, 3]
"abc" == "def"
""")

def test_for_over_iterable(self):
self.assertCodeExecution("""
total = 0
Expand Down
8 changes: 8 additions & 0 deletions tests/structures/test_if_elif_else.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_if(self):
print('Small x')
""")

# Test expression statement
self.assertCodeExecution("""
x = 1
if x < 5:
[1, 2, 3]
"abc" == "def"
""")

def test_if_else(self):
self.assertCodeExecution("""
x = 1
Expand Down
10 changes: 10 additions & 0 deletions tests/structures/test_try_catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def test_try_except(self):
print('Done.')
""")

# Test expression statement
self.assertCodeExecution("""
try:
[1, 2, 3]
"abc" == "def"
except:
print("Got an error")
print('Done.')
""")

def test_try_except_unnamed(self):
# No exception
self.assertCodeExecution("""
Expand Down
9 changes: 9 additions & 0 deletions tests/structures/test_while.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def test_while(self):
print(i, total)
""")

# Test expression statement
self.assertCodeExecution("""
x = True
while x:
[1, 2, 3]
"abc" == "def"
x = False
""")

def test_break(self):
self.assertCodeExecution("""
i = 0
Expand Down
6 changes: 3 additions & 3 deletions voc/python/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ def visit_Interactive(self, node):
def visit_Expr(self, node):
self.generic_visit(node)

# If the expression is a call, we need to ignore
# any return value from the function.
if isinstance(node.value, (ast.Call, ast.Attribute, ast.Str)):
# If the expression is not yield/yield from expression,
# we need to ignore expression value by popping it off from stack.
if not isinstance(node.value, (ast.Yield, ast.YieldFrom)):
self.context.add_opcodes(
JavaOpcodes.POP()
)
Expand Down

0 comments on commit f1243c6

Please sign in to comment.