Source file src/go/constant/value.go

     1  // Copyright 2013 The Go 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  
     5  // Package constant implements Values representing untyped
     6  // Go constants and their corresponding operations.
     7  //
     8  // A special Unknown value may be used when a value
     9  // is unknown due to an error. Operations on unknown
    10  // values produce unknown values unless specified
    11  // otherwise.
    12  package constant
    13  
    14  import (
    15  	"fmt"
    16  	"go/token"
    17  	"math"
    18  	"math/big"
    19  	"math/bits"
    20  	"strconv"
    21  	"strings"
    22  	"sync"
    23  	"unicode/utf8"
    24  )
    25  
    26  //go:generate stringer -type Kind
    27  
    28  // Kind specifies the kind of value represented by a [Value].
    29  type Kind int
    30  
    31  const (
    32  	// unknown values
    33  	Unknown Kind = iota
    34  
    35  	// non-numeric values
    36  	Bool
    37  	String
    38  
    39  	// numeric values
    40  	Int
    41  	Float
    42  	Complex
    43  )
    44  
    45  // A Value represents the value of a Go constant.
    46  type Value interface {
    47  	// Kind returns the value kind.
    48  	Kind() Kind
    49  
    50  	// String returns a short, quoted (human-readable) form of the value.
    51  	// For numeric values, the result may be an approximation;
    52  	// for String values the result may be a shortened string.
    53  	// Use ExactString for a string representing a value exactly.
    54  	String() string
    55  
    56  	// ExactString returns an exact, quoted (human-readable) form of the value.
    57  	// If the Value is of Kind String, use StringVal to obtain the unquoted string.
    58  	ExactString() string
    59  
    60  	// Prevent external implementations.
    61  	implementsValue()
    62  }
    63  
    64  // ----------------------------------------------------------------------------
    65  // Implementations
    66  
    67  // Maximum supported mantissa precision.
    68  // The spec requires at least 256 bits; typical implementations use 512 bits.
    69  const prec = 512
    70  
    71  // TODO(gri) Consider storing "error" information in an unknownVal so clients
    72  // can provide better error messages. For instance, if a number is
    73  // too large (incl. infinity), that could be recorded in unknownVal.
    74  // See also #20583 and #42695 for use cases.
    75  
    76  // Representation of values:
    77  //
    78  // Values of Int and Float Kind have two different representations each: int64Val
    79  // and intVal, and ratVal and floatVal. When possible, the "smaller", respectively
    80  // more precise (for Floats) representation is chosen. However, once a Float value
    81  // is represented as a floatVal, any subsequent results remain floatVals (unless
    82  // explicitly converted); i.e., no attempt is made to convert a floatVal back into
    83  // a ratVal. The reasoning is that all representations but floatVal are mathematically
    84  // exact, but once that precision is lost (by moving to floatVal), moving back to
    85  // a different representation implies a precision that's not actually there.
    86  
    87  type (
    88  	unknownVal struct{}
    89  	boolVal    bool
    90  	stringVal  struct {
    91  		// Lazy value: either a string (l,r==nil) or an addition (l,r!=nil).
    92  		mu   sync.Mutex
    93  		s    string
    94  		l, r *stringVal
    95  	}
    96  	int64Val   int64                    // Int values representable as an int64
    97  	intVal     struct{ val *big.Int }   // Int values not representable as an int64
    98  	ratVal     struct{ val *big.Rat }   // Float values representable as a fraction
    99  	floatVal   struct{ val *big.Float } // Float values not representable as a fraction
   100  	complexVal struct{ re, im Value }
   101  )
   102  
   103  func (unknownVal) Kind() Kind { return Unknown }
   104  func (boolVal) Kind() Kind    { return Bool }
   105  func (*stringVal) Kind() Kind { return String }
   106  func (int64Val) Kind() Kind   { return Int }
   107  func (intVal) Kind() Kind     { return Int }
   108  func (ratVal) Kind() Kind     { return Float }
   109  func (floatVal) Kind() Kind   { return Float }
   110  func (complexVal) Kind() Kind { return Complex }
   111  
   112  func (unknownVal) String() string { return "unknown" }
   113  func (x boolVal) String() string  { return strconv.FormatBool(bool(x)) }
   114  
   115  // String returns a possibly shortened quoted form of the String value.
   116  func (x *stringVal) String() string {
   117  	const maxLen = 72 // a reasonable length
   118  	s := strconv.Quote(x.string())
   119  	if utf8.RuneCountInString(s) > maxLen {
   120  		// The string without the enclosing quotes is greater than maxLen-2 runes
   121  		// long. Remove the last 3 runes (including the closing '"') by keeping
   122  		// only the first maxLen-3 runes; then add "...".
   123  		i := 0
   124  		for n := 0; n < maxLen-3; n++ {
   125  			_, size := utf8.DecodeRuneInString(s[i:])
   126  			i += size
   127  		}
   128  		s = s[:i] + "..."
   129  	}
   130  	return s
   131  }
   132  
   133  // string constructs and returns the actual string literal value.
   134  // If x represents an addition, then it rewrites x to be a single
   135  // string, to speed future calls. This lazy construction avoids
   136  // building different string values for all subpieces of a large
   137  // concatenation. See golang.org/issue/23348.
   138  func (x *stringVal) string() string {
   139  	x.mu.Lock()
   140  	defer x.mu.Unlock()
   141  	if x.l != nil {
   142  		x.s = strings.Join(reverse(x.appendReverse(nil)), "")
   143  		x.l = nil
   144  		x.r = nil
   145  	}
   146  	return x.s
   147  }
   148  
   149  // reverse reverses x in place and returns it.
   150  func reverse(x []string) []string {
   151  	n := len(x)
   152  	for i := 0; i+i < n; i++ {
   153  		x[i], x[n-1-i] = x[n-1-i], x[i]
   154  	}
   155  	return x
   156  }
   157  
   158  // appendReverse appends to list all of x's subpieces, but in reverse,
   159  // and returns the result. Appending the reversal allows processing
   160  // the right side in a recursive call and the left side in a loop.
   161  // Because a chain like a + b + c + d + e is actually represented
   162  // as ((((a + b) + c) + d) + e), the left-side loop avoids deep recursion.
   163  // x must be locked.
   164  func (x *stringVal) appendReverse(list []string) []string {
   165  	y := x
   166  	for y.r != nil {
   167  		y.r.mu.Lock()
   168  		list = y.r.appendReverse(list)
   169  		y.r.mu.Unlock()
   170  
   171  		l := y.l
   172  		if y != x {
   173  			y.mu.Unlock()
   174  		}
   175  		l.mu.Lock()
   176  		y = l
   177  	}
   178  	s := y.s
   179  	if y != x {
   180  		y.mu.Unlock()
   181  	}
   182  	return append(list, s)
   183  }
   184  
   185  func (x int64Val) String() string { return strconv.FormatInt(int64(x), 10) }
   186  func (x intVal) String() string   { return x.val.String() }
   187  func (x ratVal) String() string   { return rtof(x).String() }
   188  
   189  // String returns a decimal approximation of the Float value.
   190  func (x floatVal) String() string {
   191  	f := x.val
   192  
   193  	// Don't try to convert infinities (will not terminate).
   194  	if f.IsInf() {
   195  		return f.String()
   196  	}
   197  
   198  	// Use exact fmt formatting if in float64 range (common case):
   199  	// proceed if f doesn't underflow to 0 or overflow to inf.
   200  	if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) {
   201  		s := fmt.Sprintf("%.6g", x)
   202  		if !f.IsInt() && strings.IndexByte(s, '.') < 0 {
   203  			// f is not an integer, but its string representation
   204  			// doesn't reflect that. Use more digits. See issue 56220.
   205  			s = fmt.Sprintf("%g", x)
   206  		}
   207  		return s
   208  	}
   209  
   210  	// Out of float64 range. Do approximate manual to decimal
   211  	// conversion to avoid precise but possibly slow Float
   212  	// formatting.
   213  	// f = mant * 2**exp
   214  	var mant big.Float
   215  	exp := f.MantExp(&mant) // 0.5 <= |mant| < 1.0
   216  
   217  	// approximate float64 mantissa m and decimal exponent d
   218  	// f ~ m * 10**d
   219  	m, _ := mant.Float64()                     // 0.5 <= |m| < 1.0
   220  	d := float64(exp) * (math.Ln2 / math.Ln10) // log_10(2)
   221  
   222  	// adjust m for truncated (integer) decimal exponent e
   223  	e := int64(d)
   224  	m *= math.Pow(10, d-float64(e))
   225  
   226  	// ensure 1 <= |m| < 10
   227  	switch am := math.Abs(m); {
   228  	case am < 1-0.5e-6:
   229  		// The %.6g format below rounds m to 5 digits after the
   230  		// decimal point. Make sure that m*10 < 10 even after
   231  		// rounding up: m*10 + 0.5e-5 < 10 => m < 1 - 0.5e6.
   232  		m *= 10
   233  		e--
   234  	case am >= 10:
   235  		m /= 10
   236  		e++
   237  	}
   238  
   239  	return fmt.Sprintf("%.6ge%+d", m, e)
   240  }
   241  
   242  func (x complexVal) String() string { return fmt.Sprintf("(%s + %si)", x.re, x.im) }
   243  
   244  func (x unknownVal) ExactString() string { return x.String() }
   245  func (x boolVal) ExactString() string    { return x.String() }
   246  func (x *stringVal) ExactString() string { return strconv.Quote(x.string()) }
   247  func (x int64Val) ExactString() string   { return x.String() }
   248  func (x intVal) ExactString() string     { return x.String() }
   249  
   250  func (x ratVal) ExactString() string {
   251  	r := x.val
   252  	if r.IsInt() {
   253  		return r.Num().String()
   254  	}
   255  	return r.String()
   256  }
   257  
   258  func (x floatVal) ExactString() string { return x.val.Text('p', 0) }
   259  
   260  func (x complexVal) ExactString() string {
   261  	return fmt.Sprintf("(%s + %si)", x.re.ExactString(), x.im.ExactString())
   262  }
   263  
   264  func (unknownVal) implementsValue() {}
   265  func (boolVal) implementsValue()    {}
   266  func (*stringVal) implementsValue() {}
   267  func (int64Val) implementsValue()   {}
   268  func (ratVal) implementsValue()     {}
   269  func (intVal) implementsValue()     {}
   270  func (floatVal) implementsValue()   {}
   271  func (complexVal) implementsValue() {}
   272  
   273  func newInt() *big.Int     { return new(big.Int) }
   274  func newRat() *big.Rat     { return new(big.Rat) }
   275  func newFloat() *big.Float { return new(big.Float).SetPrec(prec) }
   276  
   277  func i64toi(x int64Val) intVal   { return intVal{newInt().SetInt64(int64(x))} }
   278  func i64tor(x int64Val) ratVal   { return ratVal{newRat().SetInt64(int64(x))} }
   279  func i64tof(x int64Val) floatVal { return floatVal{newFloat().SetInt64(int64(x))} }
   280  func itor(x intVal) ratVal       { return ratVal{newRat().SetInt(x.val)} }
   281  func itof(x intVal) floatVal     { return floatVal{newFloat().SetInt(x.val)} }
   282  func rtof(x ratVal) floatVal     { return floatVal{newFloat().SetRat(x.val)} }
   283  func vtoc(x Value) complexVal    { return complexVal{x, int64Val(0)} }
   284  
   285  func makeInt(x *big.Int) Value {
   286  	if x.IsInt64() {
   287  		return int64Val(x.Int64())
   288  	}
   289  	return intVal{x}
   290  }
   291  
   292  func makeRat(x *big.Rat) Value {
   293  	a := x.Num()
   294  	b := x.Denom()
   295  	if smallInt(a) && smallInt(b) {
   296  		// ok to remain fraction
   297  		return ratVal{x}
   298  	}
   299  	// components too large => switch to float
   300  	return floatVal{newFloat().SetRat(x)}
   301  }
   302  
   303  var floatVal0 = floatVal{newFloat()}
   304  
   305  func makeFloat(x *big.Float) Value {
   306  	// convert -0
   307  	if x.Sign() == 0 {
   308  		return floatVal0
   309  	}
   310  	if x.IsInf() {
   311  		return unknownVal{}
   312  	}
   313  	// No attempt is made to "go back" to ratVal, even if possible,
   314  	// to avoid providing the illusion of a mathematically exact
   315  	// representation.
   316  	return floatVal{x}
   317  }
   318  
   319  func makeComplex(re, im Value) Value {
   320  	if re.Kind() == Unknown || im.Kind() == Unknown {
   321  		return unknownVal{}
   322  	}
   323  	return complexVal{re, im}
   324  }
   325  
   326  func makeFloatFromLiteral(lit string) Value {
   327  	if f, ok := newFloat().SetString(lit); ok {
   328  		if smallFloat(f) {
   329  			// ok to use rationals
   330  			if f.Sign() == 0 {
   331  				// Issue 20228: If the float underflowed to zero, parse just "0".
   332  				// Otherwise, lit might contain a value with a large negative exponent,
   333  				// such as -6e-1886451601. As a float, that will underflow to 0,
   334  				// but it'll take forever to parse as a Rat.
   335  				lit = "0"
   336  			}
   337  			if r, ok := newRat().SetString(lit); ok {
   338  				return ratVal{r}
   339  			}
   340  		}
   341  		// otherwise use floats
   342  		return makeFloat(f)
   343  	}
   344  	return nil
   345  }
   346  
   347  // Permit fractions with component sizes up to maxExp
   348  // before switching to using floating-point numbers.
   349  const maxExp = 4 << 10
   350  
   351  // smallInt reports whether x would lead to "reasonably"-sized fraction
   352  // if converted to a *big.Rat.
   353  func smallInt(x *big.Int) bool {
   354  	return x.BitLen() < maxExp
   355  }
   356  
   357  // smallFloat64 reports whether x would lead to "reasonably"-sized fraction
   358  // if converted to a *big.Rat.
   359  func smallFloat64(x float64) bool {
   360  	if math.IsInf(x, 0) {
   361  		return false
   362  	}
   363  	_, e := math.Frexp(x)
   364  	return -maxExp < e && e < maxExp
   365  }
   366  
   367  // smallFloat reports whether x would lead to "reasonably"-sized fraction
   368  // if converted to a *big.Rat.
   369  func smallFloat(x *big.Float) bool {
   370  	if x.IsInf() {
   371  		return false
   372  	}
   373  	e := x.MantExp(nil)
   374  	return -maxExp < e && e < maxExp
   375  }
   376  
   377  // ----------------------------------------------------------------------------
   378  // Factories
   379  
   380  // MakeUnknown returns the [Unknown] value.
   381  func MakeUnknown() Value { return unknownVal{} }
   382  
   383  // MakeBool returns the [Bool] value for b.
   384  func MakeBool(b bool) Value { return boolVal(b) }
   385  
   386  // MakeString returns the [String] value for s.
   387  func MakeString(s string) Value {
   388  	if s == "" {
   389  		return &emptyString // common case
   390  	}
   391  	return &stringVal{s: s}
   392  }
   393  
   394  var emptyString stringVal
   395  
   396  // MakeInt64 returns the [Int] value for x.
   397  func MakeInt64(x int64) Value { return int64Val(x) }
   398  
   399  // MakeUint64 returns the [Int] value for x.
   400  func MakeUint64(x uint64) Value {
   401  	if x < 1<<63 {
   402  		return int64Val(int64(x))
   403  	}
   404  	return intVal{newInt().SetUint64(x)}
   405  }
   406  
   407  // MakeFloat64 returns the [Float] value for x.
   408  // If x is -0.0, the result is 0.0.
   409  // If x is not finite, the result is an [Unknown].
   410  func MakeFloat64(x float64) Value {
   411  	if math.IsInf(x, 0) || math.IsNaN(x) {
   412  		return unknownVal{}
   413  	}
   414  	if smallFloat64(x) {
   415  		return ratVal{newRat().SetFloat64(x + 0)} // convert -0 to 0
   416  	}
   417  	return floatVal{newFloat().SetFloat64(x + 0)}
   418  }
   419  
   420  // MakeFromLiteral returns the corresponding integer, floating-point,
   421  // imaginary, character, or string value for a Go literal string. The
   422  // tok value must be one of [token.INT], [token.FLOAT], [token.IMAG],
   423  // [token.CHAR], or [token.STRING]. The final argument must be zero.
   424  // If the literal string syntax is invalid, the result is an [Unknown].
   425  func MakeFromLiteral(lit string, tok token.Token, zero uint) Value {
   426  	if zero != 0 {
   427  		panic("MakeFromLiteral called with non-zero last argument")
   428  	}
   429  
   430  	switch tok {
   431  	case token.INT:
   432  		if x, err := strconv.ParseInt(lit, 0, 64); err == nil {
   433  			return int64Val(x)
   434  		}
   435  		if x, ok := newInt().SetString(lit, 0); ok {
   436  			return intVal{x}
   437  		}
   438  
   439  	case token.FLOAT:
   440  		if x := makeFloatFromLiteral(lit); x != nil {
   441  			return x
   442  		}
   443  
   444  	case token.IMAG:
   445  		if n := len(lit); n > 0 && lit[n-1] == 'i' {
   446  			if im := makeFloatFromLiteral(lit[:n-1]); im != nil {
   447  				return makeComplex(int64Val(0), im)
   448  			}
   449  		}
   450  
   451  	case token.CHAR:
   452  		if n := len(lit); n >= 2 {
   453  			if code, _, _, err := strconv.UnquoteChar(lit[1:n-1], '\''); err == nil {
   454  				return MakeInt64(int64(code))
   455  			}
   456  		}
   457  
   458  	case token.STRING:
   459  		if s, err := strconv.Unquote(lit); err == nil {
   460  			return MakeString(s)
   461  		}
   462  
   463  	default:
   464  		panic(fmt.Sprintf("%v is not a valid token", tok))
   465  	}
   466  
   467  	return unknownVal{}
   468  }
   469  
   470  // ----------------------------------------------------------------------------
   471  // Accessors
   472  //
   473  // For unknown arguments the result is the zero value for the respective
   474  // accessor type, except for Sign, where the result is 1.
   475  
   476  // BoolVal returns the Go boolean value of x, which must be a [Bool] or an [Unknown].
   477  // If x is [Unknown], the result is false.
   478  func BoolVal(x Value) bool {
   479  	switch x := x.(type) {
   480  	case boolVal:
   481  		return bool(x)
   482  	case unknownVal:
   483  		return false
   484  	default:
   485  		panic(fmt.Sprintf("%v not a Bool", x))
   486  	}
   487  }
   488  
   489  // StringVal returns the Go string value of x, which must be a [String] or an [Unknown].
   490  // If x is [Unknown], the result is "".
   491  func StringVal(x Value) string {
   492  	switch x := x.(type) {
   493  	case *stringVal:
   494  		return x.string()
   495  	case unknownVal:
   496  		return ""
   497  	default:
   498  		panic(fmt.Sprintf("%v not a String", x))
   499  	}
   500  }
   501  
   502  // Int64Val returns the Go int64 value of x and whether the result is exact;
   503  // x must be an [Int] or an [Unknown]. If the result is not exact, its value is undefined.
   504  // If x is [Unknown], the result is (0, false).
   505  func Int64Val(x Value) (int64, bool) {
   506  	switch x := x.(type) {
   507  	case int64Val:
   508  		return int64(x), true
   509  	case intVal:
   510  		return x.val.Int64(), false // not an int64Val and thus not exact
   511  	case unknownVal:
   512  		return 0, false
   513  	default:
   514  		panic(fmt.Sprintf("%v not an Int", x))
   515  	}
   516  }
   517  
   518  // Uint64Val returns the Go uint64 value of x and whether the result is exact;
   519  // x must be an [Int] or an [Unknown]. If the result is not exact, its value is undefined.
   520  // If x is [Unknown], the result is (0, false).
   521  func Uint64Val(x Value) (uint64, bool) {
   522  	switch x := x.(type) {
   523  	case int64Val:
   524  		return uint64(x), x >= 0
   525  	case intVal:
   526  		return x.val.Uint64(), x.val.IsUint64()
   527  	case unknownVal:
   528  		return 0, false
   529  	default:
   530  		panic(fmt.Sprintf("%v not an Int", x))
   531  	}
   532  }
   533  
   534  // Float32Val is like [Float64Val] but for float32 instead of float64.
   535  func Float32Val(x Value) (float32, bool) {
   536  	switch x := x.(type) {
   537  	case int64Val:
   538  		f := float32(x)
   539  		return f, int64Val(f) == x
   540  	case intVal:
   541  		f, acc := newFloat().SetInt(x.val).Float32()
   542  		return f, acc == big.Exact
   543  	case ratVal:
   544  		return x.val.Float32()
   545  	case floatVal:
   546  		f, acc := x.val.Float32()
   547  		return f, acc == big.Exact
   548  	case unknownVal:
   549  		return 0, false
   550  	default:
   551  		panic(fmt.Sprintf("%v not a Float", x))
   552  	}
   553  }
   554  
   555  // Float64Val returns the nearest Go float64 value of x and whether the result is exact;
   556  // x must be numeric or an [Unknown], but not [Complex]. For values too small (too close to 0)
   557  // to represent as float64, [Float64Val] silently underflows to 0. The result sign always
   558  // matches the sign of x, even for 0.
   559  // If x is [Unknown], the result is (0, false).
   560  func Float64Val(x Value) (float64, bool) {
   561  	switch x := x.(type) {
   562  	case int64Val:
   563  		f := float64(int64(x))
   564  		return f, int64Val(f) == x
   565  	case intVal:
   566  		f, acc := newFloat().SetInt(x.val).Float64()
   567  		return f, acc == big.Exact
   568  	case ratVal:
   569  		return x.val.Float64()
   570  	case floatVal:
   571  		f, acc := x.val.Float64()
   572  		return f, acc == big.Exact
   573  	case unknownVal:
   574  		return 0, false
   575  	default:
   576  		panic(fmt.Sprintf("%v not a Float", x))
   577  	}
   578  }
   579  
   580  // Val returns the underlying value for a given constant. Since it returns an
   581  // interface, it is up to the caller to type assert the result to the expected
   582  // type. The possible dynamic return types are:
   583  //
   584  //	x Kind             type of result
   585  //	-----------------------------------------
   586  //	Bool               bool
   587  //	String             string
   588  //	Int                int64 or *big.Int
   589  //	Float              *big.Float or *big.Rat
   590  //	everything else    nil
   591  func Val(x Value) any {
   592  	switch x := x.(type) {
   593  	case boolVal:
   594  		return bool(x)
   595  	case *stringVal:
   596  		return x.string()
   597  	case int64Val:
   598  		return int64(x)
   599  	case intVal:
   600  		return x.val
   601  	case ratVal:
   602  		return x.val
   603  	case floatVal:
   604  		return x.val
   605  	default:
   606  		return nil
   607  	}
   608  }
   609  
   610  // StringLen returns the length of x if x is a [String].
   611  // If x is [Unknown], the result is 0.
   612  // In all other cases, the function panics.
   613  func StringLen(x Value) int64 {
   614  	switch x := x.(type) {
   615  	case *stringVal:
   616  		return x.len()
   617  	case unknownVal:
   618  		return 0
   619  	default:
   620  		panic(fmt.Sprintf("%v not a String", x))
   621  	}
   622  }
   623  
   624  // len computes and returns the length of x without constructing the entire string.
   625  func (x *stringVal) len() int64 {
   626  	x.mu.Lock()
   627  	defer x.mu.Unlock()
   628  	if x.l != nil {
   629  		return x.l.len() + x.r.len()
   630  	}
   631  	return int64(len(x.s))
   632  }
   633  
   634  // Make returns the [Value] for x.
   635  //
   636  //	type of x        result Kind
   637  //	----------------------------
   638  //	bool             Bool
   639  //	string           String
   640  //	int64            Int
   641  //	*big.Int         Int
   642  //	*big.Float       Float
   643  //	*big.Rat         Float
   644  //	anything else    Unknown
   645  func Make(x any) Value {
   646  	switch x := x.(type) {
   647  	case bool:
   648  		return boolVal(x)
   649  	case string:
   650  		return &stringVal{s: x}
   651  	case int64:
   652  		return int64Val(x)
   653  	case *big.Int:
   654  		return makeInt(x)
   655  	case *big.Rat:
   656  		return makeRat(x)
   657  	case *big.Float:
   658  		return makeFloat(x)
   659  	default:
   660  		return unknownVal{}
   661  	}
   662  }
   663  
   664  // BitLen returns the number of bits required to represent
   665  // the absolute value x in binary representation; x must be an [Int] or an [Unknown].
   666  // If x is [Unknown], the result is 0.
   667  func BitLen(x Value) int {
   668  	switch x := x.(type) {
   669  	case int64Val:
   670  		u := uint64(x)
   671  		if x < 0 {
   672  			u = uint64(-x)
   673  		}
   674  		return 64 - bits.LeadingZeros64(u)
   675  	case intVal:
   676  		return x.val.BitLen()
   677  	case unknownVal:
   678  		return 0
   679  	default:
   680  		panic(fmt.Sprintf("%v not an Int", x))
   681  	}
   682  }
   683  
   684  // Sign returns -1, 0, or 1 depending on whether x < 0, x == 0, or x > 0;
   685  // x must be numeric or [Unknown]. For complex values x, the sign is 0 if x == 0,
   686  // otherwise it is != 0. If x is [Unknown], the result is 1.
   687  func Sign(x Value) int {
   688  	switch x := x.(type) {
   689  	case int64Val:
   690  		switch {
   691  		case x < 0:
   692  			return -1
   693  		case x > 0:
   694  			return 1
   695  		}
   696  		return 0
   697  	case intVal:
   698  		return x.val.Sign()
   699  	case ratVal:
   700  		return x.val.Sign()
   701  	case floatVal:
   702  		return x.val.Sign()
   703  	case complexVal:
   704  		return Sign(x.re) | Sign(x.im)
   705  	case unknownVal:
   706  		return 1 // avoid spurious division by zero errors
   707  	default:
   708  		panic(fmt.Sprintf("%v not numeric", x))
   709  	}
   710  }
   711  
   712  // ----------------------------------------------------------------------------
   713  // Support for assembling/disassembling numeric values
   714  
   715  const (
   716  	// Compute the size of a Word in bytes.
   717  	_m       = ^big.Word(0)
   718  	_log     = _m>>8&1 + _m>>16&1 + _m>>32&1
   719  	wordSize = 1 << _log
   720  )
   721  
   722  // Bytes returns the bytes for the absolute value of x in little-
   723  // endian binary representation; x must be an [Int].
   724  func Bytes(x Value) []byte {
   725  	var t intVal
   726  	switch x := x.(type) {
   727  	case int64Val:
   728  		t = i64toi(x)
   729  	case intVal:
   730  		t = x
   731  	default:
   732  		panic(fmt.Sprintf("%v not an Int", x))
   733  	}
   734  
   735  	words := t.val.Bits()
   736  	bytes := make([]byte, len(words)*wordSize)
   737  
   738  	i := 0
   739  	for _, w := range words {
   740  		for j := 0; j < wordSize; j++ {
   741  			bytes[i] = byte(w)
   742  			w >>= 8
   743  			i++
   744  		}
   745  	}
   746  	// remove leading 0's
   747  	for i > 0 && bytes[i-1] == 0 {
   748  		i--
   749  	}
   750  
   751  	return bytes[:i]
   752  }
   753  
   754  // MakeFromBytes returns the [Int] value given the bytes of its little-endian
   755  // binary representation. An empty byte slice argument represents 0.
   756  func MakeFromBytes(bytes []byte) Value {
   757  	words := make([]big.Word, (len(bytes)+(wordSize-1))/wordSize)
   758  
   759  	i := 0
   760  	var w big.Word
   761  	var s uint
   762  	for _, b := range bytes {
   763  		w |= big.Word(b) << s
   764  		if s += 8; s == wordSize*8 {
   765  			words[i] = w
   766  			i++
   767  			w = 0
   768  			s = 0
   769  		}
   770  	}
   771  	// store last word
   772  	if i < len(words) {
   773  		words[i] = w
   774  		i++
   775  	}
   776  	// remove leading 0's
   777  	for i > 0 && words[i-1] == 0 {
   778  		i--
   779  	}
   780  
   781  	return makeInt(newInt().SetBits(words[:i]))
   782  }
   783  
   784  // Num returns the numerator of x; x must be [Int], [Float], or [Unknown].
   785  // If x is [Unknown], or if it is too large or small to represent as a
   786  // fraction, the result is [Unknown]. Otherwise the result is an [Int]
   787  // with the same sign as x.
   788  func Num(x Value) Value {
   789  	switch x := x.(type) {
   790  	case int64Val, intVal:
   791  		return x
   792  	case ratVal:
   793  		return makeInt(x.val.Num())
   794  	case floatVal:
   795  		if smallFloat(x.val) {
   796  			r, _ := x.val.Rat(nil)
   797  			return makeInt(r.Num())
   798  		}
   799  	case unknownVal:
   800  		break
   801  	default:
   802  		panic(fmt.Sprintf("%v not Int or Float", x))
   803  	}
   804  	return unknownVal{}
   805  }
   806  
   807  // Denom returns the denominator of x; x must be [Int], [Float], or [Unknown].
   808  // If x is [Unknown], or if it is too large or small to represent as a
   809  // fraction, the result is [Unknown]. Otherwise the result is an [Int] >= 1.
   810  func Denom(x Value) Value {
   811  	switch x := x.(type) {
   812  	case int64Val, intVal:
   813  		return int64Val(1)
   814  	case ratVal:
   815  		return makeInt(x.val.Denom())
   816  	case floatVal:
   817  		if smallFloat(x.val) {
   818  			r, _ := x.val.Rat(nil)
   819  			return makeInt(r.Denom())
   820  		}
   821  	case unknownVal:
   822  		break
   823  	default:
   824  		panic(fmt.Sprintf("%v not Int or Float", x))
   825  	}
   826  	return unknownVal{}
   827  }
   828  
   829  // MakeImag returns the [Complex] value x*i;
   830  // x must be [Int], [Float], or [Unknown].
   831  // If x is [Unknown], the result is [Unknown].
   832  func MakeImag(x Value) Value {
   833  	switch x.(type) {
   834  	case unknownVal:
   835  		return x
   836  	case int64Val, intVal, ratVal, floatVal:
   837  		return makeComplex(int64Val(0), x)
   838  	default:
   839  		panic(fmt.Sprintf("%v not Int or Float", x))
   840  	}
   841  }
   842  
   843  // Real returns the real part of x, which must be a numeric or unknown value.
   844  // If x is [Unknown], the result is [Unknown].
   845  func Real(x Value) Value {
   846  	switch x := x.(type) {
   847  	case unknownVal, int64Val, intVal, ratVal, floatVal:
   848  		return x
   849  	case complexVal:
   850  		return x.re
   851  	default:
   852  		panic(fmt.Sprintf("%v not numeric", x))
   853  	}
   854  }
   855  
   856  // Imag returns the imaginary part of x, which must be a numeric or unknown value.
   857  // If x is [Unknown], the result is [Unknown].
   858  func Imag(x Value) Value {
   859  	switch x := x.(type) {
   860  	case unknownVal:
   861  		return x
   862  	case int64Val, intVal, ratVal, floatVal:
   863  		return int64Val(0)
   864  	case complexVal:
   865  		return x.im
   866  	default:
   867  		panic(fmt.Sprintf("%v not numeric", x))
   868  	}
   869  }
   870  
   871  // ----------------------------------------------------------------------------
   872  // Numeric conversions
   873  
   874  // ToInt converts x to an [Int] value if x is representable as an [Int].
   875  // Otherwise it returns an [Unknown].
   876  func ToInt(x Value) Value {
   877  	switch x := x.(type) {
   878  	case int64Val, intVal:
   879  		return x
   880  
   881  	case ratVal:
   882  		if x.val.IsInt() {
   883  			return makeInt(x.val.Num())
   884  		}
   885  
   886  	case floatVal:
   887  		// avoid creation of huge integers
   888  		// (Existing tests require permitting exponents of at least 1024;
   889  		// allow any value that would also be permissible as a fraction.)
   890  		if smallFloat(x.val) {
   891  			i := newInt()
   892  			if _, acc := x.val.Int(i); acc == big.Exact {
   893  				return makeInt(i)
   894  			}
   895  
   896  			// If we can get an integer by rounding up or down,
   897  			// assume x is not an integer because of rounding
   898  			// errors in prior computations.
   899  
   900  			const delta = 4 // a small number of bits > 0
   901  			var t big.Float
   902  			t.SetPrec(prec - delta)
   903  
   904  			// try rounding down a little
   905  			t.SetMode(big.ToZero)
   906  			t.Set(x.val)
   907  			if _, acc := t.Int(i); acc == big.Exact {
   908  				return makeInt(i)
   909  			}
   910  
   911  			// try rounding up a little
   912  			t.SetMode(big.AwayFromZero)
   913  			t.Set(x.val)
   914  			if _, acc := t.Int(i); acc == big.Exact {
   915  				return makeInt(i)
   916  			}
   917  		}
   918  
   919  	case complexVal:
   920  		if re := ToFloat(x); re.Kind() == Float {
   921  			return ToInt(re)
   922  		}
   923  	}
   924  
   925  	return unknownVal{}
   926  }
   927  
   928  // ToFloat converts x to a [Float] value if x is representable as a [Float].
   929  // Otherwise it returns an [Unknown].
   930  func ToFloat(x Value) Value {
   931  	switch x := x.(type) {
   932  	case int64Val:
   933  		return i64tor(x) // x is always a small int
   934  	case intVal:
   935  		if smallInt(x.val) {
   936  			return itor(x)
   937  		}
   938  		return itof(x)
   939  	case ratVal, floatVal:
   940  		return x
   941  	case complexVal:
   942  		if Sign(x.im) == 0 {
   943  			return ToFloat(x.re)
   944  		}
   945  	}
   946  	return unknownVal{}
   947  }
   948  
   949  // ToComplex converts x to a [Complex] value if x is representable as a [Complex].
   950  // Otherwise it returns an [Unknown].
   951  func ToComplex(x Value) Value {
   952  	switch x := x.(type) {
   953  	case int64Val, intVal, ratVal, floatVal:
   954  		return vtoc(x)
   955  	case complexVal:
   956  		return x
   957  	}
   958  	return unknownVal{}
   959  }
   960  
   961  // ----------------------------------------------------------------------------
   962  // Operations
   963  
   964  // is32bit reports whether x can be represented using 32 bits.
   965  func is32bit(x int64) bool {
   966  	const s = 32
   967  	return -1<<(s-1) <= x && x <= 1<<(s-1)-1
   968  }
   969  
   970  // is63bit reports whether x can be represented using 63 bits.
   971  func is63bit(x int64) bool {
   972  	const s = 63
   973  	return -1<<(s-1) <= x && x <= 1<<(s-1)-1
   974  }
   975  
   976  // UnaryOp returns the result of the unary expression op y.
   977  // The operation must be defined for the operand.
   978  // If prec > 0 it specifies the ^ (xor) result size in bits.
   979  // If y is [Unknown], the result is [Unknown].
   980  func UnaryOp(op token.Token, y Value, prec uint) Value {
   981  	switch op {
   982  	case token.ADD:
   983  		switch y.(type) {
   984  		case unknownVal, int64Val, intVal, ratVal, floatVal, complexVal:
   985  			return y
   986  		}
   987  
   988  	case token.SUB:
   989  		switch y := y.(type) {
   990  		case unknownVal:
   991  			return y
   992  		case int64Val:
   993  			if z := -y; z != y {
   994  				return z // no overflow
   995  			}
   996  			return makeInt(newInt().Neg(big.NewInt(int64(y))))
   997  		case intVal:
   998  			return makeInt(newInt().Neg(y.val))
   999  		case ratVal:
  1000  			return makeRat(newRat().Neg(y.val))
  1001  		case floatVal:
  1002  			return makeFloat(newFloat().Neg(y.val))
  1003  		case complexVal:
  1004  			re := UnaryOp(token.SUB, y.re, 0)
  1005  			im := UnaryOp(token.SUB, y.im, 0)
  1006  			return makeComplex(re, im)
  1007  		}
  1008  
  1009  	case token.XOR:
  1010  		z := newInt()
  1011  		switch y := y.(type) {
  1012  		case unknownVal:
  1013  			return y
  1014  		case int64Val:
  1015  			z.Not(big.NewInt(int64(y)))
  1016  		case intVal:
  1017  			z.Not(y.val)
  1018  		default:
  1019  			goto Error
  1020  		}
  1021  		// For unsigned types, the result will be negative and
  1022  		// thus "too large": We must limit the result precision
  1023  		// to the type's precision.
  1024  		if prec > 0 {
  1025  			z.AndNot(z, newInt().Lsh(big.NewInt(-1), prec)) // z &^= (-1)<<prec
  1026  		}
  1027  		return makeInt(z)
  1028  
  1029  	case token.NOT:
  1030  		switch y := y.(type) {
  1031  		case unknownVal:
  1032  			return y
  1033  		case boolVal:
  1034  			return !y
  1035  		}
  1036  	}
  1037  
  1038  Error:
  1039  	panic(fmt.Sprintf("invalid unary operation %s%v", op, y))
  1040  }
  1041  
  1042  func ord(x Value) int {
  1043  	switch x.(type) {
  1044  	default:
  1045  		// force invalid value into "x position" in match
  1046  		// (don't panic here so that callers can provide a better error message)
  1047  		return -1
  1048  	case unknownVal:
  1049  		return 0
  1050  	case boolVal, *stringVal:
  1051  		return 1
  1052  	case int64Val:
  1053  		return 2
  1054  	case intVal:
  1055  		return 3
  1056  	case ratVal:
  1057  		return 4
  1058  	case floatVal:
  1059  		return 5
  1060  	case complexVal:
  1061  		return 6
  1062  	}
  1063  }
  1064  
  1065  // match returns the matching representation (same type) with the
  1066  // smallest complexity for two values x and y. If one of them is
  1067  // numeric, both of them must be numeric. If one of them is Unknown
  1068  // or invalid (say, nil) both results are that value.
  1069  func match(x, y Value) (_, _ Value) {
  1070  	switch ox, oy := ord(x), ord(y); {
  1071  	case ox < oy:
  1072  		x, y = match0(x, y)
  1073  	case ox > oy:
  1074  		y, x = match0(y, x)
  1075  	}
  1076  	return x, y
  1077  }
  1078  
  1079  // match0 must only be called by match.
  1080  // Invariant: ord(x) < ord(y)
  1081  func match0(x, y Value) (_, _ Value) {
  1082  	// Prefer to return the original x and y arguments when possible,
  1083  	// to avoid unnecessary heap allocations.
  1084  
  1085  	switch y.(type) {
  1086  	case intVal:
  1087  		switch x1 := x.(type) {
  1088  		case int64Val:
  1089  			return i64toi(x1), y
  1090  		}
  1091  	case ratVal:
  1092  		switch x1 := x.(type) {
  1093  		case int64Val:
  1094  			return i64tor(x1), y
  1095  		case intVal:
  1096  			return itor(x1), y
  1097  		}
  1098  	case floatVal:
  1099  		switch x1 := x.(type) {
  1100  		case int64Val:
  1101  			return i64tof(x1), y
  1102  		case intVal:
  1103  			return itof(x1), y
  1104  		case ratVal:
  1105  			return rtof(x1), y
  1106  		}
  1107  	case complexVal:
  1108  		switch x1 := x.(type) {
  1109  		case int64Val, intVal, ratVal, floatVal:
  1110  			return vtoc(x1), y
  1111  		}
  1112  	}
  1113  
  1114  	// force unknown and invalid values into "x position" in callers of match
  1115  	// (don't panic here so that callers can provide a better error message)
  1116  	return x, x
  1117  }
  1118  
  1119  // BinaryOp returns the result of the binary expression x op y.
  1120  // The operation must be defined for the operands. If one of the
  1121  // operands is [Unknown], the result is [Unknown].
  1122  // BinaryOp doesn't handle comparisons or shifts; use [Compare]
  1123  // or [Shift] instead.
  1124  //
  1125  // To force integer division of [Int] operands, use op == [token.QUO_ASSIGN]
  1126  // instead of [token.QUO]; the result is guaranteed to be [Int] in this case.
  1127  // Division by zero leads to a run-time panic.
  1128  func BinaryOp(x_ Value, op token.Token, y_ Value) Value {
  1129  	x, y := match(x_, y_)
  1130  
  1131  	switch x := x.(type) {
  1132  	case unknownVal:
  1133  		return x
  1134  
  1135  	case boolVal:
  1136  		y := y.(boolVal)
  1137  		switch op {
  1138  		case token.LAND:
  1139  			return x && y
  1140  		case token.LOR:
  1141  			return x || y
  1142  		}
  1143  
  1144  	case int64Val:
  1145  		a := int64(x)
  1146  		b := int64(y.(int64Val))
  1147  		var c int64
  1148  		switch op {
  1149  		case token.ADD:
  1150  			if !is63bit(a) || !is63bit(b) {
  1151  				return makeInt(newInt().Add(big.NewInt(a), big.NewInt(b)))
  1152  			}
  1153  			c = a + b
  1154  		case token.SUB:
  1155  			if !is63bit(a) || !is63bit(b) {
  1156  				return makeInt(newInt().Sub(big.NewInt(a), big.NewInt(b)))
  1157  			}
  1158  			c = a - b
  1159  		case token.MUL:
  1160  			if !is32bit(a) || !is32bit(b) {
  1161  				return makeInt(newInt().Mul(big.NewInt(a), big.NewInt(b)))
  1162  			}
  1163  			c = a * b
  1164  		case token.QUO:
  1165  			return makeRat(big.NewRat(a, b))
  1166  		case token.QUO_ASSIGN: // force integer division
  1167  			c = a / b
  1168  		case token.REM:
  1169  			c = a % b
  1170  		case token.AND:
  1171  			c = a & b
  1172  		case token.OR:
  1173  			c = a | b
  1174  		case token.XOR:
  1175  			c = a ^ b
  1176  		case token.AND_NOT:
  1177  			c = a &^ b
  1178  		default:
  1179  			goto Error
  1180  		}
  1181  		return int64Val(c)
  1182  
  1183  	case intVal:
  1184  		a := x.val
  1185  		b := y.(intVal).val
  1186  		c := newInt()
  1187  		switch op {
  1188  		case token.ADD:
  1189  			c.Add(a, b)
  1190  		case token.SUB:
  1191  			c.Sub(a, b)
  1192  		case token.MUL:
  1193  			c.Mul(a, b)
  1194  		case token.QUO:
  1195  			return makeRat(newRat().SetFrac(a, b))
  1196  		case token.QUO_ASSIGN: // force integer division
  1197  			c.Quo(a, b)
  1198  		case token.REM:
  1199  			c.Rem(a, b)
  1200  		case token.AND:
  1201  			c.And(a, b)
  1202  		case token.OR:
  1203  			c.Or(a, b)
  1204  		case token.XOR:
  1205  			c.Xor(a, b)
  1206  		case token.AND_NOT:
  1207  			c.AndNot(a, b)
  1208  		default:
  1209  			goto Error
  1210  		}
  1211  		return makeInt(c)
  1212  
  1213  	case ratVal:
  1214  		a := x.val
  1215  		b := y.(ratVal).val
  1216  		c := newRat()
  1217  		switch op {
  1218  		case token.ADD:
  1219  			c.Add(a, b)
  1220  		case token.SUB:
  1221  			c.Sub(a, b)
  1222  		case token.MUL:
  1223  			c.Mul(a, b)
  1224  		case token.QUO:
  1225  			c.Quo(a, b)
  1226  		default:
  1227  			goto Error
  1228  		}
  1229  		return makeRat(c)
  1230  
  1231  	case floatVal:
  1232  		a := x.val
  1233  		b := y.(floatVal).val
  1234  		c := newFloat()
  1235  		switch op {
  1236  		case token.ADD:
  1237  			c.Add(a, b)
  1238  		case token.SUB:
  1239  			c.Sub(a, b)
  1240  		case token.MUL:
  1241  			c.Mul(a, b)
  1242  		case token.QUO:
  1243  			c.Quo(a, b)
  1244  		default:
  1245  			goto Error
  1246  		}
  1247  		return makeFloat(c)
  1248  
  1249  	case complexVal:
  1250  		y := y.(complexVal)
  1251  		a, b := x.re, x.im
  1252  		c, d := y.re, y.im
  1253  		var re, im Value
  1254  		switch op {
  1255  		case token.ADD:
  1256  			// (a+c) + i(b+d)
  1257  			re = add(a, c)
  1258  			im = add(b, d)
  1259  		case token.SUB:
  1260  			// (a-c) + i(b-d)
  1261  			re = sub(a, c)
  1262  			im = sub(b, d)
  1263  		case token.MUL:
  1264  			// (ac-bd) + i(bc+ad)
  1265  			ac := mul(a, c)
  1266  			bd := mul(b, d)
  1267  			bc := mul(b, c)
  1268  			ad := mul(a, d)
  1269  			re = sub(ac, bd)
  1270  			im = add(bc, ad)
  1271  		case token.QUO:
  1272  			// (ac+bd)/s + i(bc-ad)/s, with s = cc + dd
  1273  			ac := mul(a, c)
  1274  			bd := mul(b, d)
  1275  			bc := mul(b, c)
  1276  			ad := mul(a, d)
  1277  			cc := mul(c, c)
  1278  			dd := mul(d, d)
  1279  			s := add(cc, dd)
  1280  			re = add(ac, bd)
  1281  			re = quo(re, s)
  1282  			im = sub(bc, ad)
  1283  			im = quo(im, s)
  1284  		default:
  1285  			goto Error
  1286  		}
  1287  		return makeComplex(re, im)
  1288  
  1289  	case *stringVal:
  1290  		if op == token.ADD {
  1291  			return &stringVal{l: x, r: y.(*stringVal)}
  1292  		}
  1293  	}
  1294  
  1295  Error:
  1296  	panic(fmt.Sprintf("invalid binary operation %v %s %v", x_, op, y_))
  1297  }
  1298  
  1299  func add(x, y Value) Value { return BinaryOp(x, token.ADD, y) }
  1300  func sub(x, y Value) Value { return BinaryOp(x, token.SUB, y) }
  1301  func mul(x, y Value) Value { return BinaryOp(x, token.MUL, y) }
  1302  func quo(x, y Value) Value { return BinaryOp(x, token.QUO, y) }
  1303  
  1304  // Shift returns the result of the shift expression x op s
  1305  // with op == [token.SHL] or [token.SHR] (<< or >>). x must be
  1306  // an [Int] or an [Unknown]. If x is [Unknown], the result is x.
  1307  func Shift(x Value, op token.Token, s uint) Value {
  1308  	switch x := x.(type) {
  1309  	case unknownVal:
  1310  		return x
  1311  
  1312  	case int64Val:
  1313  		if s == 0 {
  1314  			return x
  1315  		}
  1316  		switch op {
  1317  		case token.SHL:
  1318  			z := i64toi(x).val
  1319  			return makeInt(z.Lsh(z, s))
  1320  		case token.SHR:
  1321  			return x >> s
  1322  		}
  1323  
  1324  	case intVal:
  1325  		if s == 0 {
  1326  			return x
  1327  		}
  1328  		z := newInt()
  1329  		switch op {
  1330  		case token.SHL:
  1331  			return makeInt(z.Lsh(x.val, s))
  1332  		case token.SHR:
  1333  			return makeInt(z.Rsh(x.val, s))
  1334  		}
  1335  	}
  1336  
  1337  	panic(fmt.Sprintf("invalid shift %v %s %d", x, op, s))
  1338  }
  1339  
  1340  func cmpZero(x int, op token.Token) bool {
  1341  	switch op {
  1342  	case token.EQL:
  1343  		return x == 0
  1344  	case token.NEQ:
  1345  		return x != 0
  1346  	case token.LSS:
  1347  		return x < 0
  1348  	case token.LEQ:
  1349  		return x <= 0
  1350  	case token.GTR:
  1351  		return x > 0
  1352  	case token.GEQ:
  1353  		return x >= 0
  1354  	}
  1355  	panic(fmt.Sprintf("invalid comparison %v %s 0", x, op))
  1356  }
  1357  
  1358  // Compare returns the result of the comparison x op y.
  1359  // The comparison must be defined for the operands.
  1360  // If one of the operands is [Unknown], the result is
  1361  // false.
  1362  func Compare(x_ Value, op token.Token, y_ Value) bool {
  1363  	x, y := match(x_, y_)
  1364  
  1365  	switch x := x.(type) {
  1366  	case unknownVal:
  1367  		return false
  1368  
  1369  	case boolVal:
  1370  		y := y.(boolVal)
  1371  		switch op {
  1372  		case token.EQL:
  1373  			return x == y
  1374  		case token.NEQ:
  1375  			return x != y
  1376  		}
  1377  
  1378  	case int64Val:
  1379  		y := y.(int64Val)
  1380  		switch op {
  1381  		case token.EQL:
  1382  			return x == y
  1383  		case token.NEQ:
  1384  			return x != y
  1385  		case token.LSS:
  1386  			return x < y
  1387  		case token.LEQ:
  1388  			return x <= y
  1389  		case token.GTR:
  1390  			return x > y
  1391  		case token.GEQ:
  1392  			return x >= y
  1393  		}
  1394  
  1395  	case intVal:
  1396  		return cmpZero(x.val.Cmp(y.(intVal).val), op)
  1397  
  1398  	case ratVal:
  1399  		return cmpZero(x.val.Cmp(y.(ratVal).val), op)
  1400  
  1401  	case floatVal:
  1402  		return cmpZero(x.val.Cmp(y.(floatVal).val), op)
  1403  
  1404  	case complexVal:
  1405  		y := y.(complexVal)
  1406  		re := Compare(x.re, token.EQL, y.re)
  1407  		im := Compare(x.im, token.EQL, y.im)
  1408  		switch op {
  1409  		case token.EQL:
  1410  			return re && im
  1411  		case token.NEQ:
  1412  			return !re || !im
  1413  		}
  1414  
  1415  	case *stringVal:
  1416  		xs := x.string()
  1417  		ys := y.(*stringVal).string()
  1418  		switch op {
  1419  		case token.EQL:
  1420  			return xs == ys
  1421  		case token.NEQ:
  1422  			return xs != ys
  1423  		case token.LSS:
  1424  			return xs < ys
  1425  		case token.LEQ:
  1426  			return xs <= ys
  1427  		case token.GTR:
  1428  			return xs > ys
  1429  		case token.GEQ:
  1430  			return xs >= ys
  1431  		}
  1432  	}
  1433  
  1434  	panic(fmt.Sprintf("invalid comparison %v %s %v", x_, op, y_))
  1435  }
  1436  

View as plain text