cue: move to external builtin packages
This includes a new generator for builtin packages.
Change-Id: Ieab84e16d9136c2ad5d2d8e8cad49c84a6e0b658
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6885
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/cue/ast/astutil/file_test.go b/cue/ast/astutil/file_test.go
index 15f0636..f2cdc80 100644
--- a/cue/ast/astutil/file_test.go
+++ b/cue/ast/astutil/file_test.go
@@ -18,12 +18,14 @@
"strings"
"testing"
+ "github.com/google/go-cmp/cmp"
+
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/token"
- "github.com/google/go-cmp/cmp"
+ _ "cuelang.org/go/pkg"
)
func TestToFile(t *testing.T) {
@@ -93,7 +95,7 @@
got := string(b)
want := strings.TrimLeft(tc.want, "\n")
if got != want {
- t.Error(cmp.Diff(got, want))
+ t.Error(cmp.Diff(want, got))
}
})
}
diff --git a/cue/ast/ident.go b/cue/ast/ident.go
index 86b50ed..c86da16 100644
--- a/cue/ast/ident.go
+++ b/cue/ast/ident.go
@@ -16,12 +16,12 @@
import (
"strconv"
+ "strings"
"unicode"
"unicode/utf8"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
- "cuelang.org/go/pkg/strings"
)
func isLetter(ch rune) bool {
diff --git a/cue/builtin.go b/cue/builtin.go
index 2eb37d9..d3f2844 100644
--- a/cue/builtin.go
+++ b/cue/builtin.go
@@ -12,159 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-//go:generate go run golang.org/x/tools/cmd/goimports -w -local cuelang.org/go builtins.go
-//go:generate gofmt -s -w builtins.go
-
package cue
import (
- "encoding/json"
- "fmt"
- "io"
- "math/big"
"path"
- "sort"
"strings"
- "github.com/cockroachdb/apd/v2"
-
- "cuelang.org/go/cue/errors"
- "cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
- "cuelang.org/go/internal/core/compile"
- "cuelang.org/go/internal/core/convert"
)
-// A Builtin is a builtin function or constant.
-//
-// A function may return and a constant may be any of the following types:
-//
-// error (translates to bottom)
-// nil (translates to null)
-// bool
-// int*
-// uint*
-// float64
-// string
-// *big.Float
-// *big.Int
-//
-// For any of the above, including interface{} and these types recursively:
-// []T
-// map[string]T
-//
-type Builtin struct {
- Name string
- Pkg adt.Feature
- Params []adt.Kind
- Result adt.Kind
- Func func(c *CallCtxt)
- Const string
-}
-
-type Package struct {
- Native []*Builtin
- CUE string
-}
-
-func (p *Package) MustCompile(ctx *adt.OpContext, pkgName string) *adt.Vertex {
- obj := &adt.Vertex{}
- pkgLabel := ctx.StringLabel(pkgName)
- st := &adt.StructLit{}
- if len(p.Native) > 0 {
- obj.AddConjunct(adt.MakeConjunct(nil, st))
- }
- for _, b := range p.Native {
- b.Pkg = pkgLabel
-
- f := ctx.StringLabel(b.Name) // never starts with _
- // n := &node{baseValue: newBase(imp.Path)}
- var v adt.Expr = toBuiltin(ctx, b)
- if b.Const != "" {
- v = mustParseConstBuiltin(ctx, b.Name, b.Const)
- }
- st.Decls = append(st.Decls, &adt.Field{
- Label: f,
- Value: v,
- })
- }
-
- // Parse builtin CUE
- if p.CUE != "" {
- expr, err := parser.ParseExpr(pkgName, p.CUE)
- if err != nil {
- panic(fmt.Errorf("could not parse %v: %v", p.CUE, err))
- }
- c, err := compile.Expr(nil, ctx.Runtime, expr)
- if err != nil {
- panic(fmt.Errorf("could compile parse %v: %v", p.CUE, err))
- }
- obj.AddConjunct(c)
- }
-
- // We could compile lazily, but this is easier for debugging.
- obj.Finalize(ctx)
- if err := obj.Err(ctx, adt.Finalized); err != nil {
- panic(err.Err)
- }
-
- return obj
-}
-
-func toBuiltin(ctx *adt.OpContext, b *Builtin) *adt.Builtin {
- x := &adt.Builtin{
- Params: b.Params,
- Result: b.Result,
- Package: b.Pkg,
- Name: b.Name,
- }
- x.Func = func(ctx *adt.OpContext, args []adt.Value) (ret adt.Expr) {
- // call, _ := ctx.Source().(*ast.CallExpr)
- c := &CallCtxt{
- // src: call,
- ctx: ctx,
- args: args,
- builtin: b,
- }
- defer func() {
- var errVal interface{} = c.Err
- if err := recover(); err != nil {
- errVal = err
- }
- ret = processErr(c, errVal, ret)
- }()
- b.Func(c)
- switch v := c.Ret.(type) {
- case adt.Value:
- return v
- case bottomer:
- return v.Bottom()
- }
- if c.Err != nil {
- return nil
- }
- return convert.GoValueToValue(ctx, c.Ret, true)
- }
- return x
-}
-
-// newConstBuiltin parses and creates any CUE expression that does not have
-// fields.
-func mustParseConstBuiltin(ctx *adt.OpContext, name, val string) adt.Expr {
- expr, err := parser.ParseExpr("<builtin:"+name+">", val)
- if err != nil {
- panic(err)
- }
- c, err := compile.Expr(nil, ctx, expr)
- if err != nil {
- panic(err)
- }
- return c.Expr()
-
-}
-
func pos(n adt.Node) (p token.Pos) {
if n == nil {
return
@@ -176,143 +34,25 @@
return src.Pos()
}
-func (x *Builtin) name(ctx *adt.OpContext) string {
- if x.Pkg == 0 {
- return x.Name
- }
- return fmt.Sprintf("%s.%s", x.Pkg.StringValue(ctx), x.Name)
-}
-
-func (x *Builtin) isValidator() bool {
- return len(x.Params) == 1 && x.Result == adt.BoolKind
-}
-
-func processErr(call *CallCtxt, errVal interface{}, ret adt.Expr) adt.Expr {
- ctx := call.ctx
- switch err := errVal.(type) {
- case nil:
- case *callError:
- ret = err.b
- case *json.MarshalerError:
- if err, ok := err.Err.(bottomer); ok {
- if b := err.Bottom(); b != nil {
- ret = b
- }
- }
- case bottomer:
- ret = wrapCallErr(call, err.Bottom())
- case errors.Error:
- ret = wrapCallErr(call, &adt.Bottom{Err: err})
- case error:
- if call.Err == internal.ErrIncomplete {
- err := ctx.NewErrf("incomplete value")
- err.Code = adt.IncompleteError
- ret = err
- } else {
- // TODO: store the underlying error explicitly
- ret = wrapCallErr(call, &adt.Bottom{Err: errors.Promote(err, "")})
- }
- default:
- // Likely a string passed to panic.
- ret = wrapCallErr(call, &adt.Bottom{
- Err: errors.Newf(call.Pos(), "%s", err),
- })
- }
- return ret
-}
-
-func wrapCallErr(c *CallCtxt, b *adt.Bottom) *adt.Bottom {
- pos := token.NoPos
- if c.src != nil {
- if src := c.src.Source(); src != nil {
- pos = src.Pos()
- }
- }
- const msg = "error in call to %s"
- return &adt.Bottom{
- Code: b.Code,
- Err: errors.Wrapf(b.Err, pos, msg, c.builtin.name(c.ctx)),
- }
-}
-
-func (c *CallCtxt) convertError(x interface{}, name string) *adt.Bottom {
- var err errors.Error
- switch v := x.(type) {
- case nil:
- return nil
-
- case *adt.Bottom:
- return v
-
- case *json.MarshalerError:
- err = errors.Promote(v, "marshal error")
-
- case errors.Error:
- err = v
-
- case error:
- if name != "" {
- err = errors.Newf(c.Pos(), "%s: %v", name, v)
- } else {
- err = errors.Newf(c.Pos(), "error in call to %s: %v", c.Name(), v)
- }
-
- default:
- err = errors.Newf(token.NoPos, "%s", name)
- }
- if err != internal.ErrIncomplete {
- return &adt.Bottom{
- // Wrap to preserve position information.
- Err: errors.Wrapf(err, c.Pos(), "error in call to %s", c.Name()),
- }
- }
- return &adt.Bottom{
- Code: adt.IncompleteError,
- Err: errors.Newf(c.Pos(), "incomplete values in call to %s", c.Name()),
- }
-}
-
-// CallCtxt is passed to builtin implementations that need to use a cue.Value. This is an internal type. It's interface may change.
-type CallCtxt struct {
- src adt.Expr // *adt.CallExpr
- ctx *adt.OpContext
- builtin *Builtin
- Err interface{}
- Ret interface{}
-
- args []adt.Value
-}
-
-func (c *CallCtxt) Pos() token.Pos {
- return c.ctx.Pos()
-}
-
-func (c *CallCtxt) Name() string {
- return c.builtin.name(c.ctx)
-}
-
var builtins = map[string]*Instance{}
-func initBuiltins(pkgs map[string]*Package) {
+func AddBuiltinPackage(importPath string, f func(*adt.OpContext) (*adt.Vertex, error)) {
ctx := sharedIndex.newContext().opCtx
- keys := []string{}
- for k := range pkgs {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- for _, k := range keys {
- b := pkgs[k]
- e := b.MustCompile(ctx, k)
- i := sharedIndex.addInst(&Instance{
- ImportPath: k,
- PkgName: path.Base(k),
- root: e,
- })
-
- builtins[k] = i
- builtins["-/"+path.Base(k)] = i
+ v, err := f(ctx)
+ if err != nil {
+ panic(err)
}
+
+ k := importPath
+ i := sharedIndex.addInst(&Instance{
+ ImportPath: k,
+ PkgName: path.Base(k),
+ root: v,
+ })
+
+ builtins[k] = i
+ builtins["-/"+path.Base(k)] = i
}
func getBuiltinPkg(ctx *context, path string) *structLit {
@@ -342,279 +82,3 @@
return v.Unify(makeValue(v.idx, a))
}
}
-
-// Do returns whether the call should be done.
-func (c *CallCtxt) Do() bool {
- return c.Err == nil
-}
-
-type bottomer interface {
- error
- Bottom() *adt.Bottom
-}
-
-type callError struct {
- b *adt.Bottom
-}
-
-func (e *callError) Error() string {
- return fmt.Sprint(e.b)
-}
-
-func (c *CallCtxt) errf(src adt.Node, underlying error, format string, args ...interface{}) {
- var errs errors.Error
- if err, ok := underlying.(bottomer); ok {
- errs = err.Bottom().Err
- }
- errs = errors.Wrapf(errs, c.ctx.Pos(), format, args...)
- c.Err = &callError{&adt.Bottom{Err: errs}}
-}
-
-func (c *CallCtxt) errcf(src adt.Node, code adt.ErrorCode, format string, args ...interface{}) {
- err := c.ctx.NewErrf(format, args...)
- err.Code = code
- c.Err = &callError{err}
-}
-
-func (c *CallCtxt) Value(i int) Value {
- v := MakeValue(c.ctx, c.args[i])
- // TODO: remove default
- // v, _ = v.Default()
- if !v.IsConcrete() {
- c.errcf(c.src, adt.IncompleteError, "non-concrete argument %d", i)
- }
- return v
-}
-
-func (c *CallCtxt) Struct(i int) *Struct {
- v := MakeValue(c.ctx, c.args[i])
- s, err := v.Struct()
- if err != nil {
- c.invalidArgType(c.args[i], i, "struct", err)
- return nil
- }
- return s
-}
-
-func (c *CallCtxt) invalidArgType(arg adt.Expr, i int, typ string, err error) {
- if ve, ok := err.(bottomer); ok && ve.Bottom().IsIncomplete() {
- c.Err = ve
- return
- }
- v, ok := arg.(adt.Value)
- // TODO: make these permanent errors if the value did not originate from
- // a reference.
- if !ok {
- c.errf(c.src, nil,
- "cannot use incomplete value %s as %s in argument %d to %s: %v",
- c.ctx.Str(arg), typ, i, c.Name(), err)
- }
- if err != nil {
- c.errf(c.src, err,
- "cannot use %s (type %s) as %s in argument %d to %s: %v",
- c.ctx.Str(arg), v.Kind(), typ, i, c.Name(), err)
- } else {
- c.errf(c.src, err,
- "cannot use %s (type %s) as %s in argument %d to %s",
- c.ctx.Str(arg), v.Kind(), typ, i, c.Name())
- }
-}
-
-func (c *CallCtxt) Int(i int) int { return int(c.intValue(i, 64, "int64")) }
-func (c *CallCtxt) Int8(i int) int8 { return int8(c.intValue(i, 8, "int8")) }
-func (c *CallCtxt) Int16(i int) int16 { return int16(c.intValue(i, 16, "int16")) }
-func (c *CallCtxt) Int32(i int) int32 { return int32(c.intValue(i, 32, "int32")) }
-func (c *CallCtxt) Rune(i int) rune { return rune(c.intValue(i, 32, "rune")) }
-func (c *CallCtxt) Int64(i int) int64 { return int64(c.intValue(i, 64, "int64")) }
-
-func (c *CallCtxt) intValue(i, bits int, typ string) int64 {
- arg := c.args[i]
- x := MakeValue(c.ctx, arg)
- n, err := x.Int(nil)
- if err != nil {
- c.invalidArgType(arg, i, typ, err)
- return 0
- }
- if n.BitLen() > bits {
- c.errf(c.src, err, "int %s overflows %s in argument %d in call to %s",
- n, typ, i, c.Name())
- }
- res, _ := x.Int64()
- return res
-}
-
-func (c *CallCtxt) Uint(i int) uint { return uint(c.uintValue(i, 64, "uint64")) }
-func (c *CallCtxt) Uint8(i int) uint8 { return uint8(c.uintValue(i, 8, "uint8")) }
-func (c *CallCtxt) Byte(i int) uint8 { return byte(c.uintValue(i, 8, "byte")) }
-func (c *CallCtxt) Uint16(i int) uint16 { return uint16(c.uintValue(i, 16, "uint16")) }
-func (c *CallCtxt) Uint32(i int) uint32 { return uint32(c.uintValue(i, 32, "uint32")) }
-func (c *CallCtxt) Uint64(i int) uint64 { return uint64(c.uintValue(i, 64, "uint64")) }
-
-func (c *CallCtxt) uintValue(i, bits int, typ string) uint64 {
- x := MakeValue(c.ctx, c.args[i])
- n, err := x.Int(nil)
- if err != nil || n.Sign() < 0 {
- c.invalidArgType(c.args[i], i, typ, err)
- return 0
- }
- if n.BitLen() > bits {
- c.errf(c.src, err, "int %s overflows %s in argument %d in call to %s",
- n, typ, i, c.Name())
- }
- res, _ := x.Uint64()
- return res
-}
-
-func (c *CallCtxt) Decimal(i int) *apd.Decimal {
- x := MakeValue(c.ctx, c.args[i])
- if _, err := x.MantExp(nil); err != nil {
- c.invalidArgType(c.args[i], i, "Decimal", err)
- return nil
- }
- return &c.args[i].(*adt.Num).X
-}
-
-func (c *CallCtxt) Float64(i int) float64 {
- x := MakeValue(c.ctx, c.args[i])
- res, err := x.Float64()
- if err != nil {
- c.invalidArgType(c.args[i], i, "float64", err)
- return 0
- }
- return res
-}
-
-func (c *CallCtxt) BigInt(i int) *big.Int {
- x := MakeValue(c.ctx, c.args[i])
- n, err := x.Int(nil)
- if err != nil {
- c.invalidArgType(c.args[i], i, "int", err)
- return nil
- }
- return n
-}
-
-var ten = big.NewInt(10)
-
-func (c *CallCtxt) BigFloat(i int) *big.Float {
- x := MakeValue(c.ctx, c.args[i])
- var mant big.Int
- exp, err := x.MantExp(&mant)
- if err != nil {
- c.invalidArgType(c.args[i], i, "float", err)
- return nil
- }
- f := &big.Float{}
- f.SetInt(&mant)
- if exp != 0 {
- var g big.Float
- e := big.NewInt(int64(exp))
- f.Mul(f, g.SetInt(e.Exp(ten, e, nil)))
- }
- return f
-}
-
-func (c *CallCtxt) String(i int) string {
- x := MakeValue(c.ctx, c.args[i])
- v, err := x.String()
- if err != nil {
- c.invalidArgType(c.args[i], i, "string", err)
- return ""
- }
- return v
-}
-
-func (c *CallCtxt) Bytes(i int) []byte {
- x := MakeValue(c.ctx, c.args[i])
- v, err := x.Bytes()
- if err != nil {
- c.invalidArgType(c.args[i], i, "bytes", err)
- return nil
- }
- return v
-}
-
-func (c *CallCtxt) Reader(i int) io.Reader {
- x := MakeValue(c.ctx, c.args[i])
- // TODO: optimize for string and bytes cases
- r, err := x.Reader()
- if err != nil {
- c.invalidArgType(c.args[i], i, "bytes|string", err)
- return nil
- }
- return r
-}
-
-func (c *CallCtxt) Bool(i int) bool {
- x := MakeValue(c.ctx, c.args[i])
- b, err := x.Bool()
- if err != nil {
- c.invalidArgType(c.args[i], i, "bool", err)
- return false
- }
- return b
-}
-
-func (c *CallCtxt) List(i int) (a []Value) {
- arg := c.args[i]
- x := MakeValue(c.ctx, arg)
- v, err := x.List()
- if err != nil {
- c.invalidArgType(c.args[i], i, "list", err)
- return a
- }
- for v.Next() {
- a = append(a, v.Value())
- }
- return a
-}
-
-func (c *CallCtxt) Iter(i int) (a Iterator) {
- arg := c.args[i]
- x := MakeValue(c.ctx, arg)
- v, err := x.List()
- if err != nil {
- c.invalidArgType(c.args[i], i, "list", err)
- }
- return v
-}
-
-func (c *CallCtxt) DecimalList(i int) (a []*apd.Decimal) {
- arg := c.args[i]
- x := MakeValue(c.ctx, arg)
- v, err := x.List()
- if err != nil {
- c.invalidArgType(c.args[i], i, "list", err)
- return nil
- }
- for j := 0; v.Next(); j++ {
- num, err := v.Value().Decimal()
- if err != nil {
- c.errf(c.src, err, "invalid list element %d in argument %d to %s: %v",
- j, i, c.Name(), err)
- break
- }
- a = append(a, num)
- }
- return a
-}
-
-func (c *CallCtxt) StringList(i int) (a []string) {
- arg := c.args[i]
- x := MakeValue(c.ctx, arg)
- v, err := x.List()
- if err != nil {
- c.invalidArgType(c.args[i], i, "list", err)
- return nil
- }
- for j := 0; v.Next(); j++ {
- str, err := v.Value().String()
- if err != nil {
- c.Err = errors.Wrapf(err, c.Pos(),
- "element %d of list argument %d", j, i)
- break
- }
- a = append(a, str)
- }
- return a
-}
diff --git a/cue/builtin_test.go b/cue/builtin_test.go
index 617235e..97a1dc8 100644
--- a/cue/builtin_test.go
+++ b/cue/builtin_test.go
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package cue
+package cue_test
import (
"fmt"
@@ -20,6 +20,12 @@
"strconv"
"strings"
"testing"
+
+ "cuelang.org/go/cue"
+ "cuelang.org/go/cue/build"
+ "cuelang.org/go/cue/token"
+
+ _ "cuelang.org/go/pkg"
)
func TestBuiltins(t *testing.T) {
@@ -667,13 +673,12 @@
}}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
- insts := Build(makeInstances(tc.instances))
+ insts := cue.Build(makeInstances(tc.instances))
if err := insts[0].Err; err != nil {
t.Fatal(err)
}
v := insts[0].Value()
- ctx := v.ctx()
- got := ctx.opCtx.Str(v.v)
+ got := fmt.Sprintf("%+v", v)
if got != tc.emit {
t.Errorf("\n got: %s\nwant: %s", got, tc.emit)
}
@@ -699,16 +704,64 @@
}}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
- insts := Build(makeInstances(tc.instances))
+ insts := cue.Build(makeInstances(tc.instances))
if err := insts[0].Err; err != nil {
t.Fatal(err)
}
v := insts[0].Value()
- ctx := v.ctx()
- got := ctx.opCtx.Str(v.v)
+ got := fmt.Sprint(v)
if got != tc.emit {
t.Errorf("\n got: %s\nwant: %s", got, tc.emit)
}
})
}
}
+
+type builder struct {
+ ctxt *build.Context
+ imports map[string]*bimport
+}
+
+func (b *builder) load(pos token.Pos, path string) *build.Instance {
+ bi := b.imports[path]
+ if bi == nil {
+ return nil
+ }
+ return b.build(bi)
+}
+
+type bimport struct {
+ path string // "" means top-level
+ files []string
+}
+
+func makeInstances(insts []*bimport) (instances []*build.Instance) {
+ b := builder{
+ ctxt: build.NewContext(),
+ imports: map[string]*bimport{},
+ }
+ for _, bi := range insts {
+ if bi.path != "" {
+ b.imports[bi.path] = bi
+ }
+ }
+ for _, bi := range insts {
+ if bi.path == "" {
+ instances = append(instances, b.build(bi))
+ }
+ }
+ return
+}
+
+func (b *builder) build(bi *bimport) *build.Instance {
+ path := bi.path
+ if path == "" {
+ path = "dir"
+ }
+ p := b.ctxt.NewInstance(path, b.load)
+ for i, f := range bi.files {
+ _ = p.AddFile(fmt.Sprintf("file%d.cue", i), f)
+ }
+ _ = p.Complete()
+ return p
+}
diff --git a/cue/builtins.go b/cue/builtins.go
deleted file mode 100644
index 90671fe..0000000
--- a/cue/builtins.go
+++ /dev/null
@@ -1,3784 +0,0 @@
-// Code generated by go generate. DO NOT EDIT.
-
-package cue
-
-import (
- "bytes"
- "crypto/md5"
- "crypto/sha1"
- "crypto/sha256"
- "crypto/sha512"
- "encoding/base64"
- "encoding/csv"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "html"
- "io"
- "math"
- "math/big"
- "math/bits"
- "net"
- "path"
- "regexp"
- "sort"
- "strconv"
- "strings"
- "text/tabwriter"
- "text/template"
- "time"
- "unicode"
- "unicode/utf8"
-
- "github.com/cockroachdb/apd/v2"
- "golang.org/x/net/idna"
-
- "cuelang.org/go/cue/errors"
- "cuelang.org/go/cue/literal"
- "cuelang.org/go/cue/parser"
- "cuelang.org/go/internal"
- cueyaml "cuelang.org/go/internal/encoding/yaml"
- "cuelang.org/go/internal/third_party/yaml"
-)
-
-func init() {
- initBuiltins(builtinPackages)
-}
-
-var _ io.Reader
-
-var roundTruncContext = apd.Context{Rounding: apd.RoundDown}
-
-var roundUpContext = apd.Context{Rounding: apd.RoundHalfUp}
-
-var roundEvenContext = apd.Context{Rounding: apd.RoundHalfEven}
-
-var mulContext = apd.BaseContext.WithPrecision(1)
-
-var apdContext = apd.BaseContext.WithPrecision(24)
-
-var zero = apd.New(0, 0)
-
-var two = apd.New(2, 0)
-
-var idnaProfile = idna.New(
- idna.ValidateLabels(true),
- idna.VerifyDNSLength(true),
- idna.StrictDomainName(true),
-)
-
-func netGetIP(ip Value) (goip net.IP) {
- switch ip.Kind() {
- case StringKind:
- s, err := ip.String()
- if err != nil {
- return nil
- }
- goip := net.ParseIP(s)
- if goip == nil {
- return nil
- }
- return goip
-
- case BytesKind:
- b, err := ip.Bytes()
- if err != nil {
- return nil
- }
- goip := net.ParseIP(string(b))
- if goip == nil {
- return nil
- }
- return goip
-
- case ListKind:
- iter, err := ip.List()
- if err != nil {
- return nil
- }
- for iter.Next() {
- v, err := iter.Value().Int64()
- if err != nil {
- return nil
- }
- if v < 0 || 255 < v {
- return nil
- }
- goip = append(goip, byte(v))
- }
- return goip
-
- default:
-
- return nil
- }
-}
-
-func netToList(ip net.IP) []uint {
- a := make([]uint, len(ip))
- for i, p := range ip {
- a[i] = uint(p)
- }
- return a
-}
-
-var split = path.Split
-
-var pathClean = path.Clean
-
-var pathExt = path.Ext
-
-var pathBase = path.Base
-
-var pathIsAbs = path.IsAbs
-
-var pathDir = path.Dir
-
-var errNoMatch = errors.New("no match")
-
-var errNoNamedGroup = errors.New("no named groups")
-
-func timeFormat(value, layout string) (bool, error) {
- _, err := time.Parse(layout, value)
- if err != nil {
-
- return false, fmt.Errorf("invalid time %q", value)
- }
- return true, nil
-}
-
-var builtinPackages = map[string]*Package{
- // "": {
- // native: []*builtin{},
- // },
- "crypto/md5": {
- Native: []*Builtin{{
- Name: "Size",
- Const: "16",
- }, {
- Name: "BlockSize",
- Const: "64",
- }, {
- Name: "Sum",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := md5.Sum(data)
- return a[:]
- }()
- }
- },
- }},
- },
- "crypto/sha1": {
- Native: []*Builtin{{
- Name: "Size",
- Const: "20",
- }, {
- Name: "BlockSize",
- Const: "64",
- }, {
- Name: "Sum",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha1.Sum(data)
- return a[:]
- }()
- }
- },
- }},
- },
- "crypto/sha256": {
- Native: []*Builtin{{
- Name: "Size",
- Const: "32",
- }, {
- Name: "Size224",
- Const: "28",
- }, {
- Name: "BlockSize",
- Const: "64",
- }, {
- Name: "Sum256",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha256.Sum256(data)
- return a[:]
- }()
- }
- },
- }, {
- Name: "Sum224",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha256.Sum224(data)
- return a[:]
- }()
- }
- },
- }},
- },
- "crypto/sha512": {
- Native: []*Builtin{{
- Name: "Size",
- Const: "64",
- }, {
- Name: "Size224",
- Const: "28",
- }, {
- Name: "Size256",
- Const: "32",
- }, {
- Name: "Size384",
- Const: "48",
- }, {
- Name: "BlockSize",
- Const: "128",
- }, {
- Name: "Sum512",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha512.Sum512(data)
- return a[:]
- }()
- }
- },
- }, {
- Name: "Sum384",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha512.Sum384(data)
- return a[:]
- }()
- }
- },
- }, {
- Name: "Sum512_224",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha512.Sum512_224(data)
- return a[:]
- }()
- }
- },
- }, {
- Name: "Sum512_256",
- Params: []kind{bytesKind | stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- a := sha512.Sum512_256(data)
- return a[:]
- }()
- }
- },
- }},
- },
- "encoding/base64": {
- Native: []*Builtin{{
- Name: "EncodedLen",
- Params: []kind{topKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- encoding, n := c.Value(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if err := encoding.Null(); err != nil {
- return 0, fmt.Errorf("base64: unsupported encoding: %v", err)
- }
- return base64.StdEncoding.EncodedLen(n), nil
- }()
- }
- },
- }, {
- Name: "DecodedLen",
- Params: []kind{topKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- encoding, x := c.Value(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if err := encoding.Null(); err != nil {
- return 0, fmt.Errorf("base64: unsupported encoding: %v", err)
- }
- return base64.StdEncoding.DecodedLen(x), nil
- }()
- }
- },
- }, {
- Name: "Encode",
- Params: []kind{topKind, bytesKind | stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- encoding, src := c.Value(0), c.Bytes(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if err := encoding.Null(); err != nil {
- return "", fmt.Errorf("base64: unsupported encoding: %v", err)
- }
- return base64.StdEncoding.EncodeToString(src), nil
- }()
- }
- },
- }, {
- Name: "Decode",
- Params: []kind{topKind, stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- encoding, s := c.Value(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if err := encoding.Null(); err != nil {
- return nil, fmt.Errorf("base64: unsupported encoding: %v", err)
- }
- return base64.StdEncoding.DecodeString(s)
- }()
- }
- },
- }},
- },
- "encoding/csv": {
- Native: []*Builtin{{
- Name: "Encode",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- x := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- buf := &bytes.Buffer{}
- w := csv.NewWriter(buf)
- iter, err := x.List()
- if err != nil {
- return "", err
- }
- for iter.Next() {
- row, err := iter.Value().List()
- if err != nil {
- return "", err
- }
- a := []string{}
- for row.Next() {
- col := row.Value()
- if str, err := col.String(); err == nil {
- a = append(a, str)
- } else {
- b, err := col.MarshalJSON()
- if err != nil {
- return "", err
- }
- a = append(a, string(b))
- }
- }
- _ = w.Write(a)
- }
- w.Flush()
- return buf.String(), nil
- }()
- }
- },
- }, {
- Name: "Decode",
- Params: []kind{bytesKind | stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- r := c.Reader(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return csv.NewReader(r).ReadAll()
- }()
- }
- },
- }},
- },
- "encoding/hex": {
- Native: []*Builtin{{
- Name: "EncodedLen",
- Params: []kind{intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- n := c.Int(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return hex.EncodedLen(n)
- }()
- }
- },
- }, {
- Name: "DecodedLen",
- Params: []kind{intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x := c.Int(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return hex.DecodedLen(x)
- }()
- }
- },
- }, {
- Name: "Decode",
- Params: []kind{stringKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return hex.DecodeString(s)
- }()
- }
- },
- }, {
- Name: "Dump",
- Params: []kind{bytesKind | stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return hex.Dump(data)
- }()
- }
- },
- }, {
- Name: "Encode",
- Params: []kind{bytesKind | stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- src := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return hex.EncodeToString(src)
- }()
- }
- },
- }},
- },
- "encoding/json": {
- Native: []*Builtin{{
- Name: "Valid",
- Params: []kind{bytesKind | stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return json.Valid(data)
- }()
- }
- },
- }, {
- Name: "Compact",
- Params: []kind{bytesKind | stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- src := c.Bytes(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- dst := bytes.Buffer{}
- if err := json.Compact(&dst, src); err != nil {
- return "", err
- }
- return dst.String(), nil
- }()
- }
- },
- }, {
- Name: "Indent",
- Params: []kind{bytesKind | stringKind, stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- src, prefix, indent := c.Bytes(0), c.String(1), c.String(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- dst := bytes.Buffer{}
- if err := json.Indent(&dst, src, prefix, indent); err != nil {
- return "", err
- }
- return dst.String(), nil
- }()
- }
- },
- }, {
- Name: "HTMLEscape",
- Params: []kind{bytesKind | stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- src := c.Bytes(0)
- if c.Do() {
- c.Ret = func() interface{} {
- dst := &bytes.Buffer{}
- json.HTMLEscape(dst, src)
- return dst.String()
- }()
- }
- },
- }, {
- Name: "Marshal",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- v := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- b, err := json.Marshal(v)
- return string(b), err
- }()
- }
- },
- }, {
- Name: "MarshalStream",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- v := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
-
- iter, err := v.List()
- if err != nil {
- return "", err
- }
- buf := &bytes.Buffer{}
- for iter.Next() {
- b, err := json.Marshal(iter.Value())
- if err != nil {
- return "", err
- }
- buf.Write(b)
- buf.WriteByte('\n')
- }
- return buf.String(), nil
- }()
- }
- },
- }, {
- Name: "Unmarshal",
- Params: []kind{bytesKind | stringKind},
- Result: topKind,
- Func: func(c *CallCtxt) {
- b := c.Bytes(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if !json.Valid(b) {
- return nil, fmt.Errorf("json: invalid JSON")
- }
- expr, err := parser.ParseExpr("json", b)
- if err != nil {
-
- return nil, fmt.Errorf("json: could not parse JSON: %v", err)
- }
- return expr, nil
- }()
- }
- },
- }, {
- Name: "Validate",
- Params: []kind{bytesKind | stringKind, topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- b, v := c.Bytes(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if !json.Valid(b) {
- return false, fmt.Errorf("json: invalid JSON")
- }
- r := internal.GetRuntimeNew(v).(*Runtime)
- inst, err := r.Compile("json.Validate", b)
- if err != nil {
- return false, err
- }
-
- t := inst.Value()
-
- v = v.Unify(t)
- if v.Err() != nil {
- return false, v.Err()
- }
- return true, nil
- }()
- }
- },
- }},
- },
- "encoding/yaml": {
- Native: []*Builtin{{
- Name: "Marshal",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- v := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if err := v.Validate(Concrete(true)); err != nil {
- if err := v.Validate(); err != nil {
- return "", err
- }
- return "", internal.ErrIncomplete
- }
- n := v.Syntax(Final(), Concrete(true))
- b, err := cueyaml.Encode(n)
- return string(b), err
- }()
- }
- },
- }, {
- Name: "MarshalStream",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- v := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
-
- iter, err := v.List()
- if err != nil {
- return "", err
- }
- buf := &bytes.Buffer{}
- for i := 0; iter.Next(); i++ {
- if i > 0 {
- buf.WriteString("---\n")
- }
- v := iter.Value()
- if err := v.Validate(Concrete(true)); err != nil {
- if err := v.Validate(); err != nil {
- return "", err
- }
- return "", internal.ErrIncomplete
- }
- n := v.Syntax(Final(), Concrete(true))
- b, err := cueyaml.Encode(n)
- if err != nil {
- return "", err
- }
- buf.Write(b)
- }
- return buf.String(), nil
- }()
- }
- },
- }, {
- Name: "Unmarshal",
- Params: []kind{bytesKind | stringKind},
- Result: topKind,
- Func: func(c *CallCtxt) {
- data := c.Bytes(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return yaml.Unmarshal("", data)
- }()
- }
- },
- }, {
- Name: "Validate",
- Params: []kind{bytesKind | stringKind, topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- b, v := c.Bytes(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- d, err := yaml.NewDecoder("yaml.Validate", b)
- if err != nil {
- return false, err
- }
- r := internal.GetRuntimeNew(v).(*Runtime)
- for {
- expr, err := d.Decode()
- if err != nil {
- if err == io.EOF {
- return true, nil
- }
- return false, err
- }
-
- inst, err := r.CompileExpr(expr)
- if err != nil {
- return false, err
- }
-
- x := v.Unify(inst.Value())
- if err := x.Err(); err != nil {
- return false, err
- }
- if err := x.Validate(Concrete(true)); err != nil {
- return false, err
- }
-
- }
- }()
- }
- },
- }, {
- Name: "ValidatePartial",
- Params: []kind{bytesKind | stringKind, topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- b, v := c.Bytes(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- d, err := yaml.NewDecoder("yaml.ValidatePartial", b)
- if err != nil {
- return false, err
- }
- r := internal.GetRuntimeNew(v).(*Runtime)
- for {
- expr, err := d.Decode()
- if err != nil {
- if err == io.EOF {
- return true, nil
- }
- return false, err
- }
-
- inst, err := r.CompileExpr(expr)
- if err != nil {
- return false, err
- }
-
- if x := v.Unify(inst.Value()); x.Err() != nil {
- return false, x.Err()
- }
- }
- }()
- }
- },
- }},
- },
- "html": {
- Native: []*Builtin{{
- Name: "Escape",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return html.EscapeString(s)
- }()
- }
- },
- }, {
- Name: "Unescape",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return html.UnescapeString(s)
- }()
- }
- },
- }},
- },
- "list": {
- Native: []*Builtin{{
- Name: "Drop",
- Params: []kind{listKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- x, n := c.List(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if n < 0 {
- return nil, fmt.Errorf("negative index")
- }
-
- if n > len(x) {
- return []Value{}, nil
- }
-
- return x[n:], nil
- }()
- }
- },
- }, {
- Name: "FlattenN",
- Params: []kind{topKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- xs, depth := c.Value(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var flattenN func(Value, int) ([]Value, error)
- flattenN = func(xs Value, depth int) ([]Value, error) {
- var res []Value
- iter, err := xs.List()
- if err != nil {
- return nil, err
- }
- for iter.Next() {
- val, _ := iter.Value().Default()
- if val.Kind() == ListKind && depth != 0 {
- d := depth - 1
- values, err := flattenN(val, d)
- if err != nil {
- return nil, err
- }
- res = append(res, values...)
- } else {
- res = append(res, val)
- }
- }
- return res, nil
- }
- return flattenN(xs, depth)
- }()
- }
- },
- }, {
- Name: "Take",
- Params: []kind{listKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- x, n := c.List(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if n < 0 {
- return nil, fmt.Errorf("negative index")
- }
-
- if n > len(x) {
- return x, nil
- }
-
- return x[:n], nil
- }()
- }
- },
- }, {
- Name: "Slice",
- Params: []kind{listKind, intKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- x, i, j := c.List(0), c.Int(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if i < 0 {
- return nil, fmt.Errorf("negative index")
- }
-
- if i > j {
- return nil, fmt.Errorf("invalid index: %v > %v", i, j)
- }
-
- if i > len(x) {
- return nil, fmt.Errorf("slice bounds out of range")
- }
-
- if j > len(x) {
- return nil, fmt.Errorf("slice bounds out of range")
- }
-
- return x[i:j], nil
- }()
- }
- },
- }, {
- Name: "MinItems",
- Params: []kind{listKind, intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- a, n := c.List(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return len(a) >= n
- }()
- }
- },
- }, {
- Name: "MaxItems",
- Params: []kind{listKind, intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- a, n := c.List(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return len(a) <= n
- }()
- }
- },
- }, {
- Name: "UniqueItems",
- Params: []kind{listKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- a := c.List(0)
- if c.Do() {
- c.Ret = func() interface{} {
- b := []string{}
- for _, v := range a {
- b = append(b, fmt.Sprint(v))
- }
- sort.Strings(b)
- for i := 1; i < len(b); i++ {
- if b[i-1] == b[i] {
- return false
- }
- }
- return true
- }()
- }
- },
- }, {
- Name: "Contains",
- Params: []kind{listKind, topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- a, v := c.List(0), c.Value(1)
- if c.Do() {
- c.Ret = func() interface{} {
- for _, w := range a {
- if v.Equals(w) {
- return true
- }
- }
- return false
- }()
- }
- },
- }, {
- Name: "Avg",
- Params: []kind{listKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- xs := c.DecimalList(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if 0 == len(xs) {
- return nil, fmt.Errorf("empty list")
- }
-
- s := apd.New(0, 0)
- for _, x := range xs {
- _, err := internal.BaseContext.Add(s, x, s)
- if err != nil {
- return nil, err
- }
- }
-
- var d apd.Decimal
- l := apd.New(int64(len(xs)), 0)
- _, err := internal.BaseContext.Quo(&d, s, l)
- if err != nil {
- return nil, err
- }
- return &d, nil
- }()
- }
- },
- }, {
- Name: "Max",
- Params: []kind{listKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- xs := c.DecimalList(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if 0 == len(xs) {
- return nil, fmt.Errorf("empty list")
- }
-
- max := xs[0]
- for _, x := range xs[1:] {
- if -1 == max.Cmp(x) {
- max = x
- }
- }
- return max, nil
- }()
- }
- },
- }, {
- Name: "Min",
- Params: []kind{listKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- xs := c.DecimalList(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if 0 == len(xs) {
- return nil, fmt.Errorf("empty list")
- }
-
- min := xs[0]
- for _, x := range xs[1:] {
- if +1 == min.Cmp(x) {
- min = x
- }
- }
- return min, nil
- }()
- }
- },
- }, {
- Name: "Product",
- Params: []kind{listKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- xs := c.DecimalList(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- d := apd.New(1, 0)
- for _, x := range xs {
- _, err := internal.BaseContext.Mul(d, x, d)
- if err != nil {
- return nil, err
- }
- }
- return d, nil
- }()
- }
- },
- }, {
- Name: "Range",
- Params: []kind{numKind, numKind, numKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- start, limit, step := c.Decimal(0), c.Decimal(1), c.Decimal(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if step.IsZero() {
- return nil, fmt.Errorf("step must be non zero")
- }
-
- if !step.Negative && +1 == start.Cmp(limit) {
- return nil, fmt.Errorf("end must be greater than start when step is positive")
- }
-
- if step.Negative && -1 == start.Cmp(limit) {
- return nil, fmt.Errorf("end must be less than start when step is negative")
- }
-
- var vals []*internal.Decimal
- num := start
- for {
- if !step.Negative && -1 != num.Cmp(limit) {
- break
- }
-
- if step.Negative && +1 != num.Cmp(limit) {
- break
- }
-
- vals = append(vals, num)
- d := apd.New(0, 0)
- _, err := internal.BaseContext.Add(d, step, num)
- if err != nil {
- return nil, err
- }
- num = d
- }
- return vals, nil
- }()
- }
- },
- }, {
- Name: "Sum",
- Params: []kind{listKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- xs := c.DecimalList(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- d := apd.New(0, 0)
- for _, x := range xs {
- _, err := internal.BaseContext.Add(d, x, d)
- if err != nil {
- return nil, err
- }
- }
- return d, nil
- }()
- }
- },
- }, {
- Name: "Sort",
- Params: []kind{listKind, topKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- list, cmp := c.List(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- s := valueSorter{list, cmp, nil}
-
- sort.Sort(&s)
- return s.ret()
- }()
- }
- },
- }, {
- Name: "SortStable",
- Params: []kind{listKind, topKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- list, cmp := c.List(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- s := valueSorter{list, cmp, nil}
- sort.Stable(&s)
- return s.ret()
- }()
- }
- },
- }, {
- Name: "SortStrings",
- Params: []kind{listKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- a := c.StringList(0)
- if c.Do() {
- c.Ret = func() interface{} {
- sort.Strings(a)
- return a
- }()
- }
- },
- }, {
- Name: "IsSorted",
- Params: []kind{listKind, topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- list, cmp := c.List(0), c.Value(1)
- if c.Do() {
- c.Ret = func() interface{} {
- s := valueSorter{list, cmp, nil}
- return sort.IsSorted(&s)
- }()
- }
- },
- }, {
- Name: "IsSortedStrings",
- Params: []kind{listKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- a := c.StringList(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return sort.StringsAreSorted(a)
- }()
- }
- },
- }},
- CUE: `{
- Comparer: {
- T: _
- less: bool
- x: T
- y: T
- }
- Ascending: {
- T: number | string
- less: true && x < y
- x: T
- y: T
- Comparer
- }
- Descending: {
- T: number | string
- less: x > y
- x: T
- y: T
- Comparer
- }
-}`,
- },
- "math": {
- Native: []*Builtin{{
- Name: "MaxExp",
- Const: "2147483647",
- }, {
- Name: "MinExp",
- Const: "-2147483648",
- }, {
- Name: "MaxPrec",
- Const: "4294967295",
- }, {
- Name: "ToNearestEven",
- Const: "0",
- }, {
- Name: "ToNearestAway",
- Const: "1",
- }, {
- Name: "ToZero",
- Const: "2",
- }, {
- Name: "AwayFromZero",
- Const: "3",
- }, {
- Name: "ToNegativeInf",
- Const: "4",
- }, {
- Name: "ToPositiveInf",
- Const: "5",
- }, {
- Name: "Below",
- Const: "-1",
- }, {
- Name: "Exact",
- Const: "0",
- }, {
- Name: "Above",
- Const: "1",
- }, {
- Name: "Jacobi",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x, y := c.BigInt(0), c.BigInt(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return big.Jacobi(x, y)
- }()
- }
- },
- }, {
- Name: "MaxBase",
- Const: "62",
- }, {
- Name: "Floor",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Floor(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Ceil",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Ceil(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Trunc",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := roundTruncContext.RoundToIntegralExact(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Round",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := roundUpContext.RoundToIntegralExact(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "RoundToEven",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := roundEvenContext.RoundToIntegralExact(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "MultipleOf",
- Params: []kind{numKind, numKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- x, y := c.Decimal(0), c.Decimal(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d apd.Decimal
- cond, err := mulContext.Quo(&d, x, y)
- return !cond.Inexact(), err
- }()
- }
- },
- }, {
- Name: "Abs",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Abs(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Acosh",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Acosh(x)
- }()
- }
- },
- }, {
- Name: "Asin",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Asin(x)
- }()
- }
- },
- }, {
- Name: "Acos",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Acos(x)
- }()
- }
- },
- }, {
- Name: "Asinh",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Asinh(x)
- }()
- }
- },
- }, {
- Name: "Atan",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Atan(x)
- }()
- }
- },
- }, {
- Name: "Atan2",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- y, x := c.Float64(0), c.Float64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Atan2(y, x)
- }()
- }
- },
- }, {
- Name: "Atanh",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Atanh(x)
- }()
- }
- },
- }, {
- Name: "Cbrt",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Cbrt(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "E",
- Const: "2.71828182845904523536028747135266249775724709369995957496696763",
- }, {
- Name: "Pi",
- Const: "3.14159265358979323846264338327950288419716939937510582097494459",
- }, {
- Name: "Phi",
- Const: "1.61803398874989484820458683436563811772030917980576286213544861",
- }, {
- Name: "Sqrt2",
- Const: "1.41421356237309504880168872420969807856967187537694807317667974",
- }, {
- Name: "SqrtE",
- Const: "1.64872127070012814684865078781416357165377610071014801157507931",
- }, {
- Name: "SqrtPi",
- Const: "1.77245385090551602729816748334114518279754945612238712821380779",
- }, {
- Name: "SqrtPhi",
- Const: "1.27201964951406896425242246173749149171560804184009624861664038",
- }, {
- Name: "Ln2",
- Const: "0.693147180559945309417232121458176568075500134360255254120680009",
- }, {
- Name: "Log2E",
- Const: "1.442695040888963407359924681001892137426645954152985934135449408",
- }, {
- Name: "Ln10",
- Const: "2.3025850929940456840179914546843642076011014886287729760333278",
- }, {
- Name: "Log10E",
- Const: "0.43429448190325182765112891891660508229439700580366656611445378",
- }, {
- Name: "Copysign",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x, y := c.Decimal(0), c.Decimal(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var d internal.Decimal
- d.Set(x)
- d.Negative = y.Negative
- return &d
- }()
- }
- },
- }, {
- Name: "Dim",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x, y := c.Decimal(0), c.Decimal(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Sub(&d, x, y)
- if err != nil {
- return nil, err
- }
- if d.Negative {
- return zero, nil
- }
- return &d, nil
- }()
- }
- },
- }, {
- Name: "Erf",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Erf(x)
- }()
- }
- },
- }, {
- Name: "Erfc",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Erfc(x)
- }()
- }
- },
- }, {
- Name: "Erfinv",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Erfinv(x)
- }()
- }
- },
- }, {
- Name: "Erfcinv",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Erfcinv(x)
- }()
- }
- },
- }, {
- Name: "Exp",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Exp(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Exp2",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Pow(&d, two, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Expm1",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Expm1(x)
- }()
- }
- },
- }, {
- Name: "Gamma",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Gamma(x)
- }()
- }
- },
- }, {
- Name: "Hypot",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- p, q := c.Float64(0), c.Float64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Hypot(p, q)
- }()
- }
- },
- }, {
- Name: "J0",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.J0(x)
- }()
- }
- },
- }, {
- Name: "Y0",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Y0(x)
- }()
- }
- },
- }, {
- Name: "J1",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.J1(x)
- }()
- }
- },
- }, {
- Name: "Y1",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Y1(x)
- }()
- }
- },
- }, {
- Name: "Jn",
- Params: []kind{intKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- n, x := c.Int(0), c.Float64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Jn(n, x)
- }()
- }
- },
- }, {
- Name: "Yn",
- Params: []kind{intKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- n, x := c.Int(0), c.Float64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Yn(n, x)
- }()
- }
- },
- }, {
- Name: "Ldexp",
- Params: []kind{numKind, intKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- frac, exp := c.Float64(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Ldexp(frac, exp)
- }()
- }
- },
- }, {
- Name: "Log",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Ln(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Log10",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Log10(&d, x)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Log2",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d, ln2 internal.Decimal
- _, _ = apdContext.Ln(&ln2, two)
- _, err := apdContext.Ln(&d, x)
- if err != nil {
- return &d, err
- }
- _, err = apdContext.Quo(&d, &d, &ln2)
- return &d, nil
- }()
- }
- },
- }, {
- Name: "Log1p",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Log1p(x)
- }()
- }
- },
- }, {
- Name: "Logb",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Logb(x)
- }()
- }
- },
- }, {
- Name: "Ilogb",
- Params: []kind{numKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Ilogb(x)
- }()
- }
- },
- }, {
- Name: "Mod",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x, y := c.Float64(0), c.Float64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Mod(x, y)
- }()
- }
- },
- }, {
- Name: "Pow",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x, y := c.Decimal(0), c.Decimal(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var d internal.Decimal
- _, err := apdContext.Pow(&d, x, y)
- return &d, err
- }()
- }
- },
- }, {
- Name: "Pow10",
- Params: []kind{intKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- n := c.Int32(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return apd.New(1, n)
- }()
- }
- },
- }, {
- Name: "Remainder",
- Params: []kind{numKind, numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x, y := c.Float64(0), c.Float64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Remainder(x, y)
- }()
- }
- },
- }, {
- Name: "Signbit",
- Params: []kind{numKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- x := c.Decimal(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return x.Negative
- }()
- }
- },
- }, {
- Name: "Cos",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Cos(x)
- }()
- }
- },
- }, {
- Name: "Sin",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Sin(x)
- }()
- }
- },
- }, {
- Name: "Sinh",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Sinh(x)
- }()
- }
- },
- }, {
- Name: "Cosh",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Cosh(x)
- }()
- }
- },
- }, {
- Name: "Sqrt",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Sqrt(x)
- }()
- }
- },
- }, {
- Name: "Tan",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Tan(x)
- }()
- }
- },
- }, {
- Name: "Tanh",
- Params: []kind{numKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- x := c.Float64(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return math.Tanh(x)
- }()
- }
- },
- }},
- },
- "math/bits": {
- Native: []*Builtin{{
- Name: "Lsh",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x, n := c.BigInt(0), c.Uint(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.Lsh(x, n)
- return &z
- }()
- }
- },
- }, {
- Name: "Rsh",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x, n := c.BigInt(0), c.Uint(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.Rsh(x, n)
- return &z
- }()
- }
- },
- }, {
- Name: "At",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x, i := c.BigInt(0), c.Uint(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if i > math.MaxInt32 {
- return 0, fmt.Errorf("bit index too large")
- }
- return x.Bit(int(i)), nil
- }()
- }
- },
- }, {
- Name: "Set",
- Params: []kind{intKind, intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x, i, bit := c.BigInt(0), c.Int(1), c.Uint(2)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.SetBit(x, i, bit)
- return &z
- }()
- }
- },
- }, {
- Name: "And",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- a, b := c.BigInt(0), c.BigInt(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.And(a, b)
- return &z
- }()
- }
- },
- }, {
- Name: "Or",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- a, b := c.BigInt(0), c.BigInt(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.Or(a, b)
- return &z
- }()
- }
- },
- }, {
- Name: "Xor",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- a, b := c.BigInt(0), c.BigInt(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.Xor(a, b)
- return &z
- }()
- }
- },
- }, {
- Name: "Clear",
- Params: []kind{intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- a, b := c.BigInt(0), c.BigInt(1)
- if c.Do() {
- c.Ret = func() interface{} {
- var z big.Int
- z.AndNot(a, b)
- return &z
- }()
- }
- },
- }, {
- Name: "OnesCount",
- Params: []kind{intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x := c.BigInt(0)
- if c.Do() {
- c.Ret = func() interface{} {
- var count int
- for _, w := range x.Bits() {
- count += bits.OnesCount64(uint64(w))
- }
- return count
- }()
- }
- },
- }, {
- Name: "Len",
- Params: []kind{intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- x := c.BigInt(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return x.BitLen()
- }()
- }
- },
- }},
- },
- "net": {
- Native: []*Builtin{{
- Name: "SplitHostPort",
- Params: []kind{stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- host, port, err := net.SplitHostPort(s)
- if err != nil {
- return nil, err
- }
- return []string{host, port}, nil
- }()
- }
- },
- }, {
- Name: "JoinHostPort",
- Params: []kind{topKind, topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- host, port := c.Value(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- var err error
- hostStr := ""
- switch host.Kind() {
- case ListKind:
- ipdata := netGetIP(host)
- if len(ipdata) != 4 && len(ipdata) != 16 {
- err = fmt.Errorf("invalid host %q", host)
- }
- hostStr = ipdata.String()
- case BytesKind:
- var b []byte
- b, err = host.Bytes()
- hostStr = string(b)
- default:
- hostStr, err = host.String()
- }
- if err != nil {
- return "", err
- }
-
- portStr := ""
- switch port.Kind() {
- case StringKind:
- portStr, err = port.String()
- case BytesKind:
- var b []byte
- b, err = port.Bytes()
- portStr = string(b)
- default:
- var i int64
- i, err = port.Int64()
- portStr = strconv.Itoa(int(i))
- }
- if err != nil {
- return "", err
- }
-
- return net.JoinHostPort(hostStr, portStr), nil
- }()
- }
- },
- }, {
- Name: "FQDN",
- Params: []kind{stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- for i := 0; i < len(s); i++ {
- if s[i] >= utf8.RuneSelf {
- return false
- }
- }
- _, err := idnaProfile.ToASCII(s)
- return err == nil
- }()
- }
- },
- }, {
- Name: "IPv4len",
- Const: "4",
- }, {
- Name: "IPv6len",
- Const: "16",
- }, {
- Name: "ParseIP",
- Params: []kind{stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- goip := net.ParseIP(s)
- if goip == nil {
- return nil, fmt.Errorf("invalid IP address %q", s)
- }
- return netToList(goip), nil
- }()
- }
- },
- }, {
- Name: "IPv4",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
-
- return netGetIP(ip).To4() != nil
- }()
- }
- },
- }, {
- Name: "IP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
-
- return netGetIP(ip) != nil
- }()
- }
- },
- }, {
- Name: "LoopbackIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsLoopback()
- }()
- }
- },
- }, {
- Name: "MulticastIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsMulticast()
- }()
- }
- },
- }, {
- Name: "InterfaceLocalMulticastIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsInterfaceLocalMulticast()
- }()
- }
- },
- }, {
- Name: "LinkLocalMulticastIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsLinkLocalMulticast()
- }()
- }
- },
- }, {
- Name: "LinkLocalUnicastIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsLinkLocalUnicast()
- }()
- }
- },
- }, {
- Name: "GlobalUnicastIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsGlobalUnicast()
- }()
- }
- },
- }, {
- Name: "UnspecifiedIP",
- Params: []kind{topKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return netGetIP(ip).IsUnspecified()
- }()
- }
- },
- }, {
- Name: "ToIP4",
- Params: []kind{topKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- ipdata := netGetIP(ip)
- if ipdata == nil {
- return nil, fmt.Errorf("invalid IP %q", ip)
- }
- ipv4 := ipdata.To4()
- if ipv4 == nil {
- return nil, fmt.Errorf("cannot convert %q to IPv4", ipdata)
- }
- return netToList(ipv4), nil
- }()
- }
- },
- }, {
- Name: "ToIP16",
- Params: []kind{topKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- ipdata := netGetIP(ip)
- if ipdata == nil {
- return nil, fmt.Errorf("invalid IP %q", ip)
- }
- return netToList(ipdata), nil
- }()
- }
- },
- }, {
- Name: "IPString",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- ip := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- ipdata := netGetIP(ip)
- if ipdata == nil {
- return "", fmt.Errorf("invalid IP %q", ip)
- }
- return ipdata.String(), nil
- }()
- }
- },
- }},
- },
- "path": {
- Native: []*Builtin{{
- Name: "Split",
- Params: []kind{stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- path := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- file, dir := split(path)
- return []string{file, dir}
- }()
- }
- },
- }, {
- Name: "Match",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- pattern, name := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return path.Match(pattern, name)
- }()
- }
- },
- }, {
- Name: "Clean",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- path := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return pathClean(path)
- }()
- }
- },
- }, {
- Name: "Ext",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- path := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return pathExt(path)
- }()
- }
- },
- }, {
- Name: "Base",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- path := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return pathBase(path)
- }()
- }
- },
- }, {
- Name: "IsAbs",
- Params: []kind{stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- path := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return pathIsAbs(path)
- }()
- }
- },
- }, {
- Name: "Dir",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- path := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return pathDir(path)
- }()
- }
- },
- }},
- },
- "regexp": {
- Native: []*Builtin{{
- Name: "Valid",
- Params: []kind{stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- pattern := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- _, err := regexp.Compile(pattern)
- return err == nil, err
- }()
- }
- },
- }, {
- Name: "Find",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- pattern, s := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- re, err := regexp.Compile(pattern)
- if err != nil {
- return "", err
- }
- m := re.FindStringIndex(s)
- if m == nil {
- return "", errNoMatch
- }
- return s[m[0]:m[1]], nil
- }()
- }
- },
- }, {
- Name: "FindAll",
- Params: []kind{stringKind, stringKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- pattern, s, n := c.String(0), c.String(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- re, err := regexp.Compile(pattern)
- if err != nil {
- return nil, err
- }
- m := re.FindAllString(s, n)
- if m == nil {
- return nil, errNoMatch
- }
- return m, nil
- }()
- }
- },
- }, {
- Name: "FindSubmatch",
- Params: []kind{stringKind, stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- pattern, s := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- re, err := regexp.Compile(pattern)
- if err != nil {
- return nil, err
- }
- m := re.FindStringSubmatch(s)
- if m == nil {
- return nil, errNoMatch
- }
- return m, nil
- }()
- }
- },
- }, {
- Name: "FindAllSubmatch",
- Params: []kind{stringKind, stringKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- pattern, s, n := c.String(0), c.String(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- re, err := regexp.Compile(pattern)
- if err != nil {
- return nil, err
- }
- m := re.FindAllStringSubmatch(s, n)
- if m == nil {
- return nil, errNoMatch
- }
- return m, nil
- }()
- }
- },
- }, {
- Name: "FindNamedSubmatch",
- Params: []kind{stringKind, stringKind},
- Result: structKind,
- Func: func(c *CallCtxt) {
- pattern, s := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- re, err := regexp.Compile(pattern)
- if err != nil {
- return nil, err
- }
- names := re.SubexpNames()
- if len(names) == 0 {
- return nil, errNoNamedGroup
- }
- m := re.FindStringSubmatch(s)
- if m == nil {
- return nil, errNoMatch
- }
- r := make(map[string]string, len(names)-1)
- for k, name := range names {
- if name != "" {
- r[name] = m[k]
- }
- }
- return r, nil
- }()
- }
- },
- }, {
- Name: "FindAllNamedSubmatch",
- Params: []kind{stringKind, stringKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- pattern, s, n := c.String(0), c.String(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- re, err := regexp.Compile(pattern)
- if err != nil {
- return nil, err
- }
- names := re.SubexpNames()
- if len(names) == 0 {
- return nil, errNoNamedGroup
- }
- m := re.FindAllStringSubmatch(s, n)
- if m == nil {
- return nil, errNoMatch
- }
- result := make([]map[string]string, len(m))
- for i, m := range m {
- r := make(map[string]string, len(names)-1)
- for k, name := range names {
- if name != "" {
- r[name] = m[k]
- }
- }
- result[i] = r
- }
- return result, nil
- }()
- }
- },
- }, {
- Name: "Match",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- pattern, s := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return regexp.MatchString(pattern, s)
- }()
- }
- },
- }, {
- Name: "QuoteMeta",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return regexp.QuoteMeta(s)
- }()
- }
- },
- }},
- },
- "strconv": {
- Native: []*Builtin{{
- Name: "Unquote",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return literal.Unquote(s)
- }()
- }
- },
- }, {
- Name: "ParseBool",
- Params: []kind{stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- str := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return strconv.ParseBool(str)
- }()
- }
- },
- }, {
- Name: "FormatBool",
- Params: []kind{boolKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- b := c.Bool(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.FormatBool(b)
- }()
- }
- },
- }, {
- Name: "ParseFloat",
- Params: []kind{stringKind, intKind},
- Result: numKind,
- Func: func(c *CallCtxt) {
- s, bitSize := c.String(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return strconv.ParseFloat(s, bitSize)
- }()
- }
- },
- }, {
- Name: "IntSize",
- Const: "64",
- }, {
- Name: "ParseUint",
- Params: []kind{stringKind, intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, base, bitSize := c.String(0), c.Int(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return strconv.ParseUint(s, base, bitSize)
- }()
- }
- },
- }, {
- Name: "ParseInt",
- Params: []kind{stringKind, intKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, base, bitSize := c.String(0), c.Int(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return strconv.ParseInt(s, base, bitSize)
- }()
- }
- },
- }, {
- Name: "Atoi",
- Params: []kind{stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return strconv.Atoi(s)
- }()
- }
- },
- }, {
- Name: "FormatFloat",
- Params: []kind{numKind, intKind, intKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- f, fmt, prec, bitSize := c.Float64(0), c.Byte(1), c.Int(2), c.Int(3)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.FormatFloat(f, fmt, prec, bitSize)
- }()
- }
- },
- }, {
- Name: "FormatUint",
- Params: []kind{intKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- i, base := c.Uint64(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.FormatUint(i, base)
- }()
- }
- },
- }, {
- Name: "FormatInt",
- Params: []kind{intKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- i, base := c.Int64(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.FormatInt(i, base)
- }()
- }
- },
- }, {
- Name: "Quote",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.Quote(s)
- }()
- }
- },
- }, {
- Name: "QuoteToASCII",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.QuoteToASCII(s)
- }()
- }
- },
- }, {
- Name: "QuoteToGraphic",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.QuoteToGraphic(s)
- }()
- }
- },
- }, {
- Name: "QuoteRune",
- Params: []kind{intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- r := c.Rune(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.QuoteRune(r)
- }()
- }
- },
- }, {
- Name: "QuoteRuneToASCII",
- Params: []kind{intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- r := c.Rune(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.QuoteRuneToASCII(r)
- }()
- }
- },
- }, {
- Name: "QuoteRuneToGraphic",
- Params: []kind{intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- r := c.Rune(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.QuoteRuneToGraphic(r)
- }()
- }
- },
- }, {
- Name: "IsPrint",
- Params: []kind{intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- r := c.Rune(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.IsPrint(r)
- }()
- }
- },
- }, {
- Name: "IsGraphic",
- Params: []kind{intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- r := c.Rune(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strconv.IsGraphic(r)
- }()
- }
- },
- }},
- },
- "strings": {
- Native: []*Builtin{{
- Name: "ByteAt",
- Params: []kind{bytesKind | stringKind, intKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- b, i := c.Bytes(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if i < 0 || i >= len(b) {
- return 0, fmt.Errorf("index out of range")
- }
- return b[i], nil
- }()
- }
- },
- }, {
- Name: "ByteSlice",
- Params: []kind{bytesKind | stringKind, intKind, intKind},
- Result: bytesKind | stringKind,
- Func: func(c *CallCtxt) {
- b, start, end := c.Bytes(0), c.Int(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if start < 0 || start > end || end > len(b) {
- return nil, fmt.Errorf("index out of range")
- }
- return b[start:end], nil
- }()
- }
- },
- }, {
- Name: "Runes",
- Params: []kind{stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return []rune(s)
- }()
- }
- },
- }, {
- Name: "MinRunes",
- Params: []kind{stringKind, intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s, min := c.String(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
-
- return len([]rune(s)) >= min
- }()
- }
- },
- }, {
- Name: "MaxRunes",
- Params: []kind{stringKind, intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s, max := c.String(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
-
- return len([]rune(s)) <= max
- }()
- }
- },
- }, {
- Name: "ToTitle",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
-
- prev := ' '
- return strings.Map(
- func(r rune) rune {
- if unicode.IsSpace(prev) {
- prev = r
- return unicode.ToTitle(r)
- }
- prev = r
- return r
- },
- s)
- }()
- }
- },
- }, {
- Name: "ToCamel",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
-
- prev := ' '
- return strings.Map(
- func(r rune) rune {
- if unicode.IsSpace(prev) {
- prev = r
- return unicode.ToLower(r)
- }
- prev = r
- return r
- },
- s)
- }()
- }
- },
- }, {
- Name: "SliceRunes",
- Params: []kind{stringKind, intKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, start, end := c.String(0), c.Int(1), c.Int(2)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- runes := []rune(s)
- if start < 0 || start > end || end > len(runes) {
- return "", fmt.Errorf("index out of range")
- }
- return string(runes[start:end]), nil
- }()
- }
- },
- }, {
- Name: "Compare",
- Params: []kind{stringKind, stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- a, b := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Compare(a, b)
- }()
- }
- },
- }, {
- Name: "Count",
- Params: []kind{stringKind, stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, substr := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Count(s, substr)
- }()
- }
- },
- }, {
- Name: "Contains",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s, substr := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Contains(s, substr)
- }()
- }
- },
- }, {
- Name: "ContainsAny",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s, chars := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.ContainsAny(s, chars)
- }()
- }
- },
- }, {
- Name: "LastIndex",
- Params: []kind{stringKind, stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, substr := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.LastIndex(s, substr)
- }()
- }
- },
- }, {
- Name: "IndexAny",
- Params: []kind{stringKind, stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, chars := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.IndexAny(s, chars)
- }()
- }
- },
- }, {
- Name: "LastIndexAny",
- Params: []kind{stringKind, stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, chars := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.LastIndexAny(s, chars)
- }()
- }
- },
- }, {
- Name: "SplitN",
- Params: []kind{stringKind, stringKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s, sep, n := c.String(0), c.String(1), c.Int(2)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.SplitN(s, sep, n)
- }()
- }
- },
- }, {
- Name: "SplitAfterN",
- Params: []kind{stringKind, stringKind, intKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s, sep, n := c.String(0), c.String(1), c.Int(2)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.SplitAfterN(s, sep, n)
- }()
- }
- },
- }, {
- Name: "Split",
- Params: []kind{stringKind, stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s, sep := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Split(s, sep)
- }()
- }
- },
- }, {
- Name: "SplitAfter",
- Params: []kind{stringKind, stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s, sep := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.SplitAfter(s, sep)
- }()
- }
- },
- }, {
- Name: "Fields",
- Params: []kind{stringKind},
- Result: listKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Fields(s)
- }()
- }
- },
- }, {
- Name: "Join",
- Params: []kind{listKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- elems, sep := c.StringList(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Join(elems, sep)
- }()
- }
- },
- }, {
- Name: "HasPrefix",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s, prefix := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.HasPrefix(s, prefix)
- }()
- }
- },
- }, {
- Name: "HasSuffix",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s, suffix := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.HasSuffix(s, suffix)
- }()
- }
- },
- }, {
- Name: "Repeat",
- Params: []kind{stringKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, count := c.String(0), c.Int(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Repeat(s, count)
- }()
- }
- },
- }, {
- Name: "ToUpper",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.ToUpper(s)
- }()
- }
- },
- }, {
- Name: "ToLower",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.ToLower(s)
- }()
- }
- },
- }, {
- Name: "Trim",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, cutset := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Trim(s, cutset)
- }()
- }
- },
- }, {
- Name: "TrimLeft",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, cutset := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.TrimLeft(s, cutset)
- }()
- }
- },
- }, {
- Name: "TrimRight",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, cutset := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.TrimRight(s, cutset)
- }()
- }
- },
- }, {
- Name: "TrimSpace",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.TrimSpace(s)
- }()
- }
- },
- }, {
- Name: "TrimPrefix",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, prefix := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.TrimPrefix(s, prefix)
- }()
- }
- },
- }, {
- Name: "TrimSuffix",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, suffix := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.TrimSuffix(s, suffix)
- }()
- }
- },
- }, {
- Name: "Replace",
- Params: []kind{stringKind, stringKind, stringKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s, old, new, n := c.String(0), c.String(1), c.String(2), c.Int(3)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Replace(s, old, new, n)
- }()
- }
- },
- }, {
- Name: "Index",
- Params: []kind{stringKind, stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s, substr := c.String(0), c.String(1)
- if c.Do() {
- c.Ret = func() interface{} {
- return strings.Index(s, substr)
- }()
- }
- },
- }},
- },
- "struct": {
- Native: []*Builtin{{
- Name: "MinFields",
- Params: []kind{structKind, intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- object, n := c.Struct(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- iter := object.Fields(Hidden(false), Optional(false))
- count := 0
- for iter.Next() {
- count++
- }
- return count >= n, nil
- }()
- }
- },
- }, {
- Name: "MaxFields",
- Params: []kind{structKind, intKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- object, n := c.Struct(0), c.Int(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- iter := object.Fields(Hidden(false), Optional(false))
- count := 0
- for iter.Next() {
- count++
- }
- return count <= n, nil
- }()
- }
- },
- }},
- },
- "text/tabwriter": {
- Native: []*Builtin{{
- Name: "Write",
- Params: []kind{topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- data := c.Value(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- buf := &bytes.Buffer{}
- tw := tabwriter.NewWriter(buf, 0, 4, 1, ' ', 0)
-
- write := func(v Value) error {
- b, err := v.Bytes()
- if err != nil {
- return err
- }
- _, err = tw.Write(b)
- if err != nil {
- return err
- }
- return nil
- }
-
- switch data.Kind() {
- case BytesKind, StringKind:
- if err := write(data); err != nil {
- return "", err
- }
- case ListKind:
- for i, _ := data.List(); i.Next(); {
- if err := write(i.Value()); err != nil {
- return "", err
- }
- _, _ = tw.Write([]byte{'\n'})
- }
- default:
- return "", fmt.Errorf("tabwriter.Write: unsupported type %v", data.Kind())
- }
-
- err := tw.Flush()
- return buf.String(), err
- }()
- }
- },
- }},
- },
- "text/template": {
- Native: []*Builtin{{
- Name: "Execute",
- Params: []kind{stringKind, topKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- templ, data := c.String(0), c.Value(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- t, err := template.New("").Parse(templ)
- if err != nil {
- return "", err
- }
- var x interface{}
- if err := data.Decode(&x); err != nil {
- return "", err
- }
- buf := &bytes.Buffer{}
- if err := t.Execute(buf, x); err != nil {
- return "", err
- }
- return buf.String(), nil
- }()
- }
- },
- }, {
- Name: "HTMLEscape",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return template.HTMLEscapeString(s)
- }()
- }
- },
- }, {
- Name: "JSEscape",
- Params: []kind{stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret = func() interface{} {
- return template.JSEscapeString(s)
- }()
- }
- },
- }},
- },
- "time": {
- Native: []*Builtin{{
- Name: "Nanosecond",
- Const: "1",
- }, {
- Name: "Microsecond",
- Const: "1000",
- }, {
- Name: "Millisecond",
- Const: "1000000",
- }, {
- Name: "Second",
- Const: "1000000000",
- }, {
- Name: "Minute",
- Const: "60000000000",
- }, {
- Name: "Hour",
- Const: "3600000000000",
- }, {
- Name: "Duration",
- Params: []kind{stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- if _, err := time.ParseDuration(s); err != nil {
- return false, err
- }
- return true, nil
- }()
- }
- },
- }, {
- Name: "ParseDuration",
- Params: []kind{stringKind},
- Result: intKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- d, err := time.ParseDuration(s)
- if err != nil {
- return 0, err
- }
- return int64(d), nil
- }()
- }
- },
- }, {
- Name: "ANSIC",
- Const: "\"Mon Jan _2 15:04:05 2006\"",
- }, {
- Name: "UnixDate",
- Const: "\"Mon Jan _2 15:04:05 MST 2006\"",
- }, {
- Name: "RubyDate",
- Const: "\"Mon Jan 02 15:04:05 -0700 2006\"",
- }, {
- Name: "RFC822",
- Const: "\"02 Jan 06 15:04 MST\"",
- }, {
- Name: "RFC822Z",
- Const: "\"02 Jan 06 15:04 -0700\"",
- }, {
- Name: "RFC850",
- Const: "\"Monday, 02-Jan-06 15:04:05 MST\"",
- }, {
- Name: "RFC1123",
- Const: "\"Mon, 02 Jan 2006 15:04:05 MST\"",
- }, {
- Name: "RFC1123Z",
- Const: "\"Mon, 02 Jan 2006 15:04:05 -0700\"",
- }, {
- Name: "RFC3339",
- Const: "\"2006-01-02T15:04:05Z07:00\"",
- }, {
- Name: "RFC3339Nano",
- Const: "\"2006-01-02T15:04:05.999999999Z07:00\"",
- }, {
- Name: "RFC3339Date",
- Const: "\"2006-01-02\"",
- }, {
- Name: "Kitchen",
- Const: "\"3:04PM\"",
- }, {
- Name: "Kitchen24",
- Const: "\"15:04\"",
- }, {
- Name: "January",
- Const: "1",
- }, {
- Name: "February",
- Const: "2",
- }, {
- Name: "March",
- Const: "3",
- }, {
- Name: "April",
- Const: "4",
- }, {
- Name: "May",
- Const: "5",
- }, {
- Name: "June",
- Const: "6",
- }, {
- Name: "July",
- Const: "7",
- }, {
- Name: "August",
- Const: "8",
- }, {
- Name: "September",
- Const: "9",
- }, {
- Name: "October",
- Const: "10",
- }, {
- Name: "November",
- Const: "11",
- }, {
- Name: "December",
- Const: "12",
- }, {
- Name: "Sunday",
- Const: "0",
- }, {
- Name: "Monday",
- Const: "1",
- }, {
- Name: "Tuesday",
- Const: "2",
- }, {
- Name: "Wednesday",
- Const: "3",
- }, {
- Name: "Thursday",
- Const: "4",
- }, {
- Name: "Friday",
- Const: "5",
- }, {
- Name: "Saturday",
- Const: "6",
- }, {
- Name: "Time",
- Params: []kind{stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- s := c.String(0)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return timeFormat(s, time.RFC3339Nano)
- }()
- }
- },
- }, {
- Name: "Format",
- Params: []kind{stringKind, stringKind},
- Result: boolKind,
- Func: func(c *CallCtxt) {
- value, layout := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- return timeFormat(value, layout)
- }()
- }
- },
- }, {
- Name: "Parse",
- Params: []kind{stringKind, stringKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- layout, value := c.String(0), c.String(1)
- if c.Do() {
- c.Ret, c.Err = func() (interface{}, error) {
- t, err := time.Parse(layout, value)
- if err != nil {
- return "", err
- }
- return t.UTC().Format(time.RFC3339Nano), nil
- }()
- }
- },
- }, {
- Name: "Unix",
- Params: []kind{intKind, intKind},
- Result: stringKind,
- Func: func(c *CallCtxt) {
- sec, nsec := c.Int64(0), c.Int64(1)
- if c.Do() {
- c.Ret = func() interface{} {
- t := time.Unix(sec, nsec)
- return t.UTC().Format(time.RFC3339Nano)
- }()
- }
- },
- }},
- },
- "tool": {
- Native: []*Builtin{},
- CUE: `{
- Command: {
- $usage?: string
- $short?: string
- $long?: string
- Tasks
- }
- Tasks: Task | {
- [name=string]: Tasks
- }
- Task: {
- $type: "tool.Task"
- $id: =~"\\."
- $after?: Task | [...Task]
- }
- Name: =~"^\\PL([-](\\PL|\\PN))*$"
-}`,
- },
- "tool/cli": {
- Native: []*Builtin{},
- CUE: `{
- Print: {
- $id: *"tool/cli.Print" | "print"
- text: string
- }
-}`,
- },
- "tool/exec": {
- Native: []*Builtin{},
- CUE: `{
- Run: {
- $id: *"tool/exec.Run" | "exec"
- cmd: string | [string, ...string]
- env: {
- [string]: string | [...=~"="]
- }
- stdout: *null | string | bytes
- stderr: *null | string | bytes
- stdin: *null | string | bytes
- success: bool
- }
-}`,
- },
- "tool/file": {
- Native: []*Builtin{},
- CUE: `{
- Read: {
- $id: "tool/file.Read"
- filename: !=""
- contents: *bytes | string
- }
- Create: {
- $id: "tool/file.Create"
- filename: !=""
- contents: bytes | string
- permissions: int | *420
- }
- Append: {
- $id: "tool/file.Append"
- filename: !=""
- contents: bytes | string
- permissions: int | *420
- }
- Glob: {
- $id: "tool/file.Glob"
- glob: !=""
- files: [...string]
- }
-}`,
- },
- "tool/http": {
- Native: []*Builtin{},
- CUE: `{
- Get: Do & {
- method: "GET"
- }
- Do: {
- $id: *"tool/http.Do" | "http"
- method: string
- response: {
- body: *bytes | string
- header: {
- [string]: string | [...string]
- }
- trailer: {
- [string]: string | [...string]
- }
- status: string
- statusCode: int
- }
- url: string
- request: {
- body: *bytes | string
- header: {
- [string]: string | [...string]
- }
- trailer: {
- [string]: string | [...string]
- }
- }
- }
- Post: Do & {
- method: "POST"
- }
- Put: Do & {
- method: "PUT"
- }
- Delete: Do & {
- method: "DELETE"
- }
-}`,
- },
- "tool/os": {
- Native: []*Builtin{},
- CUE: `{
- Name: !="" & !~"^[$]"
- Value: bool | number | *string | null
- Setenv: {
- $id: "tool/os.Setenv"
- {[Name]: Value}
- }
- Getenv: {
- $id: "tool/os.Getenv"
- {[Name]: Value}
- }
- Environ: {
- $id: "tool/os.Environ"
- {[Name]: Value}
- }
- Clearenv: {
- $id: "tool/os.Clearenv"
- }
-}`,
- },
-}
diff --git a/cue/load/loader.go b/cue/load/loader.go
index d62a2bb..c589296 100644
--- a/cue/load/loader.go
+++ b/cue/load/loader.go
@@ -25,12 +25,19 @@
"strings"
"unicode"
+ "golang.org/x/xerrors"
+
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/encoding"
"cuelang.org/go/internal/filetypes"
- "golang.org/x/xerrors"
+
+ // Trigger the unconditional loading of all core builtin packages if load
+ // is used. This was deemed the simplest way to avoid having to import
+ // this line explicitly, and thus breaking existing code, for the majority
+ // of cases, while not introducing an import cycle.
+ _ "cuelang.org/go/pkg"
)
// Instances returns the instances named by the command line arguments 'args'.
diff --git a/cue/types.go b/cue/types.go
index a8eea40..6b566cb 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -623,8 +623,9 @@
// MakeValue converts an adt.Value and given OpContext to a Value. The context
// must be directly or indirectly obtained from the NewRuntime defined in this
-// package and it will panic if this is not the case. This is for internal use
-// only.
+// package and it will panic if this is not the case.
+//
+// For internal use only.
func MakeValue(ctx *adt.OpContext, v adt.Value) Value {
runtime := ctx.Impl().(*runtime.Runtime)
index := runtime.Data.(*index)
@@ -957,18 +958,25 @@
}
}
- if d := internal.Imports(f); len(d) == 0 {
- if len(f.Decls) == 1 {
- if e, ok := f.Decls[0].(*ast.EmbedDecl); ok {
- return e.Expr
- }
- }
- return &ast.StructLit{
- Elts: f.Decls,
+outer:
+ for _, d := range f.Decls {
+ switch d.(type) {
+ case *ast.Package, *ast.ImportDecl:
+ return f
+ case *ast.CommentGroup, *ast.Attribute:
+ default:
+ break outer
}
}
- return f
+ if len(f.Decls) == 1 {
+ if e, ok := f.Decls[0].(*ast.EmbedDecl); ok {
+ return e.Expr
+ }
+ }
+ return &ast.StructLit{
+ Elts: f.Decls,
+ }
}
// Decode initializes x with Value v. If x is a struct, it will validate the
@@ -1700,7 +1708,7 @@
case state.Flag('#'):
_, _ = io.WriteString(state, ctx.str(v.v))
case state.Flag('+'):
- _, _ = io.WriteString(state, debugStr(ctx, v.v))
+ _, _ = io.WriteString(state, ctx.opCtx.Str(v.v))
default:
n, _ := export.Raw.Expr(v.idx.Runtime, v.v)
b, _ := format.Node(n)
diff --git a/encoding/gocode/generator_test.go b/encoding/gocode/generator_test.go
index 85780cc..d1c2740 100644
--- a/encoding/gocode/generator_test.go
+++ b/encoding/gocode/generator_test.go
@@ -28,6 +28,7 @@
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/load"
+ _ "cuelang.org/go/pkg"
)
var update = flag.Bool("update", false, "update test files")
diff --git a/encoding/gocode/templates.go b/encoding/gocode/templates.go
index 22fb940..34dd25c 100644
--- a/encoding/gocode/templates.go
+++ b/encoding/gocode/templates.go
@@ -28,6 +28,7 @@
"cuelang.org/go/cue"
"cuelang.org/go/encoding/gocode/gocodec"
+ _ "cuelang.org/go/pkg"
)
`))
diff --git a/encoding/gocode/testdata/pkg1/cue_gen.go b/encoding/gocode/testdata/pkg1/cue_gen.go
index c16cab9..05e313c 100644
--- a/encoding/gocode/testdata/pkg1/cue_gen.go
+++ b/encoding/gocode/testdata/pkg1/cue_gen.go
@@ -7,6 +7,7 @@
"cuelang.org/go/cue"
"cuelang.org/go/encoding/gocode/gocodec"
+ _ "cuelang.org/go/pkg"
)
var cuegenvalMyStruct = cuegenMake("MyStruct", &MyStruct{})
diff --git a/encoding/gocode/testdata/pkg2/cue_gen.go b/encoding/gocode/testdata/pkg2/cue_gen.go
index 927bf6e..5fe1aa8 100644
--- a/encoding/gocode/testdata/pkg2/cue_gen.go
+++ b/encoding/gocode/testdata/pkg2/cue_gen.go
@@ -7,6 +7,7 @@
"cuelang.org/go/cue"
"cuelang.org/go/encoding/gocode/gocodec"
+ _ "cuelang.org/go/pkg"
)
var cuegenvalImportMe = cuegenMake("ImportMe", &ImportMe{})
diff --git a/encoding/jsonschema/decode_test.go b/encoding/jsonschema/decode_test.go
index adc5bd6..8ee40bc 100644
--- a/encoding/jsonschema/decode_test.go
+++ b/encoding/jsonschema/decode_test.go
@@ -24,6 +24,10 @@
"strings"
"testing"
+ "github.com/google/go-cmp/cmp"
+ "github.com/rogpeppe/go-internal/txtar"
+ "github.com/stretchr/testify/assert"
+
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
@@ -31,9 +35,7 @@
"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/yaml"
- "github.com/google/go-cmp/cmp"
- "github.com/stretchr/testify/assert"
- "golang.org/x/tools/txtar"
+ _ "cuelang.org/go/pkg"
)
var update = flag.Bool("update", false, "update the test files")
diff --git a/encoding/openapi/openapi_test.go b/encoding/openapi/openapi_test.go
index e8c3705..0c04bfa 100644
--- a/encoding/openapi/openapi_test.go
+++ b/encoding/openapi/openapi_test.go
@@ -160,7 +160,7 @@
t.Fatal(err)
}
- if d := diff.Diff(out.String(), string(b)); d != "" {
+ if d := diff.Diff(string(b), out.String()); d != "" {
t.Errorf("files differ:\n%v", d)
}
})
diff --git a/internal/builtin/registry.go b/internal/builtin/registry.go
new file mode 100644
index 0000000..a90155e
--- /dev/null
+++ b/internal/builtin/registry.go
@@ -0,0 +1,49 @@
+// Copyright 2020 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package builtin
+
+import (
+ "sort"
+
+ "cuelang.org/go/cue"
+ "cuelang.org/go/internal/core/adt"
+)
+
+type PackageFunc func(ctx *adt.OpContext) (*adt.Vertex, error)
+
+// Register registers a builtin, the value of which will be built
+// on first use. All builtins must be registered before first use of a runtime.
+// This restriction may be eliminated in the future.
+func Register(importPath string, f PackageFunc) {
+ builtins[importPath] = f
+ // TODO: remove at some point.
+ cue.AddBuiltinPackage(importPath, f)
+}
+
+var builtins = map[string]PackageFunc{}
+
+func ImportPaths() (a []string) {
+ for s := range builtins {
+ a = append(a, s)
+ }
+ sort.Strings(a)
+ return a
+}
+
+// Get return the builder for the package with the given path.
+// It will panic if the path does not exist.
+func Get(path string) PackageFunc {
+ return builtins[path]
+}
diff --git a/internal/core/adt/expr.go b/internal/core/adt/expr.go
index d4ac293..ece0f98 100644
--- a/internal/core/adt/expr.go
+++ b/internal/core/adt/expr.go
@@ -894,6 +894,8 @@
// Remove the path of the origin for arguments. This results in
// more sensible error messages: an error should refer to the call
// site, not the original location of the argument.
+ // TODO: alternative, explicitly mark the argument number and use
+ // that in error messages.
w := *v
w.Parent = nil
args = append(args, &w)
diff --git a/internal/core/compile/compile_test.go b/internal/core/compile/compile_test.go
index 4f78d33..f5a73ac 100644
--- a/internal/core/compile/compile_test.go
+++ b/internal/core/compile/compile_test.go
@@ -17,6 +17,7 @@
import (
"flag"
"fmt"
+ "strings"
"testing"
"cuelang.org/go/cue/errors"
@@ -25,7 +26,6 @@
"cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cuetxtar"
- "cuelang.org/go/pkg/strings"
)
var (
diff --git a/internal/core/eval/eval_test.go b/internal/core/eval/eval_test.go
index af4e8c9..1344a76 100644
--- a/internal/core/eval/eval_test.go
+++ b/internal/core/eval/eval_test.go
@@ -20,12 +20,14 @@
"strings"
"testing"
+ "github.com/rogpeppe/go-internal/txtar"
+
"cuelang.org/go/cue"
"cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/validate"
"cuelang.org/go/internal/cuetxtar"
- "github.com/rogpeppe/go-internal/txtar"
+ _ "cuelang.org/go/pkg"
)
var (
diff --git a/internal/filetypes/types.go b/internal/filetypes/types.go
index 4c47c9b..33efc31 100644
--- a/internal/filetypes/types.go
+++ b/internal/filetypes/types.go
@@ -7,6 +7,7 @@
"cuelang.org/go/cue"
"cuelang.org/go/encoding/gocode/gocodec"
+ _ "cuelang.org/go/pkg"
)
var cuegenCodec, cuegenInstance = func() (*gocodec.Codec, *cue.Instance) {
diff --git a/pkg/crypto/md5/pkg.go b/pkg/crypto/md5/pkg.go
new file mode 100644
index 0000000..29ee896
--- /dev/null
+++ b/pkg/crypto/md5/pkg.go
@@ -0,0 +1,37 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package md5
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("crypto/md5", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Size",
+ Const: "16",
+ }, {
+ Name: "BlockSize",
+ Const: "64",
+ }, {
+ Name: "Sum",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum(data)
+ }
+ },
+ }},
+}
diff --git a/pkg/crypto/sha1/pkg.go b/pkg/crypto/sha1/pkg.go
new file mode 100644
index 0000000..7d5076a
--- /dev/null
+++ b/pkg/crypto/sha1/pkg.go
@@ -0,0 +1,37 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package sha1
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("crypto/sha1", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Size",
+ Const: "20",
+ }, {
+ Name: "BlockSize",
+ Const: "64",
+ }, {
+ Name: "Sum",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum(data)
+ }
+ },
+ }},
+}
diff --git a/pkg/crypto/sha256/pkg.go b/pkg/crypto/sha256/pkg.go
new file mode 100644
index 0000000..37c291a
--- /dev/null
+++ b/pkg/crypto/sha256/pkg.go
@@ -0,0 +1,50 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package sha256
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("crypto/sha256", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Size",
+ Const: "32",
+ }, {
+ Name: "Size224",
+ Const: "28",
+ }, {
+ Name: "BlockSize",
+ Const: "64",
+ }, {
+ Name: "Sum256",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum256(data)
+ }
+ },
+ }, {
+ Name: "Sum224",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum224(data)
+ }
+ },
+ }},
+}
diff --git a/pkg/crypto/sha512/pkg.go b/pkg/crypto/sha512/pkg.go
new file mode 100644
index 0000000..50c2a80
--- /dev/null
+++ b/pkg/crypto/sha512/pkg.go
@@ -0,0 +1,76 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package sha512
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("crypto/sha512", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Size",
+ Const: "64",
+ }, {
+ Name: "Size224",
+ Const: "28",
+ }, {
+ Name: "Size256",
+ Const: "32",
+ }, {
+ Name: "Size384",
+ Const: "48",
+ }, {
+ Name: "BlockSize",
+ Const: "128",
+ }, {
+ Name: "Sum512",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum512(data)
+ }
+ },
+ }, {
+ Name: "Sum384",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum384(data)
+ }
+ },
+ }, {
+ Name: "Sum512_224",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum512_224(data)
+ }
+ },
+ }, {
+ Name: "Sum512_256",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Sum512_256(data)
+ }
+ },
+ }},
+}
diff --git a/pkg/encoding/base64/pkg.go b/pkg/encoding/base64/pkg.go
new file mode 100644
index 0000000..4f881b6
--- /dev/null
+++ b/pkg/encoding/base64/pkg.go
@@ -0,0 +1,61 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package base64
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("encoding/base64", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "EncodedLen",
+ Params: []adt.Kind{adt.TopKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ encoding, n := c.Value(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = EncodedLen(encoding, n)
+ }
+ },
+ }, {
+ Name: "DecodedLen",
+ Params: []adt.Kind{adt.TopKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ encoding, x := c.Value(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = DecodedLen(encoding, x)
+ }
+ },
+ }, {
+ Name: "Encode",
+ Params: []adt.Kind{adt.TopKind, adt.BytesKind | adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ encoding, src := c.Value(0), c.Bytes(1)
+ if c.Do() {
+ c.Ret, c.Err = Encode(encoding, src)
+ }
+ },
+ }, {
+ Name: "Decode",
+ Params: []adt.Kind{adt.TopKind, adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ encoding, s := c.Value(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = Decode(encoding, s)
+ }
+ },
+ }},
+}
diff --git a/pkg/encoding/csv/pkg.go b/pkg/encoding/csv/pkg.go
new file mode 100644
index 0000000..7bf550c
--- /dev/null
+++ b/pkg/encoding/csv/pkg.go
@@ -0,0 +1,41 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package csv
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("encoding/csv", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Encode",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = Encode(x)
+ }
+ },
+ }, {
+ Name: "Decode",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ r := c.Reader(0)
+ if c.Do() {
+ c.Ret, c.Err = Decode(r)
+ }
+ },
+ }},
+}
diff --git a/pkg/encoding/hex/pkg.go b/pkg/encoding/hex/pkg.go
new file mode 100644
index 0000000..3e00667
--- /dev/null
+++ b/pkg/encoding/hex/pkg.go
@@ -0,0 +1,71 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package hex
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("encoding/hex", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "EncodedLen",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ n := c.Int(0)
+ if c.Do() {
+ c.Ret = EncodedLen(n)
+ }
+ },
+ }, {
+ Name: "DecodedLen",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Int(0)
+ if c.Do() {
+ c.Ret = DecodedLen(x)
+ }
+ },
+ }, {
+ Name: "Decode",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = Decode(s)
+ }
+ },
+ }, {
+ Name: "Dump",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Dump(data)
+ }
+ },
+ }, {
+ Name: "Encode",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ src := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Encode(src)
+ }
+ },
+ }},
+}
diff --git a/pkg/encoding/json/pkg.go b/pkg/encoding/json/pkg.go
new file mode 100644
index 0000000..a44d0bf
--- /dev/null
+++ b/pkg/encoding/json/pkg.go
@@ -0,0 +1,101 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package json
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("encoding/json", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Valid",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret = Valid(data)
+ }
+ },
+ }, {
+ Name: "Compact",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ src := c.Bytes(0)
+ if c.Do() {
+ c.Ret, c.Err = Compact(src)
+ }
+ },
+ }, {
+ Name: "Indent",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind, adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ src, prefix, indent := c.Bytes(0), c.String(1), c.String(2)
+ if c.Do() {
+ c.Ret, c.Err = Indent(src, prefix, indent)
+ }
+ },
+ }, {
+ Name: "HTMLEscape",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ src := c.Bytes(0)
+ if c.Do() {
+ c.Ret = HTMLEscape(src)
+ }
+ },
+ }, {
+ Name: "Marshal",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ v := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = Marshal(v)
+ }
+ },
+ }, {
+ Name: "MarshalStream",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ v := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = MarshalStream(v)
+ }
+ },
+ }, {
+ Name: "Unmarshal",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.TopKind,
+ Func: func(c *internal.CallCtxt) {
+ b := c.Bytes(0)
+ if c.Do() {
+ c.Ret, c.Err = Unmarshal(b)
+ }
+ },
+ }, {
+ Name: "Validate",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind, adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ b, v := c.Bytes(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = Validate(b, v)
+ }
+ },
+ }},
+}
diff --git a/pkg/encoding/yaml/pkg.go b/pkg/encoding/yaml/pkg.go
new file mode 100644
index 0000000..03f73e6
--- /dev/null
+++ b/pkg/encoding/yaml/pkg.go
@@ -0,0 +1,71 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package yaml
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("encoding/yaml", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Marshal",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ v := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = Marshal(v)
+ }
+ },
+ }, {
+ Name: "MarshalStream",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ v := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = MarshalStream(v)
+ }
+ },
+ }, {
+ Name: "Unmarshal",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind},
+ Result: adt.TopKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Bytes(0)
+ if c.Do() {
+ c.Ret, c.Err = Unmarshal(data)
+ }
+ },
+ }, {
+ Name: "Validate",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind, adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ b, v := c.Bytes(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = Validate(b, v)
+ }
+ },
+ }, {
+ Name: "ValidatePartial",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind, adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ b, v := c.Bytes(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = ValidatePartial(b, v)
+ }
+ },
+ }},
+}
diff --git a/pkg/gen/gen.go b/pkg/gen/gen.go
new file mode 100644
index 0000000..7ceb8cb
--- /dev/null
+++ b/pkg/gen/gen.go
@@ -0,0 +1,412 @@
+// Copyright 2018 The CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package gen is a command that can be used to bootstrap a new builtin package
+// directory. The directory has to reside in cuelang.org/go/pkg.
+//
+// To bootstrap a directory, run this command from within that direcory.
+// After that directory's files can be regenerated with go generate.
+//
+// Be sure to also update an entry in pkg/pkg.go, if so desired.
+package main
+
+import (
+ "bytes"
+ "flag"
+ "fmt"
+ "go/ast"
+ "go/constant"
+ "go/format"
+ "go/parser"
+ "go/printer"
+ "go/token"
+ "io"
+ "io/ioutil"
+ "log"
+ "math/big"
+ "os"
+ "path"
+ "path/filepath"
+ "strings"
+
+ "cuelang.org/go/cue"
+ "cuelang.org/go/cue/errors"
+ cueformat "cuelang.org/go/cue/format"
+ "cuelang.org/go/cue/load"
+ "cuelang.org/go/internal"
+)
+
+const genFile = "pkg.go"
+
+const prefix = "../pkg/"
+
+const header = `// Code generated by go generate. DO NOT EDIT.
+
+ //go:generate rm %s
+ //go:generate go run %sgen/gen.go
+
+package %s
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register(%q, pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+`
+
+func main() {
+ flag.Parse()
+ log.SetFlags(log.Lshortfile)
+ log.SetOutput(os.Stdout)
+
+ g := generator{
+ w: &bytes.Buffer{},
+ decls: &bytes.Buffer{},
+ fset: token.NewFileSet(),
+ }
+
+ cwd, _ := os.Getwd()
+ pkg := strings.Split(filepath.ToSlash(cwd), "/pkg/")[1]
+ gopkg := path.Base(pkg)
+ // TODO: rename list to lists and struct to structs.
+ if gopkg == "struct" {
+ gopkg = "structs"
+ }
+ dots := strings.Repeat("../", strings.Count(pkg, "/")+1)
+
+ w := &bytes.Buffer{}
+ fmt.Fprintf(w, header, genFile, dots, gopkg, pkg)
+ g.processDir(pkg)
+
+ io.Copy(w, g.decls)
+ io.Copy(w, g.w)
+
+ b, err := format.Source(w.Bytes())
+ if err != nil {
+ b = w.Bytes() // write the unformatted source
+ }
+
+ b = bytes.Replace(b, []byte(".Builtin{{}}"), []byte(".Builtin{}"), -1)
+
+ filename := filepath.Join(genFile)
+
+ if err := ioutil.WriteFile(filename, b, 0644); err != nil {
+ log.Fatal(err)
+ }
+}
+
+type generator struct {
+ w *bytes.Buffer
+ decls *bytes.Buffer
+ name string
+ fset *token.FileSet
+ defaultPkg string
+ first bool
+ iota int
+
+ imports []*ast.ImportSpec
+}
+
+func (g *generator) processDir(pkg string) {
+ goFiles, err := filepath.Glob("*.go")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ cueFiles, err := filepath.Glob("*.cue")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ if len(goFiles)+len(cueFiles) == 0 {
+ return
+ }
+
+ fmt.Fprintf(g.w, "var pkg = &internal.Package{\nNative: []*internal.Builtin{{\n")
+ g.first = true
+ for _, filename := range goFiles {
+ if filename == genFile {
+ continue
+ }
+ g.processGo(filename)
+ }
+ fmt.Fprintf(g.w, "}},\n")
+ g.processCUE(pkg)
+ fmt.Fprintf(g.w, "}\n")
+}
+
+func (g *generator) sep() {
+ if g.first {
+ g.first = false
+ return
+ }
+ fmt.Fprintln(g.w, "}, {")
+}
+
+// processCUE mixes in CUE definitions defined in the package directory.
+func (g *generator) processCUE(pkg string) {
+ instances := cue.Build(load.Instances([]string{"."}, &load.Config{
+ StdRoot: ".",
+ }))
+
+ if err := instances[0].Err; err != nil {
+ if !strings.Contains(err.Error(), "no CUE files") {
+ errors.Print(os.Stderr, err, nil)
+ log.Fatalf("error processing %s: %v", pkg, err)
+ }
+ return
+ }
+
+ v := instances[0].Value().Syntax(cue.Raw())
+ // fmt.Printf("%T\n", v)
+ // fmt.Println(internal.DebugStr(v))
+ n := internal.ToExpr(v)
+ b, err := cueformat.Node(n)
+ if err != nil {
+ log.Fatal(err)
+ }
+ b = bytes.ReplaceAll(b, []byte("\n\n"), []byte("\n"))
+ // body = strings.ReplaceAll(body, "\t", "")
+ // TODO: escape backtick
+ fmt.Fprintf(g.w, "CUE: `%s`,\n", string(b))
+}
+
+func (g *generator) processGo(filename string) {
+ if strings.HasSuffix(filename, "_test.go") {
+ return
+ }
+ f, err := parser.ParseFile(g.fset, filename, nil, parser.ParseComments)
+ if err != nil {
+ log.Fatal(err)
+ }
+ g.defaultPkg = ""
+ g.name = f.Name.Name
+ if g.name == "structs" {
+ g.name = "struct"
+ }
+
+ for _, d := range f.Decls {
+ switch x := d.(type) {
+ case *ast.GenDecl:
+ switch x.Tok {
+ case token.CONST:
+ for _, spec := range x.Specs {
+ if !ast.IsExported(spec.(*ast.ValueSpec).Names[0].Name) {
+ continue
+ }
+ g.genConst(spec.(*ast.ValueSpec))
+ }
+ case token.VAR:
+ continue
+ case token.TYPE:
+ // TODO: support type declarations.
+ continue
+ case token.IMPORT:
+ continue
+ default:
+ log.Fatalf("gen %s: unexpected spec of type %s", filename, x.Tok)
+ }
+ case *ast.FuncDecl:
+ g.genFun(x)
+ }
+ }
+}
+
+func (g *generator) genConst(spec *ast.ValueSpec) {
+ name := spec.Names[0].Name
+ value := ""
+ switch v := g.toValue(spec.Values[0]); v.Kind() {
+ case constant.Bool, constant.Int, constant.String:
+ // TODO: convert octal numbers
+ value = v.ExactString()
+ case constant.Float:
+ var rat big.Rat
+ rat.SetString(v.ExactString())
+ var float big.Float
+ float.SetRat(&rat)
+ value = float.Text('g', -1)
+ default:
+ fmt.Printf("Dropped entry %s.%s (%T: %v)\n", g.defaultPkg, name, v.Kind(), v.ExactString())
+ return
+ }
+ g.sep()
+ fmt.Fprintf(g.w, "Name: %q,\n Const: %q,\n", name, value)
+}
+
+func (g *generator) toValue(x ast.Expr) constant.Value {
+ switch x := x.(type) {
+ case *ast.BasicLit:
+ return constant.MakeFromLiteral(x.Value, x.Kind, 0)
+ case *ast.BinaryExpr:
+ return constant.BinaryOp(g.toValue(x.X), x.Op, g.toValue(x.Y))
+ case *ast.UnaryExpr:
+ return constant.UnaryOp(x.Op, g.toValue(x.X), 0)
+ default:
+ log.Fatalf("%s: unsupported expression type %T: %#v", g.defaultPkg, x, x)
+ }
+ return constant.MakeUnknown()
+}
+
+func (g *generator) genFun(x *ast.FuncDecl) {
+ if x.Body == nil || !ast.IsExported(x.Name.Name) {
+ return
+ }
+ types := []string{}
+ if x.Type.Results != nil {
+ for _, f := range x.Type.Results.List {
+ if len(f.Names) > 0 {
+ for range f.Names {
+ types = append(types, g.goKind(f.Type))
+ }
+ } else {
+ types = append(types, g.goKind(f.Type))
+ }
+ }
+ }
+ if n := len(types); n != 1 && (n != 2 || types[1] != "error") {
+ fmt.Printf("Dropped func %s.%s: must have one return value or a value and an error %v\n", g.defaultPkg, x.Name.Name, types)
+ return
+ }
+
+ if x.Recv != nil {
+ // if strings.HasPrefix(x.Name.Name, g.name) {
+ // printer.Fprint(g.decls, g.fset, x)
+ // fmt.Fprint(g.decls, "\n\n")
+ // }
+ return
+ }
+
+ g.sep()
+ fmt.Fprintf(g.w, "Name: %q,\n", x.Name.Name)
+
+ args := []string{}
+ vals := []string{}
+ kind := []string{}
+ for _, f := range x.Type.Params.List {
+ for _, name := range f.Names {
+ typ := strings.Title(g.goKind(f.Type))
+ argKind := g.goToCUE(f.Type)
+ vals = append(vals, fmt.Sprintf("c.%s(%d)", typ, len(args)))
+ args = append(args, name.Name)
+ kind = append(kind, argKind)
+ }
+ }
+
+ fmt.Fprintf(g.w, "Params: []adt.Kind{%s},\n", strings.Join(kind, ", "))
+ result := g.goToCUE(x.Type.Results.List[0].Type)
+ fmt.Fprintf(g.w, "Result: %s,\n", result)
+ argList := strings.Join(args, ", ")
+ valList := strings.Join(vals, ", ")
+ init := ""
+ if len(args) > 0 {
+ init = fmt.Sprintf("%s := %s", argList, valList)
+ }
+
+ fmt.Fprintf(g.w, "Func: func(c *internal.CallCtxt) {")
+ defer fmt.Fprintln(g.w, "},")
+ fmt.Fprintln(g.w)
+ if init != "" {
+ fmt.Fprintln(g.w, init)
+ }
+ fmt.Fprintln(g.w, "if c.Do() {")
+ defer fmt.Fprintln(g.w, "}")
+ if len(types) == 1 {
+ fmt.Fprintf(g.w, "c.Ret = %s(%s)", x.Name.Name, argList)
+ } else {
+ fmt.Fprintf(g.w, "c.Ret, c.Err = %s(%s)", x.Name.Name, argList)
+ }
+}
+
+func (g *generator) goKind(expr ast.Expr) string {
+ if star, isStar := expr.(*ast.StarExpr); isStar {
+ expr = star.X
+ }
+ w := &bytes.Buffer{}
+ printer.Fprint(w, g.fset, expr)
+ switch str := w.String(); str {
+ case "big.Int":
+ return "bigInt"
+ case "big.Float":
+ return "bigFloat"
+ case "big.Rat":
+ return "bigRat"
+ case "internal.Decimal":
+ return "decimal"
+ case "[]*internal.Decimal":
+ return "decimalList"
+ case "cue.Struct":
+ return "struct"
+ case "cue.Value":
+ return "value"
+ case "cue.List":
+ return "list"
+ case "[]string":
+ return "stringList"
+ case "[]byte":
+ return "bytes"
+ case "[]cue.Value":
+ return "list"
+ case "io.Reader":
+ return "reader"
+ case "time.Time":
+ return "string"
+ default:
+ return str
+ }
+}
+
+func (g *generator) goToCUE(expr ast.Expr) (cueKind string) {
+ // TODO: detect list and structs types for return values.
+ switch k := g.goKind(expr); k {
+ case "error":
+ cueKind += "adt.BottomKind"
+ case "bool":
+ cueKind += "adt.BoolKind"
+ case "bytes", "reader":
+ cueKind += "adt.BytesKind|adt.StringKind"
+ case "string":
+ cueKind += "adt.StringKind"
+ case "int", "int8", "int16", "int32", "rune", "int64",
+ "uint", "byte", "uint8", "uint16", "uint32", "uint64",
+ "bigInt":
+ cueKind += "adt.IntKind"
+ case "float64", "bigRat", "bigFloat", "decimal":
+ cueKind += "adt.NumKind"
+ case "list", "decimalList", "stringList":
+ cueKind += "adt.ListKind"
+ case "struct":
+ cueKind += "adt.StructKind"
+ case "value":
+ // Must use callCtxt.value method for these types and resolve manually.
+ cueKind += "adt.TopKind" // TODO: can be more precise
+ default:
+ switch {
+ case strings.HasPrefix(k, "[]"):
+ cueKind += "adt.ListKind"
+ case strings.HasPrefix(k, "map["):
+ cueKind += "adt.StructKind"
+ default:
+ // log.Println("Unknown type:", k)
+ // Must use callCtxt.value method for these types and resolve manually.
+ cueKind += "adt.TopKind" // TODO: can be more precise
+ }
+ }
+ return cueKind
+}
diff --git a/pkg/html/pkg.go b/pkg/html/pkg.go
new file mode 100644
index 0000000..b611b65
--- /dev/null
+++ b/pkg/html/pkg.go
@@ -0,0 +1,41 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package html
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("html", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Escape",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = Escape(s)
+ }
+ },
+ }, {
+ Name: "Unescape",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = Unescape(s)
+ }
+ },
+ }},
+}
diff --git a/pkg/internal/register.go b/pkg/internal/register.go
new file mode 100644
index 0000000..e25567e
--- /dev/null
+++ b/pkg/internal/register.go
@@ -0,0 +1,27 @@
+// Copyright 2020 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package internal
+
+import (
+ "cuelang.org/go/internal/builtin"
+ "cuelang.org/go/internal/core/adt"
+)
+
+func Register(importPath string, p *Package) {
+ f := func(ctx *adt.OpContext) (*adt.Vertex, error) {
+ return p.MustCompile(ctx, importPath), nil
+ }
+ builtin.Register(importPath, f)
+}
diff --git a/pkg/list/pkg.go b/pkg/list/pkg.go
new file mode 100644
index 0000000..db30ee4
--- /dev/null
+++ b/pkg/list/pkg.go
@@ -0,0 +1,233 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package list
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("list", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Drop",
+ Params: []adt.Kind{adt.ListKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ x, n := c.List(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = Drop(x, n)
+ }
+ },
+ }, {
+ Name: "FlattenN",
+ Params: []adt.Kind{adt.TopKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ xs, depth := c.Value(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = FlattenN(xs, depth)
+ }
+ },
+ }, {
+ Name: "Take",
+ Params: []adt.Kind{adt.ListKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ x, n := c.List(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = Take(x, n)
+ }
+ },
+ }, {
+ Name: "Slice",
+ Params: []adt.Kind{adt.ListKind, adt.IntKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ x, i, j := c.List(0), c.Int(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = Slice(x, i, j)
+ }
+ },
+ }, {
+ Name: "MinItems",
+ Params: []adt.Kind{adt.ListKind, adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ a, n := c.List(0), c.Int(1)
+ if c.Do() {
+ c.Ret = MinItems(a, n)
+ }
+ },
+ }, {
+ Name: "MaxItems",
+ Params: []adt.Kind{adt.ListKind, adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ a, n := c.List(0), c.Int(1)
+ if c.Do() {
+ c.Ret = MaxItems(a, n)
+ }
+ },
+ }, {
+ Name: "UniqueItems",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ a := c.List(0)
+ if c.Do() {
+ c.Ret = UniqueItems(a)
+ }
+ },
+ }, {
+ Name: "Contains",
+ Params: []adt.Kind{adt.ListKind, adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ a, v := c.List(0), c.Value(1)
+ if c.Do() {
+ c.Ret = Contains(a, v)
+ }
+ },
+ }, {
+ Name: "Avg",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ xs := c.DecimalList(0)
+ if c.Do() {
+ c.Ret, c.Err = Avg(xs)
+ }
+ },
+ }, {
+ Name: "Max",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ xs := c.DecimalList(0)
+ if c.Do() {
+ c.Ret, c.Err = Max(xs)
+ }
+ },
+ }, {
+ Name: "Min",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ xs := c.DecimalList(0)
+ if c.Do() {
+ c.Ret, c.Err = Min(xs)
+ }
+ },
+ }, {
+ Name: "Product",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ xs := c.DecimalList(0)
+ if c.Do() {
+ c.Ret, c.Err = Product(xs)
+ }
+ },
+ }, {
+ Name: "Range",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind, adt.NumKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ start, limit, step := c.Decimal(0), c.Decimal(1), c.Decimal(2)
+ if c.Do() {
+ c.Ret, c.Err = Range(start, limit, step)
+ }
+ },
+ }, {
+ Name: "Sum",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ xs := c.DecimalList(0)
+ if c.Do() {
+ c.Ret, c.Err = Sum(xs)
+ }
+ },
+ }, {
+ Name: "Sort",
+ Params: []adt.Kind{adt.ListKind, adt.TopKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ list, cmp := c.List(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = Sort(list, cmp)
+ }
+ },
+ }, {
+ Name: "SortStable",
+ Params: []adt.Kind{adt.ListKind, adt.TopKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ list, cmp := c.List(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = SortStable(list, cmp)
+ }
+ },
+ }, {
+ Name: "SortStrings",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ a := c.StringList(0)
+ if c.Do() {
+ c.Ret = SortStrings(a)
+ }
+ },
+ }, {
+ Name: "IsSorted",
+ Params: []adt.Kind{adt.ListKind, adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ list, cmp := c.List(0), c.Value(1)
+ if c.Do() {
+ c.Ret = IsSorted(list, cmp)
+ }
+ },
+ }, {
+ Name: "IsSortedStrings",
+ Params: []adt.Kind{adt.ListKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ a := c.StringList(0)
+ if c.Do() {
+ c.Ret = IsSortedStrings(a)
+ }
+ },
+ }},
+ CUE: `{
+ Comparer: {
+ T: _
+ x: T
+ y: T
+ less: bool
+ }
+ Ascending: {
+ Comparer
+ T: number | string
+ x: T
+ y: T
+ less: true && x < y
+ }
+ Descending: {
+ Comparer
+ T: number | string
+ x: T
+ y: T
+ less: x > y
+ }
+}`,
+}
diff --git a/pkg/list/sort.go b/pkg/list/sort.go
index ac33f4f..d192761 100644
--- a/pkg/list/sort.go
+++ b/pkg/list/sort.go
@@ -32,12 +32,25 @@
}
func (s *valueSorter) ret() ([]cue.Value, error) {
- panic("implemented in cue/builtinutil.go")
+ if s.err != nil {
+ return nil, s.err
+ }
+ // The input slice is already a copy and that we can modify it safely.
+ return s.a, nil
}
-func (s *valueSorter) Len() int { panic("implemented in cue/builtinutil.go") }
-func (s *valueSorter) Swap(i, j int) { panic("implemented in cue/builtinutil.go") }
-func (s *valueSorter) Less(i, j int) bool { panic("implemented in cue/builtinutil.go") }
+func (s *valueSorter) Len() int { return len(s.a) }
+func (s *valueSorter) Swap(i, j int) { s.a[i], s.a[j] = s.a[j], s.a[i] }
+func (s *valueSorter) Less(i, j int) bool {
+ v := s.cmp.Fill(s.a[i], "x")
+ v = v.Fill(s.a[j], "y")
+ isLess, err := v.Lookup("less").Bool()
+ if err != nil && s.err == nil {
+ s.err = err
+ return true
+ }
+ return isLess
+}
// Sort sorts data. It does O(n*log(n)) comparisons.
// The sort is not guaranteed to be stable.
diff --git a/pkg/math/bits/pkg.go b/pkg/math/bits/pkg.go
new file mode 100644
index 0000000..1b69513
--- /dev/null
+++ b/pkg/math/bits/pkg.go
@@ -0,0 +1,121 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package bits
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("math/bits", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Lsh",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x, n := c.BigInt(0), c.Uint(1)
+ if c.Do() {
+ c.Ret = Lsh(x, n)
+ }
+ },
+ }, {
+ Name: "Rsh",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x, n := c.BigInt(0), c.Uint(1)
+ if c.Do() {
+ c.Ret = Rsh(x, n)
+ }
+ },
+ }, {
+ Name: "At",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x, i := c.BigInt(0), c.Uint(1)
+ if c.Do() {
+ c.Ret, c.Err = At(x, i)
+ }
+ },
+ }, {
+ Name: "Set",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x, i, bit := c.BigInt(0), c.Int(1), c.Uint(2)
+ if c.Do() {
+ c.Ret = Set(x, i, bit)
+ }
+ },
+ }, {
+ Name: "And",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ a, b := c.BigInt(0), c.BigInt(1)
+ if c.Do() {
+ c.Ret = And(a, b)
+ }
+ },
+ }, {
+ Name: "Or",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ a, b := c.BigInt(0), c.BigInt(1)
+ if c.Do() {
+ c.Ret = Or(a, b)
+ }
+ },
+ }, {
+ Name: "Xor",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ a, b := c.BigInt(0), c.BigInt(1)
+ if c.Do() {
+ c.Ret = Xor(a, b)
+ }
+ },
+ }, {
+ Name: "Clear",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ a, b := c.BigInt(0), c.BigInt(1)
+ if c.Do() {
+ c.Ret = Clear(a, b)
+ }
+ },
+ }, {
+ Name: "OnesCount",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.BigInt(0)
+ if c.Do() {
+ c.Ret = OnesCount(x)
+ }
+ },
+ }, {
+ Name: "Len",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.BigInt(0)
+ if c.Do() {
+ c.Ret = Len(x)
+ }
+ },
+ }},
+}
diff --git a/pkg/math/pkg.go b/pkg/math/pkg.go
new file mode 100644
index 0000000..c079314
--- /dev/null
+++ b/pkg/math/pkg.go
@@ -0,0 +1,613 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package math
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("math", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "MaxExp",
+ Const: "2147483647",
+ }, {
+ Name: "MinExp",
+ Const: "-2147483648",
+ }, {
+ Name: "MaxPrec",
+ Const: "4294967295",
+ }, {
+ Name: "ToNearestEven",
+ Const: "0",
+ }, {
+ Name: "ToNearestAway",
+ Const: "1",
+ }, {
+ Name: "ToZero",
+ Const: "2",
+ }, {
+ Name: "AwayFromZero",
+ Const: "3",
+ }, {
+ Name: "ToNegativeInf",
+ Const: "4",
+ }, {
+ Name: "ToPositiveInf",
+ Const: "5",
+ }, {
+ Name: "Below",
+ Const: "-1",
+ }, {
+ Name: "Exact",
+ Const: "0",
+ }, {
+ Name: "Above",
+ Const: "1",
+ }, {
+ Name: "Jacobi",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.BigInt(0), c.BigInt(1)
+ if c.Do() {
+ c.Ret = Jacobi(x, y)
+ }
+ },
+ }, {
+ Name: "MaxBase",
+ Const: "62",
+ }, {
+ Name: "Floor",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Floor(x)
+ }
+ },
+ }, {
+ Name: "Ceil",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Ceil(x)
+ }
+ },
+ }, {
+ Name: "Trunc",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Trunc(x)
+ }
+ },
+ }, {
+ Name: "Round",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Round(x)
+ }
+ },
+ }, {
+ Name: "RoundToEven",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = RoundToEven(x)
+ }
+ },
+ }, {
+ Name: "MultipleOf",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.Decimal(0), c.Decimal(1)
+ if c.Do() {
+ c.Ret, c.Err = MultipleOf(x, y)
+ }
+ },
+ }, {
+ Name: "Abs",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Abs(x)
+ }
+ },
+ }, {
+ Name: "Acosh",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Acosh(x)
+ }
+ },
+ }, {
+ Name: "Asin",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Asin(x)
+ }
+ },
+ }, {
+ Name: "Acos",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Acos(x)
+ }
+ },
+ }, {
+ Name: "Asinh",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Asinh(x)
+ }
+ },
+ }, {
+ Name: "Atan",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Atan(x)
+ }
+ },
+ }, {
+ Name: "Atan2",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ y, x := c.Float64(0), c.Float64(1)
+ if c.Do() {
+ c.Ret = Atan2(y, x)
+ }
+ },
+ }, {
+ Name: "Atanh",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Atanh(x)
+ }
+ },
+ }, {
+ Name: "Cbrt",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Cbrt(x)
+ }
+ },
+ }, {
+ Name: "E",
+ Const: "2.71828182845904523536028747135266249775724709369995957496696763",
+ }, {
+ Name: "Pi",
+ Const: "3.14159265358979323846264338327950288419716939937510582097494459",
+ }, {
+ Name: "Phi",
+ Const: "1.61803398874989484820458683436563811772030917980576286213544861",
+ }, {
+ Name: "Sqrt2",
+ Const: "1.41421356237309504880168872420969807856967187537694807317667974",
+ }, {
+ Name: "SqrtE",
+ Const: "1.64872127070012814684865078781416357165377610071014801157507931",
+ }, {
+ Name: "SqrtPi",
+ Const: "1.77245385090551602729816748334114518279754945612238712821380779",
+ }, {
+ Name: "SqrtPhi",
+ Const: "1.27201964951406896425242246173749149171560804184009624861664038",
+ }, {
+ Name: "Ln2",
+ Const: "0.693147180559945309417232121458176568075500134360255254120680009",
+ }, {
+ Name: "Log2E",
+ Const: "1.442695040888963407359924681001892137426645954152985934135449408",
+ }, {
+ Name: "Ln10",
+ Const: "2.3025850929940456840179914546843642076011014886287729760333278",
+ }, {
+ Name: "Log10E",
+ Const: "0.43429448190325182765112891891660508229439700580366656611445378",
+ }, {
+ Name: "Copysign",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.Decimal(0), c.Decimal(1)
+ if c.Do() {
+ c.Ret = Copysign(x, y)
+ }
+ },
+ }, {
+ Name: "Dim",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.Decimal(0), c.Decimal(1)
+ if c.Do() {
+ c.Ret, c.Err = Dim(x, y)
+ }
+ },
+ }, {
+ Name: "Erf",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Erf(x)
+ }
+ },
+ }, {
+ Name: "Erfc",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Erfc(x)
+ }
+ },
+ }, {
+ Name: "Erfinv",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Erfinv(x)
+ }
+ },
+ }, {
+ Name: "Erfcinv",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Erfcinv(x)
+ }
+ },
+ }, {
+ Name: "Exp",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Exp(x)
+ }
+ },
+ }, {
+ Name: "Exp2",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Exp2(x)
+ }
+ },
+ }, {
+ Name: "Expm1",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Expm1(x)
+ }
+ },
+ }, {
+ Name: "Gamma",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Gamma(x)
+ }
+ },
+ }, {
+ Name: "Hypot",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ p, q := c.Float64(0), c.Float64(1)
+ if c.Do() {
+ c.Ret = Hypot(p, q)
+ }
+ },
+ }, {
+ Name: "J0",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = J0(x)
+ }
+ },
+ }, {
+ Name: "Y0",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Y0(x)
+ }
+ },
+ }, {
+ Name: "J1",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = J1(x)
+ }
+ },
+ }, {
+ Name: "Y1",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Y1(x)
+ }
+ },
+ }, {
+ Name: "Jn",
+ Params: []adt.Kind{adt.IntKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ n, x := c.Int(0), c.Float64(1)
+ if c.Do() {
+ c.Ret = Jn(n, x)
+ }
+ },
+ }, {
+ Name: "Yn",
+ Params: []adt.Kind{adt.IntKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ n, x := c.Int(0), c.Float64(1)
+ if c.Do() {
+ c.Ret = Yn(n, x)
+ }
+ },
+ }, {
+ Name: "Ldexp",
+ Params: []adt.Kind{adt.NumKind, adt.IntKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ frac, exp := c.Float64(0), c.Int(1)
+ if c.Do() {
+ c.Ret = Ldexp(frac, exp)
+ }
+ },
+ }, {
+ Name: "Log",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Log(x)
+ }
+ },
+ }, {
+ Name: "Log10",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Log10(x)
+ }
+ },
+ }, {
+ Name: "Log2",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret, c.Err = Log2(x)
+ }
+ },
+ }, {
+ Name: "Log1p",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Log1p(x)
+ }
+ },
+ }, {
+ Name: "Logb",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Logb(x)
+ }
+ },
+ }, {
+ Name: "Ilogb",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Ilogb(x)
+ }
+ },
+ }, {
+ Name: "Mod",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.Float64(0), c.Float64(1)
+ if c.Do() {
+ c.Ret = Mod(x, y)
+ }
+ },
+ }, {
+ Name: "Pow",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.Decimal(0), c.Decimal(1)
+ if c.Do() {
+ c.Ret, c.Err = Pow(x, y)
+ }
+ },
+ }, {
+ Name: "Pow10",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ n := c.Int32(0)
+ if c.Do() {
+ c.Ret = Pow10(n)
+ }
+ },
+ }, {
+ Name: "Remainder",
+ Params: []adt.Kind{adt.NumKind, adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x, y := c.Float64(0), c.Float64(1)
+ if c.Do() {
+ c.Ret = Remainder(x, y)
+ }
+ },
+ }, {
+ Name: "Signbit",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Decimal(0)
+ if c.Do() {
+ c.Ret = Signbit(x)
+ }
+ },
+ }, {
+ Name: "Cos",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Cos(x)
+ }
+ },
+ }, {
+ Name: "Sin",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Sin(x)
+ }
+ },
+ }, {
+ Name: "Sinh",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Sinh(x)
+ }
+ },
+ }, {
+ Name: "Cosh",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Cosh(x)
+ }
+ },
+ }, {
+ Name: "Sqrt",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Sqrt(x)
+ }
+ },
+ }, {
+ Name: "Tan",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Tan(x)
+ }
+ },
+ }, {
+ Name: "Tanh",
+ Params: []adt.Kind{adt.NumKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ x := c.Float64(0)
+ if c.Do() {
+ c.Ret = Tanh(x)
+ }
+ },
+ }},
+}
diff --git a/pkg/net/pkg.go b/pkg/net/pkg.go
new file mode 100644
index 0000000..4cf23a3
--- /dev/null
+++ b/pkg/net/pkg.go
@@ -0,0 +1,187 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package net
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("net", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "SplitHostPort",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = SplitHostPort(s)
+ }
+ },
+ }, {
+ Name: "JoinHostPort",
+ Params: []adt.Kind{adt.TopKind, adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ host, port := c.Value(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = JoinHostPort(host, port)
+ }
+ },
+ }, {
+ Name: "FQDN",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = FQDN(s)
+ }
+ },
+ }, {
+ Name: "IPv4len",
+ Const: "4",
+ }, {
+ Name: "IPv6len",
+ Const: "16",
+ }, {
+ Name: "ParseIP",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = ParseIP(s)
+ }
+ },
+ }, {
+ Name: "IPv4",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = IPv4(ip)
+ }
+ },
+ }, {
+ Name: "IP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = IP(ip)
+ }
+ },
+ }, {
+ Name: "LoopbackIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = LoopbackIP(ip)
+ }
+ },
+ }, {
+ Name: "MulticastIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = MulticastIP(ip)
+ }
+ },
+ }, {
+ Name: "InterfaceLocalMulticastIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = InterfaceLocalMulticastIP(ip)
+ }
+ },
+ }, {
+ Name: "LinkLocalMulticastIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = LinkLocalMulticastIP(ip)
+ }
+ },
+ }, {
+ Name: "LinkLocalUnicastIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = LinkLocalUnicastIP(ip)
+ }
+ },
+ }, {
+ Name: "GlobalUnicastIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = GlobalUnicastIP(ip)
+ }
+ },
+ }, {
+ Name: "UnspecifiedIP",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret = UnspecifiedIP(ip)
+ }
+ },
+ }, {
+ Name: "ToIP4",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = ToIP4(ip)
+ }
+ },
+ }, {
+ Name: "ToIP16",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = ToIP16(ip)
+ }
+ },
+ }, {
+ Name: "IPString",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ ip := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = IPString(ip)
+ }
+ },
+ }},
+}
diff --git a/pkg/path/pkg.go b/pkg/path/pkg.go
new file mode 100644
index 0000000..87abd8c
--- /dev/null
+++ b/pkg/path/pkg.go
@@ -0,0 +1,91 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package path
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("path", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Split",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ path := c.String(0)
+ if c.Do() {
+ c.Ret = Split(path)
+ }
+ },
+ }, {
+ Name: "Match",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, name := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = Match(pattern, name)
+ }
+ },
+ }, {
+ Name: "Clean",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ path := c.String(0)
+ if c.Do() {
+ c.Ret = Clean(path)
+ }
+ },
+ }, {
+ Name: "Ext",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ path := c.String(0)
+ if c.Do() {
+ c.Ret = Ext(path)
+ }
+ },
+ }, {
+ Name: "Base",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ path := c.String(0)
+ if c.Do() {
+ c.Ret = Base(path)
+ }
+ },
+ }, {
+ Name: "IsAbs",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ path := c.String(0)
+ if c.Do() {
+ c.Ret = IsAbs(path)
+ }
+ },
+ }, {
+ Name: "Dir",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ path := c.String(0)
+ if c.Do() {
+ c.Ret = Dir(path)
+ }
+ },
+ }},
+}
diff --git a/pkg/regexp/pkg.go b/pkg/regexp/pkg.go
new file mode 100644
index 0000000..cd484b4
--- /dev/null
+++ b/pkg/regexp/pkg.go
@@ -0,0 +1,111 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package regexp
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("regexp", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Valid",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = Valid(pattern)
+ }
+ },
+ }, {
+ Name: "Find",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = Find(pattern, s)
+ }
+ },
+ }, {
+ Name: "FindAll",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s, n := c.String(0), c.String(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = FindAll(pattern, s, n)
+ }
+ },
+ }, {
+ Name: "FindSubmatch",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = FindSubmatch(pattern, s)
+ }
+ },
+ }, {
+ Name: "FindAllSubmatch",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s, n := c.String(0), c.String(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = FindAllSubmatch(pattern, s, n)
+ }
+ },
+ }, {
+ Name: "FindNamedSubmatch",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StructKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = FindNamedSubmatch(pattern, s)
+ }
+ },
+ }, {
+ Name: "FindAllNamedSubmatch",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s, n := c.String(0), c.String(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = FindAllNamedSubmatch(pattern, s, n)
+ }
+ },
+ }, {
+ Name: "Match",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ pattern, s := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = Match(pattern, s)
+ }
+ },
+ }, {
+ Name: "QuoteMeta",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = QuoteMeta(s)
+ }
+ },
+ }},
+}
diff --git a/pkg/register.go b/pkg/register.go
new file mode 100644
index 0000000..b7696b9
--- /dev/null
+++ b/pkg/register.go
@@ -0,0 +1,47 @@
+// Copyright 2020 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package pkg
+
+import (
+ _ "cuelang.org/go/pkg/crypto/md5"
+ _ "cuelang.org/go/pkg/crypto/sha1"
+ _ "cuelang.org/go/pkg/crypto/sha256"
+ _ "cuelang.org/go/pkg/crypto/sha512"
+ _ "cuelang.org/go/pkg/encoding/base64"
+ _ "cuelang.org/go/pkg/encoding/csv"
+ _ "cuelang.org/go/pkg/encoding/hex"
+ _ "cuelang.org/go/pkg/encoding/json"
+ _ "cuelang.org/go/pkg/encoding/yaml"
+ _ "cuelang.org/go/pkg/html"
+
+ _ "cuelang.org/go/pkg/list"
+ _ "cuelang.org/go/pkg/math"
+ _ "cuelang.org/go/pkg/math/bits"
+ _ "cuelang.org/go/pkg/net"
+ _ "cuelang.org/go/pkg/path"
+ _ "cuelang.org/go/pkg/regexp"
+ _ "cuelang.org/go/pkg/strconv"
+ _ "cuelang.org/go/pkg/strings"
+ _ "cuelang.org/go/pkg/struct"
+ _ "cuelang.org/go/pkg/text/tabwriter"
+ _ "cuelang.org/go/pkg/text/template"
+ _ "cuelang.org/go/pkg/time"
+ _ "cuelang.org/go/pkg/tool"
+ _ "cuelang.org/go/pkg/tool/cli"
+ _ "cuelang.org/go/pkg/tool/exec"
+ _ "cuelang.org/go/pkg/tool/file"
+ _ "cuelang.org/go/pkg/tool/http"
+ _ "cuelang.org/go/pkg/tool/os"
+)
diff --git a/pkg/strconv/pkg.go b/pkg/strconv/pkg.go
new file mode 100644
index 0000000..80c9c64
--- /dev/null
+++ b/pkg/strconv/pkg.go
@@ -0,0 +1,204 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package strconv
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("strconv", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Unquote",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = Unquote(s)
+ }
+ },
+ }, {
+ Name: "ParseBool",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ str := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = ParseBool(str)
+ }
+ },
+ }, {
+ Name: "FormatBool",
+ Params: []adt.Kind{adt.BoolKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ b := c.Bool(0)
+ if c.Do() {
+ c.Ret = FormatBool(b)
+ }
+ },
+ }, {
+ Name: "ParseFloat",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind},
+ Result: adt.NumKind,
+ Func: func(c *internal.CallCtxt) {
+ s, bitSize := c.String(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = ParseFloat(s, bitSize)
+ }
+ },
+ }, {
+ Name: "IntSize",
+ Const: "64",
+ }, {
+ Name: "ParseUint",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, base, bitSize := c.String(0), c.Int(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = ParseUint(s, base, bitSize)
+ }
+ },
+ }, {
+ Name: "ParseInt",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, base, bitSize := c.String(0), c.Int(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = ParseInt(s, base, bitSize)
+ }
+ },
+ }, {
+ Name: "Atoi",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = Atoi(s)
+ }
+ },
+ }, {
+ Name: "FormatFloat",
+ Params: []adt.Kind{adt.NumKind, adt.IntKind, adt.IntKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ f, fmt, prec, bitSize := c.Float64(0), c.Byte(1), c.Int(2), c.Int(3)
+ if c.Do() {
+ c.Ret = FormatFloat(f, fmt, prec, bitSize)
+ }
+ },
+ }, {
+ Name: "FormatUint",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ i, base := c.Uint64(0), c.Int(1)
+ if c.Do() {
+ c.Ret = FormatUint(i, base)
+ }
+ },
+ }, {
+ Name: "FormatInt",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ i, base := c.Int64(0), c.Int(1)
+ if c.Do() {
+ c.Ret = FormatInt(i, base)
+ }
+ },
+ }, {
+ Name: "Quote",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = Quote(s)
+ }
+ },
+ }, {
+ Name: "QuoteToASCII",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = QuoteToASCII(s)
+ }
+ },
+ }, {
+ Name: "QuoteToGraphic",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = QuoteToGraphic(s)
+ }
+ },
+ }, {
+ Name: "QuoteRune",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ r := c.Rune(0)
+ if c.Do() {
+ c.Ret = QuoteRune(r)
+ }
+ },
+ }, {
+ Name: "QuoteRuneToASCII",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ r := c.Rune(0)
+ if c.Do() {
+ c.Ret = QuoteRuneToASCII(r)
+ }
+ },
+ }, {
+ Name: "QuoteRuneToGraphic",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ r := c.Rune(0)
+ if c.Do() {
+ c.Ret = QuoteRuneToGraphic(r)
+ }
+ },
+ }, {
+ Name: "IsPrint",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ r := c.Rune(0)
+ if c.Do() {
+ c.Ret = IsPrint(r)
+ }
+ },
+ }, {
+ Name: "IsGraphic",
+ Params: []adt.Kind{adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ r := c.Rune(0)
+ if c.Do() {
+ c.Ret = IsGraphic(r)
+ }
+ },
+ }},
+}
diff --git a/pkg/strings/pkg.go b/pkg/strings/pkg.go
new file mode 100644
index 0000000..fb43360
--- /dev/null
+++ b/pkg/strings/pkg.go
@@ -0,0 +1,361 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package strings
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("strings", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "ByteAt",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind, adt.IntKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ b, i := c.Bytes(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = ByteAt(b, i)
+ }
+ },
+ }, {
+ Name: "ByteSlice",
+ Params: []adt.Kind{adt.BytesKind | adt.StringKind, adt.IntKind, adt.IntKind},
+ Result: adt.BytesKind | adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ b, start, end := c.Bytes(0), c.Int(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = ByteSlice(b, start, end)
+ }
+ },
+ }, {
+ Name: "Runes",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = Runes(s)
+ }
+ },
+ }, {
+ Name: "MinRunes",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s, min := c.String(0), c.Int(1)
+ if c.Do() {
+ c.Ret = MinRunes(s, min)
+ }
+ },
+ }, {
+ Name: "MaxRunes",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s, max := c.String(0), c.Int(1)
+ if c.Do() {
+ c.Ret = MaxRunes(s, max)
+ }
+ },
+ }, {
+ Name: "ToTitle",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = ToTitle(s)
+ }
+ },
+ }, {
+ Name: "ToCamel",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = ToCamel(s)
+ }
+ },
+ }, {
+ Name: "SliceRunes",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, start, end := c.String(0), c.Int(1), c.Int(2)
+ if c.Do() {
+ c.Ret, c.Err = SliceRunes(s, start, end)
+ }
+ },
+ }, {
+ Name: "Compare",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ a, b := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = Compare(a, b)
+ }
+ },
+ }, {
+ Name: "Count",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, substr := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = Count(s, substr)
+ }
+ },
+ }, {
+ Name: "Contains",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s, substr := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = Contains(s, substr)
+ }
+ },
+ }, {
+ Name: "ContainsAny",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s, chars := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = ContainsAny(s, chars)
+ }
+ },
+ }, {
+ Name: "LastIndex",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, substr := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = LastIndex(s, substr)
+ }
+ },
+ }, {
+ Name: "IndexAny",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, chars := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = IndexAny(s, chars)
+ }
+ },
+ }, {
+ Name: "LastIndexAny",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, chars := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = LastIndexAny(s, chars)
+ }
+ },
+ }, {
+ Name: "SplitN",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s, sep, n := c.String(0), c.String(1), c.Int(2)
+ if c.Do() {
+ c.Ret = SplitN(s, sep, n)
+ }
+ },
+ }, {
+ Name: "SplitAfterN",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind, adt.IntKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s, sep, n := c.String(0), c.String(1), c.Int(2)
+ if c.Do() {
+ c.Ret = SplitAfterN(s, sep, n)
+ }
+ },
+ }, {
+ Name: "Split",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s, sep := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = Split(s, sep)
+ }
+ },
+ }, {
+ Name: "SplitAfter",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s, sep := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = SplitAfter(s, sep)
+ }
+ },
+ }, {
+ Name: "Fields",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.ListKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = Fields(s)
+ }
+ },
+ }, {
+ Name: "Join",
+ Params: []adt.Kind{adt.ListKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ elems, sep := c.StringList(0), c.String(1)
+ if c.Do() {
+ c.Ret = Join(elems, sep)
+ }
+ },
+ }, {
+ Name: "HasPrefix",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s, prefix := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = HasPrefix(s, prefix)
+ }
+ },
+ }, {
+ Name: "HasSuffix",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s, suffix := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = HasSuffix(s, suffix)
+ }
+ },
+ }, {
+ Name: "Repeat",
+ Params: []adt.Kind{adt.StringKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, count := c.String(0), c.Int(1)
+ if c.Do() {
+ c.Ret = Repeat(s, count)
+ }
+ },
+ }, {
+ Name: "ToUpper",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = ToUpper(s)
+ }
+ },
+ }, {
+ Name: "ToLower",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = ToLower(s)
+ }
+ },
+ }, {
+ Name: "Trim",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, cutset := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = Trim(s, cutset)
+ }
+ },
+ }, {
+ Name: "TrimLeft",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, cutset := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = TrimLeft(s, cutset)
+ }
+ },
+ }, {
+ Name: "TrimRight",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, cutset := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = TrimRight(s, cutset)
+ }
+ },
+ }, {
+ Name: "TrimSpace",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = TrimSpace(s)
+ }
+ },
+ }, {
+ Name: "TrimPrefix",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, prefix := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = TrimPrefix(s, prefix)
+ }
+ },
+ }, {
+ Name: "TrimSuffix",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, suffix := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = TrimSuffix(s, suffix)
+ }
+ },
+ }, {
+ Name: "Replace",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind, adt.StringKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s, old, new, n := c.String(0), c.String(1), c.String(2), c.Int(3)
+ if c.Do() {
+ c.Ret = Replace(s, old, new, n)
+ }
+ },
+ }, {
+ Name: "Index",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s, substr := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret = Index(s, substr)
+ }
+ },
+ }},
+}
diff --git a/pkg/struct/pkg.go b/pkg/struct/pkg.go
new file mode 100644
index 0000000..3456c4a
--- /dev/null
+++ b/pkg/struct/pkg.go
@@ -0,0 +1,41 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package structs
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("struct", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "MinFields",
+ Params: []adt.Kind{adt.StructKind, adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ object, n := c.Struct(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = MinFields(object, n)
+ }
+ },
+ }, {
+ Name: "MaxFields",
+ Params: []adt.Kind{adt.StructKind, adt.IntKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ object, n := c.Struct(0), c.Int(1)
+ if c.Do() {
+ c.Ret, c.Err = MaxFields(object, n)
+ }
+ },
+ }},
+}
diff --git a/pkg/text/tabwriter/pkg.go b/pkg/text/tabwriter/pkg.go
new file mode 100644
index 0000000..fc60e66
--- /dev/null
+++ b/pkg/text/tabwriter/pkg.go
@@ -0,0 +1,31 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package tabwriter
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("text/tabwriter", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Write",
+ Params: []adt.Kind{adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ data := c.Value(0)
+ if c.Do() {
+ c.Ret, c.Err = Write(data)
+ }
+ },
+ }},
+}
diff --git a/pkg/text/template/pkg.go b/pkg/text/template/pkg.go
new file mode 100644
index 0000000..16aa21f
--- /dev/null
+++ b/pkg/text/template/pkg.go
@@ -0,0 +1,51 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package template
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("text/template", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Execute",
+ Params: []adt.Kind{adt.StringKind, adt.TopKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ templ, data := c.String(0), c.Value(1)
+ if c.Do() {
+ c.Ret, c.Err = Execute(templ, data)
+ }
+ },
+ }, {
+ Name: "HTMLEscape",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = HTMLEscape(s)
+ }
+ },
+ }, {
+ Name: "JSEscape",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret = JSEscape(s)
+ }
+ },
+ }},
+}
diff --git a/pkg/time/pkg.go b/pkg/time/pkg.go
new file mode 100644
index 0000000..23c9415
--- /dev/null
+++ b/pkg/time/pkg.go
@@ -0,0 +1,195 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../gen/gen.go
+
+package time
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("time", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{{
+ Name: "Nanosecond",
+ Const: "1",
+ }, {
+ Name: "Microsecond",
+ Const: "1000",
+ }, {
+ Name: "Millisecond",
+ Const: "1000000",
+ }, {
+ Name: "Second",
+ Const: "1000000000",
+ }, {
+ Name: "Minute",
+ Const: "60000000000",
+ }, {
+ Name: "Hour",
+ Const: "3600000000000",
+ }, {
+ Name: "Duration",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = Duration(s)
+ }
+ },
+ }, {
+ Name: "ParseDuration",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.IntKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = ParseDuration(s)
+ }
+ },
+ }, {
+ Name: "ANSIC",
+ Const: "\"Mon Jan _2 15:04:05 2006\"",
+ }, {
+ Name: "UnixDate",
+ Const: "\"Mon Jan _2 15:04:05 MST 2006\"",
+ }, {
+ Name: "RubyDate",
+ Const: "\"Mon Jan 02 15:04:05 -0700 2006\"",
+ }, {
+ Name: "RFC822",
+ Const: "\"02 Jan 06 15:04 MST\"",
+ }, {
+ Name: "RFC822Z",
+ Const: "\"02 Jan 06 15:04 -0700\"",
+ }, {
+ Name: "RFC850",
+ Const: "\"Monday, 02-Jan-06 15:04:05 MST\"",
+ }, {
+ Name: "RFC1123",
+ Const: "\"Mon, 02 Jan 2006 15:04:05 MST\"",
+ }, {
+ Name: "RFC1123Z",
+ Const: "\"Mon, 02 Jan 2006 15:04:05 -0700\"",
+ }, {
+ Name: "RFC3339",
+ Const: "\"2006-01-02T15:04:05Z07:00\"",
+ }, {
+ Name: "RFC3339Nano",
+ Const: "\"2006-01-02T15:04:05.999999999Z07:00\"",
+ }, {
+ Name: "RFC3339Date",
+ Const: "\"2006-01-02\"",
+ }, {
+ Name: "Kitchen",
+ Const: "\"3:04PM\"",
+ }, {
+ Name: "Kitchen24",
+ Const: "\"15:04\"",
+ }, {
+ Name: "January",
+ Const: "1",
+ }, {
+ Name: "February",
+ Const: "2",
+ }, {
+ Name: "March",
+ Const: "3",
+ }, {
+ Name: "April",
+ Const: "4",
+ }, {
+ Name: "May",
+ Const: "5",
+ }, {
+ Name: "June",
+ Const: "6",
+ }, {
+ Name: "July",
+ Const: "7",
+ }, {
+ Name: "August",
+ Const: "8",
+ }, {
+ Name: "September",
+ Const: "9",
+ }, {
+ Name: "October",
+ Const: "10",
+ }, {
+ Name: "November",
+ Const: "11",
+ }, {
+ Name: "December",
+ Const: "12",
+ }, {
+ Name: "Sunday",
+ Const: "0",
+ }, {
+ Name: "Monday",
+ Const: "1",
+ }, {
+ Name: "Tuesday",
+ Const: "2",
+ }, {
+ Name: "Wednesday",
+ Const: "3",
+ }, {
+ Name: "Thursday",
+ Const: "4",
+ }, {
+ Name: "Friday",
+ Const: "5",
+ }, {
+ Name: "Saturday",
+ Const: "6",
+ }, {
+ Name: "Time",
+ Params: []adt.Kind{adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ s := c.String(0)
+ if c.Do() {
+ c.Ret, c.Err = Time(s)
+ }
+ },
+ }, {
+ Name: "Format",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.BoolKind,
+ Func: func(c *internal.CallCtxt) {
+ value, layout := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = Format(value, layout)
+ }
+ },
+ }, {
+ Name: "Parse",
+ Params: []adt.Kind{adt.StringKind, adt.StringKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ layout, value := c.String(0), c.String(1)
+ if c.Do() {
+ c.Ret, c.Err = Parse(layout, value)
+ }
+ },
+ }, {
+ Name: "Unix",
+ Params: []adt.Kind{adt.IntKind, adt.IntKind},
+ Result: adt.StringKind,
+ Func: func(c *internal.CallCtxt) {
+ sec, nsec := c.Int64(0), c.Int64(1)
+ if c.Do() {
+ c.Ret = Unix(sec, nsec)
+ }
+ },
+ }},
+}
diff --git a/pkg/tool/cli/pkg.go b/pkg/tool/cli/pkg.go
new file mode 100644
index 0000000..1cd3d33
--- /dev/null
+++ b/pkg/tool/cli/pkg.go
@@ -0,0 +1,27 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package cli
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("tool/cli", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{},
+ CUE: `{
+ Print: {
+ $id: *"tool/cli.Print" | "print"
+ text: string
+ }
+}`,
+}
diff --git a/pkg/tool/exec/pkg.go b/pkg/tool/exec/pkg.go
new file mode 100644
index 0000000..74d3b7e
--- /dev/null
+++ b/pkg/tool/exec/pkg.go
@@ -0,0 +1,34 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package exec
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("tool/exec", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{},
+ CUE: `{
+ Run: {
+ $id: *"tool/exec.Run" | "exec"
+ cmd: string | [string, ...string]
+ env: {
+ [string]: string | [...=~"="]
+ }
+ stdout: *null | string | bytes
+ stderr: *null | string | bytes
+ stdin: *null | string | bytes
+ success: bool
+ }
+}`,
+}
diff --git a/pkg/tool/file/pkg.go b/pkg/tool/file/pkg.go
new file mode 100644
index 0000000..35bcf4d
--- /dev/null
+++ b/pkg/tool/file/pkg.go
@@ -0,0 +1,45 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package file
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("tool/file", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{},
+ CUE: `{
+ Read: {
+ $id: "tool/file.Read"
+ filename: !=""
+ contents: *bytes | string
+ }
+ Append: {
+ $id: "tool/file.Append"
+ filename: !=""
+ permissions: int | *420
+ contents: bytes | string
+ }
+ Create: {
+ $id: "tool/file.Create"
+ filename: !=""
+ permissions: int | *420
+ contents: bytes | string
+ }
+ Glob: {
+ $id: "tool/file.Glob"
+ glob: !=""
+ files: [...string]
+ }
+}`,
+}
diff --git a/pkg/tool/http/pkg.go b/pkg/tool/http/pkg.go
new file mode 100644
index 0000000..5969e73
--- /dev/null
+++ b/pkg/tool/http/pkg.go
@@ -0,0 +1,64 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package http
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("tool/http", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{},
+ CUE: `{
+ Get: {
+ Do
+ method: "GET"
+ }
+ Post: {
+ Do
+ method: "POST"
+ }
+ Put: {
+ Do
+ method: "PUT"
+ }
+ Delete: {
+ Do
+ method: "DELETE"
+ }
+ Do: {
+ $id: *"tool/http.Do" | "http"
+ method: string
+ url: string
+ request: {
+ body: *bytes | string
+ header: {
+ [string]: string | [...string]
+ }
+ trailer: {
+ [string]: string | [...string]
+ }
+ }
+ response: {
+ status: string
+ statusCode: int
+ body: *bytes | string
+ header: {
+ [string]: string | [...string]
+ }
+ trailer: {
+ [string]: string | [...string]
+ }
+ }
+ }
+}`,
+}
diff --git a/pkg/tool/os/pkg.go b/pkg/tool/os/pkg.go
new file mode 100644
index 0000000..f1f4043
--- /dev/null
+++ b/pkg/tool/os/pkg.go
@@ -0,0 +1,46 @@
+// Code generated by go generate. DO NOT EDIT.
+
+//go:generate rm pkg.go
+//go:generate go run ../../gen/gen.go
+
+package os
+
+import (
+ "cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/pkg/internal"
+)
+
+func init() {
+ internal.Register("tool/os", pkg)
+}
+
+var _ = adt.TopKind // in case the adt package isn't used
+
+var pkg = &internal.Package{
+ Native: []*internal.Builtin{},
+ CUE: `{
+ Value: bool | number | *string | null
+ Name: !="" & !~"^[$]"
+ Setenv: {
+ {
+ [Name]: Value
+ }
+ $id: "tool/os.Setenv"
+ }
+ Getenv: {
+ {
+ [Name]: Value
+ }
+ $id: "tool/os.Getenv"
+ }
+ Environ: {
+ {
+ [Name]: Value
+ }
+ $id: "tool/os.Environ"
+ }
+ Clearenv: {
+ $id: "tool/os.Clearenv"
+ }
+}`,
+}