Overview

Package constant implements Values representing untyped Go constants and their
corresponding operations.

A special Unknown value may be used when a value is unknown due to an error.
Operations on unknown values produce unknown values unless specified otherwise.

Index

value.go

func

BitLen returns the number of bits required to represent the absolute value x in
binary representation; x must be an Int or an Unknown. If x is Unknown, the
result is 0.

func

  1. func BoolVal(x ) bool

BoolVal returns the Go boolean value of x, which must be a Bool or an Unknown.
If x is Unknown, the result is false.

  1. func Bytes(x ) []byte

Bytes returns the bytes for the absolute value of x in little- endian binary
representation; x must be an Int.

func

  1. func Compare(x_ , op token., y_ Value)

Compare returns the result of the comparison x op y. The comparison must be
defined for the operands. If one of the operands is Unknown, the result is
false.

func Float32Val

  1. func Float32Val(x Value) (, bool)

Float32Val is like Float64Val but for float32 instead of float64.

func

  1. func Float64Val(x ) (float64, )

Float64Val returns the nearest Go float64 value of x and whether the result is
exact; x must be numeric or an Unknown, but not Complex. For values too small
(too close to 0) to represent as float64, Float64Val silently underflows to 0.
The result sign always matches the sign of x, even for 0. If x is Unknown, the
result is (0, false).

func Int64Val

  1. func Int64Val(x Value) (, bool)

Int64Val returns the Go int64 value of x and whether the result is exact; x must
be an Int or an Unknown. If the result is not exact, its value is undefined. If
x is Unknown, the result is (0, false).

  1. func Sign(x ) int

Sign returns -1, 0, or 1 depending on whether x < 0, x == 0, or x > 0; x must be
numeric or Unknown. For complex values x, the sign is 0 if x == 0, otherwise it
is != 0. If x is Unknown, the result is 1.

func

  1. func StringVal(x ) string

StringVal returns the Go string value of x, which must be a String or an
Unknown. If x is Unknown, the result is “”.

func

  1. func Uint64Val(x ) (uint64, )

Uint64Val returns the Go uint64 value of x and whether the result is exact; x
must be an Int or an Unknown. If the result is not exact, its value is
undefined. If x is Unknown, the result is (0, false).

type Kind

Kind specifies the kind of value represented by a Value.

  1. const (
  2. // unknown values
  3. Unknown Kind =
  4.  
  5. // non-numeric values
  6. Bool
  7. String
  8.  
  9. // numeric values
  10. Int
  11. Float
  12. Complex
  13. )

type Value

  1. type Value interface {
  2. Kind() Kind
  3.  
  4. // String returns a short, quoted (human-readable) form of the value.
  5. // For numeric values, the result may be an approximation;
  6. // for String values the result may be a shortened string.
  7. // Use ExactString for a string representing a value exactly.
  8. String()
  9.  
  10. // ExactString returns an exact, quoted (human-readable) form of the value.
  11. // If the Value is of Kind String, use StringVal to obtain the unquoted string.
  12. ExactString() string
  13. // contains filtered or unexported methods
  14. }

A Value represents the value of a Go constant.

func

  1. func BinaryOp(x_ , op token., y_ Value)

BinaryOp returns the result of the binary expression x op y. The operation must
be defined for the operands. If one of the operands is Unknown, the result is
Unknown. BinaryOp doesn’t handle comparisons or shifts; use Compare or Shift
instead.

To force integer division of Int operands, use op == token.QUO_ASSIGN instead of
token.QUO; the result is guaranteed to be Int in this case. Division by zero
leads to a run-time panic.

func Denom

  1. func Denom(x Value)

Denom returns the denominator of x; x must be Int, Float, or Unknown. If x is
Unknown, or if it is too large or small to represent as a fraction, the result
is Unknown. Otherwise the result is an Int >= 1.

func Imag

  1. func Imag(x Value)

Imag returns the imaginary part of x, which must be a numeric or unknown value.
If x is Unknown, the result is Unknown.

func MakeBool

  1. func MakeBool(b bool)

MakeBool returns the Bool value for b.

func MakeFloat64

  1. func MakeFloat64(x float64)

MakeFloat64 returns the Float value for x. If x is not finite, the result is an
Unknown.

  1. func MakeFromBytes(bytes []byte)

MakeFromBytes returns the Int value given the bytes of its little-endian binary
representation. An empty byte slice argument represents 0.

func MakeFromLiteral

  1. func MakeFromLiteral(lit string, tok .Token, zero ) Value

MakeFromLiteral returns the corresponding integer, floating-point, imaginary,
character, or string value for a Go literal string. The tok value must be one of
token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING. The final
argument must be zero. If the literal string syntax is invalid, the result is an
Unknown.

func

MakeImag returns the Complex value x*i; x must be Int, Float, or Unknown. If x
is Unknown, the result is Unknown.

func

  1. func MakeInt64(x ) Value

MakeInt64 returns the Int value for x.

func

  1. func MakeString(s ) Value

MakeString returns the String value for s.

func

  1. func MakeUint64(x ) Value

MakeUint64 returns the Int value for x.

  1. func MakeUnknown()

MakeUnknown returns the Unknown value.

func Num

  1. func Num(x Value)

Num returns the numerator of x; x must be Int, Float, or Unknown. If x is
Unknown, or if it is too large or small to represent as a fraction, the result
is Unknown. Otherwise the result is an Int with the same sign as x.

func Real

  1. func Real(x Value)

Real returns the real part of x, which must be a numeric or unknown value. If x
is Unknown, the result is Unknown.

func Shift

  1. func Shift(x Value, op .Token, s ) Value

Shift returns the result of the shift expression x op s with op == token.SHL or
token.SHR (<< or >>). x must be an Int or an Unknown. If x is Unknown, the
result is x.

func

  1. func ToComplex(x ) Value

ToComplex converts x to a Complex value if x is representable as a Complex.
Otherwise it returns an Unknown.

func

    ToFloat converts x to a Float value if x is representable as a Float. Otherwise
    it returns an Unknown.

    ToInt converts x to an Int value if x is representable as an Int. Otherwise it
    returns an Unknown.

    func

    1. func UnaryOp(op .Token, y , prec uint)

    UnaryOp returns the result of the unary expression op y. The operation must be
    defined for the operand. If prec > 0 it specifies the ^ (xor) result size in
    bits. If y is Unknown, the result is Unknown.