Skip to content

Commit 0fc2d92

Browse files
author
hartsantler
committed
go backend: auto detect map type by looking at keys and values in a dict literal.
1 parent 6502b97 commit 0fc2d92

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,37 @@ def _visit_assign_helper(self, node, target):
17781778
if isinstance(node.value, ast.Lambda):
17791779
self.visit(node.value) ## writes function def
17801780
writer.write('%s = __lambda__' %self.visit(target))
1781+
1782+
elif isinstance(node.value, ast.Dict) and self._with_go:
1783+
key_type = None
1784+
val_type = None
1785+
1786+
for i in range( len(node.value.keys) ):
1787+
k = node.value.keys[ i ]
1788+
v = node.value.values[i]
1789+
if isinstance(k, ast.Str):
1790+
key_type = 'string'
1791+
elif isinstance(k, ast.Num):
1792+
key_type = 'int'
1793+
1794+
if isinstance(v, ast.Str):
1795+
val_type = 'string'
1796+
elif isinstance(v, ast.Num):
1797+
if isinstance(v.n, int):
1798+
val_type = 'int'
1799+
else:
1800+
val_type = 'float64'
1801+
1802+
if not key_type:
1803+
raise SyntaxError( self.format_error('can not determine dict key type') )
1804+
if not val_type:
1805+
raise SyntaxError( self.format_error('can not determine dict value type') )
1806+
1807+
t = self.visit(target)
1808+
v = self.visit(node.value)
1809+
writer.write('%s = __go__map__(%s, %s) << %s' %(t, key_type, val_type, v))
1810+
1811+
17811812
elif isinstance(node.value, ast.List) and self._with_go:
17821813
guess_type = None
17831814
for elt in node.value.elts:

regtests/go/loop_arrays.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ def main():
1616
z += v
1717
TestError( z == 'abc' )
1818

19-
#b = False
20-
#if 'a' in arr:
21-
# b = True
22-
#TestError( b == True )
23-
2419
b = 0
2520
for i in range(10):
2621
b += 1
@@ -32,3 +27,8 @@ def main():
3227
c += v
3328
d += i
3429
TestError( c == 'abc' )
30+
31+
e = 0
32+
for i in range( len(arr) ):
33+
e += 1
34+
TestError( e == 3 )

regtests/go/maps.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ def main():
77
'z': 3,
88
}
99

10-
print( a['x'] )
10+
#print( a['x'] )
11+
TestError( a['x']==1 )
1112

1213
b = map[int]string{ 0:'a', 1:'b' }
13-
print( b[0] )
14-
print( b[1] )
14+
#print( b[0] )
15+
#print( b[1] )
16+
TestError( b[0]=='a' )
17+
TestError( b[1]=='b' )
18+
19+
c = {'x':100, 'y':200}
20+
#print( c['x'] )
21+
#print( c['y'] )
22+
23+
TestError( c['x']==100 )
24+
TestError( c['y']==200 )

0 commit comments

Comments
 (0)