Search in sources :

Example 1 with Comparison

use of org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.Comparison in project xtext-core by eclipse.

the class ConditionSimplifier method areSemanticallyEqual.

public boolean areSemanticallyEqual(Expression left, Expression right) {
    if (left.eClass() != right.eClass())
        return false;
    if (left instanceof OrExpression) {
        OrExpression leftOrExpression = (OrExpression) left;
        OrExpression rightOrExpression = (OrExpression) right;
        return (areSemanticallyEqual(leftOrExpression.getLeft(), rightOrExpression.getLeft()) && areSemanticallyEqual(leftOrExpression.getRight(), rightOrExpression.getRight())) || (areSemanticallyEqual(leftOrExpression.getRight(), rightOrExpression.getLeft()) && areSemanticallyEqual(leftOrExpression.getLeft(), rightOrExpression.getRight()));
    }
    if (left instanceof AndExpression) {
        AndExpression leftAndExpression = (AndExpression) left;
        AndExpression rightAndExpression = (AndExpression) right;
        return (areSemanticallyEqual(leftAndExpression.getLeft(), rightAndExpression.getLeft()) && areSemanticallyEqual(leftAndExpression.getRight(), rightAndExpression.getRight())) || (areSemanticallyEqual(leftAndExpression.getRight(), rightAndExpression.getLeft()) && areSemanticallyEqual(leftAndExpression.getLeft(), rightAndExpression.getRight()));
    }
    if (left instanceof NotExpression) {
        return areSemanticallyEqual(((NotExpression) left).getExpression(), ((NotExpression) right).getExpression());
    }
    if (left instanceof Comparison) {
        Comparison leftComparison = (Comparison) left;
        Comparison rightComparison = (Comparison) right;
        return ((Comparison) left).getOperator().equals(rightComparison.getOperator()) && areSemanticallyEqual(leftComparison.getLeft(), rightComparison.getLeft()) && areSemanticallyEqual(leftComparison.getRight(), rightComparison.getRight());
    }
    if (left instanceof NumberLiteral) {
        int leftValue = ((NumberLiteral) left).getValue();
        int rightValue = ((NumberLiteral) right).getValue();
        return leftValue == rightValue;
    }
    if (left instanceof MethodCall) {
        String leftMethod = ((MethodCall) left).getValue();
        String rightMethod = ((MethodCall) right).getValue();
        return leftMethod.equals(rightMethod);
    }
    throw new IllegalStateException(left + " - " + right);
}
Also used : AndExpression(org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.AndExpression) Comparison(org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.Comparison) NotExpression(org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.NotExpression) OrExpression(org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.OrExpression) MethodCall(org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.MethodCall) NumberLiteral(org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.NumberLiteral)

Aggregations

AndExpression (org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.AndExpression)1 Comparison (org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.Comparison)1 MethodCall (org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.MethodCall)1 NotExpression (org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.NotExpression)1 NumberLiteral (org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.NumberLiteral)1 OrExpression (org.eclipse.xtext.xtext.generator.parser.antlr.splitting.simpleExpressions.OrExpression)1