public Value Evaluate(Environment environment)
        {
            var result   = new ValueMap();
            var localEnv = environment.NewEnv();
            var list     = AsList.From(listExpr.Evaluate(environment), what);

            foreach (var listValue in list.GetValue())
            {
                localEnv.Put(identifier, listValue);
                var key   = keyExpr.Evaluate(localEnv);
                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(key, value);
                    }
                }
                else
                {
                    result.AddItem(key, value);
                }
            }
            return(result);
        }
Пример #2
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.IsList() && b.IsList())
            {
                var lista  = a.AsList().GetValue();
                var listb  = b.AsList().GetValue();
                var result = new ValueMap();
                for (var i = 0; i < Math.Min(lista.Count, listb.Count); i++)
                {
                    result.AddItem(lista[i], listb[i]);
                }

                return(result);
            }

            throw new ControlErrorException(new ValueString("ERROR"), "Cannot zip_map " + a + " and " + b, pos);
        }
Пример #3
0
        public override ValueMap AsMap()
        {
            var result = new ValueMap();

            foreach (var item in value)
            {
                result.AddItem(item.AsList().GetValue()[0], item.AsList().GetValue()[1]);
            }
            return(result);
        }
Пример #4
0
        public override ValueMap AsMap()
        {
            var result = new ValueMap();

            foreach (var entry in value)
            {
                result.AddItem(new ValueString(entry.Key), entry.Value);
            }
            return(result);
        }
Пример #5
0
        public Value Evaluate(Environment environment)
        {
            var result = new ValueMap();

            for (var i = 0; i < keys.Count; i++)
            {
                result.AddItem(keys[i].Evaluate(environment), values[i].Evaluate(environment));
            }
            return(result);
        }
        public Value Evaluate(Environment environment)
        {
            var result   = new ValueMap();
            var localEnv = environment.NewEnv();
            var list     = listExpr.Evaluate(environment);

            if (list.IsString())
            {
                var s     = list.AsString().GetValue();
                var slist = new ValueList();
                for (var i = 0; i < s.Length; i++)
                {
                    slist.AddItem(new ValueString(s.Substring(i, 1)));
                }
                list = slist;
            }
            foreach (var listValue in list.AsList().GetValue())
            {
                localEnv.Put(identifier, listValue);
                var key   = keyExpr.Evaluate(localEnv);
                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(key, value);
                    }
                }
                else
                {
                    result.AddItem(key, value);
                }
            }
            return(result);
        }
Пример #7
0
 private static Value Parse(Lexer lexer)
 {
     try {
         var token = lexer.NextToken();
         if (token == "[")
         {
             var result = new ValueList();
             var next   = lexer.NextToken();
             while (next != "]")
             {
                 lexer.PutBackToken(next);
                 result.AddItem(Parse(lexer));
                 next = lexer.NextToken();
                 if (next != "]")
                 {
                     lexer.PutBackToken(next);
                     lexer.MatchToken(",");
                     next = lexer.NextToken();
                 }
             }
             return(result);
         }
         if (token == "{")
         {
             var result = new ValueMap();
             var next   = lexer.NextToken();
             while (next != "}")
             {
                 lexer.PutBackToken(next);
                 var key = lexer.NextToken();
                 if (!key.StartsWith("\""))
                 {
                     throw new Exception("Expected object key, but got " + key);
                 }
                 key = key.Substring(1, key.Length - 2);
                 lexer.MatchToken(":");
                 result.AddItem(new ValueString(key), Parse(lexer));
                 next = lexer.NextToken();
                 if (next != "}")
                 {
                     lexer.PutBackToken(next);
                     lexer.MatchToken(",");
                     next = lexer.NextToken();
                 }
             }
             return(result);
         }
         if (token.StartsWith("\""))
         {
             return(new ValueString(token.Substring(1, token.Length - 2)));
         }
         if (token == "true" || token == "false")
         {
             return(ValueBoolean.From(bool.Parse(token)));
         }
         if (token == "null")
         {
             return(ValueNull.NULL);
         }
         if (token.IndexOf('.') == -1)
         {
             return(new ValueInt(long.Parse(token)));
         }
         return(new ValueDecimal(decimal.Parse(token)));
     } catch (Exception) {
         return(null);
     }
 }