Skip to content

Commit 1fa8144

Browse files
author
hartsantler
committed
go backend: for x in array loops
1 parent 5396b24 commit 1fa8144

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3350,7 +3350,7 @@ def visit_For(self, node):
33503350
c.constant = True
33513351
self._call_ids += 1
33523352

3353-
if self._with_glsl:
3353+
if self._with_glsl or self._with_go:
33543354
writer.write( 'for %s in %s:' %(self.visit(node.target), self.visit(node.iter)) )
33553355
writer.push()
33563356
map(self.visit, node.body)

pythonjs/pythonjs_to_go.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,33 @@ def visit_Compare(self, node):
7070

7171
return ' '.join( comp )
7272

73+
def visit_For(self, node):
74+
target = self.visit(node.target)
75+
lines = []
76+
if isinstance(node.iter, ast.Call) and isinstance(node.iter.func, ast.Name):
77+
iter = self.visit(node.iter.args[0])
78+
79+
if node.iter.func.id == 'range':
80+
lines.append('for _,%s := range %s {' %(target, iter))
81+
elif node.iter.func.id == 'enumerate':
82+
lines.append('for %s,%s := range %s {')
83+
84+
else:
85+
raise SyntaxError('invalid for loop - bad iterator')
86+
87+
88+
else:
89+
iter = self.visit( node.iter )
90+
lines.append('for _,%s := range %s {' %(target, iter))
91+
92+
self.push()
93+
for b in node.body:
94+
lines.append( self.indent()+self.visit(b) )
95+
self.pull()
96+
lines.append( self.indent()+'}' ) ## end of for loop
97+
return '\n'.join(lines)
98+
99+
73100
def _visit_call_helper_go(self, node):
74101
name = self.visit(node.func)
75102
if name == '__go__':

regtests/go/loop_arrays.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
array loop
3+
'''
4+
5+
def main():
6+
7+
a = [1,2,3]
8+
y = 0
9+
for x in a:
10+
y += x
11+
TestError( y==6 )
12+
13+
z = ''
14+
arr = ['a', 'b', 'c']
15+
for v in arr:
16+
z += v
17+
TestError( z == 'abc' )
18+
19+
#b = False
20+
#if 'a' in arr:
21+
# b = True
22+
#TestError( b == True )
23+

0 commit comments

Comments
 (0)