constant

package
v1.21.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

constant パッケージは、Go の未型指定の定数とその対応する操作を表現する値を実装します。

エラーにより値が不明な場合、特別な Unknown 値を使用できます。 不明な値に対する操作は、明示的に指定されない限り、不明な値を生成します。

Example (ComplexNumbers)
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/go/constant"
	"github.com/shogo82148/std/go/token"
)

func main() {
	// 複素数2.3 + 5iを作成します。
	ar := constant.MakeFloat64(2.3)
	ai := constant.MakeImag(constant.MakeInt64(5))
	a := constant.BinaryOp(ar, token.ADD, ai)

	// (2.3 + 5i) * 11を計算する。
	b := constant.MakeUint64(11)
	c := constant.BinaryOp(a, token.MUL, b)

	// cをcomplex128に変換します。
	Ar, exact := constant.Float64Val(constant.Real(c))
	if !exact {
		fmt.Printf("Could not represent real part %s exactly as float64\n", constant.Real(c))
	}
	Ai, exact := constant.Float64Val(constant.Imag(c))
	if !exact {
		fmt.Printf("Could not represent imaginary part %s as exactly as float64\n", constant.Imag(c))
	}
	C := complex(Ar, Ai)

	fmt.Println("literal", 25.3+55i)
	fmt.Println("go/constant", c)
	fmt.Println("complex128", C)

}
Output:


Could not represent real part 25.3 exactly as float64
literal (25.3+55i)
go/constant (25.3 + 55i)
complex128 (25.299999999999997+55i)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BitLen

func BitLen(x Value) int

BitLenは、絶対値xを2進表現するために必要なビット数を返します。xはIntまたはUnknownである必要があります。 xがUnknownの場合、結果は0です。

func BoolVal

func BoolVal(x Value) bool

BoolValは、BoolまたはUnknownである必要があるxのGoのブール値を返します。 xがUnknownの場合、結果はfalseです。

func Bytes

func Bytes(x Value) []byte

Bytes関数はxの絶対値のバイトをリトルエンディアンの 2進表現で返します。xはInt型である必要があります。

func Compare

func Compare(x_ Value, op token.Token, y_ Value) bool

Compareはx op yの比較結果を返します。 オペランドの比較は定義されている必要があります。 オペランドの一つがUnknownの場合、結果はfalseです。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/go/constant"
	"github.com/shogo82148/std/go/token"
	"github.com/shogo82148/std/sort"
)

func main() {
	vs := []constant.Value{
		constant.MakeString("Z"),
		constant.MakeString("bacon"),
		constant.MakeString("go"),
		constant.MakeString("Frame"),
		constant.MakeString("defer"),
		constant.MakeFromLiteral(`"a"`, token.STRING, 0),
	}

	sort.Slice(vs, func(i, j int) bool {
		// vs[i] <= vs[j] と同等です。
		return constant.Compare(vs[i], token.LEQ, vs[j])
	})

	for _, v := range vs {
		fmt.Println(constant.StringVal(v))
	}

}
Output:


Frame
Z
a
bacon
defer
go

func Float32Val

func Float32Val(x Value) (float32, bool)

Float32Valは、float64ではなくfloat32のためのFloat64Valと同様です。

func Float64Val

func Float64Val(x Value) (float64, bool)

Float64Valは、xの最も近いGoのfloat64値とその結果が正確かどうかを返します。 xは数値またはUnknownである必要がありますが、Complexではありません。float64として表現するのに 小さすぎる値(0に近すぎる)の場合、Float64Valは静かに0にアンダーフローします。結果の符号は常に xの符号と一致しますが、0の場合でもです。 xがUnknownの場合、結果は(0、false)です。

func Int64Val

func Int64Val(x Value) (int64, bool)

Int64Valは、xのGo int64値と結果が正確であるかどうかを返します。 xはIntまたはUnknownでなければなりません。結果が正確でない場合は、値は未定義です。 xがUnknownの場合、結果は(0、偽)です。

func Sign

func Sign(x Value) int

Signは、xが0より小さい場合-1、xが0と等しい場合0、xが0より大きい場合1を返します。 xは数値またはUnknownである必要があります。複素数の場合、xが0と等しい場合は0であり、 それ以外の場合は0ではありません。xがUnknownの場合、結果は1です。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/go/constant"
	"github.com/shogo82148/std/go/token"
)

