cbty

package
v0.0.0-...-39a7803 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2017 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cbty contains the type and value system for Cirbo.

Index

Constants

This section is empty.

Variables

View Source
var EmptyObject = Object(map[string]Type{})

EmptyObject is an alias for an object type with no attributes at all.

One is a value of type Number that represents one

Zero is a value of type Number that represents zero

Functions

func QuantityDimensionalities

func QuantityDimensionalities() map[string]units.Dimensionality

QuantityDimensionalities returns a map from each of the named quantity types to the dimensionality of that type.

Types

type CallArgs

type CallArgs struct {
	Explicit      map[string]Value
	PosVariadic   []Value
	NamedVariadic map[string]Value

	CallRange source.Range

	// TargetName, if non-empty, is the declared name of a symbol that the
	// call result will be written to. Always empty if the result will not
	// be used to define a symbol
	//
	// Most callables can ignore this, but this can optionally be used to
	// establish the primary name of an object that needs such a name. This
	// includes circuit and device instances since they must be addressable
	// within the assignments file of a project.
	TargetName string

	// Context is actually an *eval.Context, but isn't typed as such because
	// that would create a circular dependency between our packages.
	Context interface{}
}

CallArgs represents a set of arguments being passed to Value.Call.

type CallParameter

type CallParameter struct {
	Type     Type
	Required bool
}

CallParameter describes a single parameter within a CallSignature.

type CallSignature

type CallSignature struct {
	// Parameters describes the explicit parameters of a callable, keyed
	// by the parameter name.
	Parameters map[string]CallParameter

	// Positional lists the names of the subset of parameters that may be
	// passed positionally, in the order that they are expected. If any
	// of the positional parameters are required, they will always be at
	// the start of this list.
	Positional []string

	// AcceptsVariadicPositional indicates that additional positional arguments
	// (further to those listed explicitly in Positional) are accepted.
	AcceptsVariadicPositional bool

	// AcceptsVariadicNamed indicates that additional named arguments (further
	// to those listed explicitly in Parameters) are accepted.
	AcceptsVariadicNamed bool

	// Result is the type of values returned from any call.
	Result Type
}

CallSignature describes the formal parameters of a callable value.

func (*CallSignature) Same

func (s *CallSignature) Same(o *CallSignature) bool

Same returns true if the receiver and the other given signature are equivalent.

type FunctionImpl

type FunctionImpl struct {
	Signature *CallSignature
	Callback  func(args CallArgs) (Value, source.Diags)
}

FunctionImpl represents the implementation of a particular function.

type ModelImpl

type ModelImpl interface {
	Name() string
	SuitableValue(raw interface{}) bool

	GetAttr(raw interface{}, name string) Value

	CallSignature() *CallSignature
	Call(callee interface{}, args CallArgs) (Value, source.Diags)
}

ModelImpl is an interface that can be implemented in order to create a model type, which is a type that wraps a Go object (usually a model from the cbo package) so it can be passed around in the language and have attributes accessed on it.

type Type

type Type struct {
	// contains filtered or unexported fields
}
var Angle Type = QuantityByName("Angle")

Angle is a quantity type of dimensionality [angle].

var AngularSpeed Type = QuantityByName("AngularSpeed")

AngularSpeed is a quantity type of dimensionality [angle][T]⁻¹.

var Area Type = QuantityByName("Area")

Area is a quantity type of dimensionality [L]².

var Bool Type
var Capacitance Type = QuantityByName("Capacitance")

Capacitance is a quantity type of dimensionality [I]²[T]⁴[M]⁻¹[L]⁻².

var Charge Type = QuantityByName("Charge")

Charge is a quantity type of dimensionality [I][T].

var Conductance Type = QuantityByName("Conductance")

Conductance is a quantity type of dimensionality [I]²[T]³[M]⁻¹[L]⁻².

var Conductivity Type = QuantityByName("Conductivity")

