cuelang.org/go@v0.10.1/internal/core/adt/adt.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package adt 16 17 import ( 18 "cuelang.org/go/cue/ast" 19 "cuelang.org/go/cue/token" 20 ) 21 22 func Resolve(ctx *OpContext, c Conjunct) *Vertex { 23 env := c.Env 24 // TODO: also allow resolution in parent scopes. The following will set up 25 // the environments. But the compiler also needs to resolve accordingly. 26 // 27 // // Set up environments for parent scopes, if any. 28 // root := env 29 // for p := scope; p != nil; p = p.Parent { 30 // root.Up = &Environment{Vertex: p.Parent} 31 // root = root.Up 32 // } 33 34 var v Value 35 36 expr := c.Elem() // TODO: why is this not Expr? 37 switch x := expr.(type) { 38 case Value: 39 v = x 40 41 case Resolver: 42 r, err := ctx.resolveState(c, x, attempt(finalized, allKnown)) 43 if err != nil { 44 v = err 45 break 46 } 47 // r.Finalize(ctx) // TODO: Finalize here? 48 return r 49 50 case Evaluator: 51 // TODO: have a way to evaluate, but not strip down to the value. 52 v, _ = ctx.Evaluate(env, expr.(Expr)) 53 54 default: 55 // Unknown type. 56 v = ctx.NewErrf( 57 "could not evaluate expression %s of type %T", c.Elem(), c) 58 } 59 60 return ToVertex(v) 61 } 62 63 // A Node is any abstract data type representing a value or expression. 64 type Node interface { 65 Source() ast.Node 66 node() // enforce internal. 67 } 68 69 // A Decl represents all valid StructLit elements. 70 type Decl interface { 71 Node 72 declNode() 73 } 74 75 // An Elem represents all value ListLit elements. 76 // 77 // All Elem values can be used as a Decl. 78 type Elem interface { 79 Decl 80 elemNode() 81 } 82 83 // An Expr corresponds to an ast.Expr. 84 // 85 // All Expr values can be used as an Elem or Decl. 86 type Expr interface { 87 Elem 88 expr() 89 } 90 91 // A BaseValue is any Value or a *Marker. It indicates the type of a Vertex. 92 type BaseValue interface { 93 Kind() Kind 94 } 95 96 // A Value represents a node in the evaluated data graph. 97 // 98 // All Values values can also be used as a Expr. 99 type Value interface { 100 Expr 101 Concreteness() Concreteness 102 Kind() Kind 103 } 104 105 // An Evaluator provides a method to convert to a value. 106 type Evaluator interface { 107 Node 108 109 // evaluate evaluates the underlying expression. If the expression 110 // is incomplete, it may record the error in ctx and return nil. 111 evaluate(ctx *OpContext, state combinedFlags) Value 112 } 113 114 // A Resolver represents a reference somewhere else within a tree that resolves 115 // a value. 116 type Resolver interface { 117 Node 118 resolve(ctx *OpContext, state combinedFlags) *Vertex 119 } 120 121 type YieldFunc func(env *Environment) 122 123 // A Yielder represents 0 or more labeled values of structs or lists. 124 type Yielder interface { 125 Node 126 yield(s *compState) 127 } 128 129 // A Validator validates a Value. All Validators are Values. 130 type Validator interface { 131 Value 132 validate(c *OpContext, v Value) *Bottom 133 } 134 135 // Pos returns the file position of n, or token.NoPos if it is unknown. 136 func Pos(n Node) token.Pos { 137 src := n.Source() 138 if src == nil { 139 return token.NoPos 140 } 141 return src.Pos() 142 } 143 144 // Value 145 146 func (x *Vertex) Concreteness() Concreteness { 147 // Depends on concreteness of value. 148 switch v := x.BaseValue.(type) { 149 case nil: 150 return Concrete // Should be indetermined. 151 152 case Value: 153 return v.Concreteness() 154 155 default: // *StructMarker, *ListMarker: 156 return Concrete 157 } 158 } 159 160 func (x *NodeLink) Concreteness() Concreteness { return Concrete } 161 162 func (*Conjunction) Concreteness() Concreteness { return Constraint } 163 func (*Disjunction) Concreteness() Concreteness { return Constraint } 164 func (*BoundValue) Concreteness() Concreteness { return Constraint } 165 166 func (*Builtin) Concreteness() Concreteness { return Concrete } 167 func (*BuiltinValidator) Concreteness() Concreteness { return Constraint } 168 169 // Value and Expr 170 171 func (*Bottom) Concreteness() Concreteness { return BottomLevel } 172 func (*Null) Concreteness() Concreteness { return Concrete } 173 func (*Bool) Concreteness() Concreteness { return Concrete } 174 func (*Num) Concreteness() Concreteness { return Concrete } 175 func (*String) Concreteness() Concreteness { return Concrete } 176 func (*Bytes) Concreteness() Concreteness { return Concrete } 177 func (*Top) Concreteness() Concreteness { return Any } 178 func (*BasicType) Concreteness() Concreteness { return Type } 179 180 // Expr 181 182 func (*StructLit) expr() {} 183 func (*ListLit) expr() {} 184 func (*DisjunctionExpr) expr() {} 185 186 // TODO: also allow? 187 // a: b: if cond {} 188 // 189 // It is unclear here, though, whether field `a` should be added 190 // unconditionally. 191 // func (*Comprehension) expr() {} 192 193 // Expr and Value 194 195 func (*Bottom) expr() {} 196 func (*Null) expr() {} 197 func (*Bool) expr() {} 198 func (*Num) expr() {} 199 func (*String) expr() {} 200 func (*Bytes) expr() {} 201 func (*Top) expr() {} 202 func (*BasicType) expr() {} 203 func (*Vertex) expr() {} 204 func (*ListMarker) expr() {} 205 func (*StructMarker) expr() {} 206 func (*Conjunction) expr() {} 207 func (*Disjunction) expr() {} 208 func (*BoundValue) expr() {} 209 func (*BuiltinValidator) expr() {} 210 func (*Builtin) expr() {} 211 212 // Expr and Resolver 213 214 func (*NodeLink) expr() {} 215 func (*FieldReference) expr() {} 216 func (*ValueReference) expr() {} 217 func (*LabelReference) expr() {} 218 func (*DynamicReference) expr() {} 219 func (*ImportReference) expr() {} 220 func (*LetReference) expr() {} 221 222 // Expr and Evaluator 223 224 func (*BoundExpr) expr() {} 225 func (*SelectorExpr) expr() {} 226 func (*IndexExpr) expr() {} 227 func (*SliceExpr) expr() {} 228 func (*Interpolation) expr() {} 229 func (*UnaryExpr) expr() {} 230 func (*BinaryExpr) expr() {} 231 func (*CallExpr) expr() {} 232 233 // Decl and Expr (so allow attaching original source in Conjunct) 234 235 func (*Field) declNode() {} 236 func (x *Field) expr() Expr { return x.Value } 237 func (*LetField) declNode() {} 238 func (x *LetField) expr() Expr { return x.Value } 239 func (*BulkOptionalField) declNode() {} 240 func (x *BulkOptionalField) expr() Expr { return x.Value } 241 func (*DynamicField) declNode() {} 242 func (x *DynamicField) expr() Expr { return x.Value } 243 244 // Decl, Elem, and Expr (so allow attaching original source in Conjunct) 245 246 func (*Ellipsis) elemNode() {} 247 func (*Ellipsis) declNode() {} 248 func (x *Ellipsis) expr() Expr { 249 if x.Value == nil { 250 return top 251 } 252 return x.Value 253 } 254 func (*ConjunctGroup) declNode() {} 255 func (*ConjunctGroup) elemNode() {} 256 func (*ConjunctGroup) expr() {} 257 258 var top = &Top{} 259 260 // Decl and Yielder 261 262 func (*LetClause) declNode() {} 263 264 // Decl and Elem 265 266 func (*StructLit) declNode() {} 267 func (*StructLit) elemNode() {} 268 func (*ListLit) declNode() {} 269 func (*ListLit) elemNode() {} 270 func (*Bottom) declNode() {} 271 func (*Bottom) elemNode() {} 272 func (*Null) declNode() {} 273 func (*Null) elemNode() {} 274 func (*Bool) declNode() {} 275 func (*Bool) elemNode() {} 276 func (*Num) declNode() {} 277 func (*Num) elemNode() {} 278 func (*String) declNode() {} 279 func (*String) elemNode() {} 280 func (*Bytes) declNode() {} 281 func (*Bytes) elemNode() {} 282 func (*Top) declNode() {} 283 func (*Top) elemNode() {} 284 func (*BasicType) declNode() {} 285 func (*BasicType) elemNode() {} 286 func (*BoundExpr) declNode() {} 287 func (*BoundExpr) elemNode() {} 288 func (*Vertex) declNode() {} 289 func (*Vertex) elemNode() {} 290 func (*ListMarker) declNode() {} 291 func (*ListMarker) elemNode() {} 292 func (*StructMarker) declNode() {} 293 func (*StructMarker) elemNode() {} 294 func (*Conjunction) declNode() {} 295 func (*Conjunction) elemNode() {} 296 func (*Disjunction) declNode() {} 297 func (*Disjunction) elemNode() {} 298 func (*BoundValue) declNode() {} 299 func (*BoundValue) elemNode() {} 300 func (*BuiltinValidator) declNode() {} 301 func (*BuiltinValidator) elemNode() {} 302 func (*NodeLink) declNode() {} 303 func (*NodeLink) elemNode() {} 304 func (*FieldReference) declNode() {} 305 func (*FieldReference) elemNode() {} 306 func (*ValueReference) declNode() {} 307 func (*ValueReference) elemNode() {} 308 func (*LabelReference) declNode() {} 309 func (*LabelReference) elemNode() {} 310 func (*DynamicReference) declNode() {} 311 func (*DynamicReference) elemNode() {} 312 func (*ImportReference) declNode() {} 313 func (*ImportReference) elemNode() {} 314 func (*LetReference) declNode() {} 315 func (*LetReference) elemNode() {} 316 func (*SelectorExpr) declNode() {} 317 func (*SelectorExpr) elemNode() {} 318 func (*IndexExpr) declNode() {} 319 func (*IndexExpr) elemNode() {} 320 func (*SliceExpr) declNode() {} 321 func (*SliceExpr) elemNode() {} 322 func (*Interpolation) declNode() {} 323 func (*Interpolation) elemNode() {} 324 func (*UnaryExpr) declNode() {} 325 func (*UnaryExpr) elemNode() {} 326 func (*BinaryExpr) declNode() {} 327 func (*BinaryExpr) elemNode() {} 328 func (*CallExpr) declNode() {} 329 func (*CallExpr) elemNode() {} 330 func (*Builtin) declNode() {} 331 func (*Builtin) elemNode() {} 332 func (*DisjunctionExpr) declNode() {} 333 func (*DisjunctionExpr) elemNode() {} 334 335 // Decl, Elem, and Yielder 336 337 func (*Comprehension) declNode() {} 338 func (*Comprehension) elemNode() {} 339 340 // Node 341 342 func (*Vertex) node() {} 343 func (*Conjunction) node() {} 344 func (*ConjunctGroup) node() {} 345 func (*Disjunction) node() {} 346 func (*BoundValue) node() {} 347 func (*Builtin) node() {} 348 func (*BuiltinValidator) node() {} 349 func (*Bottom) node() {} 350 func (*Null) node() {} 351 func (*Bool) node() {} 352 func (*Num) node() {} 353 func (*String) node() {} 354 func (*Bytes) node() {} 355 func (*Top) node() {} 356 func (*BasicType) node() {} 357 func (*StructLit) node() {} 358 func (*ListLit) node() {} 359 func (*BoundExpr) node() {} 360 func (*NodeLink) node() {} 361 func (*FieldReference) node() {} 362 func (*ValueReference) node() {} 363 func (*LabelReference) node() {} 364 func (*DynamicReference) node() {} 365 func (*ImportReference) node() {} 366 func (*LetReference) node() {} 367 func (*SelectorExpr) node() {} 368 func (*IndexExpr) node() {} 369 func (*SliceExpr) node() {} 370 func (*Interpolation) node() {} 371 func (*UnaryExpr) node() {} 372 func (*BinaryExpr) node() {} 373 func (*CallExpr) node() {} 374 func (*DisjunctionExpr) node() {} 375 func (*Field) node() {} 376 func (*LetField) node() {} 377 func (*BulkOptionalField) node() {} 378 func (*DynamicField) node() {} 379 func (*Ellipsis) node() {} 380 func (*Comprehension) node() {} 381 func (*ForClause) node() {} 382 func (*IfClause) node() {} 383 func (*LetClause) node() {}