public Value Evaluate(Environment environment)
        {
            var result   = new ValueSet();
            var localEnv = environment.NewEnv();
            var list1    = AsList.From(listExpr1.Evaluate(environment)).GetValue();
            var list2    = AsList.From(listExpr2.Evaluate(environment)).GetValue();

            for (var i = 0; i < Math.Max(list1.Count, list2.Count); i++)
            {
                localEnv.Put(identifier1, i < list1.Count ? list1[i] : ValueNull.NULL);
                localEnv.Put(identifier2, i < list2.Count ? list2[i] : ValueNull.NULL);
                var value = valueExpr.Evaluate(localEnv);
                if (conditionExpr != null)
                {
                    var condition = conditionExpr.Evaluate(localEnv);
                    if (!condition.IsBoolean())
                    {
                        throw new ControlErrorException(new ValueString("ERROR"),
                                                        "Condition must be boolean but got " + condition.Type(),
                                                        pos);
                    }

                    if (condition.AsBoolean().GetValue())
                    {
                        result.AddItem(value);
                    }
                }
                else
                {
                    result.AddItem(value);
                }
            }

            return(result);
        }
        public Value Evaluate(Environment environment)
        {
            var result   = new ValueSet();
            var localEnv = environment.NewEnv();
            var list     = AsList.From(listExpr.Evaluate(environment));

            foreach (var listValue in list.GetValue())
            {
                localEnv.Put(identifier, listValue);
                var value = valueExpr.Evaluate(localEnv);
                if (conditionExpr != null)
                {
                    var condition = conditionExpr.Evaluate(localEnv);
                    if (!condition.IsBoolean())
                    {
                        throw new ControlErrorException(new ValueString("ERROR"), "Condition must be boolean but got " + condition.Type(), pos);
                    }
                    if (condition.AsBoolean().GetValue())
                    {
                        result.AddItem(value);
                    }
                }
                else
                {
                    result.AddItem(value);
                }
            }
            return(result);
        }
예제 #3
0
        public Value Evaluate(Environment environment)
        {
            var result = new ValueSet();

            foreach (var item in items)
            {
                result.AddItem(item.Evaluate(environment));
            }
            return(result);
        }
예제 #4
0
        public override ValueSet AsSet()
        {
            var result = new ValueSet();

            foreach (var item in value)
            {
                result.AddItem(item);
            }
            return(result);
        }
예제 #5
0
        public override ValueSet AsSet()
        {
            var result = new ValueSet();

            foreach (var key in value.Keys)
            {
                result.AddItem(new ValueString(key));
            }
            return(result);
        }
예제 #6
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);
        }
예제 #7
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.IsList())
            {
                if (b.IsCollection())
                {
                    return(new ValueList(a.AsList().GetValue()).AddItems(b.AsList().GetValue()));
                }
                return(new ValueList(a.AsList().GetValue()).AddItem(b));
            }

            if (a.IsDate() && b.IsNumerical())
            {
                return(new ValueDate(a.AsDate().GetValue() + TimeSpan.FromDays((double)args.GetAsDecimal("b").GetValue())));
            }

            if (a.IsSet())
            {
                if (b.IsCollection())
                {
                    return(new ValueSet(a.AsSet().GetValue()).AddItems(b.AsSet().GetValue()));
                }
                return(new ValueSet(a.AsSet().GetValue()).AddItem(b));
            }

            if (b.IsList())
            {
                var result = new ValueList();
                result.AddItem(a);
                result.AddItems(b.AsList().GetValue());
                return(result);
            }

            if (b.IsSet())
            {
                var result = new ValueSet();
                result.AddItem(a);
                result.AddItems(b.AsSet().GetValue());
                return(result);
            }

            if (a.IsString() && b.IsAtomic() || a.IsAtomic() && b.IsString())
            {
                return(new ValueString(a.AsString().GetValue() + b.AsString().GetValue()));
            }

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