Skip to content

Commit

Permalink
Merge pull request beeware#357 from jootse84/master
Browse files Browse the repository at this point in the history
Added support for forelse clause
  • Loading branch information
freakboy3742 authored Feb 17, 2017
2 parents 95c220b + fcdc3f6 commit 90de6da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 0 additions & 4 deletions tests/structures/test_for.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest import expectedFailure

from ..utils import TranspileTestCase


Expand Down Expand Up @@ -52,7 +50,6 @@ def test_for_over_iterable(self):
print(i, total)
""")

@expectedFailure
def test_for_else(self):
self.assertCodeExecution("""
total = 0
Expand All @@ -63,7 +60,6 @@ def test_for_else(self):
print(total)
""")

@expectedFailure
def test_for_else_break(self):
self.assertCodeExecution("""
total = 0
Expand Down
2 changes: 0 additions & 2 deletions tests/structures/test_while.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from unittest import expectedFailure

from ..utils import TranspileTestCase

Expand Down Expand Up @@ -76,7 +75,6 @@ def test_while_not_forever(self):
break
""")

@expectedFailure
def test_while_else(self):
self.assertCodeExecution("""
i = 1
Expand Down
40 changes: 39 additions & 1 deletion voc/python/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from .types.primitives import (
ASTORE_name, ALOAD_name, free_name,
ICONST_val,
ICONST_val, ISTORE_name, ILOAD_name
)
from .types import java, python
from .debug import (
Expand Down Expand Up @@ -464,6 +464,11 @@ def visit_For(self, node):

loop = START_LOOP()

self.context.add_opcodes(
JavaOpcodes.ICONST_1(),
ISTORE_name('#loop-orelse-%x' % id(loop))
)

self.context.store_name('#for-iter-%x' % id(node))
self.context.add_opcodes(
loop,
Expand Down Expand Up @@ -496,6 +501,17 @@ def visit_For(self, node):
END_LOOP()
)

if node.orelse:
self.context.add_opcodes(
ILOAD_name('#loop-orelse-%x' % id(loop)),
IF([], JavaOpcodes.IFEQ)
)
for child in node.orelse:
self.visit(child)
self.context.add_opcodes(
END_IF()
)

# Clean up
self.context.delete_name('#for-iter-%x' % id(node))

Expand All @@ -504,6 +520,12 @@ def visit_While(self, node):
# expr test, stmt* body, stmt* orelse):

loop = START_LOOP()

self.context.add_opcodes(
JavaOpcodes.ICONST_1(),
ISTORE_name('#loop-orelse-%x' % id(loop)),
)

self.context.add_opcodes(
loop
)
Expand All @@ -525,6 +547,17 @@ def visit_While(self, node):
END_LOOP()
)

if node.orelse:
self.context.add_opcodes(
ILOAD_name('#loop-orelse-%x' % id(loop)),
IF([], JavaOpcodes.IFEQ)
)
for child in node.orelse:
self.visit(child)
self.context.add_opcodes(
END_IF()
)

@node_visitor
def visit_If(self, node):
# expr test, stmt* body, stmt* orelse):
Expand Down Expand Up @@ -879,6 +912,11 @@ def visit_Break(self, node):
for loop in self.context.loops[::-1]:
if loop.end_op is None:
break

self.context.add_opcodes(
JavaOpcodes.ICONST_0(),
ISTORE_name('#loop-orelse-%x' % id(loop)),
)
self.context.add_opcodes(
jump(JavaOpcodes.GOTO(0), self.context, loop, OpcodePosition.NEXT),
)
Expand Down

0 comments on commit 90de6da

Please sign in to comment.