From 7ac15a7557f57dcc11105f80fe1fd10fb63e45c6 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 6 May 2020 18:20:12 +0300 Subject: [PATCH] compiler: support implicit type in function arguments Go supports declaring multiple arguments of the same type without duplicating type name. Now we support this too. --- pkg/compiler/codegen.go | 7 ++++--- pkg/compiler/function_call_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 067db290e..71cf35c7e 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -251,9 +251,10 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) { // Load the arguments in scope. for _, arg := range decl.Type.Params.List { - name := arg.Names[0].Name // for now. - l := c.scope.newLocal(name) - c.emitStoreLocal(l) + for _, id := range arg.Names { + l := c.scope.newLocal(id.Name) + c.emitStoreLocal(l) + } } // Load in all the global variables in to the scope of the function. // This is not necessary for syscalls. diff --git a/pkg/compiler/function_call_test.go b/pkg/compiler/function_call_test.go index 005707d76..8851078f0 100644 --- a/pkg/compiler/function_call_test.go +++ b/pkg/compiler/function_call_test.go @@ -158,3 +158,14 @@ func TestFunctionWithVoidReturnBranch(t *testing.T) { eval(t, src, big.NewInt(2)) }) } + +func TestFunctionWithMultipleArgumentNames(t *testing.T) { + src := `package foo + func Main() int { + return add(1, 2) + } + func add(a, b int) int { + return a + b + }` + eval(t, src, big.NewInt(3)) +}