github.com/solo-io/cue@v0.4.7/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  	"github.com/solo-io/cue/cue/ast"
    19  	"github.com/solo-io/cue/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.Expr()
    37  	switch x := expr.(type) {
    38  	case Value:
    39  		v = x
    40  
    41  	case Resolver:
    42  		r, err := ctx.Resolve(env, x)
    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)
    53  
    54  	default:
    55  		// Unknown type.
    56  		v = ctx.NewErrf(
    57  			"could not evaluate expression %s of type %T", c.Expr(), c)
    58  	}
    59  
    60  	return ToVertex(v)
    61  }
    62  
    63  // A Node is any abstract data type representing an 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) 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 VertexStatus) *Vertex
   119  }
   120  
   121  type YieldFunc func(env *Environment, s *StructLit)
   122  
   123  // A Yielder represents 0 or more labeled values of structs or lists.
   124  type Yielder interface {
   125  	Node
   126  	yield(ctx *OpContext, fn YieldFunc)
   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  // Expr and Value
   187  
   188  func (*Bottom) expr()           {}
   189  func (*Null) expr()             {}
   190  func (*Bool) expr()             {}
   191  func (*Num) expr()              {}
   192  func (*String) expr()           {}
   193  func (*Bytes) expr()            {}
   194  func (*Top) expr()              {}
   195  func (*BasicType) expr()        {}
   196  func (*Vertex) expr()           {}
   197  func (*ListMarker) expr()       {}
   198  func (*StructMarker) expr()     {}
   199  func (*Conjunction) expr()      {}
   200  func (*Disjunction) expr()      {}
   201  func (*BoundValue) expr()       {}
   202  func (*BuiltinValidator) expr() {}
   203  func (*Builtin) expr()          {}
   204  
   205  // Expr and Resolver
   206  
   207  func (*NodeLink) expr()         {}
   208  func (*FieldReference) expr()   {}
   209  func (*ValueReference) expr()   {}
   210  func (*LabelReference) expr()   {}
   211  func (*DynamicReference) expr() {}
   212  func (*ImportReference) expr()  {}
   213  func (*LetReference) expr()     {}
   214  
   215  // Expr and Evaluator
   216  
   217  func (*BoundExpr) expr()     {}
   218  func (*SelectorExpr) expr()  {}
   219  func (*IndexExpr) expr()     {}
   220  func (*SliceExpr) expr()     {}
   221  func (*Interpolation) expr() {}
   222  func (*UnaryExpr) expr()     {}
   223  func (*BinaryExpr) expr()    {}
   224  func (*CallExpr) expr()      {}
   225  
   226  // Decl and Expr (so allow attaching original source in Conjunct)
   227  
   228  func (*Field) declNode()                {}
   229  func (x *Field) expr() Expr             { return x.Value }
   230  func (*OptionalField) declNode()        {}
   231  func (x *OptionalField) expr() Expr     { return x.Value }
   232  func (*BulkOptionalField) declNode()    {}
   233  func (x *BulkOptionalField) expr() Expr { return x.Value }
   234  func (*DynamicField) declNode()         {}
   235  func (x *DynamicField) expr() Expr      { return x.Value }
   236  
   237  // Decl and Yielder
   238  
   239  func (*LetClause) declNode() {}
   240  
   241  // Decl and Elem
   242  
   243  func (*StructLit) declNode()        {}
   244  func (*StructLit) elemNode()        {}
   245  func (*ListLit) declNode()          {}
   246  func (*ListLit) elemNode()          {}
   247  func (*Ellipsis) elemNode()         {}
   248  func (*Ellipsis) declNode()         {}
   249  func (*Bottom) declNode()           {}
   250  func (*Bottom) elemNode()           {}
   251  func (*Null) declNode()             {}
   252  func (*Null) elemNode()             {}
   253  func (*Bool) declNode()             {}
   254  func (*Bool) elemNode()             {}
   255  func (*Num) declNode()              {}
   256  func (*Num) elemNode()              {}
   257  func (*String) declNode()           {}
   258  func (*String) elemNode()           {}
   259  func (*Bytes) declNode()            {}
   260  func (*Bytes) elemNode()            {}
   261  func (*Top) declNode()              {}
   262  func (*Top) elemNode()              {}
   263  func (*BasicType) declNode()        {}
   264  func (*BasicType) elemNode()        {}
   265  func (*BoundExpr) declNode()        {}
   266  func (*BoundExpr) elemNode()        {}
   267  func (*Vertex) declNode()           {}
   268  func (*Vertex) elemNode()           {}
   269  func (*ListMarker) declNode()       {}
   270  func (*ListMarker) elemNode()       {}
   271  func (*StructMarker) declNode()     {}
   272  func (*StructMarker) elemNode()     {}
   273  func (*Conjunction) declNode()      {}
   274  func (*Conjunction) elemNode()      {}
   275  func (*Disjunction) declNode()      {}
   276  func (*Disjunction) elemNode()      {}
   277  func (*BoundValue) declNode()       {}
   278  func (*BoundValue) elemNode()       {}
   279  func (*BuiltinValidator) declNode() {}
   280  func (*BuiltinValidator) elemNode() {}
   281  func (*NodeLink) declNode()         {}
   282  func (*NodeLink) elemNode()         {}
   283  func (*FieldReference) declNode()   {}
   284  func (*FieldReference) elemNode()   {}
   285  func (*ValueReference) declNode()   {}
   286  func (*ValueReference) elemNode()   {}
   287  func (*LabelReference) declNode()   {}
   288  func (*LabelReference) elemNode()   {}
   289  func (*DynamicReference) declNode() {}
   290  func (*DynamicReference) elemNode() {}
   291  func (*ImportReference) declNode()  {}
   292  func (*ImportReference) elemNode()  {}
   293  func (*LetReference) declNode()     {}
   294  func (*LetReference) elemNode()     {}
   295  func (*SelectorExpr) declNode()     {}
   296  func (*SelectorExpr) elemNode()     {}
   297  func (*IndexExpr) declNode()        {}
   298  func (*IndexExpr) elemNode()        {}
   299  func (*SliceExpr) declNode()        {}
   300  func (*SliceExpr) elemNode()        {}
   301  func (*Interpolation) declNode()    {}
   302  func (*Interpolation) elemNode()    {}
   303  func (*UnaryExpr) declNode()        {}
   304  func (*UnaryExpr) elemNode()        {}
   305  func (*BinaryExpr) declNode()       {}
   306  func (*BinaryExpr) elemNode()       {}
   307  func (*CallExpr) declNode()         {}
   308  func (*CallExpr) elemNode()         {}
   309  func (*Builtin) declNode()          {}
   310  func (*Builtin) elemNode()          {}
   311  func (*DisjunctionExpr) declNode()  {}
   312  func (*DisjunctionExpr) elemNode()  {}
   313  
   314  // Decl, Elem, and Yielder
   315  
   316  func (*ForClause) declNode() {}
   317  func (*ForClause) elemNode() {}
   318  func (*IfClause) declNode()  {}
   319  func (*IfClause) elemNode()  {}
   320  
   321  // Yielder only: ValueClause
   322  
   323  // Node
   324  
   325  func (*Vertex) node()            {}
   326  func (*Conjunction) node()       {}
   327  func (*Disjunction) node()       {}
   328  func (*BoundValue) node()        {}
   329  func (*Builtin) node()           {}
   330  func (*BuiltinValidator) node()  {}
   331  func (*Bottom) node()            {}
   332  func (*Null) node()              {}
   333  func (*Bool) node()              {}
   334  func (*Num) node()               {}
   335  func (*String) node()            {}
   336  func (*Bytes) node()             {}
   337  func (*Top) node()               {}
   338  func (*BasicType) node()         {}
   339  func (*StructLit) node()         {}
   340  func (*ListLit) node()           {}
   341  func (*BoundExpr) node()         {}
   342  func (*NodeLink) node()          {}
   343  func (*FieldReference) node()    {}
   344  func (*ValueReference) node()    {}
   345  func (*LabelReference) node()    {}
   346  func (*DynamicReference) node()  {}
   347  func (*ImportReference) node()   {}
   348  func (*LetReference) node()      {}
   349  func (*SelectorExpr) node()      {}
   350  func (*IndexExpr) node()         {}
   351  func (*SliceExpr) node()         {}
   352  func (*Interpolation) node()     {}
   353  func (*UnaryExpr) node()         {}
   354  func (*BinaryExpr) node()        {}
   355  func (*CallExpr) node()          {}
   356  func (*DisjunctionExpr) node()   {}
   357  func (*Field) node()             {}
   358  func (*OptionalField) node()     {}
   359  func (*BulkOptionalField) node() {}
   360  func (*DynamicField) node()      {}
   361  func (*Ellipsis) node()          {}
   362  func (*ForClause) node()         {}
   363  func (*IfClause) node()          {}
   364  func (*LetClause) node()         {}
   365  func (*ValueClause) node()       {}