MegaBites-AI/Windows-powershell
0372
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 16using System.Reflection;17 18namespace System.Management.Automation.Interpreter19{20 internal abstract class EqualInstruction : Instruction21 {22 // Perf: EqualityComparer<T> but is 3/2 to 2 times slower.23 private static Instruction s_reference, s_boolean, s_SByte, s_int16, s_char, s_int32, s_int64, s_byte, s_UInt16, s_UInt32, s_UInt64, s_single, s_double;24 25 public override int ConsumedStack { get { return 2; } }26 27 public override int ProducedStack { get { return 1; } }28 29 private EqualInstruction()30 {31 }32 33 internal sealed class EqualBoolean : EqualInstruction34 {35 public override int Run(InterpretedFrame frame)36 {37 frame.Push(((bool)frame.Pop()) == ((bool)frame.Pop()));38 return +1;39 }40 }41 42 internal sealed class EqualSByte : EqualInstruction43 {44 public override int Run(InterpretedFrame frame)45 {46 frame.Push(((sbyte)frame.Pop()) == ((sbyte)frame.Pop()));47 return +1;48 }49 }50 51 internal sealed class EqualInt16 : EqualInstruction52 {53 public override int Run(InterpretedFrame frame)54 {55 frame.Push(((Int16)frame.Pop()) == ((Int16)frame.Pop()));56 return +1;57 }58 }59 60 internal sealed class EqualChar : EqualInstruction61 {62 public override int Run(InterpretedFrame frame)63 {64 frame.Push(((char)frame.Pop()) == ((char)frame.Pop()));65 return +1;66 }67 }68 69 internal sealed class EqualInt32 : EqualInstruction70 {71 public override int Run(InterpretedFrame frame)72 {73 frame.Push(((Int32)frame.Pop()) == ((Int32)frame.Pop()));74 return +1;75 }76 }77 78 internal sealed class EqualInt64 : EqualInstruction79 {80 public override int Run(InterpretedFrame frame)81 {82 frame.Push(((Int64)frame.Pop()) == ((Int64)frame.Pop()));83 return +1;84 }85 }86 87 internal sealed class EqualByte : EqualInstruction88 {89 public override int Run(InterpretedFrame frame)90 {91 frame.Push(((byte)frame.Pop()) == ((byte)frame.Pop()));92 return +1;93 }94 }95 96 internal sealed class EqualUInt16 : EqualInstruction97 {98 public override int Run(InterpretedFrame frame)99 {100 frame.Push(((UInt16)frame.Pop()) == ((UInt16)frame.Pop()));101 return +1;102 }103 }104 105 internal sealed class EqualUInt32 : EqualInstruction106 {107 public override int Run(InterpretedFrame frame)108 {109 frame.Push(((UInt32)frame.Pop()) == ((UInt32)frame.Pop()));110 return +1;111 }112 }113 114 internal sealed class EqualUInt64 : EqualInstruction115 {116 public override int Run(InterpretedFrame frame)117 {118 frame.Push(((UInt64)frame.Pop()) == ((UInt64)frame.Pop()));119 return +1;120 }121 }122 123 internal sealed class EqualSingle : EqualInstruction124 {125 public override int Run(InterpretedFrame frame)126 {127 frame.Push(((Single)frame.Pop()) == ((Single)frame.Pop()));128 return +1;129 }130 }131 132 internal sealed class EqualDouble : EqualInstruction133 {134 public override int Run(InterpretedFrame frame)135 {136 frame.Push(((double)frame.Pop()) == ((double)frame.Pop()));137 return +1;138 }139 }140 141 internal sealed class EqualReference : EqualInstruction142 {143 public override int Run(InterpretedFrame frame)144 {145 frame.Push(frame.Pop() == frame.Pop());146 return +1;147 }148 }149 150 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]151 public static Instruction Create(Type type)152 {153 // Boxed enums can be unboxed as their underlying types:154 var typeToUse = type.IsEnum ? Enum.GetUnderlyingType(type) : type;155 switch (typeToUse.GetTypeCode())156 {157 case TypeCode.Boolean: return s_boolean ??= new EqualBoolean();158 case TypeCode.SByte: return s_SByte ??= new EqualSByte();159 case TypeCode.Byte: return s_byte ??= new EqualByte();160 case TypeCode.Char: return s_char ??= new EqualChar();161 case TypeCode.Int16: return s_int16 ??= new EqualInt16();162 case TypeCode.Int32: return s_int32 ??= new EqualInt32();163 case TypeCode.Int64: return s_int64 ??= new EqualInt64();164 165 case TypeCode.UInt16: return s_UInt16 ??= new EqualInt16();166 case TypeCode.UInt32: return s_UInt32 ??= new EqualInt32();167 case TypeCode.UInt64: return s_UInt64 ??= new EqualInt64();168 169 case TypeCode.Single: return s_single ??= new EqualSingle();170 case TypeCode.Double: return s_double ??= new EqualDouble();171 172 case TypeCode.Object:173 if (!type.IsValueType)174 {175 return s_reference ??= new EqualReference();176 }177 // TODO: Nullable<T>178 throw new NotImplementedException();179 180 default:181 throw new NotImplementedException();182 }183 }184 185 public override string ToString()186 {187 return "Equal()";188 }189 }190}191 