Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ArrayOperations.cs134 linesDownload Raw Back to interpreter
1/* ****************************************************************************2 *3 * Copyright (c) Microsoft Corporation.4 *5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A6 * copy of the license can be found in the License.html file at the root of this distribution. If7 * you cannot locate the Apache License, Version 2.0, please send an email to8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound9 * by the terms of the Apache License, Version 2.0.10 *11 * You must not remove this notice, or any other, from this software.12 *13 *14 * ***************************************************************************/15 16namespace System.Management.Automation.Interpreter17{18    internal sealed class NewArrayInitInstruction<TElement> : Instruction19    {20        private readonly int _elementCount;21 22        internal NewArrayInitInstruction(int elementCount)23        {24            _elementCount = elementCount;25        }26 27        public override int ConsumedStack { get { return _elementCount; } }28 29        public override int ProducedStack { get { return 1; } }30 31        public override int Run(InterpretedFrame frame)32        {33            TElement[] array = new TElement[_elementCount];34            for (int i = _elementCount - 1; i >= 0; i--)35            {36                array[i] = (TElement)frame.Pop();37            }38 39            frame.Push(array);40            return +1;41        }42    }43 44    internal sealed class NewArrayInstruction<TElement> : Instruction45    {46        internal NewArrayInstruction() { }47 48        public override int ConsumedStack { get { return 1; } }49 50        public override int ProducedStack { get { return 1; } }51 52        public override int Run(InterpretedFrame frame)53        {54            int length = (int)frame.Pop();55            frame.Push(new TElement[length]);56            return +1;57        }58    }59 60    internal sealed class NewArrayBoundsInstruction : Instruction61    {62        private readonly Type _elementType;63        private readonly int _rank;64 65        internal NewArrayBoundsInstruction(Type elementType, int rank)66        {67            _elementType = elementType;68            _rank = rank;69        }70 71        public override int ConsumedStack { get { return _rank; } }72 73        public override int ProducedStack { get { return 1; } }74 75        public override int Run(InterpretedFrame frame)76        {77            var lengths = new int[_rank];78            for (int i = _rank - 1; i >= 0; i--)79            {80                lengths[i] = (int)frame.Pop();81            }82 83            var array = Array.CreateInstance(_elementType, lengths);84            frame.Push(array);85            return +1;86        }87    }88 89    internal sealed class GetArrayItemInstruction<TElement> : Instruction90    {91        internal GetArrayItemInstruction() { }92 93        public override int ConsumedStack { get { return 2; } }94 95        public override int ProducedStack { get { return 1; } }96 97        public override int Run(InterpretedFrame frame)98        {99            int index = (int)frame.Pop();100            TElement[] array = (TElement[])frame.Pop();101            frame.Push(array[index]);102            return +1;103        }104 105        public override string InstructionName106        {107            get { return "GetArrayItem"; }108        }109    }110 111    internal sealed class SetArrayItemInstruction<TElement> : Instruction112    {113        internal SetArrayItemInstruction() { }114 115        public override int ConsumedStack { get { return 3; } }116 117        public override int ProducedStack { get { return 0; } }118 119        public override int Run(InterpretedFrame frame)120        {121            TElement value = (TElement)frame.Pop();122            int index = (int)frame.Pop();123            TElement[] array = (TElement[])frame.Pop();124            array[index] = value;125            return +1;126        }127 128        public override string InstructionName129        {130            get { return "SetArrayItem"; }131        }132    }133}134