* @param destTypeExpr
* @return ValueNode
*/
public ValueNode transform(ValueNodeBuilderHelper valueNodeBuilderHelper, ValueNode sourceVN, TypeExpr destTypeExpr) {
TypeExpr sourceTypeExpr = sourceVN.getTypeExpr();
// check if we're "transforming" to the same type
if (sourceTypeExpr.sameType(destTypeExpr)) {
return sourceVN.copyValueNode(destTypeExpr);
}
Object newValue = null;
Class<? extends ValueNode> handlerClass = valueNodeBuilderHelper.getValueNodeClass(destTypeExpr);
PreludeTypeConstants typeConstants = valueNodeBuilderHelper.getPreludeTypeConstants();
if (handlerClass.equals(ListOfCharValueNode.class) || destTypeExpr.sameType(typeConstants.getStringType())) {
if (sourceVN instanceof ListValueNode && sourceVN.getTypeExpr().sameType(typeConstants.getCharListType())) {
// Converting a List to a List of Char: we convert each of the list values
ListValueNode listValueNode = (ListValueNode)sourceVN;
StringBuilder sb = new StringBuilder();
int nElements = listValueNode.getNElements();
for (int i = 0; i < nElements; i++) {
sb.append(listValueNode.getValueAt(i).getCALValue());
}
newValue = sb.toString();
} else {
newValue = sourceVN.getTextValue();
}
} else if (destTypeExpr.sameType(typeConstants.getCharType())) {
// Just take the first character of the text value (if any)
String textValue = sourceVN.getTextValue();
if (textValue.length() != 0) {
newValue = Character.valueOf(textValue.charAt(0));
}
}
if (sourceVN instanceof LiteralValueNode && !sourceVN.getTypeExpr().sameType(typeConstants.getStringType())) {
LiteralValueNode sourceLiteralVN = (LiteralValueNode) sourceVN;
if (sourceTypeExpr.sameType(typeConstants.getDoubleType()) &&
destTypeExpr.sameType(typeConstants.getIntType())) {
newValue = Integer.valueOf((int) Math.round(sourceLiteralVN.getDoubleValue().doubleValue()));
} else if (sourceTypeExpr.sameType(typeConstants.getIntType()) &&
destTypeExpr.sameType(typeConstants.getDoubleType())) {
newValue = Double.valueOf(sourceLiteralVN.getIntegerValue().intValue());
}