Conductivity is a quantity type of dimensionality [I]²[T]³[M]⁻¹[L]⁻³.

var Current Type = QuantityByName("Current")

Current is a quantity type of dimensionality [I].

var Force Type = QuantityByName("Force")

Force is a quantity type of dimensionality [M][L][T]⁻².

var Frequency Type = QuantityByName("Frequency")

Frequency is a quantity type of dimensionality [T]⁻¹.

var Illuminance Type = QuantityByName("Illuminance")

Illuminance is a quantity type of dimensionality [J][L]⁻².

var Inductance Type = QuantityByName("Inductance")

Inductance is a quantity type of dimensionality [M][L]²[I]⁻²[T]⁻².

var Length Type = QuantityByName("Length")

Length is a quantity type of dimensionality [L].

var LuminousIntensity Type = QuantityByName("LuminousIntensity")

LuminousIntensity is a quantity type of dimensionality [J].

var Mass Type = QuantityByName("Mass")

Mass is a quantity type of dimensionality [M].

var Momentum Type = QuantityByName("Momentum")

Momentum is a quantity type of dimensionality [M][L][T]⁻¹.

var NilType Type

NilType is an invalid type that serves as the zero value of type Type.

NilType is not a real type and so is used only to signal the absense of a type when returning from functions.

var Number Type = QuantityByName("Number")

Number is the dimensionless quantity type.

var Power Type = QuantityByName("Power")

Power is a quantity type of dimensionality [M][L]²[T]⁻³.

var Resistance Type = QuantityByName("Resistance")

Resistance is a quantity type of dimensionality [M][L]²[I]⁻²[T]⁻³.

var Resistivity Type = QuantityByName("Resistivity")

Resistivity is a quantity type of dimensionality [M][L]³[I]⁻²[T]⁻³.

var Speed Type = QuantityByName("Speed")

Speed is a quantity type of dimensionality [L][T]⁻¹.

var String Type
var Time Type = QuantityByName("Time")

Time is a quantity type of dimensionality [T].

var TypeType Type

TypeType is a Type whose values are themselves types.

This type is used to allow Cirbo programs to use types in expressions, primarily for the purpose of declaring the types of attributes, function arguments, etc.

var Voltage Type = QuantityByName("Voltage")

Voltage is a quantity type of dimensionality [M][L]²[I]⁻¹[T]⁻³.

func Function

func Function(sig *CallSignature) Type

Function returns a function type with the given call signature.

func Model

func Model(impl ModelImpl) Type

Model creates a new model type with the given model implementation.

Each call to Model produces a distinct type. That is, Same will return true with two Type values that were the result of the same call, but false for any two Type values that were the result of different calls, even if the underlying implementation is identical.

func Object

func Object(atys map[string]Type) Type

Object creates a new object type with the given attribute types.

Object is a generic data structure with attributes. It has no special meaning, and more meaningful types that themselves have attributes are not considered subtypes of this type.

func Quantity

func Quantity(dim units.Dimensionality) Type

func QuantityByName

func QuantityByName(name string) Type

QuantityByName returns the quantity type of the given name, or NilType if the name is not recognized.

func (Type) AttrType

func (t Type) AttrType(name string) Type

AttrType returns the type of the attribute of the given name, or NilType if the receiver has no such attribute.

func (Type) CallSignature

func (t Type) CallSignature() *CallSignature

CallSignature returns the expected signature for calls to values of the recieving type, or nil if the type cannot be called at all.

func (Type) CanConcat

func (t Type) CanConcat(o Type) bool

CanConcat returns true if the given type can concatenate values of the other given type.

Always returns false if the receiver doesn't support concatenation at all.

func (Type) CanProduct

func (t Type) CanProduct(o Type) bool

CanProduct returns true if the given type can support the Multiply and Divide operations with values of the other given type.

Always returns false if the receiver doesn't support arithmetic at all.

