예제 #1
0
        public Value Each()
        {
            using (var assistant = new ParameterAssistant(Arguments))
            {
                var block = assistant.Block();
                if (block == null)
                {
                    return(null);
                }

                RegionManager.Regions.Push("string-indexer");

                var changes = new Hash <int, string>();
                var indexes = getIndexes();

                foreach (var index in indexes)
                {
                    var correctedIndex = WrapIndex(index, text.Text.Length, true);
                    assistant.SetParameterValues(text.Text.Skip(index).Take(1), index.ToString(), correctedIndex);
                    var result = block.Evaluate();
                    var signal = ParameterAssistant.Signal();
                    if (signal == ParameterAssistant.SignalType.Breaking)
                    {
                        break;
                    }

                    switch (signal)
                    {
                    case ParameterAssistant.SignalType.Continuing:
                        continue;

                    case ParameterAssistant.SignalType.ReturningNull:
                        return(null);
                    }

                    if (result != null)
                    {
                        changes[correctedIndex] = result.Text;
                    }
                }

                Slicer slicer = text.Text;
                foreach (var item in changes)
                {
                    slicer[item.Key] = item.Value;
                }

                text.Text = slicer.ToString();

                RegionManager.Regions.Pop("string-indexer");

                return(null);
            }
        }
예제 #2
0
 public override Value Process(Array.IterItem item, int length)
 {
     signal = SignalType.None;
     using (var assistant = new ParameterAssistant(parameterBlock))
     {
         var block = assistant.Block();
         assistant.ArrayParameters();
         assistant.SetParameterValues(item);
         var value = block.Evaluate();
         signal = Signal();
         return(value);
     }
 }
예제 #3
0
        static Value forLoop(Value value, Arguments arguments, out bool handled)
        {
            handled = false;
            var array = value.YieldValue;

            if (array == null)
            {
                return(value);
            }

            using (var assistant = new ParameterAssistant(arguments))
            {
                var block = assistant.Block();
                if (block == null)
                {
                    return(value);
                }

                Regions.Push("for");

                foreach (var item in array)
                {
                    assistant.SetParameterValues(item);
                    block.Evaluate();
                    var signal = ParameterAssistant.Signal();
                    if (signal == Breaking)
                    {
                        break;
                    }

                    switch (signal)
                    {
                    case Continuing:
                        continue;

                    case ReturningNull:
                        return(null);
                    }
                }

                value.YieldValue = null;

                Regions.Pop("for");

                handled = true;
                return(array);
            }
        }
예제 #4
0
        public override Value Fill()
        {
            using var assistant = new ParameterAssistant(Arguments);
            var   block = assistant.Block();
            Value value;

            if (block != null)
            {
                assistant.ArrayParameters();
                var index = 0;
                foreach (var key in getIndicators())
                {
                    assistant.SetParameterValues(array[key], key, index++);
                    value = block.Evaluate();
                    var signal = Signal();
                    if (signal == Breaking)
                    {
                        break;
                    }

                    switch (signal)
                    {
                    case Continuing:
                        continue;

                    case ReturningNull:
                        return(null);
                    }

                    if (value != null)
                    {
                        array[key] = value;
                    }
                }

                return(array);
            }

            value = Arguments[0];
            foreach (var key in getIndicators())
            {
                array[key] = value.Clone();
            }

            return(array);
        }
예제 #5
0
        public static Value FoldL(INSGenerator generator, Arguments arguments)
        {
            var iterator = new NSIterator(generator);

            using var assistant = new ParameterAssistant(arguments);
            var block = assistant.Block();

            if (block == null)
            {
                return(NilValue);
            }

            iterator.Reset();

            assistant.TwoValueParameters();
            var initialFromArguments = arguments[0];
            var initialValue         = initialFromArguments.IsEmpty ? iterator.Next() : initialFromArguments;

            if (initialValue.IsNil)
            {
                return(initialValue);
            }

            var secondValue = iterator.Next();

            if (secondValue.IsNil)
            {
                return(initialValue);
            }

            assistant.SetParameterValues(initialValue, secondValue);
            var value  = block.Evaluate();
            var signal = Signal();

            if (signal == Breaking)
            {
                return(value);
            }

            switch (signal)
            {
            case ReturningNull:
                return(null);

            case Continuing:
                return(value);
            }

            var next = iterator.Next();

            if (next.IsNil)
            {
                return(value);
            }

            for (var i = 0; i < MAX_LOOP; i++)
            {
                assistant.SetParameterValues(value, next);
                value  = block.Evaluate();
                signal = Signal();
                if (signal == Breaking)
                {
                    break;
                }

                switch (signal)
                {
                case ReturningNull:
                    return(null);

                case Continuing:
                    continue;
                }

                next = iterator.Next();
                if (next.IsNil)
                {
                    return(value);
                }
            }

            return(value);
        }
예제 #6
0
        public override Value SelfMap()
        {
            using var assistant = new ParameterAssistant(Arguments);
            var block = assistant.Block();

            if (block == null)
            {
                return(this);
            }

            assistant.ArrayParameters();

            var changes = new Array();

            foreach (var key in getIndicators())
            {
                var value = array[key];
                var index = array.GetIndex(key);
                assistant.SetParameterValues(value, key, index);
                value = block.Evaluate();
                var signal = Signal();
                if (signal == Breaking)
                {
                    break;
                }

                switch (signal)
                {
                case Continuing:
                    continue;

                case ReturningNull:
                    return(null);
                }

                if (value.Type == ValueType.Nil)
                {
                    continue;
                }

                if (value is KeyedValue keyedValue)
                {
                    changes[keyedValue.Key] = keyedValue.Value;
                }
                else
                {
                    changes[key] = value;
                }
            }

            if (changes.Length == 0)
            {
                return(this);
            }

            foreach (var item in changes)
            {
                array[item.Key] = item.Value;
            }

            return(this);
        }