Documentation ¶
Overview ¶
Package cbty contains the type and value system for Cirbo.
Index ¶
- Variables
- func QuantityDimensionalities() map[string]units.Dimensionality
- type CallArgs
- type CallParameter
- type CallSignature
- type FunctionImpl
- type ModelImpl
- type Type
- func (t Type) AttrType(name string) Type
- func (t Type) CallSignature() *CallSignature
- func (t Type) CanConcat(o Type) bool
- func (t Type) CanProduct(o Type) bool
- func (t Type) CanSum(o Type) bool
- func (t Type) GoString() string
- func (t Type) HasArithmetic() bool
- func (t Type) HasAttr(name string) bool
- func (t Type) IsModel() bool
- func (t Type) IsNumber() bool
- func (t Type) ModelImpl() interface{}
- func (t Type) Name() string
- func (t Type) NumberDimensionality() units.Dimensionality
- func (t Type) Same(o Type) bool
- type Value
- func BoolVal(v bool) Value
- func FunctionVal(impl FunctionImpl) Value
- func ModelVal(ty Type, raw interface{}) Value
- func NumberValFloat(v float64) Value
- func NumberValInt(v int64) Value
- func ObjectVal(attrs map[string]Value) Value
- func QuantityVal(q units.Quantity) Value
- func StringVal(s string) Value
- func TypeTypeVal(ty Type) Value
- func UnknownVal(ty Type) Value
- func (v Value) Add(o Value) Value
- func (v Value) And(o Value) Value
- func (v Value) AsQuantity() units.Quantity
- func (v Value) AsString() string
- func (v Value) Call(args CallArgs) (Value, source.Diags)
- func (v Value) Concat(o Value) Value
- func (v Value) Divide(o Value) Value
- func (v Value) Equal(o Value) Value
- func (v Value) GetAttr(name string) Value
- func (v Value) GoString() string
- func (v Value) IsKnown() bool
- func (v Value) IsUnknown() bool
- func (v Value) Multiply(o Value) Value
- func (v Value) Not() Value
- func (v Value) Or(o Value) Value
- func (v Value) Same(o Value) bool
- func (v Value) SameType(o Value) bool
- func (v Value) Subtract(o Value) Value
- func (v Value) True() bool
- func (v Value) Type() Type
- func (v Value) TypeValue() Value
- func (v Value) UnwrapModel() interface{}
- func (v Value) UnwrapType() Type
Constants ¶
This section is empty.
Variables ¶
var EmptyObject = Object(map[string]Type{})
EmptyObject is an alias for an object type with no attributes at all.
var One = QuantityVal(units.MakeDimensionlessFloat(1))
One is a value of type Number that represents one
var Zero = QuantityVal(units.MakeDimensionlessInt(0))
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 ¶
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 ¶
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 ¶
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 ¶
QuantityByName returns the quantity type of the given name, or NilType if the name is not recognized.
func (Type) AttrType ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
HasArithmetic returns true if and only if the recieving type supports the arithmetic operators.
func (Type) IsNumber ¶
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 ¶
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.
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 FunctionVal ¶
func FunctionVal(impl FunctionImpl) Value
FunctionVal creates a function value with the given implementation.
func ModelVal ¶
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 NumberValInt ¶
func ObjectVal ¶
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 TypeTypeVal ¶
func UnknownVal ¶
func (Value) Add ¶
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 ¶
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 ¶
AsQuantity returns the units.Quantity value of the receiver if it is known and of a number type, or panics otherwise.
func (Value) AsString ¶
AsString returns the string value of the receiver if it is known and of type string, or panics otherwise.
func (Value) Call ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) IsKnown ¶
IsKnown returns true if the receiver is a known value.
If false is returned, only the type is known.
func (Value) Multiply ¶
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 ¶
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 ¶
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 ¶
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 ¶
SameType returns true if and only if the given value has the same type as the reciever.
func (Value) Subtract ¶
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 ¶
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) 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 ¶
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.