func main() {
	zero := constant.MakeInt64(0)
	one := constant.MakeInt64(1)
	negOne := constant.MakeInt64(-1)

	mkComplex := func(a, b constant.Value) constant.Value {
		b = constant.MakeImag(b)
		return constant.BinaryOp(a, token.ADD, b)
	}

	vs := []constant.Value{
		negOne,
		mkComplex(zero, negOne),
		mkComplex(one, negOne),
		mkComplex(negOne, one),
		mkComplex(negOne, negOne),
		zero,
		mkComplex(zero, zero),
		one,
		mkComplex(zero, one),
		mkComplex(one, one),
	}

	for _, v := range vs {
		fmt.Printf("% d %s\n", constant.Sign(v), v)
	}

}
Output:


-1 -1
-1 (0 + -1i)
-1 (1 + -1i)
-1 (-1 + 1i)
-1 (-1 + -1i)
 0 0
 0 (0 + 0i)
 1 1
 1 (0 + 1i)
 1 (1 + 1i)

func StringVal

func StringVal(x Value) string

StringValはxのGo文字列の値を返します。xはStringまたはUnknownである必要があります。 xがUnknownの場合、結果は""です。

func Uint64Val

func Uint64Val(x Value) (uint64, bool)

Uint64ValはxのGo uint64の値と結果が正確かどうかを返します。 xはIntまたはUnknownでなければなりません。結果が正確でない場合、その値は未定義です。 xがUnknownの場合、結果は(0, false)です。

func Val added in v1.13.0

func Val(x Value) any

Valは指定された定数の基になる値を返します。インターフェースを返すため、呼び出し元が結果を期待する型にキャストすることが求められます。可能な動的な戻り値の型は次の通りです:

xの種類            結果の型
-------------------------------------------
Bool               bool
String             string
Int                int64または*big.Int
Float              *big.Floatまたは*big.Rat
その他のすべて      nil
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/go/constant"
	"github.com/shogo82148/std/math"
)

func main() {
	maxint := constant.MakeInt64(math.MaxInt64)
	fmt.Printf("%v\n", constant.Val(maxint))

	e := constant.MakeFloat64(math.E)
	fmt.Printf("%v\n", constant.Val(e))

	b := constant.MakeBool(true)
	fmt.Printf("%v\n", constant.Val(b))

	b = constant.Make(false)
	fmt.Printf("%v\n", constant.Val(b))

}
Output:


9223372036854775807
6121026514868073/2251799813685248
true
false

Types

type Kind

type Kind int

KindはValueが表す値の種類を指定します。

const (
	// 未知の値
	Unknown Kind = iota

	// 数値ではない値
	Bool
	String

	// 数値の値
	Int
	Float
	Complex
)

func (Kind) String added in v1.18.0

func (i Kind) String() string

type Value

type Value interface {
	// Kind returns the value kind.
	Kind() Kind

	// String returns a short, quoted (human-readable) form of the value.
	// For numeric values, the result may be an approximation;
	// for String values the result may be a shortened string.
	// Use ExactString for a string representing a value exactly.
	String() string

	// ExactString returns an exact, quoted (human-readable) form of the value.
	// If the Value is of Kind String, use StringVal to obtain the unquoted string.
	ExactString() string
	// contains filtered or unexported methods
}

ValueはGo言語の定数の値を表します。

func BinaryOp

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

BinaryOpは、バイナリ式x op yの結果を返します。 演算は、オペランドに対して定義されている必要があります。オペランドの1つがUnknownの場合、結果はUnknownです。 BinaryOpは比較やシフトを処理しません。代わりにCompareまたはShiftを使用してください。

Intオペランドの整数除算を強制するには、token.QUOのかわりにop == token.QUO_ASSIGNを使用します。この場合、結果はIntが保証されます。 ゼロでの除算はランタイムパニックを引き起こします。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/go/constant"
	"github.com/shogo82148/std/go/token"
)

func main() {
	// 11 / 0.5
	a := constant.MakeUint64(11)
	b := constant.MakeFloat64(0.5)
	c := constant.BinaryOp(a, token.QUO, b)
	fmt.Println(c)

}
Output:

22

func Denom

func Denom(x Value) Value

Denomはxの分母を返します。xはInt、Float、またはUnknownでなければなりません。 もしxがUnknownであるか、それを分数として表現するのに大きすぎるか小さすぎる場合、結果はUnknownです。 それ以外の場合、結果は1以上のIntです。

