-
Notifications
You must be signed in to change notification settings - Fork 294
/
adt.go
365 lines (315 loc) · 10.5 KB
/
adt.go
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adt
import (
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
)
func Resolve(ctx *OpContext, c Conjunct) *Vertex {
env := c.Env
// TODO: also allow resolution in parent scopes. The following will set up
// the environments. But the compiler also needs to resolve accordingly.
//
// // Set up environments for parent scopes, if any.
// root := env
// for p := scope; p != nil; p = p.Parent {
// root.Up = &Environment{Vertex: p.Parent}
// root = root.Up
// }
var v Value
expr := c.Expr()
switch x := expr.(type) {
case Value:
v = x
case Resolver:
r, err := ctx.Resolve(env, x)
if err != nil {
v = err
break
}
// r.Finalize(ctx) // TODO: Finalize here?
return r
case Evaluator:
// TODO: have a way to evaluate, but not strip down to the value.
v, _ = ctx.Evaluate(env, expr)
default:
// Unknown type.
v = ctx.NewErrf(
"could not evaluate expression %s of type %T", c.Expr(), c)
}
return ToVertex(v)
}
// A Node is any abstract data type representing an value or expression.
type Node interface {
Source() ast.Node
node() // enforce internal.
}
// A Decl represents all valid StructLit elements.
type Decl interface {
Node
declNode()
}
// An Elem represents all value ListLit elements.
//
// All Elem values can be used as a Decl.
type Elem interface {
Decl
elemNode()
}
// An Expr corresponds to an ast.Expr.
//
// All Expr values can be used as an Elem or Decl.
type Expr interface {
Elem
expr()
}
// A BaseValue is any Value or a *Marker. It indicates the type of a Vertex.
type BaseValue interface {
Kind() Kind
}
// A Value represents a node in the evaluated data graph.
//
// All Values values can also be used as a Expr.
type Value interface {
Expr
Concreteness() Concreteness
Kind() Kind
}
// An Evaluator provides a method to convert to a value.
type Evaluator interface {
Node
// evaluate evaluates the underlying expression. If the expression
// is incomplete, it may record the error in ctx and return nil.
evaluate(ctx *OpContext) Value
}
// A Resolver represents a reference somewhere else within a tree that resolves
// a value.
type Resolver interface {
Node
resolve(ctx *OpContext, state VertexStatus) *Vertex
}
type YieldFunc func(env *Environment, s *StructLit)
// A Yielder represents 0 or more labeled values of structs or lists.
type Yielder interface {
Node
yield(ctx *OpContext, fn YieldFunc)
}
// A Validator validates a Value. All Validators are Values.
type Validator interface {
Value
validate(c *OpContext, v Value) *Bottom
}
// Pos returns the file position of n, or token.NoPos if it is unknown.
func Pos(n Node) token.Pos {
src := n.Source()
if src == nil {
return token.NoPos
}
return src.Pos()
}
// Value
func (x *Vertex) Concreteness() Concreteness {
// Depends on concreteness of value.
switch v := x.BaseValue.(type) {
case nil:
return Concrete // Should be indetermined.
case Value:
return v.Concreteness()
default: // *StructMarker, *ListMarker:
return Concrete
}
}
func (x *NodeLink) Concreteness() Concreteness { return Concrete }
func (*Conjunction) Concreteness() Concreteness { return Constraint }
func (*Disjunction) Concreteness() Concreteness { return Constraint }
func (*BoundValue) Concreteness() Concreteness { return Constraint }
func (*Builtin) Concreteness() Concreteness { return Concrete }
func (*BuiltinValidator) Concreteness() Concreteness { return Constraint }
// Value and Expr
func (*Bottom) Concreteness() Concreteness { return BottomLevel }
func (*Null) Concreteness() Concreteness { return Concrete }
func (*Bool) Concreteness() Concreteness { return Concrete }
func (*Num) Concreteness() Concreteness { return Concrete }
func (*String) Concreteness() Concreteness { return Concrete }
func (*Bytes) Concreteness() Concreteness { return Concrete }
func (*Top) Concreteness() Concreteness { return Any }
func (*BasicType) Concreteness() Concreteness { return Type }
// Expr
func (*StructLit) expr() {}
func (*ListLit) expr() {}
func (*DisjunctionExpr) expr() {}
// Expr and Value
func (*Bottom) expr() {}
func (*Null) expr() {}
func (*Bool) expr() {}
func (*Num) expr() {}
func (*String) expr() {}
func (*Bytes) expr() {}
func (*Top) expr() {}
func (*BasicType) expr() {}
func (*Vertex) expr() {}
func (*ListMarker) expr() {}
func (*StructMarker) expr() {}
func (*Conjunction) expr() {}
func (*Disjunction) expr() {}
func (*BoundValue) expr() {}
func (*BuiltinValidator) expr() {}
func (*Builtin) expr() {}
// Expr and Resolver
func (*NodeLink) expr() {}
func (*FieldReference) expr() {}
func (*ValueReference) expr() {}
func (*LabelReference) expr() {}
func (*DynamicReference) expr() {}
func (*ImportReference) expr() {}
func (*LetReference) expr() {}
// Expr and Evaluator
func (*BoundExpr) expr() {}
func (*SelectorExpr) expr() {}
func (*IndexExpr) expr() {}
func (*SliceExpr) expr() {}
func (*Interpolation) expr() {}
func (*UnaryExpr) expr() {}
func (*BinaryExpr) expr() {}
func (*CallExpr) expr() {}
// Decl and Expr (so allow attaching original source in Conjunct)
func (*Field) declNode() {}
func (x *Field) expr() Expr { return x.Value }
func (*OptionalField) declNode() {}
func (x *OptionalField) expr() Expr { return x.Value }
func (*BulkOptionalField) declNode() {}
func (x *BulkOptionalField) expr() Expr { return x.Value }
func (*DynamicField) declNode() {}
func (x *DynamicField) expr() Expr { return x.Value }
// Decl and Yielder
func (*LetClause) declNode() {}
// Decl and Elem
func (*StructLit) declNode() {}
func (*StructLit) elemNode() {}
func (*ListLit) declNode() {}
func (*ListLit) elemNode() {}
func (*Ellipsis) elemNode() {}
func (*Ellipsis) declNode() {}
func (*Bottom) declNode() {}
func (*Bottom) elemNode() {}
func (*Null) declNode() {}
func (*Null) elemNode() {}
func (*Bool) declNode() {}
func (*Bool) elemNode() {}
func (*Num) declNode() {}
func (*Num) elemNode() {}
func (*String) declNode() {}
func (*String) elemNode() {}
func (*Bytes) declNode() {}
func (*Bytes) elemNode() {}
func (*Top) declNode() {}
func (*Top) elemNode() {}
func (*BasicType) declNode() {}
func (*BasicType) elemNode() {}
func (*BoundExpr) declNode() {}
func (*BoundExpr) elemNode() {}
func (*Vertex) declNode() {}
func (*Vertex) elemNode() {}
func (*ListMarker) declNode() {}
func (*ListMarker) elemNode() {}
func (*StructMarker) declNode() {}
func (*StructMarker) elemNode() {}
func (*Conjunction) declNode() {}
func (*Conjunction) elemNode() {}
func (*Disjunction) declNode() {}
func (*Disjunction) elemNode() {}
func (*BoundValue) declNode() {}
func (*BoundValue) elemNode() {}
func (*BuiltinValidator) declNode() {}
func (*BuiltinValidator) elemNode() {}
func (*NodeLink) declNode() {}
func (*NodeLink) elemNode() {}
func (*FieldReference) declNode() {}
func (*FieldReference) elemNode() {}
func (*ValueReference) declNode() {}
func (*ValueReference) elemNode() {}
func (*LabelReference) declNode() {}
func (*LabelReference) elemNode() {}
func (*DynamicReference) declNode() {}
func (*DynamicReference) elemNode() {}
func (*ImportReference) declNode() {}
func (*ImportReference) elemNode() {}
func (*LetReference) declNode() {}
func (*LetReference) elemNode() {}
func (*SelectorExpr) declNode() {}
func (*SelectorExpr) elemNode() {}
func (*IndexExpr) declNode() {}
func (*IndexExpr) elemNode() {}
func (*SliceExpr) declNode() {}
func (*SliceExpr) elemNode() {}
func (*Interpolation) declNode() {}
func (*Interpolation) elemNode() {}
func (*UnaryExpr) declNode() {}
func (*UnaryExpr) elemNode() {}
func (*BinaryExpr) declNode() {}
func (*BinaryExpr) elemNode() {}
func (*CallExpr) declNode() {}
func (*CallExpr) elemNode() {}
func (*Builtin) declNode() {}
func (*Builtin) elemNode() {}
func (*DisjunctionExpr) declNode() {}
func (*DisjunctionExpr) elemNode() {}
// Decl, Elem, and Yielder
func (*ForClause) declNode() {}
func (*ForClause) elemNode() {}
func (*IfClause) declNode() {}
func (*IfClause) elemNode() {}
// Yielder only: ValueClause
// Node
func (*Vertex) node() {}
func (*Conjunction) node() {}
func (*Disjunction) node() {}
func (*BoundValue) node() {}
func (*Builtin) node() {}
func (*BuiltinValidator) node() {}
func (*Bottom) node() {}
func (*Null) node() {}
func (*Bool) node() {}
func (*Num) node() {}
func (*String) node() {}
func (*Bytes) node() {}
func (*Top) node() {}
func (*BasicType) node() {}
func (*StructLit) node() {}
func (*ListLit) node() {}
func (*BoundExpr) node() {}
func (*NodeLink) node() {}
func (*FieldReference) node() {}
func (*ValueReference) node() {}
func (*LabelReference) node() {}
func (*DynamicReference) node() {}
func (*ImportReference) node() {}
func (*LetReference) node() {}
func (*SelectorExpr) node() {}
func (*IndexExpr) node() {}
func (*SliceExpr) node() {}
func (*Interpolation) node() {}
func (*UnaryExpr) node() {}
func (*BinaryExpr) node() {}
func (*CallExpr) node() {}
func (*DisjunctionExpr) node() {}
func (*Field) node() {}
func (*OptionalField) node() {}
func (*BulkOptionalField) node() {}
func (*DynamicField) node() {}
func (*Ellipsis) node() {}
func (*ForClause) node() {}
func (*IfClause) node() {}
func (*LetClause) node() {}
func (*ValueClause) node() {}