From 650cd47975d958e6cd899c87e075736c7fdf553a Mon Sep 17 00:00:00 2001 From: chai2010 Date: Sun, 28 Aug 2022 10:01:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0=E5=92=8C?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E7=B1=BB=E5=9E=8B=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E7=9A=84=20:=20=E5=88=86=E9=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/wa.yml | 1 + hello.wa | 8 +++++++- internal/parser/parser.go | 22 ++++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.github/workflows/wa.yml b/.github/workflows/wa.yml index 749fa2c..e43d03f 100644 --- a/.github/workflows/wa.yml +++ b/.github/workflows/wa.yml @@ -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 diff --git a/hello.wa b/hello.wa index 2059fbb..e29361e 100644 --- a/hello.wa +++ b/hello.wa @@ -1,5 +1,11 @@ # 版权 @2019 凹语言 作者。保留所有权利。 fn main() { - println("老凹,吃了吗") + println("凹凹,吃了吗") + println(answer()) +} + +fn answer() => (x:int) { + x = 40 + 2 + return } diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 4df41c5..74bb09f 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -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 { -- GitLab