constant

Imports

Imports #

"strconv"
"fmt"
"go/token"
"math"
"math/big"
"math/bits"
"strconv"
"strings"
"sync"
"unicode/utf8"

Constants & Variables

Bool const #

non-numeric values

const Bool

Complex const #

const Complex

Float const #

const Float

Int const #

numeric values

const Int

String const #

const String

Unknown const #

unknown values

const Unknown Kind = iota

_Kind_index var #

var _Kind_index = [...]uint8{...}

_Kind_name const #

const _Kind_name = "UnknownBoolStringIntFloatComplex"

_log const #

const _log = *ast.BinaryExpr

_m const #

Compute the size of a Word in bytes.

const _m = *ast.UnaryExpr

emptyString var #

var emptyString stringVal

floatVal0 var #

var floatVal0 = floatVal{...}

maxExp const #

Permit fractions with component sizes up to maxExp before switching to using floating-point numbers.

const maxExp = *ast.BinaryExpr

prec const #

Maximum supported mantissa precision. The spec requires at least 256 bits; typical implementations use 512 bits.

const prec = 512

wordSize const #

const wordSize = *ast.BinaryExpr

Type Aliases

Kind type #

Kind specifies the kind of value represented by a [Value].

type Kind int

boolVal type #

type boolVal bool

int64Val type #

type int64Val int64

Interfaces

Value interface #

A Value represents the value of a Go constant.

type Value interface {
Kind() Kind
String() string
ExactString() string
implementsValue()
}

Structs

complexVal struct #

type complexVal struct {
re Value
im Value
}

floatVal struct #

type floatVal struct {
val *big.Float
}

intVal struct #

type intVal struct {
val *big.Int
}

ratVal struct #

type ratVal struct {
val *big.Rat
}

stringVal struct #

type stringVal struct {
mu sync.Mutex
s string
l *stringVal
r *stringVal
}

unknownVal struct #

type unknownVal struct {

}

Functions

BinaryOp function #

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 BinaryOp(x_ Value, op token.Token, y_ Value) Value

BitLen function #

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 BitLen(x Value) int

BoolVal function #

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

func BoolVal(x Value) bool

Bytes function #

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

func Bytes(x Value) []byte

Compare function #

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 Compare(x_ Value, op token.Token, y_ Value) bool

Denom function #

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 Denom(x Value) Value

ExactString method #

func (x unknownVal) ExactString() string

ExactString method #

func (x boolVal) ExactString() string

ExactString method #

func (x complexVal) ExactString() string

ExactString method #

func (x floatVal) ExactString() string

ExactString method #

func (x ratVal) ExactString() string

ExactString method #

func (x intVal) ExactString() string

ExactString method #

func (x int64Val) ExactString() string

ExactString method #

func (x *stringVal) ExactString() string

Float32Val function #

Float32Val is like [Float64Val] but for float32 instead of float64.

func Float32Val(x Value) (float32, bool)

Float64Val function #

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 Float64Val(x Value) (float64, bool)

Imag function #

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

func Imag(x Value) Value

Int64Val function #

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).

func Int64Val(x Value) (int64, bool)

Kind method #

func (int64Val) Kind() Kind

Kind method #

func (unknownVal) Kind() Kind

Kind method #

func (boolVal) Kind() Kind

Kind method #

func (*stringVal) Kind() Kind

Kind method #

func (ratVal) Kind() Kind

Kind method #

func (intVal) Kind() Kind

Kind method #

func (complexVal) Kind() Kind

Kind method #

func (floatVal) Kind() Kind

Make function #

Make returns the [Value] for x. type of x result Kind ---------------------------- bool Bool string String int64 Int *big.Int Int *big.Float Float *big.Rat Float anything else Unknown

func Make(x any) Value

MakeBool function #

MakeBool returns the [Bool] value for b.

func MakeBool(b bool) Value

MakeFloat64 function #

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

func MakeFloat64(x float64) Value

MakeFromBytes function #

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

func MakeFromBytes(bytes []byte) Value

MakeFromLiteral function #

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 MakeFromLiteral(lit string, tok token.Token, zero uint) Value

MakeImag function #

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

func MakeImag(x Value) Value

MakeInt64 function #

MakeInt64 returns the [Int] value for x.

func MakeInt64(x int64) Value

MakeString function #

MakeString returns the [String] value for s.

func MakeString(s string) Value

MakeUint64 function #

MakeUint64 returns the [Int] value for x.

func MakeUint64(x uint64) Value

MakeUnknown function #

MakeUnknown returns the [Unknown] value.

func MakeUnknown() Value

Num function #

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 Num(x Value) Value

Real function #

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

func Real(x Value) Value

