예제 #1
0
        /// <inheritdoc />
        protected override object ExecuteToken(ScriptContext context)
        {
            ScriptContext loopcontext = new ScriptContext(new VariableContext(context.Variables), context.Arguments, context.CancellationToken);

            object collectionvalue = collection.Execute(loopcontext);

            if (collectionvalue is IEnumerable enumeration)
            {
                foreach (object value in enumeration.Cast <object>())
                {
                    context.CancellationToken.ThrowIfCancellationRequested();

                    variable.Assign(new ScriptValue(value), loopcontext);
                    object bodyvalue = Body?.Execute(loopcontext);
                    if (bodyvalue is Return)
                    {
                        return(bodyvalue);
                    }
                    if (bodyvalue is Break breaktoken)
                    {
                        int depth = breaktoken.Depth.Execute <int>(loopcontext);
                        if (depth <= 1)
                        {
                            return(null);
                        }
                        return(new Break(new ScriptValue(depth - 1)));
                    }

                    if (value is Continue continuetoken)
                    {
                        int depth = continuetoken.Depth.Execute <int>(loopcontext);
                        if (depth <= 1)
                        {
                            continue;
                        }

                        return(new Continue(new ScriptValue(depth - 1)));
                    }
                }
            }
            else
            {
                throw new ScriptRuntimeException("Foreach value is not a collection", collection);
            }

            return(null);
        }