use of com.sri.ai.expresso.api.SyntaxLeaf in project aic-expresso by aic-sri-international.
the class DefaultSyntaxLeaf method createSyntaxLeaf.
public static SyntaxLeaf createSyntaxLeaf(Object inputValue, boolean isStringLiteral) {
Object value = inputValue;
SyntaxLeaf result = null;
// If global symbol table to be used and the symbol's value is not
// an expression - i.e. quoted expressions of the form:
// <X>
// as these need to be instantiated each time in order to be
// parsed correctly.
boolean useGlobalSymbolTable = _useGlobalSymbolTable && !(inputValue instanceof Expression);
if (useGlobalSymbolTable) {
if (isStringLiteral) {
result = _globalStringLiteralTable.getIfPresent(inputValue);
} else {
result = _globalSymbolTable.getIfPresent(inputValue);
}
}
if (result == null) {
if (value instanceof Number && !(value instanceof Rational)) {
value = new Rational(((Number) value).doubleValue());
} else if (value.equals("true")) {
value = Boolean.TRUE;
} else if (value.equals("false")) {
value = Boolean.FALSE;
} else if (value instanceof String) {
value = UNESCAPE_STRING_VALUE.translate((String) value);
if (!isStringLiteral) {
// attempt to implicitly convert to a Rational value if possible.
try {
value = new Rational((String) value);
} catch (NumberFormatException e) {
// ignore
}
}
}
result = new DefaultSyntaxLeaf(value, isStringLiteral);
if (useGlobalSymbolTable && !(!_cacheNumericSymbols && result.getValue() instanceof Number)) {
if (isStringLiteral) {
_globalStringLiteralTable.put(inputValue, result);
} else {
_globalSymbolTable.put(inputValue, result);
}
}
}
return result;
}
use of com.sri.ai.expresso.api.SyntaxLeaf in project aic-expresso by aic-sri-international.
the class DefaultCompoundSyntaxTree method toStringWithoutCaching.
@Override
public String toStringWithoutCaching() {
String rootTreeString = getRootTree().toString();
if (!(getRootTree() instanceof SyntaxLeaf)) {
rootTreeString = "(" + rootTreeString + ")";
}
Iterator stringOfSubTrees = new FunctionIterator<SyntaxTree, String>(new ToStringWithoutCaching(), getImmediateSubTrees());
return rootTreeString + "(" + Util.join(", ", stringOfSubTrees) + ")";
}
use of com.sri.ai.expresso.api.SyntaxLeaf 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);
}
}
use of com.sri.ai.expresso.api.SyntaxLeaf 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.
*/
@Deprecated
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");
}
Aggregations