Esempio n. 1
0
        public DyObject Print(ExecutionContext ctx, [VarArg] DyObject values, [Default(",")] DyObject separator, [Default("\n")] DyObject terminator)
        {
            var fst = true;

            foreach (var a in (DyTuple)values)
            {
                if (!fst)
                {
                    Console.Write(separator.TypeId == DyType.String ? separator.GetString() : separator.ToString(ctx).ToString());
                }

                if (a.TypeId == DyType.String)
                {
                    Console.Write(a.GetString());
                }
                else
                {
                    Console.Write(a.ToString(ctx));
                }

                fst = false;

                if (ctx.Error != null)
                {
                    break;
                }
            }

            Console.Write(terminator.TypeId == DyType.String ? terminator.GetString() : terminator.ToString(ctx).ToString());
            return(DyNil.Instance);
        }
Esempio n. 2
0
        public static T Read <T>(string[] args, IDictionary <string, object> config) where T : IOptionBag, new()
        {
            var options = Parse(args, config);
            var bag     = ProcessOptionBag <T>(options);

            if (options.Count > 0)
            {
                var arr = new DyObject[options.Count];
                var cc  = 0;

                foreach (var kv in options)
                {
                    arr[cc++] = new DyLabel(kv.Key.TrimStart('-'), kv.Value.ToObject());

                    if (kv.Key[0] != '-')
                    {
                        throw new DyaException($"Unknown switch -{kv.Key}.");
                    }
                }

                bag.UserArguments = new DyTuple(arr);
            }

            return(bag);
        }
Esempio n. 3
0
        public DyObject Parse(ExecutionContext ctx, DyObject expression)
        {
            if (expression.TypeId != DyType.String)
            {
                return(ctx.InvalidType(expression));
            }

            try
            {
                var p   = new DyParser();
                var res = p.Parse(SourceBuffer.FromString(expression.GetString()));

                if (!res.Success)
                {
                    return(ctx.FailedReadLiteral(res.Messages.First().ToString()));
                }

                if (res.Value.Root == null || res.Value.Root.Nodes.Count == 0)
                {
                    return(ctx.FailedReadLiteral("Empty expression."));
                }
                else if (res.Value.Root.Nodes.Count > 1)
                {
                    return(ctx.FailedReadLiteral("Only single expressions allowed."));
                }

                return(LiteralEvaluator.Eval(res.Value.Root.Nodes[0]));
            }
            catch (Exception ex)
            {
                return(ctx.FailedReadLiteral(ex.Message));
            }
        }
Esempio n. 4
0
    protected override DyObject SubOp(ExecutionContext ctx, DyObject left, DyObject right)
    {
        if (right is DyDateTime dt)
        {
            try
            {
                return(new DyTimeDelta(DeclaringUnit.TimeDelta, ((DyDateTime)left).TotalTicks - dt.TotalTicks));
            }
            catch (Exception)
            {
                return(ctx.InvalidValue(right));
            }
        }
        else if (right is DyTimeDelta td)
        {
            try
            {
                return(new DyDateTime(this, ((DyDateTime)left).TotalTicks - td.TotalTicks));
            }
            catch (Exception)
            {
                return(ctx.InvalidValue(right));
            }
        }

        return(ctx.InvalidType(DeclaringUnit.DateTime.TypeId, DeclaringUnit.TimeDelta.TypeId, right));
    }
