Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
NumericConvertInstruction.cs286 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 abstract class NumericConvertInstruction : Instruction19    {20        internal readonly TypeCode _from, _to;21 22        protected NumericConvertInstruction(TypeCode from, TypeCode to)23        {24            _from = from;25            _to = to;26        }27 28        public override int ConsumedStack { get { return 1; } }29 30        public override int ProducedStack { get { return 1; } }31 32        public override string ToString()33        {34            return InstructionName + "(" + _from + "->" + _to + ")";35        }36 37        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]38        internal sealed class Unchecked : NumericConvertInstruction39        {40            public override string InstructionName { get { return "UncheckedConvert"; } }41 42            public Unchecked(TypeCode from, TypeCode to)43                : base(from, to)44            {45            }46 47            public override int Run(InterpretedFrame frame)48            {49                frame.Push(Convert(frame.Pop()));50                return +1;51            }52 53            private object Convert(object obj)54            {55                switch (_from)56                {57                    case TypeCode.Byte: return ConvertInt32((byte)obj);58                    case TypeCode.SByte: return ConvertInt32((sbyte)obj);59                    case TypeCode.Int16: return ConvertInt32((Int16)obj);60                    case TypeCode.Char: return ConvertInt32((char)obj);61                    case TypeCode.Int32: return ConvertInt32((Int32)obj);62                    case TypeCode.Int64: return ConvertInt64((Int64)obj);63                    case TypeCode.UInt16: return ConvertInt32((UInt16)obj);64                    case TypeCode.UInt32: return ConvertInt64((UInt32)obj);65                    case TypeCode.UInt64: return ConvertUInt64((UInt64)obj);66                    case TypeCode.Single: return ConvertDouble((Single)obj);67                    case TypeCode.Double: return ConvertDouble((double)obj);68                    default: throw Assert.Unreachable;69                }70            }71 72            private object ConvertInt32(int obj)73            {74                unchecked75                {76                    switch (_to)77                    {78                        case TypeCode.Byte: return (byte)obj;79                        case TypeCode.SByte: return (sbyte)obj;80                        case TypeCode.Int16: return (Int16)obj;81                        case TypeCode.Char: return (char)obj;82                        case TypeCode.Int32: return (Int32)obj;83                        case TypeCode.Int64: return (Int64)obj;84                        case TypeCode.UInt16: return (UInt16)obj;85                        case TypeCode.UInt32: return (UInt32)obj;86                        case TypeCode.UInt64: return (UInt64)obj;87                        case TypeCode.Single: return (Single)obj;88                        case TypeCode.Double: return (double)obj;89                        default: throw Assert.Unreachable;90                    }91                }92            }93 94            private object ConvertInt64(Int64 obj)95            {96                unchecked97                {98                    switch (_to)99                    {100                        case TypeCode.Byte: return (byte)obj;101                        case TypeCode.SByte: return (sbyte)obj;102                        case TypeCode.Int16: return (Int16)obj;103                        case TypeCode.Char: return (char)obj;104                        case TypeCode.Int32: return (Int32)obj;105                        case TypeCode.Int64: return (Int64)obj;106                        case TypeCode.UInt16: return (UInt16)obj;107                        case TypeCode.UInt32: return (UInt32)obj;108                        case TypeCode.UInt64: return (UInt64)obj;109                        case TypeCode.Single: return (Single)obj;110                        case TypeCode.Double: return (double)obj;111                        default: throw Assert.Unreachable;112                    }113                }114            }115 116            private object ConvertUInt64(UInt64 obj)117            {118                unchecked119                {120                    switch (_to)121                    {122                        case TypeCode.Byte: return (byte)obj;123                        case TypeCode.SByte: return (sbyte)obj;124                        case TypeCode.Int16: return (Int16)obj;125                        case TypeCode.Char: return (char)obj;126                        case TypeCode.Int32: return (Int32)obj;127                        case TypeCode.Int64: return (Int64)obj;128                        case TypeCode.UInt16: return (UInt16)obj;129                        case TypeCode.UInt32: return (UInt32)obj;130                        case TypeCode.UInt64: return (UInt64)obj;131                        case TypeCode.Single: return (Single)obj;132                        case TypeCode.Double: return (double)obj;133                        default: throw Assert.Unreachable;134                    }135                }136            }137 138            private object ConvertDouble(double obj)139            {140                unchecked141                {142                    switch (_to)143                    {144                        case TypeCode.Byte: return (byte)obj;145                        case TypeCode.SByte: return (sbyte)obj;146                        case TypeCode.Int16: return (Int16)obj;147                        case TypeCode.Char: return (char)obj;148                        case TypeCode.Int32: return (Int32)obj;149                        case TypeCode.Int64: return (Int64)obj;150                        case TypeCode.UInt16: return (UInt16)obj;151                        case TypeCode.UInt32: return (UInt32)obj;152                        case TypeCode.UInt64: return (UInt64)obj;153                        case TypeCode.Single: return (Single)obj;154                        case TypeCode.Double: return (double)obj;155                        default: throw Assert.Unreachable;156                    }157                }158            }159        }160 161        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]162        internal sealed class Checked : NumericConvertInstruction163        {164            public override string InstructionName { get { return "CheckedConvert"; } }165 166            public Checked(TypeCode from, TypeCode to)167                : base(from, to)168            {169            }170 171            public override int Run(InterpretedFrame frame)172            {173                frame.Push(Convert(frame.Pop()));174                return +1;175            }176 177            private object Convert(object obj)178            {179                switch (_from)180                {181                    case TypeCode.Byte: return ConvertInt32((byte)obj);182                    case TypeCode.SByte: return ConvertInt32((sbyte)obj);183                    case TypeCode.Int16: return ConvertInt32((Int16)obj);184                    case TypeCode.Char: return ConvertInt32((char)obj);185                    case TypeCode.Int32: return ConvertInt32((Int32)obj);186                    case TypeCode.Int64: return ConvertInt64((Int64)obj);187                    case TypeCode.UInt16: return ConvertInt32((UInt16)obj);188                    case TypeCode.UInt32: return ConvertInt64((UInt32)obj);189                    case TypeCode.UInt64: return ConvertUInt64((UInt64)obj);190                    case TypeCode.Single: return ConvertDouble((Single)obj);191                    case TypeCode.Double: return ConvertDouble((double)obj);192                    default: throw Assert.Unreachable;193                }194            }195 196            private object ConvertInt32(int obj)197            {198                checked199                {200                    switch (_to)201                    {202                        case TypeCode.Byte: return (byte)obj;203                        case TypeCode.SByte: return (sbyte)obj;204                        case TypeCode.Int16: return (Int16)obj;205                        case TypeCode.Char: return (char)obj;206                        case TypeCode.Int32: return (Int32)obj;207                        case TypeCode.Int64: return (Int64)obj;208                        case TypeCode.UInt16: return (UInt16)obj;209                        case TypeCode.UInt32: return (UInt32)obj;210                        case TypeCode.UInt64: return (UInt64)obj;211                        case TypeCode.Single: return (Single)obj;212                        case TypeCode.Double: return (double)obj;213                        default: throw Assert.Unreachable;214                    }215                }216            }217 218            private object ConvertInt64(Int64 obj)219            {220                checked221                {222                    switch (_to)223                    {224                        case TypeCode.Byte: return (byte)obj;225                        case TypeCode.SByte: return (sbyte)obj;226                        case TypeCode.Int16: return (Int16)obj;227                        case TypeCode.Char: return (char)obj;228                        case TypeCode.Int32: return (Int32)obj;229                        case TypeCode.Int64: return (Int64)obj;230                        case TypeCode.UInt16: return (UInt16)obj;231                        case TypeCode.UInt32: return (UInt32)obj;232                        case TypeCode.UInt64: return (UInt64)obj;233                        case TypeCode.Single: return (Single)obj;234                        case TypeCode.Double: return (double)obj;235                        default: throw Assert.Unreachable;236                    }237                }238            }239 240            private object ConvertUInt64(UInt64 obj)241            {242                checked243                {244                    switch (_to)245                    {246                        case TypeCode.Byte: return (byte)obj;247                        case TypeCode.SByte: return (sbyte)obj;248                        case TypeCode.Int16: return (Int16)obj;249                        case TypeCode.Char: return (char)obj;250                        case TypeCode.Int32: return (Int32)obj;251                        case TypeCode.Int64: return (Int64)obj;252                        case TypeCode.UInt16: return (UInt16)obj;253                        case TypeCode.UInt32: return (UInt32)obj;254                        case TypeCode.UInt64: return (UInt64)obj;255                        case TypeCode.Single: return (Single)obj;256                        case TypeCode.Double: return (double)obj;257                        default: throw Assert.Unreachable;258                    }259                }260            }261 262            private object ConvertDouble(double obj)263            {264                checked265                {266                    switch (_to)267                    {268                        case TypeCode.Byte: return (byte)obj;269                        case TypeCode.SByte: return (sbyte)obj;270                        case TypeCode.Int16: return (Int16)obj;271                        case TypeCode.Char: return (char)obj;272                        case TypeCode.Int32: return (Int32)obj;273                        case TypeCode.Int64: return (Int64)obj;274                        case TypeCode.UInt16: return (UInt16)obj;275                        case TypeCode.UInt32: return (UInt32)obj;276                        case TypeCode.UInt64: return (UInt64)obj;277                        case TypeCode.Single: return (Single)obj;278                        case TypeCode.Double: return (double)obj;279                        default: throw Assert.Unreachable;280                    }281                }282            }283        }284    }285}286