示例#1
0
 public static PrtValue PrtMkDefaultValue(PrtType type)
 {
     if (type is PrtAnyType || type is PrtNullType || type is PrtEventType || type is PrtMachineType || type is PrtInterfaceType)
     {
         return(@null);
     }
     else if (type is PrtIntType)
     {
         return(new PrtIntValue());
     }
     else if (type is PrtFloatType)
     {
         return(new PrtFloatValue());
     }
     else if (type is PrtEnumType)
     {
         PrtEnumType enumType = type as PrtEnumType;
         return(new PrtEnumValue(enumType.DefaultConstant, 0));
     }
     else if (type is PrtBoolType)
     {
         return(new PrtBoolValue());
     }
     else if (type is PrtMapType)
     {
         return(new PrtMapValue());
     }
     else if (type is PrtSeqType)
     {
         return(new PrtSeqValue());
     }
     else if (type is PrtNamedTupleType)
     {
         return(new PrtNamedTupleValue(type as PrtNamedTupleType));
     }
     else if (type is PrtTupleType)
     {
         return(new PrtTupleValue(type as PrtTupleType));
     }
     else
     {
         throw new PrtInternalException("Invalid type in PrtMkDefaultType");
     }
 }
示例#2
0
        //public abstract bool Equals(PrtValue val);

        public static bool PrtInhabitsType(PrtValue value, PrtType type)
        {
            if (type is PrtAnyType)
            {
                return(true);
            }
            else if (value.Equals(@null))
            {
                return(type is PrtNullType || type is PrtEventType || type is PrtMachineType);
            }
            else if (type is PrtEnumType)
            {
                PrtEnumType enumType = type as PrtEnumType;
                PrtIntValue intValue = value as PrtIntValue;
                if (intValue == null)
                {
                    return(false);
                }
                return(enumType.enumConstants.ContainsKey(intValue.nt));
            }
            else if (type is PrtIntType)
            {
                return(value is PrtIntValue);
            }
            else if (type is PrtFloatType)
            {
                return(value is PrtFloatValue);
            }
            else if (type is PrtBoolType)
            {
                return(value is PrtBoolValue);
            }
            else if (type is PrtEventType)
            {
                return(value is PrtEventValue);
            }
            else if (type is PrtMachineType)
            {
                return(value is PrtMachineValue || value is PrtInterfaceValue);
            }
            else if (type is PrtInterfaceType)
            {
                var interValue = value as PrtInterfaceValue;
                if (interValue == null)
                {
                    return(false);
                }
                else
                {
                    if (interValue.permissions == null)
                    {
                        return((type as PrtInterfaceType).permissions == null);
                    }
                    else
                    {
                        if ((type as PrtInterfaceType).permissions == null)
                        {
                            return(false);
                        }
                    }

                    if (interValue.permissions.Count() != (type as PrtInterfaceType).permissions.Count())
                    {
                        return(false);
                    }
                    else
                    {
                        foreach (var ev in interValue.permissions)
                        {
                            if (!(type as PrtInterfaceType).permissions.Contains(ev))
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                }
            }
            else if (type is PrtNamedTupleType) // must come before PrtTupleType since PrtNamedTupleType derives from PrtTupleType
            {
                var nmtupType = type as PrtNamedTupleType;
                var nmtupVal  = value as PrtNamedTupleValue;
                if (nmtupVal == null)
                {
                    return(false);
                }
                if (nmtupVal.fieldValues.Count != nmtupType.fieldTypes.Count)
                {
                    return(false);
                }
                for (int i = 0; i < nmtupVal.fieldValues.Count; i++)
                {
                    if (nmtupVal.fieldNames[i] != nmtupType.fieldNames[i])
                    {
                        return(false);
                    }
                }
                for (int i = 0; i < nmtupVal.fieldValues.Count; i++)
                {
                    if (!PrtInhabitsType(nmtupVal.fieldValues[i], nmtupType.fieldTypes[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else if (type is PrtTupleType)
            {
                var tupType = type as PrtTupleType;
                var tupVal  = value as PrtTupleValue;
                if (tupVal == null)
                {
                    return(false);
                }
                if (tupVal.fieldValues.Count != tupType.fieldTypes.Count)
                {
                    return(false);
                }
                for (int i = 0; i < tupVal.fieldValues.Count; i++)
                {
                    if (!PrtInhabitsType(tupVal.fieldValues[i], tupType.fieldTypes[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else if (type is PrtMapType)
            {
                var mapType = type as PrtMapType;
                var mapVal  = value as PrtMapValue;
                if (mapVal == null)
                {
                    return(false);
                }
                foreach (var p in mapVal.keyToValueMap)
                {
                    if (!PrtInhabitsType(p.Key.key, mapType.keyType))
                    {
                        return(false);
                    }
                    if (!PrtInhabitsType(p.Value, mapType.valType))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else if (type is PrtSeqType)
            {
                var seqType = type as PrtSeqType;
                var seqVal  = value as PrtSeqValue;
                if (seqVal == null)
                {
                    return(false);
                }
                foreach (var elem in seqVal.elements)
                {
                    if (!PrtInhabitsType(elem, seqType.elemType))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                throw new PrtInternalException("Unknown type in PrtInhabitsType");
            }
        }