Skip to content

Commit

Permalink
internal/core/compile: allow resolution in custom scope
Browse files Browse the repository at this point in the history
Change-Id: Idec88d30f6770c090097ba0319aa12a8370ddd8f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6623
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 19, 2020
1 parent 0dcc335 commit cf94469
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ type Config struct {
//
// TODO
Scope *adt.Vertex

// Imports allows unresolved identifiers to resolve to imports.
//
// Under normal circumstances, identifiers bind to import specifications,
// which get resolved to an ImportReference. Use this option to automaically
// resolve identifiers to imports.
Imports func(x *ast.Ident) (pkgPath string)
}

// Files compiles the given files as a single instance. It disregards
Expand Down Expand Up @@ -195,7 +202,6 @@ func (c *compiler) popScope() {
c.stack = c.stack[:k]
}

// entry points // USE CONFIG
func (c *compiler) compileFiles(a []*ast.File) *adt.Vertex { // Or value?
c.fileScope = map[adt.Feature]bool{}

Expand All @@ -211,6 +217,21 @@ func (c *compiler) compileFiles(a []*ast.File) *adt.Vertex { // Or value?
}
}

// TODO: Assume that the other context is unified with the newly compiled
// files. This is not the same behavior as the old functionality, but we
// wanted to nix this anyway. For instance by allowing pkg_tool to be
// treated differently.
if v := c.Config.Scope; v != nil {
for _, arc := range v.Arcs {
if _, ok := c.fileScope[arc.Label]; !ok {
c.fileScope[arc.Label] = true
}
}

c.pushScope(nil, 0, v.Source()) // File scope
defer c.popScope()
}

// TODO: set doc.
res := &adt.Vertex{}

Expand All @@ -228,9 +249,35 @@ func (c *compiler) compileFiles(a []*ast.File) *adt.Vertex { // Or value?
}

func (c *compiler) compileExpr(x ast.Expr) adt.Conjunct {
c.fileScope = map[adt.Feature]bool{}

if v := c.Config.Scope; v != nil {
for _, arc := range v.Arcs {
c.fileScope[arc.Label] = true
}

c.pushScope(nil, 0, v.Source()) // File scope
defer c.popScope()
}

expr := c.expr(x)

env := &adt.Environment{}
top := env

for p := c.Config.Scope; p != nil; p = p.Parent {
top.Vertex = p
top.Up = &adt.Environment{}
top = top.Up

// TODO: do something like this to allow multi-layered scopes.
// e := &adt.Environment{Vertex: p}
// if env != nil {
// env.Up = e
// }
// env = e
}

return adt.MakeConjunct(env, expr)
}

Expand Down Expand Up @@ -266,6 +313,16 @@ func (c *compiler) resolve(n *ast.Ident) adt.Expr {
}
}

if c.Config.Imports != nil {
if pkgPath := c.Config.Imports(n); pkgPath != "" {
return &adt.ImportReference{
Src: n,
ImportPath: adt.MakeStringLabel(c.index, pkgPath),
Label: c.label(n),
}
}
}

if p := predeclared(n); p != nil {
return p
}
Expand Down

0 comments on commit cf94469

Please sign in to comment.