func (Type) CanSum

func (t Type) CanSum(o Type) bool

CanSum returns true if the given type can support the Add and Subtract operations with values of the other given type.

Always returns false if the receiver doesn't support arithmetic at all.

func (Type) GoString

func (t Type) GoString() string

GoString returns a representation of the receiving type as Go syntax, suitable for display in tests and other internal debug messages.

This result must never be displayed to cirbo end-users.

func (Type) HasArithmetic

func (t Type) HasArithmetic() bool

HasArithmetic returns true if and only if the recieving type supports the arithmetic operators.

func (Type) HasAttr

func (t Type) HasAttr(name string) bool

HasAttr returns true if the receiver has an attribute of the given name.

func (Type) IsModel

func (t Type) IsModel() bool

IsModel returns true if the receiver is a model type.

func (Type) IsNumber

func (t Type) IsNumber() bool

IsNumber returns true if the receiver is a number type. Number includes both dimensionless numbers and dimensioned quantities.

func (Type) ModelImpl

func (t Type) ModelImpl() interface{}

ModelImpl returns the underlying implementation of the receiver if it is a model type, or nil otherwise.

This should only be used by the package that created the type, to avoid exposing implementation details.

func (Type) Name

func (t Type) Name() string

Name returns a name for the receiving type that is suitable for display to cirbo end-users.

func (Type) NumberDimensionality

func (t Type) NumberDimensionality() units.Dimensionality

NumberDimensionality returns the dimensionality of the receiving number type, or panics if the receiver is not a number type.

func (Type) Same

func (t Type) Same(o Type) bool

Same returns true if and only if the given type is the same as the receiver.

type Value

type Value struct {
	// contains filtered or unexported fields
}
var EmptyObjectVal Value

EmptyObjectVal is the only known value of type EmptyObject.

var False Value

True is the falsey value of type Bool

var NilValue Value
var PlaceholderVal Value

PlaceholderVal is an unknown value whose type is also unknown. This can be used as a placeholder where a valid value is required but no specific value is appropriate. It implements all operations with itself as the result.

var True Value

True is the truthy value of type Bool

func BoolVal

func BoolVal(v bool) Value

func FunctionVal

func FunctionVal(impl FunctionImpl) Value

FunctionVal creates a function value with the given implementation.

func ModelVal

func ModelVal(ty Type, raw interface{}) Value

ModelVal creates a new value of a model type.

The given raw value must be a pointer and must be of a type appropriate for the given model type, or else this function will panic. What constitutes an "appropriate" value depends on the model type.

This function will panic also if the given type is not a model type.

func NumberValFloat

func NumberValFloat(v float64) Value

func NumberValInt

func NumberValInt(v int64) Value

func ObjectVal

func ObjectVal(attrs map[string]Value) Value

ObjectVal creates a value of an object type constructed from the types of the given attribute values.

Although this function does not enforce it, the language internals assume that attribute names will always be valid identifiers in the language syntax. An object with invalid attribute names will cause undefined behavior.

func QuantityVal

func QuantityVal(q units.Quantity) Value

func StringVal

func StringVal(s string) Value

func TypeTypeVal

func TypeTypeVal(ty Type) Value

func UnknownVal

func UnknownVal(ty Type) Value

func (Value) Add

func (v Value) Add(o Value) Value

Add returns the sum of the receiver and the given other value.

This function will panic if the value type does not support arithmetic or cannot add a value of the other type.

func (Value) And

func (v Value) And(o Value) Value

And returns True if the receiver and the other given value are both True, an unknown Bool if either is unknown, or False otherwise.

If the either value is not of type Bool, this method will panic.

func (Value) AsQuantity

func (v Value) AsQuantity() units.Quantity

AsQuantity returns the units.Quantity value of the receiver if it is known and of a number type, or panics otherwise.

func (Value) AsString

func (v Value) AsString() string

