提交 650cd479 编写于 作者: chai2010's avatar chai2010

函数参数和返回值类型支持可选的 : 分隔

上级 f708cebf
......@@ -40,6 +40,7 @@ jobs:
- run: go install
- run: wa -h
- run: wa hello.wa
- run: wa run _examples/hello
- run: wa run _examples/prime
......
# 版权 @2019 凹语言 作者。保留所有权利。
fn main() {
println("老凹,吃了吗")
println("凹凹,吃了吗")
println(answer())
}
fn answer() => (x:int) {
x = 40 + 2
return
}
......@@ -816,6 +816,7 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
var list []ast.Expr
for {
list = append(list, p.parseVarType(ellipsisOk))
// 开头的 Ident 列表, 遇到非 ',' 时结束
if p.tok != token.COMMA {
break
}
......@@ -825,11 +826,18 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
}
}
// 解析可选的 ':'
var colonPos token.Pos
if p.tok == token.COLON {
colonPos = p.pos
p.next()
}
// analyze case
if typ := p.tryVarType(ellipsisOk); typ != nil {
// IdentifierList Type
idents := p.makeIdentList(list)
field := &ast.Field{Names: idents, Type: typ}
field := &ast.Field{Names: idents, ColonPos: colonPos, Type: typ}
params = append(params, field)
// Go spec: The scope of an identifier denoting a function
// parameter or result variable is the function body.
......@@ -841,8 +849,12 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
p.next()
for p.tok != token.RPAREN && p.tok != token.EOF {
idents := p.parseIdentList()
if p.tok == token.COLON {
colonPos = p.pos
p.next()
}
typ := p.parseVarType(ellipsisOk)
field := &ast.Field{Names: idents, Type: typ}
field := &ast.Field{Names: idents, ColonPos: colonPos, Type: typ}
params = append(params, field)
// Go spec: The scope of an identifier denoting a function
// parameter or result variable is the function body.
......@@ -856,6 +868,12 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
return
}
// 缺少类型信息
if colonPos != token.NoPos {
p.errorExpected(p.pos, "type")
return
}
// Type { "," Type } (anonymous parameters)
params = make([]*ast.Field, len(list))
for i, typ := range list {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册