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
15package eval
16
17import (
18 "cuelang.org/go/cue/stats"
19 "cuelang.org/go/internal/core/adt"
20 "cuelang.org/go/internal/core/debug"
21)
22
23func 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
31func New(r adt.Runtime) *Unifier {
32 ctx := NewContext(r, nil)
33 return &Unifier{r: r, e: ctx}
34}
35
36type Unifier struct {
37 r adt.Runtime
38 e *adt.OpContext
39}
40
41func (e *Unifier) Stats() *stats.Counts {
42 return e.e.Stats()
43}
44
45// TODO: Note: NewContext takes essentially a cue.Value. By making this
46// type more central, we can perhaps avoid context creation.
47func NewContext(r adt.Runtime, v *adt.Vertex) *adt.OpContext {
48 return adt.New(v, &adt.Config{
49 Runtime: r,
50 Format: nodeFormat,
51 })
52}
53
54func (e *Unifier) NewContext(v *adt.Vertex) *adt.OpContext {
55 return NewContext(e.r, v)
56}
57
58var printConfig = &debug.Config{Compact: true}
59
60func nodeFormat(r adt.Runtime, n adt.Node) string {
61 return debug.NodeString(r, n, printConfig)
62}