Skip to content

Commit

Permalink
Map string (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnnnblaser authored Jul 21, 2023
1 parent da9edf1 commit 7996c6a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
39 changes: 31 additions & 8 deletions ttcn3/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,38 +179,53 @@ type ListType struct {
}

func (t *ListType) String() string {
elem := "any"
if t.ElementType != nil && !isString(t.Kind) {
elem = t.ElementType.String()
}
switch t.Kind {
case RecordOf:
case RecordOf, Any:
var lengthConstraint string = " "
if t.LengthConstraint.Expr != nil {
lengthConstraint = " length(" + t.LengthConstraint.String() + ") "
}
return "record" + lengthConstraint + "of " + t.ElementType.String()
return "record" + lengthConstraint + "of " + elem
case SetOf:
var lengthConstraint string = " "
if t.LengthConstraint.Expr != nil {
lengthConstraint = " length(" + t.LengthConstraint.String() + ") "
}
return "set" + lengthConstraint + "of " + t.ElementType.String()
return "set" + lengthConstraint + "of " + elem
case Map:
if _, ok := t.ElementType.(*PairType); !ok {
return "map from " + elem + " to any"
}
return "map from " + elem
case Charstring, Octetstring, Hexstring, Bitstring, UniversalCharstring:
var lengthConstraint string = ""
if t.LengthConstraint.Expr != nil {
lengthConstraint = " length(" + t.LengthConstraint.String() + ")"
}
return t.Kind.String() + lengthConstraint
case Array:
var arrayType string = t.ElementType.String()
if eleType, ok := t.ElementType.(*ListType); ok && (eleType.Kind == RecordOf || eleType.Kind == SetOf || eleType.Kind == Map) {
arrayType = "(" + arrayType + ")"
elem = "(" + elem + ")"
}
if t.LengthConstraint.Expr == nil {
return arrayType + "[]"
return elem + "[]"
}
return arrayType + "[" + t.LengthConstraint.String() + "]"
return elem + "[" + t.LengthConstraint.String() + "]"
}
return ""
}

func isString(t Kind) bool {
if t == Bitstring || t == Charstring || t == Octetstring || t == Hexstring || t == UniversalCharstring {
return true
}
return false
}

// A StructuredType represents structured types, such as record, set, union,
// class, ...
type StructuredType struct {
Expand Down Expand Up @@ -262,7 +277,15 @@ type PairType struct {
}

func (t *PairType) String() string {
return ""
res := []string{"any", "any"}
if t.First != nil {
res[0] = t.First.String()
}
if t.Second != nil {
res[1] = t.Second.String()
}

return strings.Join(res, " to ")
}

// A Value represents a single value constraint, such as '1' or '10..20'.
Expand Down
54 changes: 46 additions & 8 deletions ttcn3/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func TestTypeStrings(t *testing.T) {

// List types

{skip: true, Output: "record of any",
{Output: "record of any",
Type: &types.ListType{}},

{skip: true, Output: "record of any",
{Output: "record of any",
Type: &types.ListType{Kind: types.RecordOf}},

{Output: "hexstring",
Expand Down Expand Up @@ -135,12 +135,12 @@ func TestTypeStrings(t *testing.T) {

// Arrays

{skip: true, Output: "any[]",
{Output: "any[]",
Type: &types.ListType{
Kind: types.Array,
}},

{skip: true, Output: "any[1..10]",
{Output: "any[1..10]",
Type: &types.ListType{
Kind: types.Array,
LengthConstraint: types.Value{Expr: rangeVal},
Expand Down Expand Up @@ -183,22 +183,22 @@ func TestTypeStrings(t *testing.T) {
},
}},

{skip: true, Output: "map from any to any",
{Output: "map from any to any",
Type: &types.ListType{Kind: types.Map}},

{skip: true, Output: "map from hexstring to any",
{Output: "map from integer to any",
Type: &types.ListType{
Kind: types.Map,
ElementType: types.Predefined["integer"],
}},

{skip: true, Output: "map from any to any",
{Output: "map from any to any",
Type: &types.ListType{
Kind: types.Map,
ElementType: &types.PairType{},
}},

{skip: true, Output: "map from integer to charstring",
{Output: "map from integer to charstring",
Type: &types.ListType{
Kind: types.Map,
ElementType: &types.PairType{
Expand All @@ -207,6 +207,44 @@ func TestTypeStrings(t *testing.T) {
},
}},

{Output: "(map from integer to charstring)[]",
Type: &types.ListType{
Kind: types.Array,
ElementType: &types.ListType{
Kind: types.Map,
ElementType: &types.PairType{
First: types.Predefined["integer"],
Second: types.Predefined["charstring"],
},
},
}},

{Output: "map from record of integer[] to map from charstring to (set of float)[]",
Type: &types.ListType{
Kind: types.Map,
ElementType: &types.PairType{
First: &types.ListType{
Kind: types.RecordOf,
ElementType: &types.ListType{
Kind: types.Array,
ElementType: types.Predefined["integer"],
},
},
Second: &types.ListType{
Kind: types.Map,
ElementType: &types.PairType{
First: types.Predefined["charstring"],
Second: &types.ListType{
Kind: types.Array,
ElementType: &types.ListType{
Kind: types.SetOf,
ElementType: types.Float,
}},
},
},
},
}},

// Named types

{skip: true, Output: "foo [any]",
Expand Down

0 comments on commit 7996c6a

Please sign in to comment.