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