AsString returns the string value of the receiver if it is known and of type string, or panics otherwise.

func (Value) Call

func (v Value) Call(args CallArgs) (Value, source.Diags)

Call invokes the receiver, or panics if it is not callable.

The given arguments must conform to the call signature of the receiver's type, or the result is undefined.

posVarArgs and namedVarArgs must be nil when calling callables whose signatures don't permit the respective kind of variadic argument.

func (Value) Concat

func (v Value) Concat(o Value) Value

Concat concatenates the other given value onto the end of the reciever and returns the result.

This function will panic if the receiver type does not support concatenation with the other value's type.

func (Value) Divide

func (v Value) Divide(o Value) Value

Divide returns the quotient of the receiver by the given other value.

This function will panic if the value type does not support arithmetic or cannot divide by a value of the other type.

func (Value) Equal

func (v Value) Equal(o Value) Value

Equal returns True if and only if the value and the receiver represent the same value.

No two values of different types are ever equal. If either value is unknown then the result itself is an unknown boolean.

func (Value) GetAttr

func (v Value) GetAttr(name string) Value

GetAttr returns the value of the attribute with the given name on the receiver, or panics if the receiever has no such attribute.

func (Value) GoString

func (v Value) GoString() string

func (Value) IsKnown

func (v Value) IsKnown() bool

IsKnown returns true if the receiver is a known value.

If false is returned, only the type is known.

func (Value) IsUnknown

func (v Value) IsUnknown() bool

IsUnknown is the opposite of IsKnown, for convenience.

func (Value) Multiply

func (v Value) Multiply(o Value) Value

Multiply returns the product of the receiver and the given other value.

This function will panic if the value type does not support arithmetic or cannot multiply a value of the other type.

func (Value) Not

func (v Value) Not() Value

Not returns the inverse of the reciever, which must be of type Bool.

If the receiver is not a boolean value, this method will panic.

func (Value) Or

func (v Value) Or(o Value) Value

Or returns True if either the receiver or the other given value is True, or if both are true. It returns an unknown Bool if either is unknown. Otherwise, it returns False.

If the either value is not of type Bool, this method will panic.

func (Value) Same

func (v Value) Same(o Value) bool

Same returns true if and only if the given value and the reciever are identical.

Identity is different than equality in that two unknown values are identical if their types are identical, whereas an equality test would return an unknown boolean value.

This method is primarily for test assertions. All code implementing the language itself should use Equal and handle unknown values.

func (Value) SameType

func (v Value) SameType(o Value) bool

SameType returns true if and only if the given value has the same type as the reciever.

func (Value) Subtract

func (v Value) Subtract(o Value) Value

Subtract returns the difference between the receiver and the given other value.

This function will panic if the value type does not support arithmetic or cannot subtract a value of the other type.

func (Value) True

func (v Value) True() bool

True returns true if the receiver is True, false if the receiver is False, and panics otherwise.

This method converts a known cty.Bool value into a native Go bool value.

func (Value) Type

func (v Value) Type() Type

Type returns the type of the receiever.

func (Value) TypeValue

func (v Value) TypeValue() Value

TypeValue returns the type of the receiver as a Value of type TypeType.

func (Value) UnwrapModel

func (v Value) UnwrapModel() interface{}

UnwrapModel returns the raw value encapsulated in the receiver if it is a known value of a model type.

If the receiver is unknown or if it is of a non-model type then this method will panic.

The dynamic type of the returned value depends on which model type the value belongs to.

func (Value) UnwrapType

func (v Value) UnwrapType() Type

UnwrapType returns the Type encapsulated in the receiver if it is a known value of type TypeType.

If the receiver is unknown or is of some other type then this method will panic.

Directories

Path Synopsis
Package globals contains definitions of the symbols available in the top-most symbol table.
Package globals contains definitions of the symbols available in the top-most symbol table.

Jump to

Keyboard shortcuts

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