...
1 package ast
2
3 func NonNullNamedType(named string, pos *Position) *Type {
4 return &Type{NamedType: named, NonNull: true, Position: pos}
5 }
6
7 func NamedType(named string, pos *Position) *Type {
8 return &Type{NamedType: named, NonNull: false, Position: pos}
9 }
10
11 func NonNullListType(elem *Type, pos *Position) *Type {
12 return &Type{Elem: elem, NonNull: true, Position: pos}
13 }
14
15 func ListType(elem *Type, pos *Position) *Type {
16 return &Type{Elem: elem, NonNull: false, Position: pos}
17 }
18
19 type Type struct {
20 NamedType string
21 Elem *Type
22 NonNull bool
23 Position *Position `dump:"-"`
24 }
25
26 func (t *Type) Name() string {
27 if t.NamedType != "" {
28 return t.NamedType
29 }
30
31 return t.Elem.Name()
32 }
33
34 func (t *Type) String() string {
35 nn := ""
36 if t.NonNull {
37 nn = "!"
38 }
39 if t.NamedType != "" {
40 return t.NamedType + nn
41 }
42
43 return "[" + t.Elem.String() + "]" + nn
44 }
45
46 func (t *Type) IsCompatible(other *Type) bool {
47 if t.NamedType != other.NamedType {
48 return false
49 }
50
51 if t.Elem != nil && other.Elem == nil {
52 return false
53 }
54
55 if t.Elem != nil && !t.Elem.IsCompatible(other.Elem) {
56 return false
57 }
58
59 if other.NonNull {
60 return t.NonNull
61 }
62
63 return true
64 }
65
66 func (t *Type) Dump() string {
67 return t.String()
68 }
69
View as plain text