opconst

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: BSD-3-Clause Imports: 6 Imported by: 4

Documentation

Overview

Package opconst defines the representation and operations for VDL constants.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryOp

type BinaryOp uint

BinaryOp represents a binary operation to be performed on two Consts.

const (
	InvalidBinaryOp BinaryOp = iota
	LogicAnd                 //  && logical and
	LogicOr                  //  || logical or
	EQ                       //  == equal
	NE                       //  != not equal
	LT                       //  <  less than
	LE                       //  <= less than or equal
	GT                       //  >  greater than
	GE                       //  >= greater than or equal
	Add                      //  +  add
	Sub                      //  -  subtract
	Mul                      //  *  multiply
	Div                      //  /  divide
	Mod                      //  %  modulo
	BitAnd                   //  &  bitwise and
	BitOr                    //  |  bitwise or
	BitXor                   //  ^  bitwise xor
	LeftShift                //  << left shift
	RightShift               //  >> right shift
)

func ToBinaryOp

func ToBinaryOp(s string) BinaryOp

ToBinaryOp converts s into a BinaryOp, or returns InvalidBinaryOp if it couldn't be converted.

func (BinaryOp) String

func (op BinaryOp) String() string

type Const

type Const struct {
	// contains filtered or unexported fields
}

Const represents a constant value, similar in spirit to Go constants. Consts may be typed or untyped. Typed consts represent unchanging Values; all Values may be converted into valid typed consts, and all typed consts may be converted into valid Values. Untyped consts belong to one of the following categories:

untyped boolean
untyped string
untyped integer
untyped rational

Literal consts are untyped, as are expressions only containing untyped consts. The result of comparison operations is untyped boolean.

Operations are represented by UnaryOp and BinaryOp, and are supported on Consts, but not Values. We support common logical, bitwise, comparison and arithmetic operations. Not all operations are supported on all consts.

Binary ops where both sides are typed consts return errors on type mismatches; e.g. uint32(1) + uint64(1) is an invalid binary add. Ops on typed consts also return errors on loss of precision; e.g. uint32(1.1) returns an error.

Binary ops where one or both sides are untyped consts perform implicit type conversion. E.g. uint32(1) + 1 is a valid binary add, where the right-hand-side is the untyped integer const 1, which is coerced to the uint32 type before the op is performed. Operations only containing untyped consts are performed with "infinite" precision.

The zero Const is invalid.

func Boolean

func Boolean(x bool) Const

Boolean returns an untyped boolean Const.

func EvalBinary

func EvalBinary(op BinaryOp, x, y Const) (Const, error)

EvalBinary returns the result of evaluating (x op y).

func EvalUnary

func EvalUnary(op UnaryOp, x Const) (Const, error)

EvalUnary returns the result of evaluating (op x).

func FromValue

func FromValue(v *vdl.Value) Const

FromValue returns a typed Const based on value v.

func Integer

func Integer(x *big.Int) Const

Integer returns an untyped integer Const.

func Rational

func Rational(x *big.Rat) Const

Rational returns an untyped rational Const.

func String

func String(x string) Const

String returns an untyped string Const.

func (Const) Convert

func (c Const) Convert(t *vdl.Type) (Const, error)

Convert converts c to the target type t, and returns the resulting const. Returns an error if t is nil; you're not allowed to convert into an untyped const.

func (Const) IsValid

func (c Const) IsValid() bool

IsValid returns true iff the c represents a const; it returns false for the zero Const.

func (Const) String

func (c Const) String() string

func (Const) ToValue

func (c Const) ToValue() (*vdl.Value, error)

ToValue converts Const c to a Value.

func (Const) Type

func (c Const) Type() *vdl.Type

Type returns the type of c. Nil indicates c is an untyped const.

type UnaryOp

type UnaryOp uint

UnaryOp represents a unary operation to be performed on a Const.

const (
	InvalidUnaryOp UnaryOp = iota
	LogicNot               //  ! logical not
	Pos                    //  + positive (nop)
	Neg                    //  - negate
	BitNot                 //  ^ bitwise not
)

func ToUnaryOp

func ToUnaryOp(s string) UnaryOp

ToUnaryOp converts s into a UnaryOp, or returns InvalidUnaryOp if it couldn't be converted.

func (UnaryOp) String

func (op UnaryOp) String() string

Jump to

Keyboard shortcuts

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