1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
// 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 compile
import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
)
// This file contains predeclared builtins.
const supportedByLen = adt.StructKind | adt.BytesKind | adt.StringKind | adt.ListKind
var (
structParam = adt.Param{Value: &adt.BasicType{K: adt.StructKind}}
listParam = adt.Param{Value: &adt.BasicType{K: adt.ListKind}}
intParam = adt.Param{Value: &adt.BasicType{K: adt.IntKind}}
topParam = adt.Param{Value: &adt.BasicType{K: adt.TopKind}}
)
var lenBuiltin = &adt.Builtin{
Name: "len",
Params: []adt.Param{{Value: &adt.BasicType{K: supportedByLen}}},
Result: adt.IntKind,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
v := args[0]
if x, ok := v.(*adt.Vertex); ok {
x.LockArcs = true
switch x.BaseValue.(type) {
case nil:
// This should not happen, but be defensive.
return c.NewErrf("unevaluated vertex")
case *adt.ListMarker:
return c.NewInt64(int64(len(x.Elems())), v)
case *adt.StructMarker:
n := 0
v, _ := v.(*adt.Vertex)
for _, a := range v.Arcs {
if a.Label.IsRegular() && a.IsDefined(c) {
n++
}
}
return c.NewInt64(int64(n), v)
default:
v = x.Value()
}
}
switch x := v.(type) {
case *adt.Bytes:
return c.NewInt64(int64(len(x.B)), v)
case *adt.String:
return c.NewInt64(int64(len(x.Str)), v)
default:
k := x.Kind()
if k&supportedByLen == adt.BottomKind {
return c.NewErrf("invalid argument type %v", k)
}
b := c.NewErrf("incomplete argument %s (type %v)", v, k)
b.Code = adt.IncompleteError
return b
}
},
}
var closeBuiltin = &adt.Builtin{
Name: "close",
Params: []adt.Param{structParam},
Result: adt.StructKind,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
s, ok := args[0].(*adt.Vertex)
if !ok {
return c.NewErrf("struct argument must be concrete")
}
var v *adt.Vertex
if c.Version == internal.DevVersion {
// TODO(evalv3) this is a rather convoluted and inefficient way to
// accomplish signaling vertex should be closed. In most cases, it
// would suffice to set IsClosed in the CloseInfo. However, that
// does not cover all code paths. Consider simplifying this.
v = c.Wrap(s, c.CloseInfo())
v.ClosedNonRecursive = true
} else {
if m, ok := s.BaseValue.(*adt.StructMarker); ok && m.NeedClose {
return s
}
v = s.Clone()
v.BaseValue = &adt.StructMarker{NeedClose: true}
}
return v
},
}
var andBuiltin = &adt.Builtin{
Name: "and",
Params: []adt.Param{listParam},
Result: adt.IntKind,
RawFunc: func(c *adt.OpContext, args []adt.Expr) adt.Value {
// Pass through the cycle information from evaluating the first argument.
v := c.EvaluateKeepState(args[0])
list := c.RawElems(v)
if len(list) == 0 {
return &adt.Top{}
}
a := []adt.Value{}
for _, c := range list {
a = append(a, c)
}
return &adt.Conjunction{Values: a}
},
}
var orBuiltin = &adt.Builtin{
Name: "or",
Params: []adt.Param{listParam},
Result: adt.IntKind,
NonConcrete: true,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
d := []adt.Disjunct{}
for _, c := range c.RawElems(args[0]) {
d = append(d, adt.Disjunct{Val: c, Default: false})
}
if len(d) == 0 {
// TODO(manifest): This should not be unconditionally incomplete,
// but it requires results from comprehensions and all to have
// some special status. Maybe this can be solved by having results
// of list comprehensions be open if they result from iterating over
// an open list or struct. This would actually be exactly what
// that means. The error here could then only add an incomplete
// status if the source is open.
return &adt.Bottom{
Code: adt.IncompleteError,
// TODO: get and set Vertex
Err: errors.Newf(c.Pos(), "empty list in call to or"),
}
}
v := &adt.Vertex{}
// TODO: make a Disjunction.
closeInfo := c.CloseInfo()
v.AddConjunct(adt.MakeConjunct(nil,
&adt.DisjunctionExpr{Values: d, HasDefaults: false},
closeInfo,
))
v.CompleteArcs(c)
return v
},
}
var divBuiltin = &adt.Builtin{
Name: "div",
Params: []adt.Param{intParam, intParam},
Result: adt.IntKind,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
const name = "argument to div builtin"
return intDivOp(c, (*adt.OpContext).IntDiv, name, args)
},
}
var modBuiltin = &adt.Builtin{
Name: "mod",
Params: []adt.Param{intParam, intParam},
Result: adt.IntKind,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
const name = "argument to mod builtin"
return intDivOp(c, (*adt.OpContext).IntMod, name, args)
},
}
var quoBuiltin = &adt.Builtin{
Name: "quo",
Params: []adt.Param{intParam, intParam},
Result: adt.IntKind,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
const name = "argument to quo builtin"
return intDivOp(c, (*adt.OpContext).IntQuo, name, args)
},
}
var remBuiltin = &adt.Builtin{
Name: "rem",
Params: []adt.Param{intParam, intParam},
Result: adt.IntKind,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
const name = "argument to rem builtin"
return intDivOp(c, (*adt.OpContext).IntRem, name, args)
},
}
type intFunc func(c *adt.OpContext, x, y *adt.Num) adt.Value
func intDivOp(c *adt.OpContext, fn intFunc, name string, args []adt.Value) adt.Value {
a := c.Num(args[0], name)
b := c.Num(args[1], name)
if c.HasErr() {
return nil
}
return fn(c, a, b)
}
|