Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/core/compile: separate out phase of let clauses
Browse files Browse the repository at this point in the history
This allows referring to lets later in scope, as the spec allows.

By hindsight, a better mechanism would be to treat lets as
arcs with special handling. This will preserve formatting
information and will obviate the need for special caching
to avoid exponential blowup.

This can be done as an overall rework of making Vertex
processing linear, instead of the current O(n^2).

Change-Id: I7b5f04c3fb434425d9f1c53fc4b1c1f2d218ed25
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6624
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Jul 19, 2020
1 parent cf94469 commit e986a8f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
24 changes: 23 additions & 1 deletion cue/testdata/compile/scope.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ e & { // Is this allowed? Probably not as per comprehension rule (ref fixes.)
B = {open: int}
f: B

schema: {
next: _schema_1
}

let _schema_1 = schema

-- out/compile --
--- in.cue
{
Expand Down Expand Up @@ -64,9 +70,17 @@ f: B
}
}
f: 〈0;let B〉
schema: {
next: 〈1;let _schema_1〉
}
}
-- out/eval --
(struct){
Errors:
structural cycle

Result:
(_|_){
// [structural cycle]
e: (struct){
}
a: (struct){
Expand All @@ -86,4 +100,12 @@ f: B
f: (struct){
open: (int){ int }
}
schema: (_|_){
// [structural cycle]
next: (_|_){
// [structural cycle] structural cycle
next: (_|_){// 〈1;let _schema_1〉
}
}
}
}
58 changes: 32 additions & 26 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ func (c *compiler) resolve(n *ast.Ident) adt.Expr {
}

func (c *compiler) addDecls(st *adt.StructLit, a []ast.Decl) {
for _, d := range a {
c.addLetDecl(d)
}
for _, d := range a {
if x := c.decl(d); x != nil {
st.Decls = append(st.Decls, x)
Expand Down Expand Up @@ -535,32 +538,8 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
}
}

// An alias reference will have an expression that is looked up in the
// environment cash.
case *ast.LetClause:
// Cache the parsed expression. Creating a unique expression for each
// reference allows the computation to be shared given that we don't
// have fields for expressions. This, in turn, prevents exponential
// blowup. in x2: x1+x1, x3: x2+x2, ... patterns.

expr := c.labeledExpr(nil, (*letScope)(x), x.Expr)

a := aliasEntry{source: x, expr: expr}

if err := c.insertAlias(x.Ident, a); err != nil {
return err
}

case *ast.Alias:

expr := c.labeledExpr(nil, (*deprecatedAliasScope)(x), x.Expr)

// TODO(legacy): deprecated, remove this use of Alias
a := aliasEntry{source: x, expr: expr}

if err := c.insertAlias(x.Ident, a); err != nil {
return err
}
// Handled in addLetDecl.
case *ast.LetClause, *ast.Alias:

case *ast.CommentGroup:
// Nothing to do for a free-floating comment group.
Expand All @@ -586,6 +565,33 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
return nil
}

func (c *compiler) addLetDecl(d ast.Decl) {
switch x := d.(type) {
// An alias reference will have an expression that is looked up in the
// environment cash.
case *ast.LetClause:
// Cache the parsed expression. Creating a unique expression for each
// reference allows the computation to be shared given that we don't
// have fields for expressions. This, in turn, prevents exponential
// blowup in x2: x1+x1, x3: x2+x2, ... patterns.

expr := c.labeledExpr(nil, (*letScope)(x), x.Expr)

a := aliasEntry{source: x, expr: expr}

c.insertAlias(x.Ident, a)

case *ast.Alias:

expr := c.labeledExpr(nil, (*deprecatedAliasScope)(x), x.Expr)

// TODO(legacy): deprecated, remove this use of Alias
a := aliasEntry{source: x, expr: expr}

c.insertAlias(x.Ident, a)
}
}

func (c *compiler) elem(n ast.Expr) adt.Elem {
switch x := n.(type) {
case *ast.Ellipsis:
Expand Down
1 change: 1 addition & 0 deletions internal/core/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var needFix = map[string]string{
"export/030": "cycle",

"cycle/025_cannot_resolve_references_that_would_be_ambiguous": "cycle",
"compile/scope": "cycle",

"resolve/034_closing_structs": "close()",
"fulleval/053_issue312": "close()",
Expand Down

0 comments on commit e986a8f

Please sign in to comment.