VariableType ValidateExpression(LogicalExpression exp, out bool valid) { VariableType retType = VariableType.INT; valid = true; if (exp.HasUnary()) { // condition is of type Unary retType = ValidateExpression(exp.GetUnary().GetLogicalExpression(), out valid); if (retType != VariableType.BOOLEAN) { valid = false; } } else if (exp.HasValue()) { if (exp.GetValue().HasVariable()) //is a variable { retType = (VariableType)exp.GetValue().GetVariable().GetType2().Value; } else // is a constant { retType = GetConstantType(exp.GetValue().GetConstant()); } } else if (exp.HasLogical())// condition is of type Logical { bool valid1; bool valid2; VariableType t1 = ValidateExpression(exp.GetLogical().GetLHSLogicalExpression(), out valid1); VariableType t2 = ValidateExpression(exp.GetLogical().GetRHSLogicalExpression(), out valid2); valid = valid1 && valid2; retType = VariableType.BOOLEAN; } else // condition is of type relational { VariableType type = VariableType.INT; if (IsValidRelational(exp.GetRelational().GetLHSArithmeticExpression(), exp.GetRelational().GetRHSArithmeticExpression(), exp.GetRelational().GetRelationalOperator(), type)) { retType = type; } else { valid = false; } } return(retType); }
void VisitLogicalExpression(LogicalExpression exp) { if (exp.HasUnary()) { VisitLogicalExpression(exp.GetUnary().GetLogicalExpression()); } else if (exp.HasRelational()) { VisitArithmeticExpression(exp.GetRelational().GetLHSArithmeticExpression()); VisitArithmeticExpression(exp.GetRelational().GetRHSArithmeticExpression()); } else if (exp.HasLogical()) { VisitLogicalExpression(exp.GetLogical().GetLHSLogicalExpression()); VisitLogicalExpression(exp.GetLogical().GetRHSLogicalExpression()); } else if (exp.HasValue()) { if (exp.GetValue().HasVariable()) { m_VariableReferences.Add(exp.GetValue().GetVariable().GetName().Value); } } }
string GetLogicalExpressionString(LogicalExpression exp) { string retVal = ""; if (exp.HasUnary()) { retVal = GetLogicalExpressionString(exp.GetUnary().GetLogicalExpression()); } else if (exp.HasRelational()) { RelationalOperator op = exp.GetRelational().GetRelationalOperator(); string lhs = GetArithmeticExpressionString(exp.GetRelational().GetLHSArithmeticExpression()); string rhs = GetArithmeticExpressionString(exp.GetRelational().GetRHSArithmeticExpression()); string opString = ""; if (op.HasEquals()) { opString = "=="; } else if (op.HasNotEquals()) { opString = "!="; } else if (op.HasPartOf()) { opString = "partof"; } else if (op.HasNotPartOf()) { opString = "notpartof"; } else if (op.HasLessThan()) { opString = "<"; } else if (op.HasLessThanOrEquals()) { opString = "<="; } else if (op.HasGreaterThan()) { opString = ">"; } else if (op.HasGreaterThanOrEquals()) { opString = ">="; } retVal = lhs + " " + opString + " " + rhs; } else if (exp.HasLogical()) { string lhs = GetLogicalExpressionString(exp.GetLogical().GetLHSLogicalExpression()); string rhs = GetLogicalExpressionString(exp.GetLogical().GetRHSLogicalExpression()); string opString; if (exp.GetLogical().GetLogicalOperator().HasAnd()) { opString = "and"; } else //OR { opString = "or"; } retVal = lhs + " " + opString + " " + rhs; } else if (exp.HasValue()) { if (exp.GetValue().HasConstant()) { if ((bool)exp.GetValue().GetConstant().GetBoolean().Value) { retVal = "true"; } else { retVal = "false"; } } else { retVal = exp.GetValue().GetVariable().GetName().Value; } } return(retVal); }
bool EvaluateLogicalExpression(LogicalExpression exp) { bool retVal = false; if (exp.HasUnary()) { retVal = EvaluateLogicalExpression(exp.GetUnary().GetLogicalExpression()); } else if (exp.HasRelational()) { RelationalOperator op = exp.GetRelational().GetRelationalOperator(); ConstantType lhs = EvaluateArithmeticExpression(exp.GetRelational().GetLHSArithmeticExpression()); ConstantType rhs = EvaluateArithmeticExpression(exp.GetRelational().GetRHSArithmeticExpression()); if (op.HasEquals()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { retVal = ((bool)lhs.GetBoolean().Value == (bool)rhs.GetBoolean().Value); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { string lhsString = (string)lhs.GetString2().Value; string rhsString = (string)rhs.GetString2().Value; retVal = (lhsString == rhsString); } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetInteger().Value); } } } else if (op.HasNotEquals()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { retVal = ((bool)lhs.GetBoolean().Value != (bool)rhs.GetBoolean().Value); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { retVal = (lhs.GetString2() != rhs.GetString2()); } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetInteger().Value); } } } else if (op.HasPartOf()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { retVal = ((bool)lhs.GetBoolean().Value == (bool)rhs.GetBoolean().Value); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { retVal = (lhs.GetString2() == rhs.GetString2()); } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetInteger().Value); } } } else if (op.HasNotPartOf()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { retVal = ((bool)lhs.GetBoolean().Value != (bool)rhs.GetBoolean().Value); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { retVal = (lhs.GetString2().Value != rhs.GetString2().Value); } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetInteger().Value); } } } else if (op.HasLessThan()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { throw new InvalidRuleException("Incompatible types in expression"); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value < (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value < (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { retVal = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value, true) == -1; } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value < (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value < (float)rhs.GetInteger().Value); } } } else if (op.HasLessThanOrEquals()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { throw new InvalidRuleException("Incompatible types in expression"); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value <= (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value <= (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { int result = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value); retVal = (result == -1) || (result == 0); } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value <= (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value <= (float)rhs.GetInteger().Value); } } } else if (op.HasGreaterThan()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { throw new InvalidRuleException("Incompatible types in expression"); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value > (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value > (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { retVal = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value, true) == 1; } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value > (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value > (float)rhs.GetInteger().Value); } } } else if (op.HasGreaterThanOrEquals()) { if (lhs.HasBoolean() && rhs.HasBoolean()) { throw new InvalidRuleException("Incompatible types in expression"); } else if (lhs.HasInteger()) { if (rhs.HasInteger()) { retVal = ((int)lhs.GetInteger().Value >= (int)rhs.GetInteger().Value); } else if (rhs.HasFloat2()) { retVal = ((int)lhs.GetInteger().Value >= (int)rhs.GetFloat2().Value); } } else if (lhs.HasString2() && rhs.HasString2()) { int result = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value, true); retVal = (result == 1) || (result == 0); } else if (lhs.HasFloat2()) { if (rhs.HasFloat2()) { retVal = ((float)lhs.GetFloat2().Value >= (float)rhs.GetFloat2().Value); } else if (rhs.HasInteger()) { retVal = ((float)lhs.GetFloat2().Value >= (float)rhs.GetInteger().Value); } } } } else if (exp.HasLogical()) { bool lhs = EvaluateLogicalExpression(exp.GetLogical().GetLHSLogicalExpression()); bool rhs = EvaluateLogicalExpression(exp.GetLogical().GetRHSLogicalExpression()); if (exp.GetLogical().GetLogicalOperator().HasAnd()) { retVal = lhs && rhs; } else//OR { retVal = lhs || rhs; } } else if (exp.HasValue()) { if (exp.GetValue().HasConstant()) { retVal = exp.GetValue().GetConstant().GetBoolean().Value; } else { string currName = exp.GetValue().GetVariable().GetName().Value; Variable variable; if (m_Context.GetState().GetVariable(currName, out variable)) { retVal = (bool)variable.GetValue(); } } } return(retVal); }
NodeBase CreateLogicalNode(LogicalExpression exp) { NodeBase node = null; if (exp.HasUnary()) { node = new LogicalNode(m_Container); ((LogicalNode)node).m_LogicalType = LogicalOperatorType.NOT; node.AddChild(CreateLogicalNode(exp.GetUnary().GetLogicalExpression())); } else if (exp.HasRelational()) { node = new RelationalNode(m_Container); RelationalOperator op = exp.GetRelational().GetRelationalOperator(); if (op.HasEquals()) { ((RelationalNode)node).m_RelationType = RelationType.EQUAL; } else if (op.HasGreaterThan()) { ((RelationalNode)node).m_RelationType = RelationType.GREATERTHAN; } else if (op.HasGreaterThanOrEquals()) { ((RelationalNode)node).m_RelationType = RelationType.GREATERTHANOREQUAL; } else if (op.HasLessThan()) { ((RelationalNode)node).m_RelationType = RelationType.LESSTHAN; } else if (op.HasLessThanOrEquals()) { ((RelationalNode)node).m_RelationType = RelationType.LESSTHANOREQUAL; } else if (op.HasNotEquals()) { ((RelationalNode)node).m_RelationType = RelationType.NOTEQUAL; } node.AddChild(CreateArithmeticNode(exp.GetRelational().GetLHSArithmeticExpression())); node.AddChild(CreateArithmeticNode(exp.GetRelational().GetRHSArithmeticExpression())); } else if (exp.HasLogical()) { node = new LogicalNode(m_Container); if (exp.GetLogical().GetLogicalOperator().HasAnd()) { ((LogicalNode)node).m_LogicalType = LogicalOperatorType.AND; } else { ((LogicalNode)node).m_LogicalType = LogicalOperatorType.OR; } node.AddChild(CreateLogicalNode(exp.GetLogical().GetLHSLogicalExpression())); node.AddChild(CreateLogicalNode(exp.GetLogical().GetRHSLogicalExpression())); } else if (exp.HasValue()) { if (exp.GetValue().HasConstant()) { node = new ConstantNode(m_Container); ((ConstantNode)node).m_ConstantValue = exp.GetValue().GetConstant().GetBoolean().Value; ((ConstantNode)node).m_ConstantType = VariableType.BOOLEAN; } else { node = new VariableNode(m_Container); ((VariableNode)node).m_VariableName = exp.GetValue().GetVariable().GetName().Value; ((VariableNode)node).m_VariableType = VariableType.BOOLEAN; } } return(node); }