1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package muesli
- /*
- Amount of types that will be considered inside a signature.
- Limited mosty to allow hashability, that is, Signature is a map key.
- */
- const TypesInSignature = 32
- /* Parameter describes the name and type of a parameter of a command. */
- type Parameter struct {
- Name WordValue
- Type TypeValue
- }
- // Signature describes the types of the arguments that a callable takes. */
- type Signature struct {
- Types [TypesInSignature]TypeValue
- Names [TypesInSignature]WordValue
- }
- func (s Signature) String() string {
- res := "["
- sep := ""
- for _, typ := range s.Types {
- if typ != ZeroTypeValue {
- res = res + sep + typ.String()
- sep = " "
- }
- }
- res = res + "]"
- return res
- }
- func NewSignature(types ... TypeValue) Signature {
- signature := Signature{}
- for i := 0 ; i < len(types) && i < len(signature.Types) ; i ++ {
- signature.Types[i] = types[i]
- }
- return signature
- }
- // NoSignature is the default signature which means the VM will not do
- // any argument type checking when the callable is called.
- func NoSignature() Signature {
- return Signature{}
- }
- func CalculateSignature(arguments ...Value) Signature {
- signature := Signature{}
- for i := 0; i < len(signature.Types); i++ {
- if i < len(arguments) {
- signature.Types[i] = arguments[i].Type()
- } else {
- signature.Types[i] = AnyTypeValue
- }
- }
- return signature
- }
- func (tv TypeValue) IsMatch(other TypeValue) bool {
- if tv == AnyTypeValue || other == AnyTypeValue {
- return true
- }
- if tv == ZeroTypeValue || other == ZeroTypeValue {
- return true
- }
- return tv == other
- }
- func (signature Signature) IsMatch(other Signature) bool {
- for i, kind := range signature.Types {
- t1 := kind
- t2 := other.Types[i]
- if !t1.IsMatch(t2) {
- return false
- }
- }
- return true
- }
|