cuelang.org/go@v0.10.1/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 "cuelang.org/go/cue/stats" 19 "cuelang.org/go/internal/core/adt" 20 "cuelang.org/go/internal/core/debug" 21 ) 22 23 func Evaluate(r adt.Runtime, v *adt.Vertex) { 24 c := adt.New(v, &adt.Config{ 25 Runtime: r, 26 Format: nodeFormat, 27 }) 28 v.Finalize(c) 29 } 30 31 func New(r adt.Runtime) *Unifier { 32 ctx := NewContext(r, nil) 33 // TODO: we could access these directly if we can use runtime.Runtime directly. 34 ctx.Version, ctx.Config = r.Settings() 35 return &Unifier{r: r, e: ctx} 36 } 37 38 type Unifier struct { 39 r adt.Runtime 40 e *adt.OpContext 41 } 42 43 func (e *Unifier) Stats() *stats.Counts { 44 return e.e.Stats() 45 } 46 47 // TODO: Note: NewContext takes essentially a cue.Value. By making this 48 // type more central, we can perhaps avoid context creation. 49 func NewContext(r adt.Runtime, v *adt.Vertex) *adt.OpContext { 50 return adt.New(v, &adt.Config{ 51 Runtime: r, 52 Format: nodeFormat, 53 }) 54 } 55 56 func (e *Unifier) NewContext(v *adt.Vertex) *adt.OpContext { 57 return NewContext(e.r, v) 58 } 59 60 var printConfig = &debug.Config{Compact: true} 61 62 func nodeFormat(r adt.Runtime, n adt.Node) string { 63 return debug.NodeString(r, n, printConfig) 64 }