Skip to content

Commit

Permalink
internal/core/adt: pass Conjunct instead of Environment to Resolve
Browse files Browse the repository at this point in the history
This prepares for the cycle rework.

The PushX methods could probably be simplified later on.

Signed-off-by: Marcel van Lohuizen <[email protected]>

Change-Id: I18b596e42488851c973bb2bef2f57bd2b062589f
Signed-off-by: Marcel van Lohuizen <[email protected]>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/537829
Reviewed-by: Roger Peppe <[email protected]>
Unity-Result: CUEcueckoo <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mpvl committed Aug 15, 2022
1 parent 3bedf8e commit 428d395
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ func Dereference(v Value) Value {
}

ctx := v.ctx()
n, b := ctx.Resolve(c.Env, r)
n, b := ctx.Resolve(c, r)
if b != nil {
return newErrValue(v, b)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Resolve(ctx *OpContext, c Conjunct) *Vertex {
v = x

case Resolver:
r, err := ctx.Resolve(env, x)
r, err := ctx.Resolve(c, x)
if err != nil {
v = err
break
Expand Down
33 changes: 28 additions & 5 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ type OpContext struct {
freeListNode *nodeContext

e *Environment
ci CloseInfo
src ast.Node
errs *Bottom
positions []Node // keep track of error positions
Expand All @@ -239,7 +240,7 @@ type OpContext struct {

// inDisjunct indicates that non-monotonic checks should be skipped.
// This is used if we want to do some extra work to eliminate disjunctions
// early. The result of unificantion should be thrown away if this check is
// early. The result of unification should be thrown away if this check is
// used.
//
// TODO: replace this with a mechanism to determine the correct set (per
Expand Down Expand Up @@ -392,12 +393,14 @@ type frame struct {
env *Environment
err *Bottom
src ast.Node
ci CloseInfo
}

func (c *OpContext) PushState(env *Environment, src ast.Node) (saved frame) {
saved.env = c.e
saved.err = c.errs
saved.src = c.src
saved.ci = c.ci

c.errs = nil
if src != nil {
Expand All @@ -408,11 +411,30 @@ func (c *OpContext) PushState(env *Environment, src ast.Node) (saved frame) {
return saved
}

func (c *OpContext) PushConjunct(x Conjunct) (saved frame) {
src := x.Expr().Source()

saved.env = c.e
saved.err = c.errs
saved.src = c.src
saved.ci = c.ci

c.errs = nil
if src != nil {
c.src = src
}
c.e = x.Env
c.ci = x.CloseInfo

return saved
}

func (c *OpContext) PopState(s frame) *Bottom {
err := c.errs
c.e = s.env
c.errs = s.err
c.src = s.src
c.ci = s.ci
return err
}

Expand All @@ -433,8 +455,8 @@ func (c *OpContext) PopArc(saved *Vertex) {
//
// Should only be used to insert Conjuncts. TODO: perhaps only return Conjuncts
// and error.
func (c *OpContext) Resolve(env *Environment, r Resolver) (*Vertex, *Bottom) {
s := c.PushState(env, r.Source())
func (c *OpContext) Resolve(x Conjunct, r Resolver) (*Vertex, *Bottom) {
s := c.PushConjunct(x)

arc := r.resolve(c, Partial)

Expand Down Expand Up @@ -580,8 +602,9 @@ func (c *OpContext) Evaluate(env *Environment, x Expr) (result Value, complete b
return val, true
}

func (c *OpContext) evaluateRec(env *Environment, x Expr, state VertexStatus) Value {
s := c.PushState(env, x.Source())
func (c *OpContext) evaluateRec(v Conjunct, state VertexStatus) Value {
x := v.Expr()
s := c.PushConjunct(v)

val := c.evalState(x, state)
if val == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ func (n *nodeContext) evalExpr(v Conjunct) {

switch x := v.Expr().(type) {
case Resolver:
arc, err := ctx.Resolve(v.Env, x)
arc, err := ctx.Resolve(v, x)
if err != nil && !err.IsIncomplete() {
n.addBottom(err)
break
Expand All @@ -1452,7 +1452,7 @@ func (n *nodeContext) evalExpr(v Conjunct) {
case Evaluator:
// Interpolation, UnaryExpr, BinaryExpr, CallExpr
// Could be unify?
val := ctx.evaluateRec(v.Env, v.Expr(), Partial)
val := ctx.evaluateRec(v, Partial)
if b, ok := val.(*Bottom); ok && b.IsIncomplete() {
n.exprs = append(n.exprs, envExpr{v, b})
break
Expand Down
2 changes: 1 addition & 1 deletion internal/core/dep/dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (c *visitor) markResolver(env *adt.Environment, r adt.Resolver) {
return
}

if ref, _ := c.ctxt.Resolve(env, r); ref != nil {
if ref, _ := c.ctxt.Resolve(adt.MakeConjunct(env, r, adt.CloseInfo{}), r); ref != nil {
if ref != c.node && ref != empty {
d := Dependency{
Node: ref,
Expand Down
2 changes: 1 addition & 1 deletion tools/trim/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (t *trimmer) addDominators(d, v *adt.Vertex, hasDisjunction bool) (doms *ad
doms.AddConjunct(c)
default:
if r, ok := c.Elem().(adt.Resolver); ok {
x, _ := t.ctx.Resolve(c.Env, r)
x, _ := t.ctx.Resolve(c, r)
// Even if this is not a dominator now, descendants will be.
if x != nil && x.Label.IsDef() {
for _, c := range x.Conjuncts {
Expand Down

0 comments on commit 428d395

Please sign in to comment.