Пример #1
0
        private static IEnumerable <T> ReadIndex <T>(BinaryReader reader, Unit unit) where T : DyObject
        {
            var count = reader.ReadInt32();

            var typeId =
                typeof(T) == typeof(DyString) ? DyType.String
                : typeof(T) == typeof(DyInteger) ? DyType.Integer
                : typeof(T) == typeof(DyFloat) ? DyType.Float
                : DyType.Char;

            for (var i = 0; i < count; i++)
            {
                if (typeId == DyType.String)
                {
                    yield return((T)(object)new DyString(reader.ReadString()));
                }
                else if (typeId == DyType.Integer)
                {
                    yield return((T)(object)DyInteger.Get(reader.ReadInt64()));
                }
                else if (typeId == DyType.Float)
                {
                    yield return((T)(object)new DyFloat(reader.ReadDouble()));
                }
                else if (typeId == DyType.Char)
                {
                    yield return((T)(object)new DyChar(reader.ReadChar()));
                }
            }
        }
Пример #2
0
        private int IndexInteger(long val)
        {
            if (!integers.TryGetValue(val, out var idx))
            {
                frame.IndexedIntegers.Add(DyInteger.Get(val));
                idx = frame.IndexedIntegers.Count - 1;
                integers.Add(val, idx);
            }

            return idx;
        }
Пример #3
0
        public DyObject Randomize(ExecutionContext _, [Default(int.MaxValue)] DyObject max, [Default(0)] DyObject min, [Default] DyObject seed)
        {
            int iseed;

            if (seed.TypeId != DyType.Nil)
            {
                iseed = (int)seed.GetInteger();
            }
            else
            {
                var dt  = DateTime.Now;
                var dt2 = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0);
                iseed = (int)(dt2 - dt).Ticks;
            }

            var rnd = new Random(iseed);

            return(DyInteger.Get(rnd.Next((int)min.GetInteger(), (int)max.GetInteger())));
        }
Пример #4
0
        public static DyObject DeserializeObject(BinaryReader reader)
        {
            switch (reader.ReadInt32())
            {
            case -1: return(null);

            case DyType.Nil: return(DyNil.Instance);

            case DyType.Integer: return(DyInteger.Get(reader.ReadInt32()));

            case DyType.Float: return(new DyFloat(reader.ReadDouble()));

            case DyType.String: return(new DyString(reader.ReadString()));

            case DyType.Char: return(new DyChar(reader.ReadChar()));

            case DyType.Bool: return((DyBool)reader.ReadBoolean());

            default: throw new NotSupportedException();
            }
        }
Пример #5
0
        public static DyObject Eval(DNode node)
        {
            switch (node.NodeType)
            {
            case NodeType.String:
                return(new DyString(((DStringLiteral)node).Value));

            case NodeType.Integer:
                return(DyInteger.Get(((DIntegerLiteral)node).Value));

            case NodeType.Float:
                return(new DyFloat(((DFloatLiteral)node).Value));

            case NodeType.Char:
                return(new DyChar(((DCharLiteral)node).Value));

            case NodeType.Boolean:
                return(((DBooleanLiteral)node).Value ? DyBool.True : DyBool.False);

            case NodeType.Nil:
                return(DyNil.Instance);

            case NodeType.Tuple:
            {
                var t = (DTupleLiteral)node;
                return(new DyTuple(GetArray(t.Elements, allowLabels: true)));
            }

            case NodeType.Array:
            {
                var t = (DArrayLiteral)node;
                return(new DyArray(GetArray(t.Elements, allowLabels: false)));
            }

            default:
                throw new DyException($"Node of type {node.NodeType} is not supported.");
            }
        }
Пример #6
0
    protected override DyObject LengthOp(ExecutionContext ctx, DyObject self)
    {
        var seq = DyIterator.ToEnumerable(ctx, self);

        return(ctx.HasErrors ? Nil : DyInteger.Get(seq.Count()));
    }
Пример #7
0
 public DefaultAttribute(long value)
 {
     Value = DyInteger.Get(value);
 }
Пример #8
0
 protected override DyObject LengthOp(ExecutionContext ctx, DyObject self) =>
 DyInteger.Get(((DyEnumerable)self).Count);
Пример #9
0
 public static DyObject GetLength(ExecutionContext ctx, DyObject self) =>
 DyInteger.Get(((DyClass)self).Fields.Count);