Пример #1
0
        /// <summary>
        /// Converts the provided trilean to an I4 stack value, pushes it onto the stack and returns the success
        /// dispatcher result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <param name="result">The trilean value.</param>
        /// <returns>The dispatch result.</returns>
        protected static DispatchResult ConvertToI4AndReturnSuccess(CilExecutionContext context, Trilean result)
        {
            var i4Result = new I4Value(result.ToBooleanOrFalse() ? 1 : 0, 0xFFFFFFFEu | (result.IsKnown ? 1u : 0u));

            context.ProgramState.Stack.Push(i4Result);
            return(DispatchResult.Success());
        }
Пример #2
0
        /// <summary>
        /// Parses a (partially) known bit string and sets the contents of integer to the parsed result.
        /// </summary>
        /// <param name="bitString">The bit string to parse.</param>
        /// <exception cref="OverflowException"></exception>
        public void SetBits(string bitString)
        {
            Span <byte> backingBits = stackalloc byte[Size];
            Span <byte> backingMask = stackalloc byte[Size];

            backingMask.Fill(0xFF);

            var bits = new BitField(backingBits);
            var mask = new BitField(backingMask);

            for (int i = 0; i < bitString.Length; i++)
            {
                Trilean bit = bitString[bitString.Length - i - 1] switch
                {
                    '0' => Trilean.False,
                    '1' => Trilean.True,
                    '?' => Trilean.Unknown,
                    _ => throw new FormatException()
                };

                if (i >= 8 * backingBits.Length)
                {
                    if (bit.IsUnknown || bit)
                    {
                        throw new OverflowException();
                    }
                }
                else if (bit.IsKnown)
                {
                    bits[i] = bit.ToBooleanOrFalse();
                }
                else
                {
                    mask[i] = false;
                }
            }

            SetBits(backingBits, backingMask);
        }