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

Commit

Permalink
internal/core/convert: initial commit
Browse files Browse the repository at this point in the history
Change-Id: Ifc51d85dbef3995ae198cf092058ce3010412681
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6505
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 19, 2020
1 parent 1d29ac3 commit 08a9fdb
Show file tree
Hide file tree
Showing 6 changed files with 1,175 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cue/testdata/compile/erralias.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ b: "foo"
c: {}
for x in c { a: E }

Z1=[Z2=string]: Z1+Z2

-- out/compile --
unreferenced alias or let clause X:
./in.cue:1:1
Expand All @@ -29,4 +31,5 @@ for[].a: reference "E" not found:
for _, x in 〈0;c〉 {
a: _|_(reference "E" not found)
}
[string]: (〈0;(〈0;-〉)〉 + 〈0;-〉)
}
8 changes: 8 additions & 0 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package adt

import (
"fmt"
"reflect"
"regexp"

"github.com/cockroachdb/apd/v2"
Expand Down Expand Up @@ -64,6 +65,13 @@ type Runtime interface {
// LoadImport loads a unique Vertex associated with a given import path. It
// returns an error if no import for this package could be found.
LoadImport(importPath string) (*Vertex, errors.Error)

// StoreType associates a CUE expression with a Go type.
StoreType(t reflect.Type, src ast.Expr, expr Expr)

// LoadType retrieves a previously stored CUE expression for a given Go
// type if available.
LoadType(t reflect.Type) (src ast.Expr, expr Expr, ok bool)
}

type Config struct {
Expand Down
35 changes: 32 additions & 3 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import (

// Config configures a compilation.
type Config struct {
// Scope specifies a node in which to look up unresolved references. This
// is useful for evaluating expressions within an already evaluated
// configuration.
//
// TODO
Scope *adt.Vertex
}

// Files compiles the given files as a single instance. It disregards
Expand All @@ -47,6 +53,23 @@ func Files(cfg *Config, r adt.Runtime, files ...*ast.File) (*adt.Vertex, errors.
return v, nil
}

func Expr(cfg *Config, r adt.Runtime, x ast.Expr) (adt.Conjunct, errors.Error) {
if cfg == nil {
cfg = &Config{}
}
c := &compiler{
Config: *cfg,
index: r,
}

v := c.compileExpr(x)

if c.errs != nil {
return v, c.errs
}
return v, nil
}

func newCompiler(cfg *Config, r adt.Runtime) *compiler {
c := &compiler{
index: r,
Expand Down Expand Up @@ -204,8 +227,11 @@ func (c *compiler) compileFiles(a []*ast.File) *adt.Vertex { // Or value?
return res
}

func (c *compiler) compileExpr(x ast.Expr) adt.Expr {
return c.expr(x)
func (c *compiler) compileExpr(x ast.Expr) adt.Conjunct {
expr := c.expr(x)

env := &adt.Environment{}
return adt.MakeConjunct(env, expr)
}

// resolve assumes that all existing resolutions are legal. Validation should
Expand Down Expand Up @@ -379,7 +405,7 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
e := aliasEntry{source: a}

switch lab.(type) {
case *ast.Ident, *ast.BasicLit:
case *ast.Ident, *ast.BasicLit, *ast.ListLit:
// Even though we won't need the alias, we still register it
// for duplicate and failed reference detection.
default:
Expand Down Expand Up @@ -425,17 +451,20 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
// error
return c.errf(x, "list label must have one element")
}
var label adt.Feature
elem := l.Elts[0]
// TODO: record alias for error handling? In principle it is okay
// to have duplicates, but we do want it to be used.
if a, ok := elem.(*ast.Alias); ok {
label = c.label(a.Ident)
elem = a.Expr
}

return &adt.BulkOptionalField{
Src: x,
Filter: c.expr(elem),
Value: value,
Label: label,
}

case *ast.Interpolation: // *ast.ParenExpr,
Expand Down

0 comments on commit 08a9fdb

Please sign in to comment.