Beispiel #1
0
        /// <summary>
        /// Tests if the expression will evaluate False with any combination of remaining inputs
        /// </summary>
        /// <returns>true if all possible combinations result in a False evaluation</returns>
        /// <remarks>
        /// This determines if the search for operands may be halted before the end of the list.
        /// For example, the expression 'a AND b' can be halted if 'a' is false because it does not
        /// matter what 'b' is. Conversely, the expression 'a OR b' cannot be halted if 'a' is false
        /// since 'b' can be true. Even if the expression is known to be True before evaluating all
        /// the operands, processing operands should continue to find all the matches in the remaining
        /// operands.
        /// </remarks>
        public bool IsShortCircuitFalse()
        {
            var savedState = Operands.Select(o => o.EvaluatedResult).ToList();

            List <List <bool> > values = new List <List <bool> >();

            for (int idx = 0; idx < Math.Pow(2, Operands.Count); idx++)
            {
                values.Add(new List <bool>());
                string binary = Convert.ToString(idx, 2).PadLeft(Operands.Count(), '0');

                for (int jdx = 0; jdx < Operands.Count; jdx++)
                {
                    var b = binary[jdx];
                    values[idx].Add(b != '0');
                }
            }

            EvaluationResult result = EvaluationResult.Undetermined;

            foreach (var row in values)
            {
                for (int col = 0; col < row.Count; col++)
                {
                    if (!Operands[col].EvaluatedResult.HasValue)
                    {
                        Operands[col].EvaluatedResult = row[col];
                    }
                }

                EvaluationResult rowResult = EvaluateExpression(false) ? EvaluationResult.True : EvaluationResult.False;

                // restore original state
                for (int col = 0; col < Operands.Count; col++)
                {
                    Operands[col].EvaluatedResult = savedState[col];
                }

                if (result == EvaluationResult.Undetermined)
                {
                    result = rowResult;
                }
                else if (result != rowResult)
                {
                    return(false);
                }
            }

            return(result == EvaluationResult.False);
        }
Beispiel #2
0
        protected override void Compile(Program program)
        {
            if (Operands[0].OperandType == OperandType.Direct)
            {
                throw new ArgumentException();
            }

            var w = Operands.ToW();

            if (Operands.Count(x => x.Value == "al" || x.Value == "ax") == 1)
            {
                var d = Operands[0].OperandType == OperandType.Memory ? 1 : 0;
                BinaryCode.AddBits(1, 0, 1, 0, 0, 0, d, w);
                var inMemoryOperand = Operands.First(x => x.OperandType == OperandType.Memory);
                var reference       = program.Labels[inMemoryOperand.Value];
                BinaryCode.AddBytes(reference.InReverseByteOrder(program.ReferenceSize));
                program.ObjectFile.SegmentsBlock.CodeSegment.AddReference(
                    new BinaryCode(0xC4)
                    .AddBytes((Address - program.DataSize + 1 - reference).InReverseByteOrder(1))
                    .AddBytes(0x14, 0x01, 0x02));
            }
            else if (Operands[0].OperandType == OperandType.Register)
            {
                if (Operands[1].OperandType == OperandType.Direct)
                {
                    BinaryCode.AddBits(1, 0, 1, 1, w);
                    BinaryCode.AddBits(RegistersManager.GetCode(Operands[0].Value));
                    BinaryCode.AddBytes(int.Parse(Operands[1].Value).InReverseByteOrder(w + 1));
                }
                else if (Operands[1].OperandType == OperandType.Register)
                {
                    BinaryCode.AddBits(1, 0, 0, 0, 1, 0, 1, w, 1, 1);
                    BinaryCode.AddBits(RegistersManager.GetCode(Operands[0].Value));
                    BinaryCode.AddBits(RegistersManager.GetCode(Operands[1].Value));
                }
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Beispiel #3
0
        public bool TryParse(string input)
        {
            ParserState = ParserErrorState.None;
            bool result = true;

            try
            {
                BooleanTokenizer tokenizer = new BooleanTokenizer();
                var list = tokenizer.Tokenize(input).ToList();

                result = BuildExpression(list);

                PostfixTokens     = InfixToPostfix(list).ToList();
                PostfixExpression = string.Join(" ", PostfixTokens.Select(t => t.Value));

                int numUnaryOperators  = PostfixTokens.Where(t => t.TokenType == TokenType.NOT).Count();
                int numBinaryOperators = PostfixTokens.Where(t => TokenType.Operator.HasFlag(t.TokenType)).Count() - numUnaryOperators;
                int numOperands        = Operands.Count();

                if (numBinaryOperators < numOperands - 1)
                {
                    ParserState = ParserErrorState.MissingOperator;
                    result      = false;
                }
            }
            catch (InvalidStateException ex)
            {
                ParserState = ex.State;
                result      = false;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                result      = false;
                ParserState = ParserErrorState.UnknownError;
            }

            return(result);
        }
Beispiel #4
0
 /// <summary>
 /// Return true if this node has operands, otherwise return false
 /// </summary>
 /// <returns></returns>
 public bool HasOperands()
 {
     return(Operands.Count() > 0);
 }