Skip to content

Commit 292168f

Browse files
committed
parser 增加泛型宏参数解析; 类型定义必须 : 分隔
1 parent 7ed7e83 commit 292168f

File tree

21 files changed

+98
-57
lines changed

21 files changed

+98
-57
lines changed

internal/parser/parser.go

+49-9
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
847847
colonPos = p.pos
848848
p.next()
849849
} else {
850-
if p.tok != token.RPAREN {
850+
if p.tok != token.RPAREN && p.tok != token.RBRACK {
851851
p.expect(token.COLON)
852852
}
853853
}
@@ -941,6 +941,33 @@ func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
941941
return nil
942942
}
943943

944+
func (p *parser) parseTypeParams(scope *ast.Scope) (tparams *ast.FieldList) {
945+
if p.trace {
946+
defer un(trace(p, "TypeParams"))
947+
}
948+
949+
// 数组和切片歧义, 必须加 ':' 分隔
950+
// type byteReplacer :[256]byte
951+
// type byteReplacer :[]byte
952+
953+
if p.tok != token.LBRACK {
954+
return nil
955+
}
956+
957+
lparen := p.expect(token.LBRACK)
958+
959+
if p.tok == token.RBRACK {
960+
p.next()
961+
return nil
962+
}
963+
964+
params := p.parseParameterList(scope, false)
965+
rparen := p.expect(token.RBRACK)
966+
967+
tparams = &ast.FieldList{Opening: lparen, List: params, Closing: rparen}
968+
return
969+
}
970+
944971
func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList, arrowPos token.Pos) {
945972
if p.trace {
946973
defer un(trace(p, "Signature"))
@@ -986,13 +1013,16 @@ func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) {
9861013

9871014
pos := p.expect(token.FUNC)
9881015
scope := ast.NewScope(p.topScope) // function scope
1016+
1017+
tparams := p.parseTypeParams(scope)
9891018
params, results, arrowPos := p.parseSignature(scope)
9901019

9911020
return &ast.FuncType{
992-
Func: pos,
993-
Params: params,
994-
ArrowPos: arrowPos,
995-
Results: results,
1021+
Func: pos,
1022+
TypeParams: tparams,
1023+
Params: params,
1024+
ArrowPos: arrowPos,
1025+
Results: results,
9961026
}, scope
9971027
}
9981028

@@ -2439,12 +2469,20 @@ func (p *parser) parseTypeSpec(doc *ast.CommentGroup, _ token.Token, _ int) ast.
24392469
// containing block.
24402470
// (Global identifiers are resolved in a separate phase after parsing.)
24412471
spec := &ast.TypeSpec{Doc: doc, Name: ident}
2472+
spec.TypeParams = p.parseTypeParams(p.topScope)
24422473
p.declare(spec, nil, p.topScope, ast.Typ, ident)
24432474
if p.tok == token.COLON {
24442475
spec.ColonPos = p.pos
24452476
p.next()
24462477
}
24472478
spec.Type = p.parseType()
2479+
2480+
if _, ok := spec.Type.(*ast.StructType); !ok {
2481+
if spec.TypeParams != nil {
2482+
p.error(spec.TypeParams.Opening, "type params only support struct type")
2483+
}
2484+
}
2485+
24482486
p.expectSemi() // call before accessing p.linecomment
24492487
spec.Comment = p.lineComment
24502488

@@ -2523,6 +2561,7 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
25232561
}
25242562
}
25252563

2564+
tparams := p.parseTypeParams(scope)
25262565
params, results, arrowPos := p.parseSignature(scope)
25272566

25282567
var body *ast.BlockStmt
@@ -2536,10 +2575,11 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
25362575
Recv: recv,
25372576
Name: ident,
25382577
Type: &ast.FuncType{
2539-
Func: pos,
2540-
Params: params,
2541-
ArrowPos: arrowPos,
2542-
Results: results,
2578+
Func: pos,
2579+
TypeParams: tparams,
2580+
Params: params,
2581+
ArrowPos: arrowPos,
2582+
Results: results,
25432583
},
25442584
Body: body,
25452585
}

internal/printer/nodes.go

