Skip to content

Commit

Permalink
feat: Rust flatbuffer serialization for MonoType and TypeExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapacik committed Aug 3, 2020
1 parent 4a99e08 commit 3fa170d
Show file tree
Hide file tree
Showing 10 changed files with 1,443 additions and 269 deletions.
18 changes: 12 additions & 6 deletions ast/ast.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace fbast;

union MonoType {
NamedType,
TvarType,
ArrayType,
RecordType,
FunctionType,
Expand All @@ -12,15 +13,20 @@ table NamedType {
id:Identifier;
}

table TvarType {
base_node:BaseNode;
id:Identifier;
}

table ArrayType {
base_node:BaseNode;
element_type:MonoType;
element:MonoType;
}

table PropertyType {
base_node:BaseNode;
id:Identifier;
ty:MonoType;
monotype:MonoType;
}

table RecordType {
Expand All @@ -37,15 +43,15 @@ enum ParameterKind : byte {

table ParameterType {
base_node:BaseNode;
name:Identifier;
ty:MonoType;
id:Identifier;
monotype:MonoType;
kind:ParameterKind;
}

table FunctionType {
base_node:BaseNode;
parameters:[ParameterType];
retn:MonoType;
monotype:MonoType;
}

table TypeConstraint {
Expand All @@ -56,7 +62,7 @@ table TypeConstraint {

table TypeExpression {
base_node:BaseNode;
ty:MonoType;
monotype:MonoType;
constraints:[TypeConstraint];
}

Expand Down
10 changes: 10 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type MonoType interface {
}

func (NamedType) monotype() {}
func (TvarType) monotype() {}
func (ArrayType) monotype() {}
func (RecordType) monotype() {}
func (FunctionType) monotype() {}
Expand All @@ -194,6 +195,15 @@ func (NamedType) Type() string {
return "NamedType"
}

type TvarType struct {
BaseNode
ID *Identifier
}

func (TvarType) Type() string {
return "TvarType"
}

type ArrayType struct {
BaseNode
ElementType MonoType
Expand Down
1 change: 1 addition & 0 deletions ast/asttest/cmpopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var IgnoreBaseNodeOptions = []cmp.Option{
cmpopts.IgnoreFields(ast.StringLiteral{}, "BaseNode"),
cmpopts.IgnoreFields(ast.TestStatement{}, "BaseNode"),
cmpopts.IgnoreFields(ast.TextPart{}, "BaseNode"),
cmpopts.IgnoreFields(ast.TvarType{}, "BaseNode"),
cmpopts.IgnoreFields(ast.TypeConstraint{}, "BaseNode"),
cmpopts.IgnoreFields(ast.TypeExpression{}, "BaseNode"),
cmpopts.IgnoreFields(ast.UnaryExpression{}, "BaseNode"),
Expand Down
22 changes: 16 additions & 6 deletions ast/fbuffers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (b *BaseNode) FromBuf(buf *fbast.BaseNode) {

func (t TypeExpression) FromBuf(buf *fbast.TypeExpression) *TypeExpression {
t.BaseNode.FromBuf(buf.BaseNode(nil))
t.Ty = DecodeMonoType(newFBTable(buf.Ty, &t.BaseNode), buf.TyType())
t.Ty = DecodeMonoType(newFBTable(buf.Monotype, &t.BaseNode), buf.MonotypeType())
for i := 0; i < buf.ConstraintsLength(); i++ {
if c := new(fbast.TypeConstraint); !buf.Constraints(c, i) {
t.BaseNode.Errors = append(t.BaseNode.Errors, Error{
Expand Down Expand Up @@ -81,9 +81,15 @@ func (t NamedType) FromBuf(buf *fbast.NamedType) *NamedType {
return &t
}

func (t TvarType) FromBuf(buf *fbast.TvarType) *TvarType {
t.BaseNode.FromBuf(buf.BaseNode(nil))
t.ID = Identifier{}.FromBuf(buf.Id(nil))
return &t
}

func (t ArrayType) FromBuf(buf *fbast.ArrayType) *ArrayType {
t.BaseNode.FromBuf(buf.BaseNode(nil))
t.ElementType = DecodeMonoType(newFBTable(buf.ElementType, &t.BaseNode), buf.ElementTypeType())
t.ElementType = DecodeMonoType(newFBTable(buf.Element, &t.BaseNode), buf.ElementType())
return &t
}

Expand All @@ -105,13 +111,13 @@ func (t RecordType) FromBuf(buf *fbast.RecordType) *RecordType {
func (p PropertyType) FromBuf(buf *fbast.PropertyType) *PropertyType {
p.BaseNode.FromBuf(buf.BaseNode(nil))
p.Name = Identifier{}.FromBuf(buf.Id(nil))
p.Ty = DecodeMonoType(newFBTable(buf.Ty, &p.BaseNode), buf.TyType())
p.Ty = DecodeMonoType(newFBTable(buf.Monotype, &p.BaseNode), buf.MonotypeType())
return &p
}

func (t FunctionType) FromBuf(buf *fbast.FunctionType) *FunctionType {
t.BaseNode.FromBuf(buf.BaseNode(nil))
t.Return = DecodeMonoType(newFBTable(buf.Retn, &t.BaseNode), buf.RetnType())
t.Return = DecodeMonoType(newFBTable(buf.Monotype, &t.BaseNode), buf.MonotypeType())
for i := 0; i < buf.ParametersLength(); i++ {
if p := new(fbast.ParameterType); !buf.Parameters(p, i) {
t.BaseNode.Errors = append(t.BaseNode.Errors, Error{
Expand All @@ -126,8 +132,8 @@ func (t FunctionType) FromBuf(buf *fbast.FunctionType) *FunctionType {

func (p ParameterType) FromBuf(buf *fbast.ParameterType) *ParameterType {
p.BaseNode.FromBuf(buf.BaseNode(nil))
p.Name = Identifier{}.FromBuf(buf.Name(nil))
p.Ty = DecodeMonoType(newFBTable(buf.Ty, &p.BaseNode), buf.TyType())
p.Name = Identifier{}.FromBuf(buf.Id(nil))
p.Ty = DecodeMonoType(newFBTable(buf.Monotype, &p.BaseNode), buf.MonotypeType())
p.Kind = paramKindMap[buf.Kind()]
return &p
}
Expand All @@ -146,6 +152,10 @@ func DecodeMonoType(t *flatbuffers.Table, ty byte) MonoType {
b := new(fbast.NamedType)
b.Init(t.Bytes, t.Pos)
return NamedType{}.FromBuf(b)
case fbast.MonoTypeTvarType:
b := new(fbast.TvarType)
b.Init(t.Bytes, t.Pos)
return TvarType{}.FromBuf(b)
case fbast.MonoTypeArrayType:
b := new(fbast.ArrayType)
b.Init(t.Bytes, t.Pos)
Expand Down
103 changes: 64 additions & 39 deletions ast/flatbuffers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@ func TestDecodeMonoType(t *testing.T) {
t.Errorf("unexpected AST -want/+got:\n%s", cmp.Diff(want, got, CompareOptions...))
}
})
t.Run("tvar", func(t *testing.T) {
b := flatbuffers.NewBuilder(1024)

name := b.CreateString("T")

fbast.IdentifierStart(b)
fbast.IdentifierAddName(b, name)
id := fbast.IdentifierEnd(b)

fbast.TvarTypeStart(b)
fbast.TvarTypeAddId(b, id)
tv := fbast.TvarTypeEnd(b)

b.Finish(tv)
fbt := fbast.GetRootAsTvarType(b.FinishedBytes(), 0)
tbl := fbt.Table()

want := &ast.TvarType{
ID: &ast.Identifier{Name: "T"},
}

if got := ast.DecodeMonoType(&tbl, fbast.MonoTypeTvarType); !cmp.Equal(want, got, CompareOptions...) {
t.Errorf("unexpected AST -want/+got:\n%s", cmp.Diff(want, got, CompareOptions...))
}
})
t.Run("array", func(t *testing.T) {
b := flatbuffers.NewBuilder(1024)

Expand All @@ -171,8 +196,8 @@ func TestDecodeMonoType(t *testing.T) {
el := fbast.NamedTypeEnd(b)

fbast.ArrayTypeStart(b)
fbast.ArrayTypeAddElementTypeType(b, fbast.MonoTypeNamedType)
fbast.ArrayTypeAddElementType(b, el)
fbast.ArrayTypeAddElementType(b, fbast.MonoTypeNamedType)
fbast.ArrayTypeAddElement(b, el)
ty := fbast.ArrayTypeEnd(b)

b.Finish(ty)
Expand Down Expand Up @@ -226,8 +251,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.PropertyTypeStart(b)
fbast.PropertyTypeAddId(b, label)
fbast.PropertyTypeAddTy(b, basic)
fbast.PropertyTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.PropertyTypeAddMonotype(b, basic)
fbast.PropertyTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
p := fbast.PropertyTypeEnd(b)

fbast.RecordTypeStartPropertiesVector(b, 1)
Expand Down Expand Up @@ -278,8 +303,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.PropertyTypeStart(b)
fbast.PropertyTypeAddId(b, label)
fbast.PropertyTypeAddTy(b, basic)
fbast.PropertyTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.PropertyTypeAddMonotype(b, basic)
fbast.PropertyTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
p := fbast.PropertyTypeEnd(b)

fbast.RecordTypeStartPropertiesVector(b, 1)
Expand Down Expand Up @@ -337,8 +362,8 @@ func TestDecodeMonoType(t *testing.T) {
retn := fbast.NamedTypeEnd(b)

fbast.FunctionTypeStart(b)
fbast.FunctionTypeAddRetn(b, retn)
fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType)
fbast.FunctionTypeAddMonotype(b, retn)
fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
f := fbast.FunctionTypeEnd(b)

b.Finish(f)
Expand Down Expand Up @@ -370,8 +395,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.ParameterTypeStart(b)
fbast.ParameterTypeAddKind(b, fbast.ParameterKindPipe)
fbast.ParameterTypeAddTy(b, ty)
fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.ParameterTypeAddMonotype(b, ty)
fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
pipe := fbast.ParameterTypeEnd(b)

fbast.FunctionTypeStartParametersVector(b, 1)
Expand All @@ -380,8 +405,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.FunctionTypeStart(b)
fbast.FunctionTypeAddParameters(b, params)
fbast.FunctionTypeAddRetn(b, ty)
fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType)
fbast.FunctionTypeAddMonotype(b, ty)
fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
f := fbast.FunctionTypeEnd(b)

b.Finish(f)
Expand Down Expand Up @@ -429,9 +454,9 @@ func TestDecodeMonoType(t *testing.T) {

fbast.ParameterTypeStart(b)
fbast.ParameterTypeAddKind(b, fbast.ParameterKindPipe)
fbast.ParameterTypeAddName(b, pipeParam)
fbast.ParameterTypeAddTy(b, ty)
fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.ParameterTypeAddId(b, pipeParam)
fbast.ParameterTypeAddMonotype(b, ty)
fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
pipe := fbast.ParameterTypeEnd(b)

name = b.CreateString("a")
Expand All @@ -442,9 +467,9 @@ func TestDecodeMonoType(t *testing.T) {

fbast.ParameterTypeStart(b)
fbast.ParameterTypeAddKind(b, fbast.ParameterKindRequired)
fbast.ParameterTypeAddName(b, requiredParam)
fbast.ParameterTypeAddTy(b, ty)
fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.ParameterTypeAddId(b, requiredParam)
fbast.ParameterTypeAddMonotype(b, ty)
fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
req := fbast.ParameterTypeEnd(b)

name = b.CreateString("b")
Expand All @@ -455,9 +480,9 @@ func TestDecodeMonoType(t *testing.T) {

fbast.ParameterTypeStart(b)
fbast.ParameterTypeAddKind(b, fbast.ParameterKindOptional)
fbast.ParameterTypeAddName(b, optionalParam)
fbast.ParameterTypeAddTy(b, ty)
fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.ParameterTypeAddId(b, optionalParam)
fbast.ParameterTypeAddMonotype(b, ty)
fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
opt := fbast.ParameterTypeEnd(b)

fbast.FunctionTypeStartParametersVector(b, 3)
Expand All @@ -468,8 +493,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.FunctionTypeStart(b)
fbast.FunctionTypeAddParameters(b, params)
fbast.FunctionTypeAddRetn(b, ty)
fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType)
fbast.FunctionTypeAddMonotype(b, ty)
fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeNamedType)
f := fbast.FunctionTypeEnd(b)

b.Finish(f)
Expand Down Expand Up @@ -530,9 +555,9 @@ func TestDecodeMonoType(t *testing.T) {
fbast.IdentifierAddName(b, name)
id := fbast.IdentifierEnd(b)

fbast.NamedTypeStart(b)
fbast.NamedTypeAddId(b, id)
tvar := fbast.NamedTypeEnd(b)
fbast.TvarTypeStart(b)
fbast.TvarTypeAddId(b, id)
tvar := fbast.TvarTypeEnd(b)

name = b.CreateString("x")

Expand All @@ -542,9 +567,9 @@ func TestDecodeMonoType(t *testing.T) {

fbast.ParameterTypeStart(b)
fbast.ParameterTypeAddKind(b, fbast.ParameterKindRequired)
fbast.ParameterTypeAddName(b, x)
fbast.ParameterTypeAddTy(b, tvar)
fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.ParameterTypeAddId(b, x)
fbast.ParameterTypeAddMonotype(b, tvar)
fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeTvarType)
x = fbast.ParameterTypeEnd(b)

name = b.CreateString("y")
Expand All @@ -555,9 +580,9 @@ func TestDecodeMonoType(t *testing.T) {

fbast.ParameterTypeStart(b)
fbast.ParameterTypeAddKind(b, fbast.ParameterKindRequired)
fbast.ParameterTypeAddName(b, y)
fbast.ParameterTypeAddTy(b, tvar)
fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType)
fbast.ParameterTypeAddId(b, y)
fbast.ParameterTypeAddMonotype(b, tvar)
fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeTvarType)
y = fbast.ParameterTypeEnd(b)

fbast.FunctionTypeStartParametersVector(b, 2)
Expand All @@ -567,8 +592,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.FunctionTypeStart(b)
fbast.FunctionTypeAddParameters(b, params)
fbast.FunctionTypeAddRetn(b, tvar)
fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType)
fbast.FunctionTypeAddMonotype(b, tvar)
fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeTvarType)
f := fbast.FunctionTypeEnd(b)

name = b.CreateString("Addable")
Expand Down Expand Up @@ -599,8 +624,8 @@ func TestDecodeMonoType(t *testing.T) {

fbast.TypeExpressionStart(b)
fbast.TypeExpressionAddConstraints(b, constraints)
fbast.TypeExpressionAddTy(b, f)
fbast.TypeExpressionAddTyType(b, fbast.MonoTypeFunctionType)
fbast.TypeExpressionAddMonotype(b, f)
fbast.TypeExpressionAddMonotypeType(b, fbast.MonoTypeFunctionType)
texpr := fbast.TypeExpressionEnd(b)

b.Finish(texpr)
Expand All @@ -613,7 +638,7 @@ func TestDecodeMonoType(t *testing.T) {
Name: &ast.Identifier{
Name: "x",
},
Ty: &ast.NamedType{
Ty: &ast.TvarType{
ID: &ast.Identifier{
Name: "T",
},
Expand All @@ -624,15 +649,15 @@ func TestDecodeMonoType(t *testing.T) {
Name: &ast.Identifier{
Name: "y",
},
Ty: &ast.NamedType{
Ty: &ast.TvarType{
ID: &ast.Identifier{
Name: "T",
},
},
Kind: ast.Required,
},
},
Return: &ast.NamedType{
Return: &ast.TvarType{
ID: &ast.Identifier{
Name: "T",
},
Expand Down
Loading

0 comments on commit 3fa170d

Please sign in to comment.