File: ast.go

package info (click to toggle)
golang-sourcehut-sircmpwn-go-bare 0.0~git20210406.ab86bc2-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 268 kB
  • sloc: makefile: 5
file content (293 lines) | stat: -rw-r--r-- 4,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package schema

type SchemaTypeKind int

type SchemaType interface {
	Name() string
}

type UserDefinedType struct {
	name string
	type_ Type
}

func (udt *UserDefinedType) Name() string {
	return udt.name
}

func (udt *UserDefinedType) Type() Type {
	return udt.type_
}

type UserDefinedEnum struct {
	name   string
	kind   TypeKind
	values []EnumValue
}

func (ude *UserDefinedEnum) Name() string {
	return ude.name
}

func (ude *UserDefinedEnum) Kind() TypeKind {
	return ude.kind
}

func (ude *UserDefinedEnum) Values() []EnumValue {
	return ude.values
}

type EnumValue struct {
	name  string
	value uint
}

func (ev *EnumValue) Name() string {
	return ev.name
}

func (ev *EnumValue) Value() uint {
	return ev.value
}

type TypeKind int

const (
	UINT TypeKind = iota
	U8
	U16
	U32
	U64
	INT
	I8
	I16
	I32
	I64
	F32
	F64
	Bool
	String
	Void
	// data
	Data
	// data<length>
	DataFixed
	// [len]type
	Array
	// []type
	Slice
	// optional<type>
	Optional
	// data<len>
	DataArray
	// data
	DataSlice
	// map[type]type
	Map
	// (type | type | ...)
	Union
	// { fields... }
	Struct
	// Named user type
	UserType
)

func (tk TypeKind) String() string {
	switch tk {
	case UINT:
		return "UINT";
	case U8:
		return "U8";
	case U16:
		return "U16";
	case U32:
		return "U32";
	case U64:
		return "U64";
	case INT:
		return "INT";
	case I8:
		return "I8";
	case I16:
		return "I16";
	case I32:
		return "I32";
	case I64:
		return "I64";
	case F32:
		return "F32";
	case F64:
		return "F64";
	case Bool:
		return "Bool";
	case String:
		return "String";
	case Void:
		return "Void";
	case Data:
		return "Data";
	case DataFixed:
		return "DataFixed";
	case Array:
		return "Array";
	case Slice:
		return "Slice";
	case Optional:
		return "Optional";
	case DataArray:
		return "DataArray";
	case DataSlice:
		return "DataSlice";
	case Map:
		return "Map";
	case Union:
		return "Union";
	case Struct:
		return "Struct";
	case UserType:
		return "UserType";
	default:
		return "?"
	}
}

type Type interface {
	Kind() TypeKind
}

type PrimitiveType struct {
	kind TypeKind
}

func (pt *PrimitiveType) Kind() TypeKind {
	return pt.kind
}

type OptionalType struct {
	subtype Type
}

func (ot *OptionalType) Kind() TypeKind {
	return Optional
}

func (ot *OptionalType) Subtype() Type {
	return ot.subtype
}

type DataType struct {
	length uint
}

func (dt *DataType) Kind() TypeKind {
	if dt.length == 0 {
		return DataSlice
	}
	return DataArray
}

func (dt *DataType) Length() uint {
	return dt.length
}

type MapType struct {
	key   Type
	value Type
}

func (mt *MapType) Kind() TypeKind {
	return Map
}

func (mt *MapType) Key() Type {
	return mt.key
}

func (mt *MapType) Value() Type {
	return mt.value
}

type ArrayType struct {
	member Type
	length uint
}

func (at *ArrayType) Kind() TypeKind {
	if at.length == 0 {
		return Slice
	}
	return Array
}

func (at *ArrayType) Member() Type {
	return at.member
}

func (at *ArrayType) Length() uint {
	return at.length
}

type UnionType struct {
	types []UnionSubtype
}

func (ut *UnionType) Kind() TypeKind {
	return Union
}

func (ut *UnionType) Types() []UnionSubtype {
	return ut.types
}

type UnionSubtype struct {
	subtype Type
	tag     uint64
}

func (ust *UnionSubtype) Type() Type {
	return ust.subtype
}

func (ust *UnionSubtype) Tag() uint64 {
	return ust.tag
}

type StructType struct {
	fields []StructField
}

func (st *StructType) Kind() TypeKind {
	return Struct
}

func (st *StructType) Fields() []StructField {
	return st.fields
}

type StructField struct {
	name string
	type_ Type
}

func (sf *StructField) Name() string {
	return sf.name
}

func (sf *StructField) Type() Type {
	return sf.type_
}

// This has not been compared with the list of user-defined types and is not
// guaranteed to actually exist; the consumer of this type must perform this
// lookup itself.
type NamedUserType struct {
	name string
}

func (nut *NamedUserType) Kind() TypeKind {
	return UserType
}

func (nut *NamedUserType) Name() string {
	return nut.name
}