+1
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
15711571
} else {
15721572
p.print(vtab)
15731573
}
1574+
p.print(token.COLON)
15741575
p.expr(s.Type)
15751576
p.setComment(s.Comment)
15761577

waroot/examples/brainfuck.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func main {
77
vm.Run()
88
}
99

10-
type BrainFuck struct {
10+
type BrainFuck :struct {
1111
mem: [30000]byte
1212
code: string
1313
pos: int

waroot/examples/brainfuck/src/bfpkg/brpkg.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 版权 @2022 凹语言 作者。保留所有权利。
22

33
// BF 虚拟机
4-
type BrainFuck struct {
4+
type BrainFuck :struct {
55
mem: [30000]byte
66
code: string
77
pos: int

waroot/examples/eq.wa

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 版权 @2021 凹语言 作者。保留所有权利。
22

3-
type T1 struct {
3+
type T1 :struct {
44
a: i32
55
b: string
66
}
@@ -9,11 +9,11 @@ func T1.print {
99
println("a: ", this.a)
1010
}
1111

12-
type I interface {
12+
type I :interface {
1313
print()
1414
}
1515

16-
type T2 struct {
16+
type T2 :struct {
1717
a: []i32
1818
}
1919

waroot/examples/expr/y.wa

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import "strconv" => __yystrconv__
88

9-
type exprSymType struct {
9+
type exprSymType :struct {
1010
yys: int
1111
num: int
1212
}
@@ -35,12 +35,12 @@ const exprInitialStackSize = 16
3535
// Lex 结束标志
3636
const eof = 0
3737

38-
type exprToken struct {
38+
type exprToken :struct {
3939
Kind: int
4040
Value: int
4141
}
4242

43-
type exprLexer struct {
43+
type exprLexer :struct {
4444
tokens: []exprToken
4545
pos: int
4646
}
@@ -143,7 +143,7 @@ global exprTok3 = [...]int{
143143
0,
144144
}
145145

146-
type exprErrorMessageInfo struct {
146+
type exprErrorMessageInfo :struct {
147147
state: int
148148
token: int
149149
msg: string
@@ -158,7 +158,7 @@ global (
158158
exprErrorVerbose = false
159159
)
160160

161-
type exprParser struct {
161+
type exprParser :struct {
162162
lval: exprSymType
163163
stack: [exprInitialStackSize]exprSymType
164164
char: int

waroot/examples/interface_named.wa

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// 版权 @2021 凹语言 作者。保留所有权利。
22

3-
type T1 struct {
3+
type T1 :struct {
44
a: i32
55
}
66

7-
type T2 struct {
7+
type T2 :struct {
88
b: i32
99
}
1010

11-
type I1 interface {
11+
type I1 :interface {
1212
f()
1313
}
1414

15-
type I2 interface {
15+
type I2 :interface {
1616
f2()
1717
}
1818

waroot/examples/life-game/src/life.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func LifeInit(w, h, s: int) {
4040
}
4141
}
4242

43-
type DIR struct {
43+
type DIR :struct {
4444
x: int
4545
y: int
4646
}

waroot/examples/misc/closure.wa

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// 版权 @2021 凹语言 作者。保留所有权利。
22

3-
type FP func(i: i32) => i32
3+
type FP :func(i: i32) => i32
44

5-
type ST struct {
5+
type ST :struct {
66
i: i32
77
}
88

waroot/examples/misc/multi_ret.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ func Swap(i, j: ST) => (ST, ST) {
1414
return j, i
1515
}
1616

17-
type ST struct {
17+
type ST :struct {
1818
i, j: i32
1919
}

waroot/examples/misc/native_test_.wa

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func test_array(a: int, b: int) {
146146
println("arr[", b, "][", a, "] = ", arr[b][a])
147147
}
148148

149-
type t0 struct {
149+
type t0 :struct {
150150
a: i64
151151
b: i32
152152
c: [4]f64
@@ -181,7 +181,7 @@ func test_struct1(a: int, b: int, c: int) {
181181
}
182182
}
183183

184-
type struct_t0 struct {
184+
type struct_t0 :struct {
185185
arr0: [16]int
186186
arr1: [16]f32
187187
}
@@ -234,7 +234,7 @@ func test_struct4 {
234234
}
235235
}
236236

237-
type fff32 f32
237+
type fff32 :f32
238238

239239
global arr0: [32]fff32
240240
global arr1: [32]fff32
@@ -359,7 +359,7 @@ func test_global_consts() {
359359
}
360360
}
361361

362-
type ty0 struct {
362+
type ty0 :struct {
363363
v0: int
364364
v1: f64
365365
}

waroot/examples/misc/struct.wa

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ func main {
1010
println(j.b)
1111
}
1212

13-
type sp struct {
13+
type sp :struct {
1414
a: *i32
1515
}
1616

17-
type sc struct {
17+
type sc :struct {
1818
b: i32
1919
sp
2020
}

waroot/examples/pkg/src/main.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"pkg/mypkg"
55
)
66

7-
type ST struct {
7+
type ST :struct {
88
A: string
99
B: i32
1010
}

waroot/examples/pkg/src/mypkg/mypkg.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 版权 @2022 hello 作者。保留所有权利。
22

3-
type ST struct {
3+
type ST :struct {
44
A: i32
55
B: string
66
}

waroot/examples/snake/src/canvas/canvas.wa

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ buf为帧缓存指针
1717
func updateCanvas_JS(id: u32, buf: *u32)
1818

1919
//画布事件回调函数原型
20-
type OnTouch func(x, y: u32)
21-
type OnKey func(key: u32)
20+
type OnTouch :func(x, y: u32)
21+
type OnKey :func(key: u32)
2222

2323
//画布对象
24-
type Canvas struct {
24+
type Canvas :struct {
2525
device_id: u32 //画布对象对应的网页DOM对象id
2626
width: u32 //画布宽度,以像素为单位
2727
height: u32 //画布高度,以像素为单位
2828
frame_buf: []u32 //画布帧缓存,容量为Width * Height
2929
}
3030

3131
//画布事件
32-
type CanvasEvents struct {
32+
type CanvasEvents :struct {
3333
Device_id: u32 //画布设备ID
3434
OnMouseDown: OnTouch //鼠标按下时的回调处理函数
3535
OnMouseUp: OnTouch //鼠标松开时的回调处理函数

waroot/examples/snake/src/main.wa

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Canvas_OnKeyUp(id, key: u32) {
1717
canvas.OnKeyUp(id, key)
1818
}
1919

20-
type Position struct {
20+
type Position :struct {
2121
x, y: i32
2222
}
2323

@@ -41,7 +41,7 @@ const (
4141

4242
global Dirs: [5]Position
4343

44-
type GameState struct {
44+
type GameState :struct {
4545
w, h: i32
4646
scale: i32
4747
grid: []u8

waroot/examples/struct.wa

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func main {
3030
println(Info.name, " ", Info.age) //李四 66
3131
}
3232

33-
type sp struct {
33+
type sp :struct {
3434
a: *i32
3535
}
3636

37-
type sc struct {
37+
type sc :struct {
3838
b: i32
3939
sp
4040
}

waroot/src/hash/crc32/crc32.wa

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
)
3636

3737
// Table is a 256-word table representing the polynomial for efficient processing.
38-
type Table [256]uint32
38+
type Table :[256]uint32
3939

4040
// This file makes use of functions implemented in architecture-specific files.
4141
// The interface that they implement is as follows:
@@ -148,7 +148,7 @@ func MakeTable(poly: uint32) => *Table {
148148
}
149149

150150
// digest represents the partial evaluation of a checksum.
151-
type digest struct {
151+
type digest :struct {
152152
crc: uint32
153153
tab: *Table
154154
}

waroot/src/hash/crc32/crc32_generic.wa

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func simpleUpdate(crc: uint32, tab: *Table, p: []byte) => uint32 {
4747
const slicing8Cutoff = 16
4848

4949
// slicing8Table is array of 8 Tables, used by the slicing-by-8 algorithm.
50-
type slicing8Table [8]Table
50+
type slicing8Table :[8]Table
5151

5252
// slicingMakeTable constructs a slicing8Table for the specified polynomial. The
5353
// table is suitable for use with the slicing-by-8 algorithm (slicingUpdate).

0 commit comments

Comments
 (0)