Esempio n. 5
0
    //Returns a function if an objects is a function or implements "Call" method
    public static DyFunction?ToFunction(this DyObject self, ExecutionContext ctx)
    {
        if (self is DyFunction func)
        {
            return(func);
        }

        if (self.Is(Dy.TypeInfo))
        {
            var ti = (DyTypeInfo)self;

            if (ti.TryGetStaticMember(ctx, ti.ReflectedTypeName, out var value))
            {
                return(value as DyFunction);
            }
        }
        else
        {
            var typ = ctx.RuntimeContext.Types[self.TypeId];

            if (typ.TryGetInstanceMember(ctx, self, Builtins.Call, out var value))
            {
                return(value as DyFunction);
            }
        }

        ctx.InvalidType(Dy.Function, self);
        return(default);
Esempio n. 6
0
    private static void WriteToConsole(ExecutionContext ctx, DyObject value, string?color, string?backColor, bool newLine)
    {
        var str = value.ToString(ctx).Value;

        if (ctx.HasErrors)
        {
            return;
        }

        var oldColor     = Console.ForegroundColor;
        var oldBackColor = Console.BackgroundColor;

        if (color is not null)
        {
            Console.ForegroundColor = GetColor(color);
        }
        if (backColor is not null)
        {
            Console.BackgroundColor = GetColor(backColor);
        }

        if (newLine)
        {
            Console.Write(str + Environment.NewLine);
        }
        else
        {
            Console.Write(str);
        }

        Console.ForegroundColor = oldColor;
        Console.BackgroundColor = oldBackColor;
    }
Esempio n. 7
0
    protected override DyObject SubOp(ExecutionContext ctx, DyObject left, DyObject right)
    {
        var self = (DyLocalDateTime)left;

        if (right is DyLocalDateTime dt)
        {
            try
            {
                if (!self.Offset.Equals(dt.Offset))
                {
                    return(ctx.InvalidValue(right));
                }

                return(new DyTimeDelta(DeclaringUnit.TimeDelta, self.Ticks - dt.Ticks));
            }
            catch (Exception)
            {
                return(ctx.InvalidValue(right));
            }
        }
        else if (right is DyTimeDelta td)
        {
            try
            {
                return(new DyLocalDateTime(this, self.Ticks - td.TotalTicks, self.Offset));
            }
            catch (Exception)
            {
                return(ctx.InvalidValue(right));
            }
        }

        return(ctx.InvalidType(DeclaringUnit.LocalDateTime.TypeId, DeclaringUnit.TimeDelta.TypeId, right));
    }
Esempio n. 8
0
    public void Write(ExecutionContext ctx, DyObject obj)
    {
        Reset();

        switch (obj.TypeId)
        {
        case Dy.Integer:
            Write(((DyInteger)obj).Value);
            break;

        case Dy.Float:
            Write(((DyFloat)obj).Value);
            break;

        case Dy.Bool:
            Write(obj.IsTrue());
            break;

        case Dy.Char:
        case Dy.String:
            Write(obj.ToString());
            break;

        default:
            ctx.InvalidType(obj);
            break;
        }
    }
Esempio n. 9
0
    internal static DyObject AddTo(ExecutionContext ctx, DyObject self, int years = 0, int months = 0, int days = 0)
    {
        var s = (DyDate)self.Clone();

        try
        {
            if (days != 0)
            {
                s.AddDays(days);
            }
            if (months != 0)
            {
                s.AddMonths(months);
            }
            if (years != 0)
            {
                s.AddYears(years);
            }
        }
        catch (ArgumentOutOfRangeException)
        {
            return(ctx.Overflow());
        }

        return(s);
    }
Esempio n. 10
0
 protected override DyObject ShiftLeftOp(ExecutionContext ctx, DyObject left, DyObject right)
 {
     if (left.TypeId != right.TypeId)
     {
         return(base.ShiftLeftOp(ctx, left, right));
     }
     return(new DyInteger(((DyInteger)left).Value << (int)((DyInteger)right).Value));
 }
Esempio n. 11
0
        public DyObject Sqrt(ExecutionContext ctx, DyObject n)
        {
            if (n.TypeId != DyType.Float && n.TypeId != DyType.Integer)
            {
                return(ctx.InvalidType(n));
            }

            return(new DyFloat(Math.Sqrt(n.GetFloat())));
        }
Esempio n. 12
0
    protected override DyObject BindOrRun(ExecutionContext ctx, DyObject arg)
    {
        if (!Auto)
        {
            return(BindToInstance(ctx, arg));
        }

        return(func(ctx, arg, Array.Empty <DyObject>()));
    }
Esempio n. 13
0
    protected override DyObject InOp(ExecutionContext ctx, DyObject self, DyObject field)
    {
        if (field.TypeId is not Dy.String and not Dy.Char)
        {
            return(ctx.InvalidType(field));
        }

        return(((DyTuple)self).GetOrdinal(field.ToString()) is not - 1 ? True : False);
    }
Esempio n. 14
0
    protected override DyObject AddOp(ExecutionContext ctx, DyObject left, DyObject right)
    {
        if (right.TypeId != left.TypeId)
        {
            return(ctx.InvalidType(left.TypeId, right));
        }

        return(new DyTimeDelta(this, ((DyTimeDelta)left).TotalTicks + ((DyTimeDelta)right).TotalTicks));
    }
Esempio n. 15
0
        public DyObject Assert(ExecutionContext ctx, DyObject expected, DyObject got)
        {
            if (!Eq(expected?.ToObject(), got?.ToObject()))
            {
                return(ctx.AssertFailed($"Expected {expected?.ToString(ctx)}, got {got?.ToString(ctx)}"));
            }

            return(DyNil.Instance);
        }
Esempio n. 16
0
    protected override DyObject LteOp(ExecutionContext ctx, DyObject left, DyObject right)
    {
        if (right.TypeId != left.TypeId)
        {
            return(ctx.InvalidType(left.TypeId, right));
        }

        return(((T)left).TotalTicks <= ((T)right).TotalTicks ? True : False);
    }
Esempio n. 17
0
    protected override DyObject NeqOp(ExecutionContext ctx, DyObject left, DyObject right)
    {
        if (right.TypeId != left.TypeId)
        {
            return(DyBool.True);
        }

        return(((T)left).TotalTicks != ((T)right).TotalTicks ? True : False);
    }
Esempio n. 18
0
    private static DyObject IsIn(ExecutionContext ctx, DyObject self, DyObject field)
    {
        if (field.TypeId is not Dy.String and not Dy.Char)
        {
            return(False);
        }

        return(((DyClass)self).Fields.GetOrdinal(field.ToString()) is not - 1 ? True : False);
    }
Esempio n. 19
0
    protected override DyObject IterateOp(ExecutionContext ctx, DyObject self)
    {
        if (self is IEnumerable <DyObject> seq)
        {
            return(DyIterator.Create(seq));
        }

        return(Nil);
    }
Esempio n. 20
0
    internal static DyVariant ToError(this DyObject self)
    {
        if (self is DyVariant v)
        {
            return(v);
        }

        return(new DyVariant(DyError.Failure, self));
    }
Esempio n. 21
0
    //Generates error if type check fails
    public static bool Is(this DyObject self, ExecutionContext ctx, int typeId)
    {
        if (self.TypeId != typeId)
        {
            ctx.InvalidType(typeId, self);
            return(false);
        }

        return(true);
    }
Esempio n. 22
0
    public static object?ConvertTo(ExecutionContext ctx, DyObject obj, Type type)
    {
        if (!TryConvert(obj, type, out var result))
        {
            ctx.InvalidCast(obj.TypeName, type.FullName ?? type.Name);
            return(null);
        }

        return(result);
    }
Esempio n. 23
0
    protected override DyObject ToStringOp(ExecutionContext ctx, DyObject arg, DyObject format)
    {
        var ret = ctx.RuntimeContext.Types[((DyTypeInfo)arg).ReflectedTypeId].GetStaticMember(Builtins.String, ctx);

        if (ctx.HasErrors || ret is null)
        {
            return(Nil);
        }

        return(ret.Invoke(ctx));
    }
Esempio n. 24
0
 public DyObject Max(ExecutionContext ctx, DyObject x, DyObject y)
 {
     if (x.Type(ctx).Gt(ctx, x, y).GetBool())
     {
         return(x);
     }
     else
     {
         return(y);
     }
 }
Esempio n. 25
0
    public static DyObject Negate(this DyObject self, ExecutionContext ctx)
    {
        var type = ctx.RuntimeContext.Types[self.TypeId];
        var ret  = type.Neg(ctx, self);

        if (ReferenceEquals(ctx.Error, DyVariant.Eta))
        {
            ret = ctx.InvokeCallBackFunction();
        }

        return(ret);
    }
Esempio n. 26
0
    public static DyObject Add(this DyObject left, DyObject right, ExecutionContext ctx)
    {
        var type = ctx.RuntimeContext.Types[left.TypeId];
        var ret  = type.Add(ctx, left, right);

        if (ReferenceEquals(ctx.Error, DyVariant.Eta))
        {
            ret = ctx.InvokeCallBackFunction(right);
        }

        return(ret);
    }
Esempio n. 27
0
 private static DyObject Lesser(ExecutionContext ctx, DyObject left, DyObject right)
 {
     try
     {
         return(DyTuple.Lesser(ctx, ((DyClass)left).Fields, ((DyClass)right).Fields));
     }
     catch (DyCodeException e)
     {
         ctx.Error = e.Error;
         return(Nil);
     }
 }
Esempio n. 28
0
 public static IEnumerable <DyObject> ToEnumerable(ExecutionContext ctx, DyObject val)
 {
     if (val is IEnumerable <DyObject> seq)
     {
         return(seq);
     }
     else
     {
         var iter = val.GetIterator(ctx);
         return(InternalRun(ctx, iter));
     }
 }
Esempio n. 29
0
    public static DyObject Concat(this DyObject left, DyObject right, ExecutionContext ctx)
    {
        var self = left.ToString(ctx);
        var type = ctx.RuntimeContext.String;
        var ret  = type.Add(ctx, self, right);

        if (ReferenceEquals(ctx.Error, DyVariant.Eta))
        {
            ret = ctx.InvokeCallBackFunction(right);
        }

        return(ret);
    }
Esempio n. 30
0
        public DyObject Round(ExecutionContext ctx, DyObject number, [Default(2)] DyObject digits)
        {
            if (number.TypeId != DyType.Float)
            {
                ctx.InvalidType(number);
            }
            else if (digits.TypeId != DyType.Integer)
            {
                ctx.InvalidType(digits);
            }

            return(new DyFloat(Math.Round(number.GetFloat(), (int)digits.GetInteger())));
        }