Search in sources :

Example 1 with CompoundSyntaxTree

use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.

the class DefaultCompoundSyntaxTree method equals.

@Override
public boolean equals(Object anotherObject) {
    boolean result;
    if (anotherObject instanceof CompoundSyntaxTree) {
        CompoundSyntaxTree anotherCompoundSyntaxTree = (CompoundSyntaxTree) anotherObject;
        if (this.hashCode() == anotherCompoundSyntaxTree.hashCode()) {
            List<SyntaxTree> anotherSubTrees = anotherCompoundSyntaxTree.getImmediateSubTrees();
            result = this.getRootTree().equals(anotherCompoundSyntaxTree.getRootTree()) && this.getImmediateSubTrees().equals(anotherSubTrees);
        } else {
            result = false;
        }
    } else {
        result = false;
    }
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 2 with CompoundSyntaxTree

use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.

the class Expressions method makeFromSyntaxTree.

/**
 * Returns an expression represented by a given syntax tree.
 * Scheduled to be removed once expressions are no longer based on syntax trees.
 */
public static Expression makeFromSyntaxTree(SyntaxTree syntaxTree) {
    if (syntaxTree instanceof CompoundSyntaxTree) {
        Expression result = makeExpressionOnSyntaxTreeWithLabelAndSubTrees(syntaxTree.getLabel(), syntaxTree.getImmediateSubTrees().toArray());
        // Expression result = new ExpressionOnCompoundSyntaxTree(syntaxTree);
        return result;
    }
    if (syntaxTree instanceof SyntaxLeaf) {
        SyntaxLeaf syntaxLeaf = (SyntaxLeaf) syntaxTree;
        Expression result;
        if (syntaxLeaf.isStringLiteral()) {
            result = Expressions.makeStringLiteral((String) syntaxLeaf.getValue());
        } else {
            result = Expressions.makeSymbol(syntaxLeaf.getValue());
        }
        // Expression result = new ExpressionOnSyntaxLeaf(syntaxTree);
        return result;
    }
    throw new Error("Syntax tree " + syntaxTree + " should be either a CompoundSyntaxTree or a Symbol");
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 3 with CompoundSyntaxTree

use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.

the class DefaultCompoundSyntaxTree method compareTo.

@Override
public /**
 * Compares this CompoundSyntaxTree to other syntax trees, placing it after {@link Symbol}s and comparing
 * it lexicographically to other CompoundSyntaxTree, using both the label and sub trees.
 */
int compareTo(Object anotherObject) {
    int result;
    if (anotherObject instanceof CompoundSyntaxTree) {
        CompoundSyntaxTree normalizedAnotherCompoundSyntaxTree = (CompoundSyntaxTree) anotherObject;
        Iterator<SyntaxTree> subTreesIncludingLabel = getImmediateSubTreesIncludingRootOneIterator();
        Iterator<SyntaxTree> anotherSubTreesIncludingLabel = normalizedAnotherCompoundSyntaxTree.getImmediateSubTreesIncludingRootOneIterator();
        result = lexicographicComparison.compare(subTreesIncludingLabel, anotherSubTreesIncludingLabel);
    } else {
        // Symbols "come first" in the default order
        result = 1;
    }
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 4 with CompoundSyntaxTree

use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.

the class DefaultSyntaxLeaf method compareTo.

@Override
public /**
 * Compares this Symbol to other syntax trees, placing it before {@link CompoundSyntaxTree}s and comparing
 * it to other Symbols by comparing their values if they are in the same type (as returned by {@link #getValueType()},
 * or their types if they are of different types.
 */
int compareTo(Object another) {
    if (this == another) {
        return 0;
    }
    if (another instanceof CompoundSyntaxTree) {
        // Symbols come first
        return -1;
    }
    SyntaxLeaf anotherSymbol = null;
    if (another instanceof SyntaxLeaf) {
        anotherSymbol = (SyntaxLeaf) another;
    } else {
        anotherSymbol = createSyntaxLeaf(another, false);
        // Test again, as may have had self returned from the symbol table.
        if (this == anotherSymbol) {
            return 0;
        }
    }
    try {
        @SuppressWarnings("unchecked") Comparable<Object> value = (Comparable<Object>) getValue();
        @SuppressWarnings("unchecked") Comparable<Object> anotherValue = (Comparable<Object>) anotherSymbol.getValue();
        int result;
        String valueType = getValueType();
        String anotherValueType = anotherSymbol.getValueType();
        if (valueType.equals(anotherValueType)) {
            result = value.compareTo(anotherValue);
        } else {
            result = valueType.compareTo(anotherValueType);
        }
        return result;
    } catch (ClassCastException e) {
        throw new Error("Using DefaultSymbol.compareTo method with non-comparable values.", e);
    }
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Aggregations

CompoundSyntaxTree (com.sri.ai.expresso.api.CompoundSyntaxTree)4 SyntaxLeaf (com.sri.ai.expresso.api.SyntaxLeaf)2 SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)2 Expression (com.sri.ai.expresso.api.Expression)1 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)1 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)1