v.io: v.io/x/ref/lib/vdl/opconst Index | Files

package opconst

import "v.io/x/ref/lib/vdl/opconst"

Package opconst defines the representation and operations for VDL constants.


Package Files

const.go op.go

type BinaryOp Uses

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 Uses

func ToBinaryOp(s string) BinaryOp

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

func (BinaryOp) String Uses

func (op BinaryOp) String() string

type Const Uses

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 Uses

func Boolean(x bool) Const

Boolean returns an untyped boolean Const.

func EvalBinary Uses

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

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

func EvalUnary Uses

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

EvalUnary returns the result of evaluating (op x).

func FromValue Uses

func FromValue(v *vdl.Value) Const

FromValue returns a typed Const based on value v.

func Integer Uses

func Integer(x *big.Int) Const

Integer returns an untyped integer Const.

func Rational Uses

func Rational(x *big.Rat) Const

Rational returns an untyped rational Const.

func String Uses

func String(x string) Const

String returns an untyped string Const.

func (Const) Convert Uses

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 Uses

func (c Const) IsValid() bool

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

func (Const) String Uses

func (c Const) String() string

func (Const) ToValue Uses

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

ToValue converts Const c to a Value.

func (Const) Type Uses

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

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

type UnaryOp Uses

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 Uses

func ToUnaryOp(s string) UnaryOp

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

func (UnaryOp) String Uses

func (op UnaryOp) String() string

Package opconst imports 6 packages (graph) and is imported by 2 packages. Updated 2020-06-09. Refresh now. Tools for package owners.