github.com/solo-io/cue@v0.4.7/internal/core/eval/eval.go (about)

     1  // Copyright 2021 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 eval
    16  
    17  import (
    18  	"github.com/solo-io/cue/internal/core/adt"
    19  	"github.com/solo-io/cue/internal/core/debug"
    20  )
    21  
    22  func Evaluate(r adt.Runtime, v *adt.Vertex) {
    23  	format := func(n adt.Node) string {
    24  		return debug.NodeString(r, n, printConfig)
    25  	}
    26  	c := adt.New(v, &adt.Config{
    27  		Runtime: r,
    28  		Format:  format,
    29  	})
    30  	c.Unify(v, adt.Finalized)
    31  }
    32  
    33  func New(r adt.Runtime) *Unifier {
    34  	return &Unifier{r: r, e: NewContext(r, nil)}
    35  }
    36  
    37  type Unifier struct {
    38  	r adt.Runtime
    39  	e *adt.OpContext
    40  }
    41  
    42  func (e *Unifier) Unify(ctx *adt.OpContext, v *adt.Vertex, state adt.VertexStatus) {
    43  	e.e.Unify(v, state)
    44  }
    45  
    46  func (e *Unifier) Stats() *adt.Stats {
    47  	return e.e.Stats()
    48  }
    49  
    50  // TODO: Note: NewContext takes essentially a cue.Value. By making this
    51  // type more central, we can perhaps avoid context creation.
    52  func NewContext(r adt.Runtime, v *adt.Vertex) *adt.OpContext {
    53  	format := func(n adt.Node) string {
    54  		return debug.NodeString(r, n, printConfig)
    55  	}
    56  	return adt.New(v, &adt.Config{
    57  		Runtime: r,
    58  		Format:  format,
    59  	})
    60  }
    61  
    62  func (e *Unifier) NewContext(v *adt.Vertex) *adt.OpContext {
    63  	return NewContext(e.r, v)
    64  }
    65  
    66  var printConfig = &debug.Config{Compact: true}