Example #1
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var env    = environment.NewEnv();
            var lst    = args.GetAsList("lst");
            var cmp    = args.HasArg("cmp") ? args.GetFunc("cmp") : environment.Get("compare", pos).AsFunc();
            var key    = args.HasArg("key") ? args.GetFunc("key") : environment.Get("identity", pos).AsFunc();
            var result = new List <Value>();

            result.AddRange(lst.GetValue());
            for (var i = 1; i < result.Count; i++)
            {
                var v = key.Execute(new Args(key.GetArgNames()[0], result[i], pos), env, pos);
                for (var j = i - 1; j >= 0; j--)
                {
                    var v2         = key.Execute(new Args(key.GetArgNames()[0], result[j], pos), env, pos);
                    var cmpargs    = new Args(cmp.GetArgNames()[0], cmp.GetArgNames()[1], v, v2, pos);
                    var comparison = (int)cmp.Execute(cmpargs, env, pos).AsInt().GetValue();
                    if (comparison < 0)
                    {
                        var temp = result[j + 1];
                        result[j + 1] = result[j];
                        result[j]     = temp;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(new ValueList(result));
        }
Example #2
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (!args.HasArg("obj"))
     {
         return(new ValueList());
     }
     return(args.GetAsList("obj"));
 }
Example #3
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var a = args.Get("a");
            var b = args.Get("b");

            if (a.IsNull() || b.IsNull())
            {
                return(ValueNull.NULL);
            }

            if (a.IsInt() && b.IsInt())
            {
                return(new ValueInt(a.AsInt().GetValue() - b.AsInt().GetValue()));
            }

            if (a.IsNumerical() && b.IsNumerical())
            {
                return(new ValueDecimal(a.AsDecimal().GetValue() - b.AsDecimal().GetValue()));
            }

            if (a.IsDate())
            {
                if (b.IsDate())
                {
                    return(new ValueInt(a.AsDate().GetValue().Subtract(b.AsDate().GetValue()).Days));
                }
                return(new ValueDate(a.AsDate().GetValue() - TimeSpan.FromDays((double)args.GetAsDecimal("b").GetValue())));
            }

            if (a.IsList())
            {
                var result = new ValueList();
                foreach (var item in a.AsList().GetValue())
                {
                    var add = true;
                    foreach (var val in args.GetAsList("b").GetValue())
                    {
                        if (!item.IsEquals(val))
                        {
                            continue;
                        }
                        add = false;
                        break;
                    }
                    if (add)
                    {
                        result.AddItem(item);
                    }
                }

                return(result);
            }

            if (a.IsSet())
            {
                var result = new ValueSet();
                var minus  = new SortedSet <Value>(new ComparerValue());
                if (b.IsSet())
                {
                    minus = new SortedSet <Value>(b.AsSet().GetValue(), new ComparerValue());
                }
                else if (b.IsList())
                {
                    foreach (var element in b.AsList().GetValue())
                    {
                        minus.Add(element);
                    }
                }
                else
                {
                    minus.Add(b);
                }
                foreach (var element in a.AsSet().GetValue())
                {
                    if (!minus.Contains(element))
                    {
                        result.AddItem(element);
                    }
                }
                return(result);
            }

            throw new ControlErrorException(new ValueString("ERROR"), "Cannot subtract " + b + " from " + a, pos);
        }