Skip to content

Commit

Permalink
add map typing (genshinsim#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
srliao authored Sep 6, 2023
1 parent 5dced62 commit ee7cf73
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/gcs/ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,10 @@ type (
StringType struct {
Pos //position of :
}

MapType struct {
Pos //position of keyword map
}
FuncType struct {
Pos //position of opening (
ArgsType []ExprType
Expand All @@ -1038,6 +1042,7 @@ type (
// exprTypeNode()
func (*NumberType) exprTypeNode() {}
func (*StringType) exprTypeNode() {}
func (*MapType) exprTypeNode() {}
func (*FuncType) exprTypeNode() {}

// NumberType.
Expand Down Expand Up @@ -1088,6 +1093,30 @@ func (s *StringType) writeTo(sb *strings.Builder) {
sb.WriteString("string")
}

// MapType.
func (m *MapType) CopyExprType() ExprType {
if m == nil {
return nil
}
return &MapType{
Pos: m.Pos,
}
}

func (m *MapType) Copy() Node {
return m.CopyExprType()
}

func (m *MapType) String() string {
var sb strings.Builder
m.writeTo(&sb)
return sb.String()
}

func (m *MapType) writeTo(sb *strings.Builder) {
sb.WriteString("map")
}

// FuncType.
func (f *FuncType) copyFuncType() *FuncType {
next := &FuncType{
Expand Down
5 changes: 5 additions & 0 deletions pkg/gcs/ast/parseFn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func TestFnTyping(t *testing.T) {
t,
)

parseAndPrint(
`fn z(a number, b number, c map) { print(c); }`,
t,
)

}

func TestAnonFn(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/gcs/ast/parseTyping.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (p *Parser) parseBasicType() (ExprType, error) {
return &StringType{Pos: n.pos}, nil
case "number":
return &NumberType{Pos: n.pos}, nil
case "map":
return &MapType{Pos: n.pos}, nil
default:
return nil, fmt.Errorf("ln%v: unexpected basic type parsing type info; got %v", n.line, n.Val)
}
Expand Down

0 comments on commit ee7cf73

Please sign in to comment.