提交 796578cc 编写于 作者: chai2010's avatar chai2010

函数签名支持可选 => 符号分隔返回值

上级 cd5d785b
...@@ -12,13 +12,13 @@ import ( ...@@ -12,13 +12,13 @@ import (
fmt.Println(42) fmt.Println(42)
pkg.Println(42 + 1) pkg.Println(42 + 1)
mypkg.Println(求和(100)) mypkg.Println(sum(100))
mymath.PrintPrime(30) mymath.PrintPrime(30)
heart() heart()
} }
函数 求和(n int) int { fn sum(n int) => int {
var v int var v int
for i := 1; i <= n; i++ { for i := 1; i <= n; i++ {
v += i v += i
......
...@@ -899,12 +899,18 @@ func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList { ...@@ -899,12 +899,18 @@ func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
return nil return nil
} }
func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList) { func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList, arrowPos token.Pos) {
if p.trace { if p.trace {
defer un(trace(p, "Signature")) defer un(trace(p, "Signature"))
} }
params = p.parseParameters(scope, true) params = p.parseParameters(scope, true)
if p.tok == token.ARROW {
arrowPos = p.pos
p.next()
}
results = p.parseResult(scope) results = p.parseResult(scope)
return return
...@@ -917,9 +923,14 @@ func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) { ...@@ -917,9 +923,14 @@ func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) {
pos := p.expect(token.FN) pos := p.expect(token.FN)
scope := ast.NewScope(p.topScope) // function scope scope := ast.NewScope(p.topScope) // function scope
params, results := p.parseSignature(scope) params, results, arrowPos := p.parseSignature(scope)
return &ast.FuncType{Func: pos, Params: params, Results: results}, scope return &ast.FuncType{
Func: pos,
Params: params,
ArrowPos: arrowPos,
Results: results,
}, scope
} }
func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field { func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field {
...@@ -935,8 +946,13 @@ func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field { ...@@ -935,8 +946,13 @@ func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field {
// method // method
idents = []*ast.Ident{ident} idents = []*ast.Ident{ident}
scope := ast.NewScope(nil) // method scope scope := ast.NewScope(nil) // method scope
params, results := p.parseSignature(scope) params, results, arrowPos := p.parseSignature(scope)
typ = &ast.FuncType{Func: token.NoPos, Params: params, Results: results} typ = &ast.FuncType{
Func: token.NoPos,
Params: params,
ArrowPos: arrowPos,
Results: results,
}
} else { } else {
// embedded interface // embedded interface
typ = x typ = x
...@@ -2321,7 +2337,7 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl { ...@@ -2321,7 +2337,7 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
ident := p.parseIdent() ident := p.parseIdent()
params, results := p.parseSignature(scope) params, results, arrowPos := p.parseSignature(scope)
var body *ast.BlockStmt var body *ast.BlockStmt
if p.tok == token.LBRACE { if p.tok == token.LBRACE {
...@@ -2334,9 +2350,10 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl { ...@@ -2334,9 +2350,10 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
Recv: recv, Recv: recv,
Name: ident, Name: ident,
Type: &ast.FuncType{ Type: &ast.FuncType{
Func: pos, Func: pos,
Params: params, Params: params,
Results: results, ArrowPos: arrowPos,
Results: results,
}, },
Body: body, Body: body,
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册