func Imag

func Imag(x Value) Value

Imagはxの虚数部分を返します。xは数値または不明な値である必要があります。 xが不明な場合、結果は不明です。

func Make added in v1.13.0

func Make(x any) Value

Makeはxの値に対するValueを返します。

xの型            結果のKind
----------------------------
bool             Bool
string           String
int64            Int
*big.Int         Int
*big.Float       Float
*big.Rat         Float
それ以外の型     Unknown

func MakeBool

func MakeBool(b bool) Value

MakeBoolはbのBool値を返します。

func MakeFloat64

func MakeFloat64(x float64) Value

MakeFloat64はxのFloat値を返します。 xが-0.0の場合、結果は0.0です。 xが有限ではない場合、結果はUnknownです。

func MakeFromBytes

func MakeFromBytes(bytes []byte) Value

MakeFromBytesは、リトルエンディアンのバイナリ表現のバイトを与えられた場合に、Int値を返します。空のバイトスライス引数は0を表します。

func MakeFromLiteral

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

MakeFromLiteralは、Goリテラル文字列に対応する整数、浮動小数点、虚数、文字、または文字列の値を返します。tokの値は、token.INT、token.FLOAT、token.IMAG、token.CHAR、またはtoken.STRINGのいずれかでなければなりません。最後の引数はゼロでなければなりません。リテラルの文字列構文が無効な場合、結果はUnknownです。

func MakeImag

func MakeImag(x Value) Value

MakeImagはComplex値 x*iを返します; xはInt、Float、またはUnknownである必要があります。 もしxがUnknownの場合、結果もUnknownです。

func MakeInt64

func MakeInt64(x int64) Value

MakeInt64はxのInt値を返します。

func MakeString

func MakeString(s string) Value

MakeStringはsのString値を返します。

func MakeUint64

func MakeUint64(x uint64) Value

MakeUint64はxのInt値を返します。

func MakeUnknown

func MakeUnknown() Value

MakeUnknownはUnknownの値を返します。

func Num

func Num(x Value) Value

Numはxの分子を返します。xはInt、Float、またはUnknownでなければなりません。 xがUnknownであるか、分数として表現するには大きすぎるまたは小さすぎる場合は、結果はUnknownです。 それ以外の場合、結果はxと同じ符号のIntです。

func Real

func Real(x Value) Value

Realは、数値または未知の値でなければならないxの実数部を返します。 xがUnknownの場合、結果はUnknownです。

func Shift

func Shift(x Value, op token.Token, s uint) Value

Shiftはshift式x op sの結果を返します opがtoken.SHLまたはtoken.SHR(<<または>>)である場合の結果です。xは IntまたはUnknownでなければなりません。xがUnknownの場合、結果はxです。

func ToComplex added in v1.6.0

func ToComplex(x Value) Value

ToComplexは、xがComplexとして表現可能な場合はComplexの値に変換します。 それ以外の場合はUnknownを返します。

func ToFloat added in v1.6.0

func ToFloat(x Value) Value

ToFloatはxがFloatとして表現可能な場合、xをFloat値に変換します。 それ以外の場合は、Unknownを返します。

func ToInt added in v1.6.0

func ToInt(x Value) Value

ToIntは、xがIntとして表現可能な場合、xをInt値に変換します。 それ以外の場合は、Unknownを返します。

func UnaryOp

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

UnaryOpは単項演算子op yの結果を返します。 演算はオペランドに対して定義されている必要があります。 prec > 0の場合、^(XOR)の結果のビット数を指定します。 yがUnknownの場合、結果はUnknownです。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/go/constant"
	"github.com/shogo82148/std/go/token"
)

func main() {
	vs := []constant.Value{
		constant.MakeBool(true),
		constant.MakeFloat64(2.7),
		constant.MakeUint64(42),
	}

	for i, v := range vs {
		switch v.Kind() {
		case constant.Bool:
			vs[i] = constant.UnaryOp(token.NOT, v, 0)

		case constant.Float:
			vs[i] = constant.UnaryOp(token.SUB, v, 0)

		case constant.Int:

			// 16ビットの精度を使用します。
			// これは^uint16(v)と等価です。
			vs[i] = constant.UnaryOp(token.XOR, v, 16)
		}
	}

	for _, v := range vs {
		fmt.Println(v)
	}

}
Output:


false
-2.7
65493

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL