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())); } } }
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; }
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()))); }
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(); } }
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."); } }
protected override DyObject LengthOp(ExecutionContext ctx, DyObject self) { var seq = DyIterator.ToEnumerable(ctx, self); return(ctx.HasErrors ? Nil : DyInteger.Get(seq.Count())); }
public DefaultAttribute(long value) { Value = DyInteger.Get(value); }
protected override DyObject LengthOp(ExecutionContext ctx, DyObject self) => DyInteger.Get(((DyEnumerable)self).Count);
public static DyObject GetLength(ExecutionContext ctx, DyObject self) => DyInteger.Get(((DyClass)self).Fields.Count);
private Par[] CompileFunctionParameters(List <DParameter> pars) { var arr = new Par[pars.Count]; var hasVarArg = false; for (var i = 0; i < pars.Count; i++) { var p = pars[i]; if (p.IsVarArgs) { if (hasVarArg) { AddError(CompilerError.VarArgOnlyOne, p.Location); } hasVarArg = true; } if (p.DefaultValue != null) { if (p.IsVarArgs) { AddError(CompilerError.VarArgNoDefaultValue, p.Location); } DyObject val = null; switch (p.DefaultValue.NodeType) { case NodeType.Integer: val = new DyInteger(((DIntegerLiteral)p.DefaultValue).Value); break; case NodeType.Float: val = new DyFloat(((DFloatLiteral)p.DefaultValue).Value); break; case NodeType.Char: val = new DyChar(((DCharLiteral)p.DefaultValue).Value); break; case NodeType.Boolean: val = ((DBooleanLiteral)p.DefaultValue).Value ? DyBool.True : DyBool.False; break; case NodeType.String: val = new DyString(((DStringLiteral)p.DefaultValue).Value); break; case NodeType.Nil: val = DyNil.Instance; break; default: AddError(CompilerError.InvalidDefaultValue, p.DefaultValue.Location, p.Name); break; } arr[i] = new Par(p.Name, val, false); } else { arr[i] = new Par(p.Name, null, p.IsVarArgs); } } return(arr); }