Skip to content

Commit bda74ee

Browse files
author
hartsantler
committed
support while/else syntax.
1 parent cbbdeb7 commit bda74ee

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,6 +2901,7 @@ def visit_Continue(self, node):
29012901
return ''
29022902

29032903
def visit_Break(self, node):
2904+
writer.write('__break__ = True')
29042905
writer.write('break')
29052906

29062907
def visit_For(self, node):
@@ -3131,27 +3132,34 @@ def visit_For(self, node):
31313132

31323133
_call_ids = 0
31333134
def visit_While(self, node):
3134-
log('while loop:')
31353135
if self._cache_while_body_calls: ## TODO add option for this
31363136
for n in node.body:
31373137
calls = collect_calls(n)
31383138
for c in calls:
3139-
log('--call: %s' %c)
3140-
log('------: %s' %c.func)
31413139
if isinstance(c.func, ast.Name): ## these are constant for sure
31423140
i = self._call_ids
31433141
writer.write( '__call__%s = __get__(%s,"__call__")' %(i,self.visit(c.func)) )
31443142
c.func.id = '__call__%s'%i
31453143
c.constant = True
31463144
self._call_ids += 1
31473145

3146+
if node.orelse:
3147+
writer.write('var(__break__)')
3148+
writer.write('__break__ = False')
3149+
31483150
self._in_while_test = True
31493151
writer.write('while %s:' % self.visit(node.test))
31503152
self._in_while_test = False
31513153
writer.push()
31523154
map(self.visit, node.body)
31533155
writer.pull()
31543156

3157+
if node.orelse:
3158+
writer.write('if __break__ == False:')
3159+
writer.push()
3160+
map(self.visit, node.orelse)
3161+
writer.pull()
3162+
31553163
def visit_With(self, node):
31563164
global writer
31573165

regtests/loop/while_else.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
while else loop
3+
'''
4+
5+
def main():
6+
7+
a = False
8+
i = 0
9+
while i < 10:
10+
i += 1
11+
else:
12+
a = True
13+
14+
TestError( a==True )
15+
16+
b = False
17+
i = 0
18+
while i < 10:
19+
i += 1
20+
break
21+
else:
22+
b = True
23+
24+
TestError( b==False )
25+

0 commit comments

Comments
 (0)