示例#1
0
        public Value Evaluate(Environment environment)
        {
            var list = expression.Evaluate(environment);

            if (list.IsInput())
            {
                var    input  = list.AsInput();
                Value  result = ValueBoolean.TRUE;
                string line   = null;
                try
                {
                    line = input.ReadLine();
                    while (line != null)
                    {
                        var value = new ValueString(line);
                        if (identifiers.Count == 1)
                        {
                            environment.Put(identifiers[0], value);
                        }
                        else
                        {
                            var vals = value.AsList().GetValue();
                            for (int i = 0; i < identifiers.Count; i++)
                            {
                                environment.Put(identifiers[i], vals[i]);
                            }
                        }
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                        line = input.ReadLine();
                    }
                    if (identifiers.Count == 1)
                    {
                        environment.Remove(identifiers[0]);
                    }
                    else
                    {
                        for (var i = 0; i < identifiers.Count; i++)
                        {
                            environment.Remove(identifiers[i]);
                        }
                    }
                }
                catch (IOException)
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot read from input", pos);
                }
                return(result);
            }
            if (list.IsList())
            {
                Value result = ValueBoolean.TRUE;
                foreach (var value in list.AsList().GetValue())
                {
                    if (identifiers.Count == 1)
                    {
                        environment.Put(identifiers[0], value);
                    }
                    else
                    {
                        var vals = value.AsList().GetValue();
                        for (int i = 0; i < identifiers.Count; i++)
                        {
                            environment.Put(identifiers[i], vals[i]);
                        }
                    }
                    result = block.Evaluate(environment);
                    if (result.IsBreak())
                    {
                        result = ValueBoolean.TRUE;
                        break;
                    }

                    if (result.IsContinue())
                    {
                        result = ValueBoolean.TRUE;
                        // continue
                    }

                    if (result.IsReturn())
                    {
                        break;
                    }
                }
                if (identifiers.Count == 1)
                {
                    environment.Remove(identifiers[0]);
                }
                else
                {
                    for (var i = 0; i < identifiers.Count; i++)
                    {
                        environment.Remove(identifiers[i]);
                    }
                }
                return(result);
            }
            if (list.IsSet())
            {
                Value result = ValueBoolean.TRUE;
                foreach (var value in list.AsSet().GetValue())
                {
                    if (identifiers.Count == 1)
                    {
                        environment.Put(identifiers[0], value);
                    }
                    else
                    {
                        var vals = value.AsList().GetValue();
                        for (int i = 0; i < identifiers.Count; i++)
                        {
                            environment.Put(identifiers[i], vals[i]);
                        }
                    }
                    result = block.Evaluate(environment);
                    if (result.IsBreak())
                    {
                        result = ValueBoolean.TRUE;
                        break;
                    }

                    if (result.IsContinue())
                    {
                        result = ValueBoolean.TRUE;
                        // continue
                    }

                    if (result.IsReturn())
                    {
                        break;
                    }
                }
                if (identifiers.Count == 1)
                {
                    environment.Remove(identifiers[0]);
                }
                else
                {
                    for (var i = 0; i < identifiers.Count; i++)
                    {
                        environment.Remove(identifiers[i]);
                    }
                }
                return(result);
            }
            if (list.IsMap())
            {
                Value result = ValueBoolean.TRUE;
                if (what == "keys")
                {
                    foreach (var val in list.AsMap().GetValue().Keys.ToList())
                    {
                        if (identifiers.Count == 1)
                        {
                            environment.Put(identifiers[0], val);
                        }
                        else
                        {
                            var vals = val.AsList().GetValue();
                            for (int i = 0; i < identifiers.Count; i++)
                            {
                                environment.Put(identifiers[i], vals[i]);
                            }
                        }
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                    }
                }
                else if (what == "values")
                {
                    foreach (var val in list.AsMap().GetValue().Values.ToList())
                    {
                        if (identifiers.Count == 1)
                        {
                            environment.Put(identifiers[0], val);
                        }
                        else
                        {
                            var vals = val.AsList().GetValue();
                            for (int i = 0; i < identifiers.Count; i++)
                            {
                                environment.Put(identifiers[i], vals[i]);
                            }
                        }
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                    }
                }
                else if (what == "entries")
                {
                    foreach (var entry in list.AsMap().GetValue().ToList())
                    {
                        ValueList val = new ValueList();
                        val.AddItem(entry.Key);
                        val.AddItem(entry.Value);
                        if (identifiers.Count == 1)
                        {
                            environment.Put(identifiers[0], val);
                        }
                        else
                        {
                            var vals = val.AsList().GetValue();
                            for (int i = 0; i < identifiers.Count; i++)
                            {
                                environment.Put(identifiers[i], vals[i]);
                            }
                        }
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                    }
                }
                if (identifiers.Count == 1)
                {
                    environment.Remove(identifiers[0]);
                }
                else
                {
                    for (var i = 0; i < identifiers.Count; i++)
                    {
                        environment.Remove(identifiers[i]);
                    }
                }
                return(result);
            }
            if (list.IsObject())
            {
                Value result = ValueBoolean.TRUE;
                if (what == "keys")
                {
                    foreach (var key in list.AsObject().value.Keys.ToList())
                    {
                        Value val = new ValueString(key);
                        environment.Put(identifiers[0], val);
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                    }
                }
                else if (what == "values")
                {
                    foreach (var val in list.AsObject().value.Values.ToList())
                    {
                        if (identifiers.Count == 1)
                        {
                            environment.Put(identifiers[0], val);
                        }
                        else
                        {
                            var vals = val.AsList().GetValue();
                            for (int i = 0; i < identifiers.Count; i++)
                            {
                                environment.Put(identifiers[i], vals[i]);
                            }
                        }
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                    }
                }
                else if (what == "entries")
                {
                    foreach (var entry in list.AsObject().value.ToList())
                    {
                        var val = new ValueList();
                        val.AddItem(new ValueString(entry.Key));
                        val.AddItem(entry.Value);
                        if (identifiers.Count == 1)
                        {
                            environment.Put(identifiers[0], val);
                        }
                        else
                        {
                            var vals = val.AsList().GetValue();
                            for (int i = 0; i < identifiers.Count; i++)
                            {
                                environment.Put(identifiers[i], vals[i]);
                            }
                        }
                        result = block.Evaluate(environment);
                        if (result.IsBreak())
                        {
                            result = ValueBoolean.TRUE;
                            break;
                        }

                        if (result.IsContinue())
                        {
                            result = ValueBoolean.TRUE;
                            // continue
                        }

                        if (result.IsReturn())
                        {
                            break;
                        }
                    }
                }
                if (identifiers.Count == 1)
                {
                    environment.Remove(identifiers[0]);
                }
                else
                {
                    for (var i = 0; i < identifiers.Count; i++)
                    {
                        environment.Remove(identifiers[i]);
                    }
                }
                return(result);
            }
            if (list.IsString())
            {
                var   str    = list.AsString().GetValue();
                Value result = ValueBoolean.TRUE;
                foreach (var value in str)
                {
                    environment.Put(identifiers[0], new ValueString(value.ToString()));
                    result = block.Evaluate(environment);
                    if (result.IsBreak())
                    {
                        result = ValueBoolean.TRUE;
                        break;
                    }

                    if (result.IsContinue())
                    {
                        result = ValueBoolean.TRUE;
                        // continue
                    }

                    if (result.IsReturn())
                    {
                        break;
                    }
                }
                environment.Remove(identifiers[0]);
                return(result);
            }
            throw new ControlErrorException(new ValueString("ERROR"), "Cannot iterate over " + list, pos);
        }