Skip to content

Commit 2e09cd7

Browse files
author
hartsantler
committed
go backend: support function calls with variable arguments def f(*args)
1 parent ecec73d commit 2e09cd7

4 files changed

Lines changed: 31 additions & 6 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,8 @@ def visit_Call(self, node):
22762276
args = list( map(self.visit, node.args) )
22772277
if node.keywords:
22782278
args.extend( ['%s=%s'%(x.arg,self.visit(x.value)) for x in node.keywords] )
2279+
if node.starargs:
2280+
args.append('*%s' %self.visit(node.starargs))
22792281
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
22802282

22812283
elif self._with_js or self._with_dart:
@@ -2931,10 +2933,6 @@ def visit_FunctionDef(self, node):
29312933

29322934
elif self._with_go:
29332935

2934-
if node.args.vararg:
2935-
raise SyntaxError( 'TODO go backend, variable arguments (*args)' )
2936-
2937-
29382936
args = []
29392937
offset = len(node.args.args) - len(node.args.defaults)
29402938
for i, arg in enumerate(node.args.args):
@@ -2946,6 +2944,9 @@ def visit_FunctionDef(self, node):
29462944
else:
29472945
args.append( a )
29482946

2947+
if node.args.vararg:
2948+
args.append( '*%s' %node.args.vararg )
2949+
29492950
writer.write( 'def %s( %s ):' % (node.name, ','.join(args)) )
29502951

29512952

pythonjs/pythonjs_to_go.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def _visit_call_helper(self, node):
129129
args += ','.join( x )
130130
args += '}'
131131

132+
if node.starargs:
133+
if args: args += ','
134+
args += '%s...' %self.visit(node.starargs)
135+
132136
return '%s(%s)' % (fname, args)
133137

134138
def _visit_call_helper_go(self, node):
@@ -192,6 +196,11 @@ def _visit_function(self, node):
192196
#args.append( '{%s}' % ','.join(oargs) )
193197
args.append( '__kwargs _kwargs_type_')
194198

199+
if node.args.vararg:
200+
starargs = node.args.vararg
201+
assert starargs in args_typedefs
202+
args.append( '%s ...%s' %(starargs, args_typedefs[starargs]))
203+
195204
####
196205
out = []
197206
if return_type:

pythonjs/typedpython.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,15 @@ def transform_source( source, strip=False ):
259259
chan = False
260260
if len(arg.split()) == 2:
261261
chan, arg = arg.split()
262+
if '*' in arg:
263+
arg_name = arg.split('*')[-1]
264+
else:
265+
arg_name = arg
266+
262267
if chan:
263-
output.append('%s@typedef_chan(%s=%s)' %(indent, arg,typedef))
268+
output.append('%s@typedef_chan(%s=%s)' %(indent, arg_name, typedef))
264269
else:
265-
output.append('%s@typedef(%s=%s)' %(indent, arg,typedef))
270+
output.append('%s@typedef(%s=%s)' %(indent, arg_name, typedef))
266271
if kw:
267272
arg += '=' + kw
268273
args.append(arg)
@@ -405,6 +410,9 @@ def f(a:int, b:int, c:int) ->int:
405410
def f(a:int=100, b:int=100) ->int:
406411
return a+b
407412
413+
def f(*args:int, **kwargs:int) ->int:
414+
return a+b
415+
408416
'''
409417

410418
if __name__ == '__main__':

regtests/go/func_calls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ def f(a:int, b:int, c:int) ->int:
55
def f2(a:int=1, b:int=2, c:int=3) ->int:
66
return a+b+c
77

8+
def f3( *args:int ) ->int:
9+
return args[0] + args[1] + args[2]
10+
811

912
def main():
1013
TestError( f(1,2,3) == 6)
1114

1215
x = f2( b=100 )
1316
TestError(x==104)
17+
18+
arr = [1,2,3]
19+
y = f3( *arr )
20+
TestError( y==6 )

0 commit comments

Comments
 (0)