Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 5 | // Package starlark provides a Starlark interpreter. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 6 | // |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 7 | // Starlark values are represented by the Value interface. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 8 | // The following built-in Value types are known to the evaluator: |
| 9 | // |
| 10 | // NoneType -- NoneType |
| 11 | // Bool -- bool |
| 12 | // Int -- int |
| 13 | // Float -- float |
| 14 | // String -- string |
| 15 | // *List -- list |
| 16 | // Tuple -- tuple |
| 17 | // *Dict -- dict |
| 18 | // *Set -- set |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 19 | // *Function -- function (implemented in Starlark) |
alandonovan | 4cbd896 | 2017-10-19 13:18:36 -0400 | [diff] [blame] | 20 | // *Builtin -- builtin_function_or_method (function or method implemented in Go) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 21 | // |
| 22 | // Client applications may define new data types that satisfy at least |
| 23 | // the Value interface. Such types may provide additional operations by |
| 24 | // implementing any of these optional interfaces: |
| 25 | // |
| 26 | // Callable -- value is callable like a function |
| 27 | // Comparable -- value defines its own comparison operations |
| 28 | // Iterable -- value is iterable using 'for' loops |
| 29 | // Sequence -- value is iterable sequence of known length |
| 30 | // Indexable -- value is sequence with efficient random access |
jmillikin-stripe | 3ccab94 | 2018-10-05 07:09:12 -0700 | [diff] [blame] | 31 | // Mapping -- value maps from keys to values, like a dictionary |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 32 | // HasBinary -- value defines binary operations such as * and + |
| 33 | // HasAttrs -- value has readable fields or methods x.f |
| 34 | // HasSetField -- value has settable fields x.f |
| 35 | // HasSetIndex -- value supports element update using x[i]=y |
jmillikin-stripe | 3ccab94 | 2018-10-05 07:09:12 -0700 | [diff] [blame] | 36 | // HasSetKey -- value supports map update using x[k]=v |
alandonovan | 58f9101 | 2019-01-03 16:32:47 -0500 | [diff] [blame] | 37 | // HasUnary -- value defines unary operations such as + and - |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 38 | // |
| 39 | // Client applications may also define domain-specific functions in Go |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 40 | // and make them available to Starlark programs. Use NewBuiltin to |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 41 | // construct a built-in value that wraps a Go function. The |
| 42 | // implementation of the Go function may use UnpackArgs to make sense of |
| 43 | // the positional and keyword arguments provided by the caller. |
| 44 | // |
alandonovan | 6677ee5 | 2020-02-03 09:41:50 -0500 | [diff] [blame] | 45 | // Starlark's None value is not equal to Go's nil. Go's nil is not a legal |
| 46 | // Starlark value, but the compiler will not stop you from converting nil |
| 47 | // to Value. Be careful to avoid allowing Go nil values to leak into |
| 48 | // Starlark data structures. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 49 | // |
| 50 | // The Compare operation requires two arguments of the same |
| 51 | // type, but this constraint cannot be expressed in Go's type system. |
| 52 | // (This is the classic "binary method problem".) |
| 53 | // So, each Value type's CompareSameType method is a partial function |
| 54 | // that compares a value only against others of the same type. |
| 55 | // Use the package's standalone Compare (or Equal) function to compare |
| 56 | // an arbitrary pair of values. |
| 57 | // |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 58 | // To parse and evaluate a Starlark source file, use ExecFile. The Eval |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 59 | // function evaluates a single expression. All evaluator functions |
| 60 | // require a Thread parameter which defines the "thread-local storage" |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 61 | // of a Starlark thread and may be used to plumb application state |
alandonovan | 6677ee5 | 2020-02-03 09:41:50 -0500 | [diff] [blame] | 62 | // through Starlark code and into callbacks. When evaluation fails it |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 63 | // returns an EvalError from which the application may obtain a |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 64 | // backtrace of active Starlark calls. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 65 | // |
Alan Donovan | 6beab7e | 2018-10-31 17:53:09 -0400 | [diff] [blame] | 66 | package starlark // import "go.starlark.net/starlark" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 67 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 68 | // This file defines the data types of Starlark and their basic operations. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 69 | |
| 70 | import ( |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 71 | "fmt" |
| 72 | "math" |
| 73 | "math/big" |
| 74 | "reflect" |
| 75 | "strconv" |
| 76 | "strings" |
| 77 | "unicode/utf8" |
| 78 | |
Alan Donovan | 6beab7e | 2018-10-31 17:53:09 -0400 | [diff] [blame] | 79 | "go.starlark.net/internal/compile" |
| 80 | "go.starlark.net/syntax" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 81 | ) |
| 82 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 83 | // Value is a value in the Starlark interpreter. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 84 | type Value interface { |
| 85 | // String returns the string representation of the value. |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 86 | // Starlark string values are quoted as if by Python's repr. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 87 | String() string |
| 88 | |
| 89 | // Type returns a short string describing the value's type. |
| 90 | Type() string |
| 91 | |
| 92 | // Freeze causes the value, and all values transitively |
| 93 | // reachable from it through collections and closures, to be |
| 94 | // marked as frozen. All subsequent mutations to the data |
| 95 | // structure through this API will fail dynamically, making the |
| 96 | // data structure immutable and safe for publishing to other |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 97 | // Starlark interpreters running concurrently. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 98 | Freeze() |
| 99 | |
alandonovan | 5ce1e42 | 2017-10-17 15:20:32 -0400 | [diff] [blame] | 100 | // Truth returns the truth value of an object. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 101 | Truth() Bool |
| 102 | |
| 103 | // Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y). |
| 104 | // Hash may fail if the value's type is not hashable, or if the value |
alandonovan | e35f71a | 2019-05-14 14:50:48 -0400 | [diff] [blame] | 105 | // contains a non-hashable value. The hash is used only by dictionaries and |
| 106 | // is not exposed to the Starlark program. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 107 | Hash() (uint32, error) |
| 108 | } |
| 109 | |
| 110 | // A Comparable is a value that defines its own equivalence relation and |
| 111 | // perhaps ordered comparisons. |
| 112 | type Comparable interface { |
| 113 | Value |
| 114 | // CompareSameType compares one value to another of the same Type(). |
| 115 | // The comparison operation must be one of EQL, NEQ, LT, LE, GT, or GE. |
| 116 | // CompareSameType returns an error if an ordered comparison was |
| 117 | // requested for a type that does not support it. |
| 118 | // |
| 119 | // Implementations that recursively compare subcomponents of |
| 120 | // the value should use the CompareDepth function, not Compare, to |
| 121 | // avoid infinite recursion on cyclic structures. |
| 122 | // |
| 123 | // The depth parameter is used to bound comparisons of cyclic |
| 124 | // data structures. Implementations should decrement depth |
| 125 | // before calling CompareDepth and should return an error if depth |
| 126 | // < 1. |
| 127 | // |
| 128 | // Client code should not call this method. Instead, use the |
| 129 | // standalone Compare or Equals functions, which are defined for |
| 130 | // all pairs of operands. |
| 131 | CompareSameType(op syntax.Token, y Value, depth int) (bool, error) |
| 132 | } |
| 133 | |
| 134 | var ( |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 135 | _ Comparable = Int{} |
| 136 | _ Comparable = False |
| 137 | _ Comparable = Float(0) |
| 138 | _ Comparable = String("") |
| 139 | _ Comparable = (*Dict)(nil) |
| 140 | _ Comparable = (*List)(nil) |
| 141 | _ Comparable = Tuple(nil) |
| 142 | _ Comparable = (*Set)(nil) |
| 143 | ) |
| 144 | |
| 145 | // A Callable value f may be the operand of a function call, f(x). |
alandonovan | aeec83f | 2018-10-22 13:24:13 -0400 | [diff] [blame] | 146 | // |
| 147 | // Clients should use the Call function, never the CallInternal method. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 148 | type Callable interface { |
| 149 | Value |
| 150 | Name() string |
alandonovan | aeec83f | 2018-10-22 13:24:13 -0400 | [diff] [blame] | 151 | CallInternal(thread *Thread, args Tuple, kwargs []Tuple) (Value, error) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 152 | } |
| 153 | |
alandonovan | 40b4ab6 | 2019-04-03 16:41:37 -0400 | [diff] [blame] | 154 | type callableWithPosition interface { |
| 155 | Callable |
| 156 | Position() syntax.Position |
| 157 | } |
| 158 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 159 | var ( |
alandonovan | 40b4ab6 | 2019-04-03 16:41:37 -0400 | [diff] [blame] | 160 | _ Callable = (*Builtin)(nil) |
| 161 | _ Callable = (*Function)(nil) |
| 162 | _ callableWithPosition = (*Function)(nil) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 163 | ) |
| 164 | |
| 165 | // An Iterable abstracts a sequence of values. |
| 166 | // An iterable value may be iterated over by a 'for' loop or used where |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 167 | // any other Starlark iterable is allowed. Unlike a Sequence, the length |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 168 | // of an Iterable is not necessarily known in advance of iteration. |
| 169 | type Iterable interface { |
| 170 | Value |
| 171 | Iterate() Iterator // must be followed by call to Iterator.Done |
| 172 | } |
| 173 | |
| 174 | // A Sequence is a sequence of values of known length. |
| 175 | type Sequence interface { |
| 176 | Iterable |
| 177 | Len() int |
| 178 | } |
| 179 | |
| 180 | var ( |
| 181 | _ Sequence = (*Dict)(nil) |
| 182 | _ Sequence = (*Set)(nil) |
| 183 | ) |
| 184 | |
| 185 | // An Indexable is a sequence of known length that supports efficient random access. |
| 186 | // It is not necessarily iterable. |
| 187 | type Indexable interface { |
| 188 | Value |
| 189 | Index(i int) Value // requires 0 <= i < Len() |
| 190 | Len() int |
| 191 | } |
| 192 | |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 193 | // A Sliceable is a sequence that can be cut into pieces with the slice operator (x[i:j:step]). |
| 194 | // |
| 195 | // All native indexable objects are sliceable. |
| 196 | // This is a separate interface for backwards-compatibility. |
| 197 | type Sliceable interface { |
| 198 | Indexable |
| 199 | // For positive strides (step > 0), 0 <= start <= end <= n. |
| 200 | // For negative strides (step < 0), -1 <= end <= start < n. |
Josh Bleecher Snyder | d5c553a | 2019-01-16 12:51:06 -0800 | [diff] [blame] | 201 | // The caller must ensure that the start and end indices are valid |
| 202 | // and that step is non-zero. |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 203 | Slice(start, end, step int) Value |
| 204 | } |
| 205 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 206 | // A HasSetIndex is an Indexable value whose elements may be assigned (x[i] = y). |
| 207 | // |
| 208 | // The implementation should not add Len to a negative index as the |
| 209 | // evaluator does this before the call. |
| 210 | type HasSetIndex interface { |
| 211 | Indexable |
| 212 | SetIndex(index int, v Value) error |
| 213 | } |
| 214 | |
| 215 | var ( |
| 216 | _ HasSetIndex = (*List)(nil) |
| 217 | _ Indexable = Tuple(nil) |
| 218 | _ Indexable = String("") |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 219 | _ Sliceable = Tuple(nil) |
| 220 | _ Sliceable = String("") |
| 221 | _ Sliceable = (*List)(nil) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 222 | ) |
| 223 | |
| 224 | // An Iterator provides a sequence of values to the caller. |
| 225 | // |
| 226 | // The caller must call Done when the iterator is no longer needed. |
| 227 | // Operations that modify a sequence will fail if it has active iterators. |
| 228 | // |
| 229 | // Example usage: |
| 230 | // |
| 231 | // iter := iterable.Iterator() |
| 232 | // defer iter.Done() |
| 233 | // var x Value |
| 234 | // for iter.Next(&x) { |
| 235 | // ... |
| 236 | // } |
| 237 | // |
| 238 | type Iterator interface { |
| 239 | // If the iterator is exhausted, Next returns false. |
| 240 | // Otherwise it sets *p to the current element of the sequence, |
| 241 | // advances the iterator, and returns true. |
| 242 | Next(p *Value) bool |
| 243 | Done() |
| 244 | } |
| 245 | |
jmillikin-stripe | 3ccab94 | 2018-10-05 07:09:12 -0700 | [diff] [blame] | 246 | // A Mapping is a mapping from keys to values, such as a dictionary. |
alandonovan | 1ed6497 | 2019-01-04 13:04:39 -0500 | [diff] [blame] | 247 | // |
| 248 | // If a type satisfies both Mapping and Iterable, the iterator yields |
| 249 | // the keys of the mapping. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 250 | type Mapping interface { |
| 251 | Value |
| 252 | // Get returns the value corresponding to the specified key, |
| 253 | // or !found if the mapping does not contain the key. |
alandonovan | 7f065b6 | 2018-03-19 14:58:49 -0400 | [diff] [blame] | 254 | // |
| 255 | // Get also defines the behavior of "v in mapping". |
| 256 | // The 'in' operator reports the 'found' component, ignoring errors. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 257 | Get(Value) (v Value, found bool, err error) |
| 258 | } |
| 259 | |
alandonovan | 1ed6497 | 2019-01-04 13:04:39 -0500 | [diff] [blame] | 260 | // An IterableMapping is a mapping that supports key enumeration. |
| 261 | type IterableMapping interface { |
| 262 | Mapping |
| 263 | Iterate() Iterator // see Iterable interface |
| 264 | Items() []Tuple // a new slice containing all key/value pairs |
| 265 | } |
| 266 | |
| 267 | var _ IterableMapping = (*Dict)(nil) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 268 | |
jmillikin-stripe | 3ccab94 | 2018-10-05 07:09:12 -0700 | [diff] [blame] | 269 | // A HasSetKey supports map update using x[k]=v syntax, like a dictionary. |
| 270 | type HasSetKey interface { |
| 271 | Mapping |
| 272 | SetKey(k, v Value) error |
| 273 | } |
| 274 | |
| 275 | var _ HasSetKey = (*Dict)(nil) |
| 276 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 277 | // A HasBinary value may be used as either operand of these binary operators: |
alandonovan | 93b8d14 | 2019-01-06 11:57:29 -0500 | [diff] [blame] | 278 | // + - * / // % in not in | & ^ << >> |
| 279 | // |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 280 | // The Side argument indicates whether the receiver is the left or right operand. |
| 281 | // |
| 282 | // An implementation may decline to handle an operation by returning (nil, nil). |
| 283 | // For this reason, clients should always call the standalone Binary(op, x, y) |
| 284 | // function rather than calling the method directly. |
| 285 | type HasBinary interface { |
| 286 | Value |
| 287 | Binary(op syntax.Token, y Value, side Side) (Value, error) |
| 288 | } |
| 289 | |
| 290 | type Side bool |
| 291 | |
| 292 | const ( |
| 293 | Left Side = false |
| 294 | Right Side = true |
| 295 | ) |
| 296 | |
alandonovan | 58f9101 | 2019-01-03 16:32:47 -0500 | [diff] [blame] | 297 | // A HasUnary value may be used as the operand of these unary operators: |
| 298 | // + - ~ |
| 299 | // |
| 300 | // An implementation may decline to handle an operation by returning (nil, nil). |
| 301 | // For this reason, clients should always call the standalone Unary(op, x) |
| 302 | // function rather than calling the method directly. |
| 303 | type HasUnary interface { |
| 304 | Value |
| 305 | Unary(op syntax.Token) (Value, error) |
| 306 | } |
| 307 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 308 | // A HasAttrs value has fields or methods that may be read by a dot expression (y = x.f). |
| 309 | // Attribute names may be listed using the built-in 'dir' function. |
| 310 | // |
| 311 | // For implementation convenience, a result of (nil, nil) from Attr is |
| 312 | // interpreted as a "no such field or method" error. Implementations are |
| 313 | // free to return a more precise error. |
| 314 | type HasAttrs interface { |
| 315 | Value |
| 316 | Attr(name string) (Value, error) // returns (nil, nil) if attribute not present |
| 317 | AttrNames() []string // callers must not modify the result. |
| 318 | } |
| 319 | |
| 320 | var ( |
| 321 | _ HasAttrs = String("") |
| 322 | _ HasAttrs = new(List) |
| 323 | _ HasAttrs = new(Dict) |
| 324 | _ HasAttrs = new(Set) |
| 325 | ) |
| 326 | |
| 327 | // A HasSetField value has fields that may be written by a dot expression (x.f = y). |
Alan Donovan | 557c1f1 | 2019-02-04 16:12:53 -0500 | [diff] [blame] | 328 | // |
alandonovan | 6afa1bb | 2019-02-06 17:49:05 -0500 | [diff] [blame] | 329 | // An implementation of SetField may return a NoSuchAttrError, |
| 330 | // in which case the runtime may augment the error message to |
| 331 | // warn of possible misspelling. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 332 | type HasSetField interface { |
| 333 | HasAttrs |
| 334 | SetField(name string, val Value) error |
| 335 | } |
| 336 | |
alandonovan | 6afa1bb | 2019-02-06 17:49:05 -0500 | [diff] [blame] | 337 | // A NoSuchAttrError may be returned by an implementation of |
| 338 | // HasAttrs.Attr or HasSetField.SetField to indicate that no such field |
| 339 | // exists. In that case the runtime may augment the error message to |
| 340 | // warn of possible misspelling. |
| 341 | type NoSuchAttrError string |
| 342 | |
| 343 | func (e NoSuchAttrError) Error() string { return string(e) } |
Alan Donovan | 557c1f1 | 2019-02-04 16:12:53 -0500 | [diff] [blame] | 344 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 345 | // NoneType is the type of None. Its only legal value is None. |
| 346 | // (We represent it as a number, not struct{}, so that None may be constant.) |
| 347 | type NoneType byte |
| 348 | |
| 349 | const None = NoneType(0) |
| 350 | |
| 351 | func (NoneType) String() string { return "None" } |
| 352 | func (NoneType) Type() string { return "NoneType" } |
| 353 | func (NoneType) Freeze() {} // immutable |
| 354 | func (NoneType) Truth() Bool { return False } |
| 355 | func (NoneType) Hash() (uint32, error) { return 0, nil } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 356 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 357 | // Bool is the type of a Starlark bool. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 358 | type Bool bool |
| 359 | |
| 360 | const ( |
| 361 | False Bool = false |
| 362 | True Bool = true |
| 363 | ) |
| 364 | |
| 365 | func (b Bool) String() string { |
| 366 | if b { |
| 367 | return "True" |
| 368 | } else { |
| 369 | return "False" |
| 370 | } |
| 371 | } |
| 372 | func (b Bool) Type() string { return "bool" } |
| 373 | func (b Bool) Freeze() {} // immutable |
| 374 | func (b Bool) Truth() Bool { return b } |
| 375 | func (b Bool) Hash() (uint32, error) { return uint32(b2i(bool(b))), nil } |
| 376 | func (x Bool) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 377 | y := y_.(Bool) |
| 378 | return threeway(op, b2i(bool(x))-b2i(bool(y))), nil |
| 379 | } |
| 380 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 381 | // Float is the type of a Starlark float. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 382 | type Float float64 |
| 383 | |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 384 | func (f Float) String() string { |
| 385 | var buf strings.Builder |
| 386 | f.format(&buf, 'g') |
| 387 | return buf.String() |
| 388 | } |
| 389 | |
| 390 | func (f Float) format(buf *strings.Builder, conv byte) { |
| 391 | ff := float64(f) |
| 392 | if !isFinite(ff) { |
| 393 | if math.IsInf(ff, +1) { |
| 394 | buf.WriteString("+inf") |
| 395 | } else if math.IsInf(ff, -1) { |
| 396 | buf.WriteString("-inf") |
| 397 | } else { |
| 398 | buf.WriteString("nan") |
| 399 | } |
| 400 | return |
| 401 | } |
| 402 | |
| 403 | // %g is the default format used by str. |
| 404 | // It uses the minimum precision to avoid ambiguity, |
| 405 | // and always includes a '.' or an 'e' so that the value |
| 406 | // is self-evidently a float, not an int. |
| 407 | if conv == 'g' || conv == 'G' { |
| 408 | s := strconv.FormatFloat(ff, conv, -1, 64) |
| 409 | buf.WriteString(s) |
| 410 | // Ensure result always has a decimal point if no exponent. |
| 411 | // "123" -> "123.0" |
| 412 | if strings.IndexByte(s, conv-'g'+'e') < 0 && strings.IndexByte(s, '.') < 0 { |
| 413 | buf.WriteString(".0") |
| 414 | } |
| 415 | return |
| 416 | } |
| 417 | |
| 418 | // %[eEfF] use 6-digit precision |
| 419 | buf.WriteString(strconv.FormatFloat(ff, conv, 6, 64)) |
| 420 | } |
| 421 | |
| 422 | func (f Float) Type() string { return "float" } |
| 423 | func (f Float) Freeze() {} // immutable |
| 424 | func (f Float) Truth() Bool { return f != 0.0 } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 425 | func (f Float) Hash() (uint32, error) { |
| 426 | // Equal float and int values must yield the same hash. |
| 427 | // TODO(adonovan): opt: if f is non-integral, and thus not equal |
| 428 | // to any Int, we can avoid the Int conversion and use a cheaper hash. |
| 429 | if isFinite(float64(f)) { |
| 430 | return finiteFloatToInt(f).Hash() |
| 431 | } |
| 432 | return 1618033, nil // NaN, +/-Inf |
| 433 | } |
| 434 | |
| 435 | func floor(f Float) Float { return Float(math.Floor(float64(f))) } |
| 436 | |
| 437 | // isFinite reports whether f represents a finite rational value. |
| 438 | // It is equivalent to !math.IsNan(f) && !math.IsInf(f, 0). |
| 439 | func isFinite(f float64) bool { |
| 440 | return math.Abs(f) <= math.MaxFloat64 |
| 441 | } |
| 442 | |
| 443 | func (x Float) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 444 | y := y_.(Float) |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 445 | return threeway(op, floatCmp(x, y)), nil |
| 446 | } |
| 447 | |
| 448 | // floatCmp performs a three-valued comparison on floats, |
| 449 | // which are totally ordered with NaN > +Inf. |
| 450 | func floatCmp(x, y Float) int { |
| 451 | if x > y { |
| 452 | return +1 |
| 453 | } else if x < y { |
| 454 | return -1 |
| 455 | } else if x == y { |
| 456 | return 0 |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 457 | } |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 458 | |
| 459 | // At least one operand is NaN. |
| 460 | if x == x { |
| 461 | return -1 // y is NaN |
| 462 | } else if y == y { |
| 463 | return +1 // x is NaN |
| 464 | } |
| 465 | return 0 // both NaN |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | func (f Float) rational() *big.Rat { return new(big.Rat).SetFloat64(float64(f)) } |
| 469 | |
| 470 | // AsFloat returns the float64 value closest to x. |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 471 | // The f result is undefined if x is not a float or Int. |
| 472 | // The result may be infinite if x is a very large Int. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 473 | func AsFloat(x Value) (f float64, ok bool) { |
| 474 | switch x := x.(type) { |
| 475 | case Float: |
| 476 | return float64(x), true |
| 477 | case Int: |
| 478 | return float64(x.Float()), true |
| 479 | } |
| 480 | return 0, false |
| 481 | } |
| 482 | |
alandonovan | 227f4aa | 2020-10-06 17:39:52 -0400 | [diff] [blame] | 483 | func (x Float) Mod(y Float) Float { |
| 484 | z := Float(math.Mod(float64(x), float64(y))) |
| 485 | if (x < 0) != (y < 0) && z != 0 { |
| 486 | z += y |
| 487 | } |
| 488 | return z |
| 489 | } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 490 | |
alandonovan | 58f9101 | 2019-01-03 16:32:47 -0500 | [diff] [blame] | 491 | // Unary implements the operations +float and -float. |
| 492 | func (f Float) Unary(op syntax.Token) (Value, error) { |
| 493 | switch op { |
| 494 | case syntax.MINUS: |
| 495 | return -f, nil |
| 496 | case syntax.PLUS: |
| 497 | return +f, nil |
| 498 | } |
| 499 | return nil, nil |
| 500 | } |
| 501 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 502 | // String is the type of a Starlark text string. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 503 | // |
alandonovan | a21eb0f | 2018-06-25 15:30:48 -0400 | [diff] [blame] | 504 | // A String encapsulates an an immutable sequence of bytes, |
| 505 | // but strings are not directly iterable. Instead, iterate |
| 506 | // over the result of calling one of these four methods: |
| 507 | // codepoints, codepoint_ords, elems, elem_ords. |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 508 | // |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 509 | // Strings typically contain text; use Bytes for binary strings. |
| 510 | // The Starlark spec defines text strings as sequences of UTF-k |
| 511 | // codes that encode Unicode code points. In this Go implementation, |
| 512 | // k=8, whereas in a Java implementation, k=16. For portability, |
| 513 | // operations on strings should aim to avoid assumptions about |
| 514 | // the value of k. |
| 515 | // |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 516 | // Warning: the contract of the Value interface's String method is that |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 517 | // it returns the value printed in Starlark notation, |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 518 | // so s.String() or fmt.Sprintf("%s", s) returns a quoted string. |
| 519 | // Use string(s) or s.GoString() or fmt.Sprintf("%#v", s) to obtain the raw contents |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 520 | // of a Starlark string as a Go string. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 521 | type String string |
| 522 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 523 | func (s String) String() string { return syntax.Quote(string(s), false) } |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 524 | func (s String) GoString() string { return string(s) } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 525 | func (s String) Type() string { return "string" } |
| 526 | func (s String) Freeze() {} // immutable |
| 527 | func (s String) Truth() Bool { return len(s) > 0 } |
| 528 | func (s String) Hash() (uint32, error) { return hashString(string(s)), nil } |
| 529 | func (s String) Len() int { return len(s) } // bytes |
| 530 | func (s String) Index(i int) Value { return s[i : i+1] } |
| 531 | |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 532 | func (s String) Slice(start, end, step int) Value { |
| 533 | if step == 1 { |
alandonovan | 5c7d5aa | 2018-12-03 17:05:15 -0500 | [diff] [blame] | 534 | return s[start:end] |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | sign := signum(step) |
| 538 | var str []byte |
| 539 | for i := start; signum(end-i) == sign; i += step { |
| 540 | str = append(str, s[i]) |
| 541 | } |
| 542 | return String(str) |
| 543 | } |
| 544 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 545 | func (s String) Attr(name string) (Value, error) { return builtinAttr(s, name, stringMethods) } |
| 546 | func (s String) AttrNames() []string { return builtinAttrNames(stringMethods) } |
| 547 | |
| 548 | func (x String) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 549 | y := y_.(String) |
| 550 | return threeway(op, strings.Compare(string(x), string(y))), nil |
| 551 | } |
| 552 | |
| 553 | func AsString(x Value) (string, bool) { v, ok := x.(String); return string(v), ok } |
| 554 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 555 | // A stringElems is an iterable whose iterator yields a sequence of |
| 556 | // elements (bytes), either numerically or as successive substrings. |
| 557 | // It is an indexable sequence. |
| 558 | type stringElems struct { |
| 559 | s String |
| 560 | ords bool |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 561 | } |
| 562 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 563 | var ( |
| 564 | _ Iterable = (*stringElems)(nil) |
| 565 | _ Indexable = (*stringElems)(nil) |
| 566 | ) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 567 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 568 | func (si stringElems) String() string { |
alandonovan | 0569d1c | 2018-03-29 14:47:42 -0400 | [diff] [blame] | 569 | if si.ords { |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 570 | return si.s.String() + ".elem_ords()" |
alandonovan | 0569d1c | 2018-03-29 14:47:42 -0400 | [diff] [blame] | 571 | } else { |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 572 | return si.s.String() + ".elems()" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 573 | } |
| 574 | } |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 575 | func (si stringElems) Type() string { return "string.elems" } |
| 576 | func (si stringElems) Freeze() {} // immutable |
| 577 | func (si stringElems) Truth() Bool { return True } |
| 578 | func (si stringElems) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", si.Type()) } |
| 579 | func (si stringElems) Iterate() Iterator { return &stringElemsIterator{si, 0} } |
| 580 | func (si stringElems) Len() int { return len(si.s) } |
| 581 | func (si stringElems) Index(i int) Value { |
| 582 | if si.ords { |
| 583 | return MakeInt(int(si.s[i])) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 584 | } else { |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 585 | // TODO(adonovan): opt: preallocate canonical 1-byte strings |
| 586 | // to avoid interface allocation. |
| 587 | return si.s[i : i+1] |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 588 | } |
| 589 | } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 590 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 591 | type stringElemsIterator struct { |
| 592 | si stringElems |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 593 | i int |
| 594 | } |
| 595 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 596 | func (it *stringElemsIterator) Next(p *Value) bool { |
| 597 | if it.i == len(it.si.s) { |
| 598 | return false |
| 599 | } |
| 600 | *p = it.si.Index(it.i) |
| 601 | it.i++ |
| 602 | return true |
| 603 | } |
| 604 | |
| 605 | func (*stringElemsIterator) Done() {} |
| 606 | |
| 607 | // A stringCodepoints is an iterable whose iterator yields a sequence of |
| 608 | // Unicode code points, either numerically or as successive substrings. |
| 609 | // It is not indexable. |
| 610 | type stringCodepoints struct { |
| 611 | s String |
| 612 | ords bool |
| 613 | } |
| 614 | |
| 615 | var _ Iterable = (*stringCodepoints)(nil) |
| 616 | |
| 617 | func (si stringCodepoints) String() string { |
| 618 | if si.ords { |
| 619 | return si.s.String() + ".codepoint_ords()" |
| 620 | } else { |
| 621 | return si.s.String() + ".codepoints()" |
| 622 | } |
| 623 | } |
| 624 | func (si stringCodepoints) Type() string { return "string.codepoints" } |
| 625 | func (si stringCodepoints) Freeze() {} // immutable |
| 626 | func (si stringCodepoints) Truth() Bool { return True } |
| 627 | func (si stringCodepoints) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", si.Type()) } |
| 628 | func (si stringCodepoints) Iterate() Iterator { return &stringCodepointsIterator{si, 0} } |
| 629 | |
| 630 | type stringCodepointsIterator struct { |
| 631 | si stringCodepoints |
| 632 | i int |
| 633 | } |
| 634 | |
| 635 | func (it *stringCodepointsIterator) Next(p *Value) bool { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 636 | s := it.si.s[it.i:] |
| 637 | if s == "" { |
| 638 | return false |
| 639 | } |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 640 | r, sz := utf8.DecodeRuneInString(string(s)) |
| 641 | if !it.si.ords { |
| 642 | if r == utf8.RuneError { |
| 643 | *p = String(r) |
| 644 | } else { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 645 | *p = s[:sz] |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 646 | } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 647 | } else { |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 648 | *p = MakeInt(int(r)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 649 | } |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 650 | it.i += sz |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 651 | return true |
| 652 | } |
| 653 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 654 | func (*stringCodepointsIterator) Done() {} |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 655 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 656 | // A Function is a function defined by a Starlark def statement or lambda expression. |
| 657 | // The initialization behavior of a Starlark module is also represented by a Function. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 658 | type Function struct { |
alandonovan | 8c4023c | 2018-04-02 13:08:46 -0400 | [diff] [blame] | 659 | funcode *compile.Funcode |
alandonovan | 3ee1685 | 2019-05-28 15:56:13 -0400 | [diff] [blame] | 660 | module *module |
alandonovan | 8c4023c | 2018-04-02 13:08:46 -0400 | [diff] [blame] | 661 | defaults Tuple |
| 662 | freevars Tuple |
alandonovan | 3ee1685 | 2019-05-28 15:56:13 -0400 | [diff] [blame] | 663 | } |
alandonovan | 8c4023c | 2018-04-02 13:08:46 -0400 | [diff] [blame] | 664 | |
alandonovan | 3ee1685 | 2019-05-28 15:56:13 -0400 | [diff] [blame] | 665 | // A module is the dynamic counterpart to a Program. |
| 666 | // All functions in the same program share a module. |
| 667 | type module struct { |
| 668 | program *compile.Program |
alandonovan | 8c4023c | 2018-04-02 13:08:46 -0400 | [diff] [blame] | 669 | predeclared StringDict |
| 670 | globals []Value |
| 671 | constants []Value |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 672 | } |
| 673 | |
alandonovan | 3ee1685 | 2019-05-28 15:56:13 -0400 | [diff] [blame] | 674 | // makeGlobalDict returns a new, unfrozen StringDict containing all global |
| 675 | // variables so far defined in the module. |
| 676 | func (m *module) makeGlobalDict() StringDict { |
| 677 | r := make(StringDict, len(m.program.Globals)) |
| 678 | for i, id := range m.program.Globals { |
| 679 | if v := m.globals[i]; v != nil { |
| 680 | r[id.Name] = v |
| 681 | } |
| 682 | } |
| 683 | return r |
| 684 | } |
| 685 | |
alandonovan | 93f3e0c | 2018-03-30 10:42:28 -0400 | [diff] [blame] | 686 | func (fn *Function) Name() string { return fn.funcode.Name } // "lambda" for anonymous functions |
Alessandro Arzilli | 3b628ff | 2018-12-05 15:04:35 +0100 | [diff] [blame] | 687 | func (fn *Function) Doc() string { return fn.funcode.Doc } |
alandonovan | 93f3e0c | 2018-03-30 10:42:28 -0400 | [diff] [blame] | 688 | func (fn *Function) Hash() (uint32, error) { return hashString(fn.funcode.Name), nil } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 689 | func (fn *Function) Freeze() { fn.defaults.Freeze(); fn.freevars.Freeze() } |
| 690 | func (fn *Function) String() string { return toString(fn) } |
| 691 | func (fn *Function) Type() string { return "function" } |
| 692 | func (fn *Function) Truth() Bool { return true } |
| 693 | |
alandonovan | 93f3e0c | 2018-03-30 10:42:28 -0400 | [diff] [blame] | 694 | // Globals returns a new, unfrozen StringDict containing all global |
| 695 | // variables so far defined in the function's module. |
alandonovan | 3ee1685 | 2019-05-28 15:56:13 -0400 | [diff] [blame] | 696 | func (fn *Function) Globals() StringDict { return fn.module.makeGlobalDict() } |
alandonovan | 93f3e0c | 2018-03-30 10:42:28 -0400 | [diff] [blame] | 697 | |
| 698 | func (fn *Function) Position() syntax.Position { return fn.funcode.Pos } |
| 699 | func (fn *Function) NumParams() int { return fn.funcode.NumParams } |
Alan Donovan | 5215385 | 2019-02-13 19:18:15 -0500 | [diff] [blame] | 700 | func (fn *Function) NumKwonlyParams() int { return fn.funcode.NumKwonlyParams } |
alandonovan | 9b05555 | 2018-11-02 14:29:51 -0400 | [diff] [blame] | 701 | |
| 702 | // Param returns the name and position of the ith parameter, |
| 703 | // where 0 <= i < NumParams(). |
Alan Donovan | 5215385 | 2019-02-13 19:18:15 -0500 | [diff] [blame] | 704 | // The *args and **kwargs parameters are at the end |
| 705 | // even if there were optional parameters after *args. |
alandonovan | 93f3e0c | 2018-03-30 10:42:28 -0400 | [diff] [blame] | 706 | func (fn *Function) Param(i int) (string, syntax.Position) { |
alandonovan | fc7a7f4 | 2019-07-16 22:31:58 -0400 | [diff] [blame] | 707 | if i >= fn.NumParams() { |
| 708 | panic(i) |
| 709 | } |
alandonovan | 93f3e0c | 2018-03-30 10:42:28 -0400 | [diff] [blame] | 710 | id := fn.funcode.Locals[i] |
| 711 | return id.Name, id.Pos |
| 712 | } |
| 713 | func (fn *Function) HasVarargs() bool { return fn.funcode.HasVarargs } |
| 714 | func (fn *Function) HasKwargs() bool { return fn.funcode.HasKwargs } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 715 | |
| 716 | // A Builtin is a function implemented in Go. |
| 717 | type Builtin struct { |
| 718 | name string |
| 719 | fn func(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, error) |
| 720 | recv Value // for bound methods (e.g. "".startswith) |
| 721 | } |
| 722 | |
| 723 | func (b *Builtin) Name() string { return b.name } |
| 724 | func (b *Builtin) Freeze() { |
| 725 | if b.recv != nil { |
| 726 | b.recv.Freeze() |
| 727 | } |
| 728 | } |
| 729 | func (b *Builtin) Hash() (uint32, error) { |
| 730 | h := hashString(b.name) |
| 731 | if b.recv != nil { |
| 732 | h ^= 5521 |
| 733 | } |
| 734 | return h, nil |
| 735 | } |
| 736 | func (b *Builtin) Receiver() Value { return b.recv } |
| 737 | func (b *Builtin) String() string { return toString(b) } |
alandonovan | 4cbd896 | 2017-10-19 13:18:36 -0400 | [diff] [blame] | 738 | func (b *Builtin) Type() string { return "builtin_function_or_method" } |
alandonovan | aeec83f | 2018-10-22 13:24:13 -0400 | [diff] [blame] | 739 | func (b *Builtin) CallInternal(thread *Thread, args Tuple, kwargs []Tuple) (Value, error) { |
| 740 | return b.fn(thread, b, args, kwargs) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 741 | } |
| 742 | func (b *Builtin) Truth() Bool { return true } |
| 743 | |
alandonovan | 4cbd896 | 2017-10-19 13:18:36 -0400 | [diff] [blame] | 744 | // NewBuiltin returns a new 'builtin_function_or_method' value with the specified name |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 745 | // and implementation. It compares unequal with all other values. |
| 746 | func NewBuiltin(name string, fn func(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, error)) *Builtin { |
| 747 | return &Builtin{name: name, fn: fn} |
| 748 | } |
| 749 | |
| 750 | // BindReceiver returns a new Builtin value representing a method |
| 751 | // closure, that is, a built-in function bound to a receiver value. |
| 752 | // |
alandonovan | 4cbd896 | 2017-10-19 13:18:36 -0400 | [diff] [blame] | 753 | // In the example below, the value of f is the string.index |
| 754 | // built-in method bound to the receiver value "abc": |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 755 | // |
| 756 | // f = "abc".index; f("a"); f("b") |
| 757 | // |
| 758 | // In the common case, the receiver is bound only during the call, |
| 759 | // but this still results in the creation of a temporary method closure: |
| 760 | // |
| 761 | // "abc".index("a") |
| 762 | // |
| 763 | func (b *Builtin) BindReceiver(recv Value) *Builtin { |
| 764 | return &Builtin{name: b.name, fn: b.fn, recv: recv} |
| 765 | } |
| 766 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 767 | // A *Dict represents a Starlark dictionary. |
alandonovan | f83458d | 2019-04-02 20:34:11 -0400 | [diff] [blame] | 768 | // The zero value of Dict is a valid empty dictionary. |
| 769 | // If you know the exact final number of entries, |
| 770 | // it is more efficient to call NewDict. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 771 | type Dict struct { |
| 772 | ht hashtable |
| 773 | } |
| 774 | |
alandonovan | f83458d | 2019-04-02 20:34:11 -0400 | [diff] [blame] | 775 | // NewDict returns a set with initial space for |
| 776 | // at least size insertions before rehashing. |
| 777 | func NewDict(size int) *Dict { |
| 778 | dict := new(Dict) |
| 779 | dict.ht.init(size) |
| 780 | return dict |
| 781 | } |
| 782 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 783 | func (d *Dict) Clear() error { return d.ht.clear() } |
| 784 | func (d *Dict) Delete(k Value) (v Value, found bool, err error) { return d.ht.delete(k) } |
| 785 | func (d *Dict) Get(k Value) (v Value, found bool, err error) { return d.ht.lookup(k) } |
| 786 | func (d *Dict) Items() []Tuple { return d.ht.items() } |
| 787 | func (d *Dict) Keys() []Value { return d.ht.keys() } |
| 788 | func (d *Dict) Len() int { return int(d.ht.len) } |
| 789 | func (d *Dict) Iterate() Iterator { return d.ht.iterate() } |
jmillikin-stripe | 3ccab94 | 2018-10-05 07:09:12 -0700 | [diff] [blame] | 790 | func (d *Dict) SetKey(k, v Value) error { return d.ht.insert(k, v) } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 791 | func (d *Dict) String() string { return toString(d) } |
| 792 | func (d *Dict) Type() string { return "dict" } |
| 793 | func (d *Dict) Freeze() { d.ht.freeze() } |
| 794 | func (d *Dict) Truth() Bool { return d.Len() > 0 } |
| 795 | func (d *Dict) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable type: dict") } |
| 796 | |
| 797 | func (d *Dict) Attr(name string) (Value, error) { return builtinAttr(d, name, dictMethods) } |
| 798 | func (d *Dict) AttrNames() []string { return builtinAttrNames(dictMethods) } |
| 799 | |
| 800 | func (x *Dict) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 801 | y := y_.(*Dict) |
| 802 | switch op { |
| 803 | case syntax.EQL: |
| 804 | ok, err := dictsEqual(x, y, depth) |
| 805 | return ok, err |
| 806 | case syntax.NEQ: |
| 807 | ok, err := dictsEqual(x, y, depth) |
| 808 | return !ok, err |
| 809 | default: |
| 810 | return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type()) |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | func dictsEqual(x, y *Dict, depth int) (bool, error) { |
| 815 | if x.Len() != y.Len() { |
| 816 | return false, nil |
| 817 | } |
| 818 | for _, xitem := range x.Items() { |
| 819 | key, xval := xitem[0], xitem[1] |
| 820 | |
| 821 | if yval, found, _ := y.Get(key); !found { |
| 822 | return false, nil |
| 823 | } else if eq, err := EqualDepth(xval, yval, depth-1); err != nil { |
| 824 | return false, err |
| 825 | } else if !eq { |
| 826 | return false, nil |
| 827 | } |
| 828 | } |
| 829 | return true, nil |
| 830 | } |
| 831 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 832 | // A *List represents a Starlark list value. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 833 | type List struct { |
| 834 | elems []Value |
| 835 | frozen bool |
| 836 | itercount uint32 // number of active iterators (ignored if frozen) |
| 837 | } |
| 838 | |
| 839 | // NewList returns a list containing the specified elements. |
| 840 | // Callers should not subsequently modify elems. |
| 841 | func NewList(elems []Value) *List { return &List{elems: elems} } |
| 842 | |
| 843 | func (l *List) Freeze() { |
| 844 | if !l.frozen { |
| 845 | l.frozen = true |
| 846 | for _, elem := range l.elems { |
| 847 | elem.Freeze() |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | // checkMutable reports an error if the list should not be mutated. |
| 853 | // verb+" list" should describe the operation. |
alandonovan | 7c0e5a3 | 2018-11-02 15:38:05 -0400 | [diff] [blame] | 854 | func (l *List) checkMutable(verb string) error { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 855 | if l.frozen { |
| 856 | return fmt.Errorf("cannot %s frozen list", verb) |
| 857 | } |
alandonovan | 7c0e5a3 | 2018-11-02 15:38:05 -0400 | [diff] [blame] | 858 | if l.itercount > 0 { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 859 | return fmt.Errorf("cannot %s list during iteration", verb) |
| 860 | } |
| 861 | return nil |
| 862 | } |
| 863 | |
| 864 | func (l *List) String() string { return toString(l) } |
| 865 | func (l *List) Type() string { return "list" } |
| 866 | func (l *List) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable type: list") } |
| 867 | func (l *List) Truth() Bool { return l.Len() > 0 } |
| 868 | func (l *List) Len() int { return len(l.elems) } |
| 869 | func (l *List) Index(i int) Value { return l.elems[i] } |
| 870 | |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 871 | func (l *List) Slice(start, end, step int) Value { |
| 872 | if step == 1 { |
| 873 | elems := append([]Value{}, l.elems[start:end]...) |
| 874 | return NewList(elems) |
| 875 | } |
| 876 | |
| 877 | sign := signum(step) |
| 878 | var list []Value |
| 879 | for i := start; signum(end-i) == sign; i += step { |
| 880 | list = append(list, l.elems[i]) |
| 881 | } |
| 882 | return NewList(list) |
| 883 | } |
| 884 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 885 | func (l *List) Attr(name string) (Value, error) { return builtinAttr(l, name, listMethods) } |
| 886 | func (l *List) AttrNames() []string { return builtinAttrNames(listMethods) } |
| 887 | |
| 888 | func (l *List) Iterate() Iterator { |
| 889 | if !l.frozen { |
| 890 | l.itercount++ |
| 891 | } |
| 892 | return &listIterator{l: l} |
| 893 | } |
| 894 | |
| 895 | func (x *List) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 896 | y := y_.(*List) |
| 897 | // It's tempting to check x == y as an optimization here, |
| 898 | // but wrong because a list containing NaN is not equal to itself. |
| 899 | return sliceCompare(op, x.elems, y.elems, depth) |
| 900 | } |
| 901 | |
| 902 | func sliceCompare(op syntax.Token, x, y []Value, depth int) (bool, error) { |
| 903 | // Fast path: check length. |
| 904 | if len(x) != len(y) && (op == syntax.EQL || op == syntax.NEQ) { |
| 905 | return op == syntax.NEQ, nil |
| 906 | } |
| 907 | |
| 908 | // Find first element that is not equal in both lists. |
| 909 | for i := 0; i < len(x) && i < len(y); i++ { |
| 910 | if eq, err := EqualDepth(x[i], y[i], depth-1); err != nil { |
| 911 | return false, err |
| 912 | } else if !eq { |
| 913 | switch op { |
| 914 | case syntax.EQL: |
| 915 | return false, nil |
| 916 | case syntax.NEQ: |
| 917 | return true, nil |
| 918 | default: |
| 919 | return CompareDepth(op, x[i], y[i], depth-1) |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return threeway(op, len(x)-len(y)), nil |
| 925 | } |
| 926 | |
| 927 | type listIterator struct { |
| 928 | l *List |
| 929 | i int |
| 930 | } |
| 931 | |
| 932 | func (it *listIterator) Next(p *Value) bool { |
| 933 | if it.i < it.l.Len() { |
| 934 | *p = it.l.elems[it.i] |
| 935 | it.i++ |
| 936 | return true |
| 937 | } |
| 938 | return false |
| 939 | } |
| 940 | |
| 941 | func (it *listIterator) Done() { |
| 942 | if !it.l.frozen { |
| 943 | it.l.itercount-- |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | func (l *List) SetIndex(i int, v Value) error { |
alandonovan | 7c0e5a3 | 2018-11-02 15:38:05 -0400 | [diff] [blame] | 948 | if err := l.checkMutable("assign to element of"); err != nil { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 949 | return err |
| 950 | } |
| 951 | l.elems[i] = v |
| 952 | return nil |
| 953 | } |
| 954 | |
| 955 | func (l *List) Append(v Value) error { |
alandonovan | 7c0e5a3 | 2018-11-02 15:38:05 -0400 | [diff] [blame] | 956 | if err := l.checkMutable("append to"); err != nil { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 957 | return err |
| 958 | } |
| 959 | l.elems = append(l.elems, v) |
| 960 | return nil |
| 961 | } |
| 962 | |
| 963 | func (l *List) Clear() error { |
alandonovan | 7c0e5a3 | 2018-11-02 15:38:05 -0400 | [diff] [blame] | 964 | if err := l.checkMutable("clear"); err != nil { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 965 | return err |
| 966 | } |
| 967 | for i := range l.elems { |
| 968 | l.elems[i] = nil // aid GC |
| 969 | } |
| 970 | l.elems = l.elems[:0] |
| 971 | return nil |
| 972 | } |
| 973 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 974 | // A Tuple represents a Starlark tuple value. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 975 | type Tuple []Value |
| 976 | |
| 977 | func (t Tuple) Len() int { return len(t) } |
| 978 | func (t Tuple) Index(i int) Value { return t[i] } |
Nick Santos | d3cd736 | 2018-05-18 12:58:53 -0400 | [diff] [blame] | 979 | |
| 980 | func (t Tuple) Slice(start, end, step int) Value { |
| 981 | if step == 1 { |
| 982 | return t[start:end] |
| 983 | } |
| 984 | |
| 985 | sign := signum(step) |
| 986 | var tuple Tuple |
| 987 | for i := start; signum(end-i) == sign; i += step { |
| 988 | tuple = append(tuple, t[i]) |
| 989 | } |
| 990 | return tuple |
| 991 | } |
| 992 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 993 | func (t Tuple) Iterate() Iterator { return &tupleIterator{elems: t} } |
| 994 | func (t Tuple) Freeze() { |
| 995 | for _, elem := range t { |
| 996 | elem.Freeze() |
| 997 | } |
| 998 | } |
| 999 | func (t Tuple) String() string { return toString(t) } |
| 1000 | func (t Tuple) Type() string { return "tuple" } |
| 1001 | func (t Tuple) Truth() Bool { return len(t) > 0 } |
| 1002 | |
| 1003 | func (x Tuple) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 1004 | y := y_.(Tuple) |
| 1005 | return sliceCompare(op, x, y, depth) |
| 1006 | } |
| 1007 | |
| 1008 | func (t Tuple) Hash() (uint32, error) { |
| 1009 | // Use same algorithm as Python. |
| 1010 | var x, mult uint32 = 0x345678, 1000003 |
| 1011 | for _, elem := range t { |
| 1012 | y, err := elem.Hash() |
| 1013 | if err != nil { |
| 1014 | return 0, err |
| 1015 | } |
| 1016 | x = x ^ y*mult |
| 1017 | mult += 82520 + uint32(len(t)+len(t)) |
| 1018 | } |
| 1019 | return x, nil |
| 1020 | } |
| 1021 | |
| 1022 | type tupleIterator struct{ elems Tuple } |
| 1023 | |
| 1024 | func (it *tupleIterator) Next(p *Value) bool { |
| 1025 | if len(it.elems) > 0 { |
| 1026 | *p = it.elems[0] |
| 1027 | it.elems = it.elems[1:] |
| 1028 | return true |
| 1029 | } |
| 1030 | return false |
| 1031 | } |
| 1032 | |
| 1033 | func (it *tupleIterator) Done() {} |
| 1034 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 1035 | // A Set represents a Starlark set value. |
alandonovan | f83458d | 2019-04-02 20:34:11 -0400 | [diff] [blame] | 1036 | // The zero value of Set is a valid empty set. |
| 1037 | // If you know the exact final number of elements, |
| 1038 | // it is more efficient to call NewSet. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1039 | type Set struct { |
| 1040 | ht hashtable // values are all None |
| 1041 | } |
| 1042 | |
alandonovan | f83458d | 2019-04-02 20:34:11 -0400 | [diff] [blame] | 1043 | // NewSet returns a dictionary with initial space for |
| 1044 | // at least size insertions before rehashing. |
| 1045 | func NewSet(size int) *Set { |
| 1046 | set := new(Set) |
| 1047 | set.ht.init(size) |
| 1048 | return set |
| 1049 | } |
| 1050 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1051 | func (s *Set) Delete(k Value) (found bool, err error) { _, found, err = s.ht.delete(k); return } |
| 1052 | func (s *Set) Clear() error { return s.ht.clear() } |
| 1053 | func (s *Set) Has(k Value) (found bool, err error) { _, found, err = s.ht.lookup(k); return } |
| 1054 | func (s *Set) Insert(k Value) error { return s.ht.insert(k, None) } |
| 1055 | func (s *Set) Len() int { return int(s.ht.len) } |
| 1056 | func (s *Set) Iterate() Iterator { return s.ht.iterate() } |
| 1057 | func (s *Set) String() string { return toString(s) } |
| 1058 | func (s *Set) Type() string { return "set" } |
| 1059 | func (s *Set) elems() []Value { return s.ht.keys() } |
| 1060 | func (s *Set) Freeze() { s.ht.freeze() } |
| 1061 | func (s *Set) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable type: set") } |
| 1062 | func (s *Set) Truth() Bool { return s.Len() > 0 } |
| 1063 | |
| 1064 | func (s *Set) Attr(name string) (Value, error) { return builtinAttr(s, name, setMethods) } |
| 1065 | func (s *Set) AttrNames() []string { return builtinAttrNames(setMethods) } |
| 1066 | |
| 1067 | func (x *Set) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 1068 | y := y_.(*Set) |
| 1069 | switch op { |
| 1070 | case syntax.EQL: |
| 1071 | ok, err := setsEqual(x, y, depth) |
| 1072 | return ok, err |
| 1073 | case syntax.NEQ: |
| 1074 | ok, err := setsEqual(x, y, depth) |
| 1075 | return !ok, err |
| 1076 | default: |
| 1077 | return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type()) |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | func setsEqual(x, y *Set, depth int) (bool, error) { |
| 1082 | if x.Len() != y.Len() { |
| 1083 | return false, nil |
| 1084 | } |
| 1085 | for _, elem := range x.elems() { |
| 1086 | if found, _ := y.Has(elem); !found { |
| 1087 | return false, nil |
| 1088 | } |
| 1089 | } |
| 1090 | return true, nil |
| 1091 | } |
| 1092 | |
| 1093 | func (s *Set) Union(iter Iterator) (Value, error) { |
| 1094 | set := new(Set) |
| 1095 | for _, elem := range s.elems() { |
| 1096 | set.Insert(elem) // can't fail |
| 1097 | } |
| 1098 | var x Value |
| 1099 | for iter.Next(&x) { |
| 1100 | if err := set.Insert(x); err != nil { |
| 1101 | return nil, err |
| 1102 | } |
| 1103 | } |
| 1104 | return set, nil |
| 1105 | } |
| 1106 | |
| 1107 | // toString returns the string form of value v. |
| 1108 | // It may be more efficient than v.String() for larger values. |
| 1109 | func toString(v Value) string { |
Josh Bleecher Snyder | 8cb25c8 | 2019-03-01 14:24:35 -0800 | [diff] [blame] | 1110 | buf := new(strings.Builder) |
| 1111 | writeValue(buf, v, nil) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1112 | return buf.String() |
| 1113 | } |
| 1114 | |
alandonovan | 0ed7e5b | 2019-01-03 16:11:58 -0500 | [diff] [blame] | 1115 | // writeValue writes x to out. |
| 1116 | // |
| 1117 | // path is used to detect cycles. |
| 1118 | // It contains the list of *List and *Dict values we're currently printing. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1119 | // (These are the only potentially cyclic structures.) |
Josh Bleecher Snyder | 81e440d | 2019-03-02 07:02:02 -0800 | [diff] [blame] | 1120 | // Callers should generally pass nil for path. |
alandonovan | 0ed7e5b | 2019-01-03 16:11:58 -0500 | [diff] [blame] | 1121 | // It is safe to re-use the same path slice for multiple calls. |
Josh Bleecher Snyder | 8cb25c8 | 2019-03-01 14:24:35 -0800 | [diff] [blame] | 1122 | func writeValue(out *strings.Builder, x Value, path []Value) { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1123 | switch x := x.(type) { |
alandonovan | 15a68dc | 2018-03-05 16:23:41 -0500 | [diff] [blame] | 1124 | case nil: |
| 1125 | out.WriteString("<nil>") // indicates a bug |
| 1126 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 1127 | // These four cases are duplicates of T.String(), for efficiency. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1128 | case NoneType: |
| 1129 | out.WriteString("None") |
| 1130 | |
| 1131 | case Int: |
| 1132 | out.WriteString(x.String()) |
| 1133 | |
| 1134 | case Bool: |
| 1135 | if x { |
| 1136 | out.WriteString("True") |
| 1137 | } else { |
| 1138 | out.WriteString("False") |
| 1139 | } |
| 1140 | |
| 1141 | case String: |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 1142 | out.WriteString(syntax.Quote(string(x), false)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1143 | |
| 1144 | case *List: |
| 1145 | out.WriteByte('[') |
| 1146 | if pathContains(path, x) { |
| 1147 | out.WriteString("...") // list contains itself |
| 1148 | } else { |
| 1149 | for i, elem := range x.elems { |
| 1150 | if i > 0 { |
| 1151 | out.WriteString(", ") |
| 1152 | } |
| 1153 | writeValue(out, elem, append(path, x)) |
| 1154 | } |
| 1155 | } |
| 1156 | out.WriteByte(']') |
| 1157 | |
| 1158 | case Tuple: |
| 1159 | out.WriteByte('(') |
| 1160 | for i, elem := range x { |
| 1161 | if i > 0 { |
| 1162 | out.WriteString(", ") |
| 1163 | } |
| 1164 | writeValue(out, elem, path) |
| 1165 | } |
| 1166 | if len(x) == 1 { |
| 1167 | out.WriteByte(',') |
| 1168 | } |
| 1169 | out.WriteByte(')') |
| 1170 | |
| 1171 | case *Function: |
| 1172 | fmt.Fprintf(out, "<function %s>", x.Name()) |
| 1173 | |
| 1174 | case *Builtin: |
| 1175 | if x.recv != nil { |
| 1176 | fmt.Fprintf(out, "<built-in method %s of %s value>", x.Name(), x.recv.Type()) |
| 1177 | } else { |
| 1178 | fmt.Fprintf(out, "<built-in function %s>", x.Name()) |
| 1179 | } |
| 1180 | |
| 1181 | case *Dict: |
| 1182 | out.WriteByte('{') |
| 1183 | if pathContains(path, x) { |
| 1184 | out.WriteString("...") // dict contains itself |
| 1185 | } else { |
| 1186 | sep := "" |
| 1187 | for _, item := range x.Items() { |
| 1188 | k, v := item[0], item[1] |
| 1189 | out.WriteString(sep) |
| 1190 | writeValue(out, k, path) |
| 1191 | out.WriteString(": ") |
| 1192 | writeValue(out, v, append(path, x)) // cycle check |
| 1193 | sep = ", " |
| 1194 | } |
| 1195 | } |
| 1196 | out.WriteByte('}') |
| 1197 | |
| 1198 | case *Set: |
| 1199 | out.WriteString("set([") |
| 1200 | for i, elem := range x.elems() { |
| 1201 | if i > 0 { |
| 1202 | out.WriteString(", ") |
| 1203 | } |
| 1204 | writeValue(out, elem, path) |
| 1205 | } |
| 1206 | out.WriteString("])") |
| 1207 | |
| 1208 | default: |
| 1209 | out.WriteString(x.String()) |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | func pathContains(path []Value, x Value) bool { |
| 1214 | for _, y := range path { |
| 1215 | if x == y { |
| 1216 | return true |
| 1217 | } |
| 1218 | } |
| 1219 | return false |
| 1220 | } |
| 1221 | |
| 1222 | const maxdepth = 10 |
| 1223 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 1224 | // Equal reports whether two Starlark values are equal. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1225 | func Equal(x, y Value) (bool, error) { |
alandonovan | 4bb5ab6 | 2018-03-06 20:21:45 -0500 | [diff] [blame] | 1226 | if x, ok := x.(String); ok { |
| 1227 | return x == y, nil // fast path for an important special case |
| 1228 | } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1229 | return EqualDepth(x, y, maxdepth) |
| 1230 | } |
| 1231 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 1232 | // EqualDepth reports whether two Starlark values are equal. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1233 | // |
| 1234 | // Recursive comparisons by implementations of Value.CompareSameType |
| 1235 | // should use EqualDepth to prevent infinite recursion. |
| 1236 | func EqualDepth(x, y Value, depth int) (bool, error) { |
| 1237 | return CompareDepth(syntax.EQL, x, y, depth) |
| 1238 | } |
| 1239 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 1240 | // Compare compares two Starlark values. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1241 | // The comparison operation must be one of EQL, NEQ, LT, LE, GT, or GE. |
| 1242 | // Compare returns an error if an ordered comparison was |
| 1243 | // requested for a type that does not support it. |
| 1244 | // |
| 1245 | // Recursive comparisons by implementations of Value.CompareSameType |
| 1246 | // should use CompareDepth to prevent infinite recursion. |
| 1247 | func Compare(op syntax.Token, x, y Value) (bool, error) { |
| 1248 | return CompareDepth(op, x, y, maxdepth) |
| 1249 | } |
| 1250 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 1251 | // CompareDepth compares two Starlark values. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1252 | // The comparison operation must be one of EQL, NEQ, LT, LE, GT, or GE. |
| 1253 | // CompareDepth returns an error if an ordered comparison was |
| 1254 | // requested for a pair of values that do not support it. |
| 1255 | // |
| 1256 | // The depth parameter limits the maximum depth of recursion |
| 1257 | // in cyclic data structures. |
| 1258 | func CompareDepth(op syntax.Token, x, y Value, depth int) (bool, error) { |
| 1259 | if depth < 1 { |
| 1260 | return false, fmt.Errorf("comparison exceeded maximum recursion depth") |
| 1261 | } |
| 1262 | if sameType(x, y) { |
| 1263 | if xcomp, ok := x.(Comparable); ok { |
| 1264 | return xcomp.CompareSameType(op, y, depth) |
| 1265 | } |
| 1266 | |
| 1267 | // use identity comparison |
| 1268 | switch op { |
| 1269 | case syntax.EQL: |
| 1270 | return x == y, nil |
| 1271 | case syntax.NEQ: |
| 1272 | return x != y, nil |
| 1273 | } |
| 1274 | return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type()) |
| 1275 | } |
| 1276 | |
| 1277 | // different types |
| 1278 | |
| 1279 | // int/float ordered comparisons |
| 1280 | switch x := x.(type) { |
| 1281 | case Int: |
| 1282 | if y, ok := y.(Float); ok { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1283 | var cmp int |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 1284 | if y != y { |
| 1285 | cmp = -1 // y is NaN |
| 1286 | } else if !math.IsInf(float64(y), 0) { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1287 | cmp = x.rational().Cmp(y.rational()) // y is finite |
| 1288 | } else if y > 0 { |
| 1289 | cmp = -1 // y is +Inf |
| 1290 | } else { |
| 1291 | cmp = +1 // y is -Inf |
| 1292 | } |
| 1293 | return threeway(op, cmp), nil |
| 1294 | } |
| 1295 | case Float: |
| 1296 | if y, ok := y.(Int); ok { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1297 | var cmp int |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 1298 | if x != x { |
| 1299 | cmp = +1 // x is NaN |
| 1300 | } else if !math.IsInf(float64(x), 0) { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1301 | cmp = x.rational().Cmp(y.rational()) // x is finite |
| 1302 | } else if x > 0 { |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 1303 | cmp = +1 // x is +Inf |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1304 | } else { |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame] | 1305 | cmp = -1 // x is -Inf |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1306 | } |
| 1307 | return threeway(op, cmp), nil |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | // All other values of different types compare unequal. |
| 1312 | switch op { |
| 1313 | case syntax.EQL: |
| 1314 | return false, nil |
| 1315 | case syntax.NEQ: |
| 1316 | return true, nil |
| 1317 | } |
| 1318 | return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type()) |
| 1319 | } |
| 1320 | |
| 1321 | func sameType(x, y Value) bool { |
| 1322 | return reflect.TypeOf(x) == reflect.TypeOf(y) || x.Type() == y.Type() |
| 1323 | } |
| 1324 | |
| 1325 | // threeway interprets a three-way comparison value cmp (-1, 0, +1) |
| 1326 | // as a boolean comparison (e.g. x < y). |
| 1327 | func threeway(op syntax.Token, cmp int) bool { |
| 1328 | switch op { |
| 1329 | case syntax.EQL: |
| 1330 | return cmp == 0 |
| 1331 | case syntax.NEQ: |
| 1332 | return cmp != 0 |
| 1333 | case syntax.LE: |
| 1334 | return cmp <= 0 |
| 1335 | case syntax.LT: |
| 1336 | return cmp < 0 |
| 1337 | case syntax.GE: |
| 1338 | return cmp >= 0 |
| 1339 | case syntax.GT: |
| 1340 | return cmp > 0 |
| 1341 | } |
| 1342 | panic(op) |
| 1343 | } |
| 1344 | |
| 1345 | func b2i(b bool) int { |
| 1346 | if b { |
| 1347 | return 1 |
| 1348 | } else { |
| 1349 | return 0 |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | // Len returns the length of a string or sequence value, |
| 1354 | // and -1 for all others. |
| 1355 | // |
| 1356 | // Warning: Len(x) >= 0 does not imply Iterate(x) != nil. |
| 1357 | // A string has a known length but is not directly iterable. |
| 1358 | func Len(x Value) int { |
| 1359 | switch x := x.(type) { |
| 1360 | case String: |
| 1361 | return x.Len() |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 1362 | case Indexable: |
| 1363 | return x.Len() |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1364 | case Sequence: |
| 1365 | return x.Len() |
| 1366 | } |
| 1367 | return -1 |
| 1368 | } |
| 1369 | |
| 1370 | // Iterate return a new iterator for the value if iterable, nil otherwise. |
| 1371 | // If the result is non-nil, the caller must call Done when finished with it. |
| 1372 | // |
| 1373 | // Warning: Iterate(x) != nil does not imply Len(x) >= 0. |
| 1374 | // Some iterables may have unknown length. |
| 1375 | func Iterate(x Value) Iterator { |
| 1376 | if x, ok := x.(Iterable); ok { |
| 1377 | return x.Iterate() |
| 1378 | } |
| 1379 | return nil |
| 1380 | } |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 1381 | |
| 1382 | // Bytes is the type of a Starlark binary string. |
| 1383 | // |
| 1384 | // A Bytes encapsulates an immutable sequence of bytes. |
| 1385 | // It is comparable, indexable, and sliceable, but not direcly iterable; |
| 1386 | // use bytes.elems() for an iterable view. |
| 1387 | // |
| 1388 | // In this Go implementation, the elements of 'string' and 'bytes' are |
| 1389 | // both bytes, but in other implementations, notably Java, the elements |
| 1390 | // of a 'string' are UTF-16 codes (Java chars). The spec abstracts text |
| 1391 | // strings as sequences of UTF-k codes that encode Unicode code points, |
| 1392 | // and operations that convert from text to binary incur UTF-k-to-UTF-8 |
| 1393 | // transcoding; conversely, conversion from binary to text incurs |
| 1394 | // UTF-8-to-UTF-k transcoding. Because k=8 for Go, these operations |
| 1395 | // are the identity function, at least for valid encodings of text. |
| 1396 | type Bytes string |
| 1397 | |
| 1398 | var ( |
| 1399 | _ Comparable = Bytes("") |
| 1400 | _ Sliceable = Bytes("") |
| 1401 | _ Indexable = Bytes("") |
| 1402 | ) |
| 1403 | |
| 1404 | func (b Bytes) String() string { return syntax.Quote(string(b), true) } |
| 1405 | func (b Bytes) Type() string { return "bytes" } |
| 1406 | func (b Bytes) Freeze() {} // immutable |
| 1407 | func (b Bytes) Truth() Bool { return len(b) > 0 } |
| 1408 | func (b Bytes) Hash() (uint32, error) { return String(b).Hash() } |
| 1409 | func (b Bytes) Len() int { return len(b) } |
| 1410 | func (b Bytes) Index(i int) Value { return b[i : i+1] } |
| 1411 | |
| 1412 | func (b Bytes) Attr(name string) (Value, error) { return builtinAttr(b, name, bytesMethods) } |
| 1413 | func (b Bytes) AttrNames() []string { return builtinAttrNames(bytesMethods) } |
| 1414 | |
| 1415 | func (b Bytes) Slice(start, end, step int) Value { |
| 1416 | if step == 1 { |
| 1417 | return b[start:end] |
| 1418 | } |
| 1419 | |
| 1420 | sign := signum(step) |
| 1421 | var str []byte |
| 1422 | for i := start; signum(end-i) == sign; i += step { |
| 1423 | str = append(str, b[i]) |
| 1424 | } |
| 1425 | return Bytes(str) |
| 1426 | } |
| 1427 | |
| 1428 | func (x Bytes) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) { |
| 1429 | y := y_.(Bytes) |
| 1430 | return threeway(op, strings.Compare(string(x), string(y))), nil |
| 1431 | } |