Shift function #

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 Shift(x Value, op token.Token, s uint) Value

Sign function #

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 Sign(x Value) int

String method #

func (x boolVal) String() string

String method #

func (x complexVal) String() string

String method #

func (i Kind) String() string

String method #

func (unknownVal) String() string

String method #

String returns a possibly shortened quoted form of the String value.

func (x *stringVal) String() string

String method #

func (x int64Val) String() string

String method #

func (x intVal) String() string

String method #

func (x ratVal) String() string

String method #

String returns a decimal approximation of the Float value.

func (x floatVal) String() string

StringVal function #

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

func StringVal(x Value) string

ToComplex function #

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

func ToComplex(x Value) Value

ToFloat function #

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

func ToFloat(x Value) Value

ToInt function #

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

func ToInt(x Value) Value

Uint64Val function #

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).

func Uint64Val(x Value) (uint64, bool)

UnaryOp function #

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].

func UnaryOp(op token.Token, y Value, prec uint) Value

Val function #

Val returns the underlying value for a given constant. Since it returns an interface, it is up to the caller to type assert the result to the expected type. The possible dynamic return types are: x Kind type of result ----------------------------------------- Bool bool String string Int int64 or *big.Int Float *big.Float or *big.Rat everything else nil

func Val(x Value) any

_ function #

func _()

add function #

func add(x Value, y Value) Value

appendReverse method #

appendReverse appends to list all of x's subpieces, but in reverse, and returns the result. Appending the reversal allows processing the right side in a recursive call and the left side in a loop. Because a chain like a + b + c + d + e is actually represented as ((((a + b) + c) + d) + e), the left-side loop avoids deep recursion. x must be locked.

func (x *stringVal) appendReverse(list []string) []string

cmpZero function #

func cmpZero(x int, op token.Token) bool

i64tof function #

func i64tof(x int64Val) floatVal

i64toi function #

func i64toi(x int64Val) intVal

i64tor function #

func i64tor(x int64Val) ratVal

implementsValue method #

func (complexVal) implementsValue()

implementsValue method #

func (unknownVal) implementsValue()

implementsValue method #

func (ratVal) implementsValue()

implementsValue method #

func (intVal) implementsValue()

implementsValue method #

func (*stringVal) implementsValue()

implementsValue method #

func (int64Val) implementsValue()

implementsValue method #

func (floatVal) implementsValue()

implementsValue method #

func (boolVal) implementsValue()

is32bit function #

is32bit reports whether x can be represented using 32 bits.

func is32bit(x int64) bool

is63bit function #

is63bit reports whether x can be represented using 63 bits.

func is63bit(x int64) bool

itof function #

func itof(x intVal) floatVal

itor function #

func itor(x intVal) ratVal

makeComplex function #

func makeComplex(re Value, im Value) Value

makeFloat function #

func makeFloat(x *big.Float) Value

makeFloatFromLiteral function #

func makeFloatFromLiteral(lit string) Value

makeInt function #

func makeInt(x *big.Int) Value

makeRat function #

func makeRat(x *big.Rat) Value

match function #

match returns the matching representation (same type) with the smallest complexity for two values x and y. If one of them is numeric, both of them must be numeric. If one of them is Unknown or invalid (say, nil) both results are that value.

func match(x Value, y Value) (_ Value, _ Value)

match0 function #

match0 must only be called by match. Invariant: ord(x) < ord(y)

func match0(x Value, y Value) (_ Value, _ Value)

mul function #

func mul(x Value, y Value) Value

newFloat function #

func newFloat() *big.Float

newInt function #

func newInt() *big.Int

newRat function #

func newRat() *big.Rat

ord function #

func ord(x Value) int

quo function #

func quo(x Value, y Value) Value

reverse function #

reverse reverses x in place and returns it.

func reverse(x []string) []string

rtof function #

func rtof(x ratVal) floatVal

smallFloat function #

smallFloat reports whether x would lead to "reasonably"-sized fraction if converted to a *big.Rat.

func smallFloat(x *big.Float) bool

smallFloat64 function #

smallFloat64 reports whether x would lead to "reasonably"-sized fraction if converted to a *big.Rat.

func smallFloat64(x float64) bool

smallInt function #

smallInt reports whether x would lead to "reasonably"-sized fraction if converted to a *big.Rat.

func smallInt(x *big.Int) bool

string method #

string constructs and returns the actual string literal value. If x represents an addition, then it rewrites x to be a single string, to speed future calls. This lazy construction avoids building different string values for all subpieces of a large concatenation. See golang.org/issue/23348.

func (x *stringVal) string() string

sub function #

func sub(x Value, y Value) Value

vtoc function #

func vtoc(x Value) complexVal

Generated with Arrow