Exemplo n.º 1
0
        internal static object StringToValue(TypeVar type, string value)
        {
            //if (value == "0,0") value = "0";
            try
            {
                switch (type)
                {
                case TypeVar.Boolean: return(bool.Parse(value));

                case TypeVar.Integer: return((int)(float.Parse(value)));

                case TypeVar.Float: return(float.Parse(value));

                case TypeVar.Long: return((int)(long.Parse(value)));

                case TypeVar.Double: return(double.Parse(value));

                case TypeVar.String: return(value);
                }
            }
            catch (Exception)
            {   // не смог сконвертировать  --> 0,0
                Util.errorMessage("type:" + type.ToString() + "; не смог сконвертировать ", value);
                return(null);
            }
            return(null);
        }
Exemplo n.º 2
0
 public static IType TypeMap(Func <int, TypeVar, IType> onVar, int c, IType t)
 {
     IType Walk(int c, IType t)
     {
         return(t switch
         {
             TypeVar v => onVar(c, v),
             TypeId i => i,
             TypeString ts => ts,
             TypeUnit u => u,
             TypeRecord r => new TypeRecord(r.Variants.Select(p => (p.Item1, Walk(c, p.Item2)))),
             TypeFloat f => f,
             TypeBool b => b,
             TypeNat n => n,
             TypeArrow a => new TypeArrow(Walk(c, a.From), Walk(c, a.To)),
             TypeVariant tv => new TypeVariant(tv.Variants.Select(p => (p.Item1, Walk(c, p.Item2)))),
             _ => throw new InvalidOperationException()
         });
     }
Exemplo n.º 3
0
        internal static object DefaultValue(TypeVar type)
        {
            switch (type)
            {
            case TypeVar.Boolean: return(false);

            case TypeVar.Integer: return(0);

            case TypeVar.Float: return(0.0f);

            case TypeVar.Long: return(0L);

            case TypeVar.Double: return(0d);

            case TypeVar.String: return("");
            }
            { Util.errorMessage("не смог установить значения по дефолту ", type.ToString()); }
            return(null);
        }
Exemplo n.º 4
0
        internal static object DefaultValue(TypeVar type, int size)
        {
            if (size <= 1)
            {
                return(Util.DefaultValue(type));
            }
            switch (type)
            {
            case TypeVar.Boolean: { bool[] b = new bool[size]; for (int i = 0; i < b.Length; i++)
                                    {
                                        b[i] = false;
                                    }
                                    return(b); }

            case TypeVar.Integer: { int[] b = new int[size]; for (int i = 0; i < b.Length; i++)
                                    {
                                        b[i] = 0;
                                    }
                                    return(b); }

            case TypeVar.Float: { float[] b = new float[size]; for (int i = 0; i < b.Length; i++)
                                  {
                                      b[i] = 0.0F;
                                  }
                                  return(b); }

            case TypeVar.Long: { long[] b = new long[size]; for (int i = 0; i < b.Length; i++)
                                 {
                                     b[i] = 0L;
                                 }
                                 return(b); }

            case TypeVar.Double: { double[] b = new double[size]; for (int i = 0; i < b.Length; i++)
                                   {
                                       b[i] = 0.0D;
                                   }
                                   return(b); }

            case TypeVar.String: return("");
            }
            Util.errorMessage("не смог установить значения по дефолту ", type.ToString() + " " + size.ToString());
            return(null);
        }
        private void SpecializeMembers()
        {
            var location = new Location(this);

            _members["TypeVar"] = new TypeVar(this);
            _members["NewType"] = SpecializeNewType(location);
            _members["Type"]    = new Type(this);

            _members["Iterator"] = new SpecializedGenericType("Iterator", CreateIteratorType, this);

            _members["Iterable"]        = new SpecializedGenericType("Iterable", typeArgs => CreateListType("Iterable", BuiltinTypeId.List, typeArgs, false), this);
            _members["Sequence"]        = new SpecializedGenericType("Sequence", typeArgs => CreateListType("Sequence", BuiltinTypeId.List, typeArgs, false), this);
            _members["MutableSequence"] = new SpecializedGenericType("MutableSequence",
                                                                     typeArgs => CreateListType("MutableSequence", BuiltinTypeId.List, typeArgs, true), this);
            _members["List"] = new SpecializedGenericType("List",
                                                          typeArgs => CreateListType("List", BuiltinTypeId.List, typeArgs, true), this);

            _members["MappingView"] = new SpecializedGenericType("MappingView",
                                                                 typeArgs => CreateDictionary("MappingView", typeArgs, false), this);

            _members["KeysView"]   = new SpecializedGenericType("KeysView", CreateKeysViewType, this);
            _members["ValuesView"] = new SpecializedGenericType("ValuesView", CreateValuesViewType, this);
            _members["ItemsView"]  = new SpecializedGenericType("ItemsView", CreateItemsViewType, this);

            _members["AbstractSet"] = new SpecializedGenericType("AbstractSet",
                                                                 typeArgs => CreateListType("AbstractSet", BuiltinTypeId.Set, typeArgs, true), this);
            _members["Set"] = new SpecializedGenericType("Set",
                                                         typeArgs => CreateListType("Set", BuiltinTypeId.Set, typeArgs, true), this);
            _members["MutableSet"] = new SpecializedGenericType("MutableSet",
                                                                typeArgs => CreateListType("MutableSet", BuiltinTypeId.Set, typeArgs, true), this);
            _members["FrozenSet"] = new SpecializedGenericType("FrozenSet",
                                                               typeArgs => CreateListType("FrozenSet", BuiltinTypeId.Set, typeArgs, false), this);

            _members["Tuple"] = new SpecializedGenericType("Tuple", CreateTupleType, this);

            _members["Mapping"] = new SpecializedGenericType("Mapping",
                                                             typeArgs => CreateDictionary("Mapping", typeArgs, false), this);
            _members["MutableMapping"] = new SpecializedGenericType("MutableMapping",
                                                                    typeArgs => CreateDictionary("MutableMapping", typeArgs, true), this);
            _members["Dict"] = new SpecializedGenericType("Dict",
                                                          typeArgs => CreateDictionary("Dict", typeArgs, true), this);
            _members["OrderedDict"] = new SpecializedGenericType("OrderedDict",
                                                                 typeArgs => CreateDictionary("OrderedDict", typeArgs, true), this);
            _members["DefaultDict"] = new SpecializedGenericType("DefaultDict",
                                                                 typeArgs => CreateDictionary("DefaultDict", typeArgs, true), this);

            _members["Union"] = new SpecializedGenericType("Union", CreateUnion, this);

            _members["Counter"] = Specialized.Function("Counter", this, GetMemberDocumentation("Counter"),
                                                       Interpreter.GetBuiltinType(BuiltinTypeId.Int)).CreateInstance(ArgumentSet.WithoutContext);

            // TODO: make these classes that support __float__, etc per spec.
            //_members["SupportsInt"] = Interpreter.GetBuiltinType(BuiltinTypeId.Int);
            //_members["SupportsFloat"] = Interpreter.GetBuiltinType(BuiltinTypeId.Float);
            //_members["SupportsComplex"] = Interpreter.GetBuiltinType(BuiltinTypeId.Complex);
            //_members["SupportsBytes"] = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            _members["ByteString"] = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);

            _members["NamedTuple"] = new NamedTuple(this);

            _members["Any"]    = new AnyType(this);
            _members["AnyStr"] = CreateAnyStr();

            _members["Optional"] = new SpecializedGenericType("Optional", CreateOptional, this);
            _members["Type"]     = new SpecializedGenericType("Type", CreateType, this);

            _members["Generic"] = new SpecializedGenericType("Generic", CreateGenericClassBase, this);
        }