public Value Evaluate(Environment environment)
        {
            var result   = new ValueList();
            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);
        }
        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);
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var         inparg   = args.Get("input");
            var         callback = args.Get("callback").AsFunc();
            Environment env      = environment.NewEnv();

            if (inparg is ValueInput)
            {
                ValueInput input = inparg.AsInput();
                return(new ValueInt(input.Process(line => {
                    Args args_ = new Args(callback.GetArgNames()[0], new ValueString(line), pos);
                    return callback.Execute(args_, env, pos);
                })));
            }
            if (inparg is ValueList)
            {
                var list = inparg.AsList().GetValue();
                foreach (var element in list)
                {
                    Args args_ = new Args(callback.GetArgNames()[0], element.AsString(), pos);
                    callback.Execute(args_, env, pos);
                }
                ;
                return(new ValueInt(list.Count));
            }
            throw new ControlErrorException(new ValueString("ERROR"), "Cannot process lines from " + inparg, pos);
        }
Ejemplo n.º 5
0
 public Interpreter(bool secure = true, bool legacy = true)
 {
     baseEnvironment = Environment.GetBaseEnvironment(secure, legacy);
     environment     = baseEnvironment.NewEnv();
     baseEnvironment.Put("stdout", new ValueOutput(new StringWriter()));
     baseEnvironment.Put("stdin", new ValueInput(new StringReader("")));
     if (!secure)
     {
         baseEnvironment.Put("run", new FuncRun(this));
     }
 }
Ejemplo n.º 6
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("obj"))
            {
                return(ValueNull.NULL);
            }
            var obj = args.Get("obj");
            var key = args.HasArg("key") ? args.GetFunc("key") : null;

            if (obj.IsString())
            {
                var str   = obj.AsString().GetValue();
                var start = (int)args.GetInt("start", str.Length - 1).GetValue();
                var part  = args.GetString("part").GetValue();
                return(new ValueInt(str.LastIndexOf(part, start, StringComparison.Ordinal)));
            }
            if (obj.IsList())
            {
                var env = environment;
                if (key != null)
                {
                    env = environment.NewEnv();
                }
                var item  = args.Get("part");
                var list  = obj.AsList().GetValue();
                var start = (int)args.GetInt("start", list.Count - 1).GetValue();
                for (var idx = start; idx >= 0; idx--)
                {
                    var elem = list[idx];
                    if (key != null)
                    {
                        elem = key.Execute(new Args(key.GetArgNames()[0], elem, pos), env, pos);
                    }
                    if (elem.IsEquals(item))
                    {
                        return(new ValueInt(idx));
                    }
                }
                return(new ValueInt(-1));
            }
            throw new ControlErrorException(new ValueString("ERROR"), "Find_last only works with strings and lists", pos);
        }
        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);
        }
Ejemplo n.º 8
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var env = lexicalEnv.NewEnv();

            for (var i = 0; i < argNames.Count; i++)
            {
                if (args.HasArg(argNames[i]))
                {
                    env.Put(argNames[i], args.Get(argNames[i]));
                }
                else if (defValues[i] != null)
                {
                    env.Put(argNames[i], defValues[i].Evaluate(env));
                }
                else
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Missing argument " + argNames[i], pos);
                }
            }

            var result = body.Evaluate(env);

            if (result.IsReturn())
            {
                return(result.AsReturn().value);
            }
            if (result.IsBreak())
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot use break without surrounding for loop", result.AsBreak().pos);
            }
            if (result.IsContinue())
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot use continue without surrounding for loop", result.AsContinue().pos);
            }
            return(result);
        }