public override BooleanExp Replace(char name, BooleanExp exp) { if (_name == name) { return(exp.Copy()); } else { return(new VariableExp(_name)); } }
public bool ParseBlocks(string input) { // [expression] opt ended by ";" // (block) // [(block) operator (block)] // [(var op var) operator (var op (block))] // [(var op var) operator (var op (op var))] // var & op delimited by ' ' : // [(str str str) str (str str (str str))] // op is determined by: // 1. Outside of any block // 2. Item 1 if two items // 3. Even # items if odd # items total // 4. Error if even # items > 2 // var is determined by: // Remainders in the lists // Parse text into useable form IBlockParser parser = new BlockCharParser(OPEN_TAG, CLOSE_TAG, DELIMITER); Block expression; if (parser.ValidateBalancedTags(input)) { expression = new Block(input, parser); Console.WriteLine(expression); } else { Console.WriteLine(parser.Log); return(false); } // Create operations BooleanExp operation = ParseOperators(expression); // Create assignment context. Dictionary <string, string> allAssignments = GetVariablesText(input, ASSIGNMENT_TRIGGER, ASSIGNMENT_OPERATOR); Dictionary <VariableExp, bool> validAssignments = ConvertVariableAssignment(allAssignments); Context context = new Context(); context = AssignVariables(validAssignments, context); // Perform calculation return(operation.Evaluate(context)); }
/// <summary> /// Returns the boolean operator with the appropriate variable assignments. /// </summary> /// <param name="boolOperator"></param> /// <param name="operand1"></param> /// <param name="operand2"></param> /// <returns></returns> private BooleanExp GetOperator(string boolOperator, BooleanExp operand1, BooleanExp operand2 = null) { switch (boolOperator) { case AND: return(new AndExp(operand1, operand2)); case NOT: return(new NotExp(operand1)); case OR: return(new OrExp(operand1, operand2)); default: throw new InvalidOperationException("Boolean comparison operator is invalid: " + boolOperator); } }
private bool IsSubOperationComplete(string operatorItem, BooleanExp operand1, BooleanExp operand2) { if (IsOperatorWithSingularOperand(operatorItem) && operand1 != null) { return(true); } else if (!string.IsNullOrEmpty(operatorItem) && operand1 != null && operand2 != null) { return(true); } else { return(false); } }
public abstract BooleanExp Replace(char name, BooleanExp exp);
public override BooleanExp Replace(char name, BooleanExp exp) { return(new Constant(_value)); }
public override BooleanExp Replace(char name, BooleanExp exp) { return(new AndExp( _operand1.Replace(name, exp), _operand2.Replace(name, exp))); }
public AndExp(BooleanExp op1, BooleanExp op2) { _operand1 = op1; _operand2 = op2; }
private BooleanExp ParseOperators(Block expression) { // Get list of blocks as strings List <string> blocks = new List <string>(); foreach (Block block in expression.ChildBlocks) { blocks.Add(OPEN_TAG + block.ToString() + CLOSE_TAG); } BooleanExp operand1 = null; BooleanExp operand2 = null; string operatorItem = null; int j = 0; for (int i = 0; i < expression.ChildText.Count; i++) { string item = expression.ChildText[i]; if (item == ASSIGNMENT_TRIGGER) { break; } if (blocks.Count > j && item == blocks[j]) { if (IsFirstOperand(operand1)) { operand1 = ParseOperators(expression.ChildBlocks[j]); } else { operand2 = ParseOperators(expression.ChildBlocks[j]); } j++; } else if (IsOperator(item)) { if (!string.IsNullOrEmpty(operatorItem)) { throw new InvalidOperationException("Two operators cannot follow each other. Each operator must be separated by an operand. \n" + "Operator '" + operatorItem + "' at item index " + i); } operatorItem = item; } else { if (IsFirstOperand(operand1)) { operand1 = ConvertOperand(item); } else { operand2 = ConvertOperand(item); } } // Assign and reset values if sub-operation is complete if (IsSubOperationComplete(operatorItem, operand1, operand2)) { operand1 = GetOperator(operatorItem, operand1, operand2); operatorItem = null; operand2 = null; } } return(operand1); }
private bool IsFirstOperand(BooleanExp operand1) { return(operand1 == null); }
public NotExp(BooleanExp op1) { _operand1 = op1; }