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.Diagnostics;17 18namespace System.Management.Automation.Interpreter19{20 internal abstract class DivInstruction : Instruction21 {22 private static Instruction s_int16, s_int32, s_int64, s_UInt16, s_UInt32, s_UInt64, s_single, s_double;23 24 public override int ConsumedStack { get { return 2; } }25 26 public override int ProducedStack { get { return 1; } }27 28 private DivInstruction()29 {30 }31 32 internal sealed class DivInt32 : DivInstruction33 {34 public override int Run(InterpretedFrame frame)35 {36 object l = frame.Data[frame.StackIndex - 2];37 object r = frame.Data[frame.StackIndex - 1];38 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject((Int32)l / (Int32)r);39 frame.StackIndex--;40 return 1;41 }42 }43 44 internal sealed class DivInt16 : DivInstruction45 {46 public override int Run(InterpretedFrame frame)47 {48 object l = frame.Data[frame.StackIndex - 2];49 object r = frame.Data[frame.StackIndex - 1];50 frame.Data[frame.StackIndex - 2] = (Int16)((Int16)l / (Int16)r);51 frame.StackIndex--;52 return 1;53 }54 }55 56 internal sealed class DivInt64 : DivInstruction57 {58 public override int Run(InterpretedFrame frame)59 {60 object l = frame.Data[frame.StackIndex - 2];61 object r = frame.Data[frame.StackIndex - 1];62 frame.Data[frame.StackIndex - 2] = (Int64)((Int64)l / (Int64)r);63 frame.StackIndex--;64 return 1;65 }66 }67 68 internal sealed class DivUInt16 : DivInstruction69 {70 public override int Run(InterpretedFrame frame)71 {72 object l = frame.Data[frame.StackIndex - 2];73 object r = frame.Data[frame.StackIndex - 1];74 frame.Data[frame.StackIndex - 2] = (UInt16)((UInt16)l / (UInt16)r);75 frame.StackIndex--;76 return 1;77 }78 }79 80 internal sealed class DivUInt32 : DivInstruction81 {82 public override int Run(InterpretedFrame frame)83 {84 object l = frame.Data[frame.StackIndex - 2];85 object r = frame.Data[frame.StackIndex - 1];86 frame.Data[frame.StackIndex - 2] = (UInt32)((UInt32)l / (UInt32)r);87 frame.StackIndex--;88 return 1;89 }90 }91 92 internal sealed class DivUInt64 : DivInstruction93 {94 public override int Run(InterpretedFrame frame)95 {96 object l = frame.Data[frame.StackIndex - 2];97 object r = frame.Data[frame.StackIndex - 1];98 frame.Data[frame.StackIndex - 2] = (UInt64)((Int16)l / (Int16)r);99 frame.StackIndex--;100 return 1;101 }102 }103 104 internal sealed class DivSingle : DivInstruction105 {106 public override int Run(InterpretedFrame frame)107 {108 object l = frame.Data[frame.StackIndex - 2];109 object r = frame.Data[frame.StackIndex - 1];110 frame.Data[frame.StackIndex - 2] = (Single)((Single)l / (Single)r);111 frame.StackIndex--;112 return 1;113 }114 }115 116 internal sealed class DivDouble : DivInstruction117 {118 public override int Run(InterpretedFrame frame)119 {120 object l = frame.Data[frame.StackIndex - 2];121 object r = frame.Data[frame.StackIndex - 1];122 frame.Data[frame.StackIndex - 2] = (double)l / (double)r;123 frame.StackIndex--;124 return 1;125 }126 }127 128 public static Instruction Create(Type type)129 {130 Debug.Assert(!type.IsEnum);131 switch (type.GetTypeCode())132 {133 case TypeCode.Int16: return s_int16 ??= new DivInt16();134 case TypeCode.Int32: return s_int32 ??= new DivInt32();135 case TypeCode.Int64: return s_int64 ??= new DivInt64();136 case TypeCode.UInt16: return s_UInt16 ??= new DivUInt16();137 case TypeCode.UInt32: return s_UInt32 ??= new DivUInt32();138 case TypeCode.UInt64: return s_UInt64 ??= new DivUInt64();139 case TypeCode.Single: return s_single ??= new DivSingle();140 case TypeCode.Double: return s_double ??= new DivDouble();141 142 default:143 throw Assert.Unreachable;144 }145 }146 147 public override string ToString()148 {149 return "Div()";150 }151 }152}153 