Skip to content

Commit

Permalink
feat(codegen): more accurate error reportings
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 28, 2022
1 parent fb83aa6 commit ed71d06
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ private String toExpression(Field field, TypeReference resultType, Term term, Fu
} else if (term instanceof TernaryTerm) {
return toTernaryTermExpression(field, resultType, (TernaryTerm) term, variableExpressionGenerator, tracer);
} else {
throw new RuntimeException("Unsupported Term type " + term.getClass().getName());
throw new RuntimeException("Unsupported Term type " + term.getClass().getName() + ". Actual type " + resultType);
}
}

Expand Down Expand Up @@ -714,20 +714,20 @@ private String toUnaryTermExpression(Field field, TypeReference resultType, Unar
case "!":
tracer = tracer.dive("case !");
if ((resultType != getAnyTypeReference()) && !resultType.isBooleanTypeReference()) {
throw new IllegalArgumentException("'!(...)' expression requires boolean type");
throw new IllegalArgumentException("'!(...)' expression requires boolean type. Actual type " + resultType);
}
return tracer + "!(" + toExpression(field, resultType, a, variableExpressionGenerator) + ")";
case "-":
tracer = tracer.dive("case -");
if ((resultType != getAnyTypeReference()) && !resultType.isIntegerTypeReference() && !resultType.isFloatTypeReference()) {
throw new IllegalArgumentException("'-(...)' expression requires integer or floating-point type");
throw new IllegalArgumentException("'-(...)' expression requires integer or floating-point type. Actual type " + resultType);
}
return tracer + "-(" + toExpression(field, resultType, a, variableExpressionGenerator) + ")";
case "()":
tracer = tracer.dive("case ()");
return tracer + "(" + toExpression(field, resultType, a, variableExpressionGenerator) + ")";
default:
throw new RuntimeException("Unsupported unary operation type " + unaryTerm.getOperation());
throw new RuntimeException("Unsupported unary operation type " + unaryTerm.getOperation() + ". Actual type " + resultType);
}
}

Expand All @@ -740,7 +740,7 @@ private String toBinaryTermExpression(Field field, TypeReference resultType, Bin
case "^": {
tracer = tracer.dive(operation);
if ((resultType != getAnyTypeReference()) && !resultType.isIntegerTypeReference() && !resultType.isFloatTypeReference()) {
throw new IllegalArgumentException("'A^B' expression requires numeric result type");
throw new IllegalArgumentException("'A^B' expression requires numeric result type. Actual type " + resultType);
}
return tracer + "Math.pow((" + toExpression(field, resultType, a, variableExpressionGenerator) + "), (" + toExpression(field, resultType, b, variableExpressionGenerator) + "))";
}
Expand All @@ -751,7 +751,7 @@ private String toBinaryTermExpression(Field field, TypeReference resultType, Bin
case "-": {
tracer = tracer.dive(operation);
if ((resultType != getAnyTypeReference()) && !resultType.isIntegerTypeReference() && !resultType.isFloatTypeReference()) {
throw new IllegalArgumentException("'A" + operation + "B' expression requires numeric result type");
throw new IllegalArgumentException("'A" + operation + "B' expression requires numeric result type. Actual type " + resultType);
}
return tracer + "(" + toExpression(field, resultType, a, variableExpressionGenerator) + ") " + operation + " (" + toExpression(field, resultType, b, variableExpressionGenerator) + ")";
}
Expand All @@ -767,20 +767,20 @@ private String toBinaryTermExpression(Field field, TypeReference resultType, Bin
case "==":
case "!=":
if ((resultType != getAnyTypeReference()) && !resultType.isBooleanTypeReference()) {
throw new IllegalArgumentException("'A" + operation + "B' expression requires boolean result type");
throw new IllegalArgumentException("'A" + operation + "B' expression requires boolean result type. Actual type " + resultType);
}
// TODO: Try to infer the types of the arguments in this case
return tracer + "(" + toExpression(field, ANY_TYPE_REFERENCE, a, variableExpressionGenerator) + ") " + operation + " (" + toExpression(field, ANY_TYPE_REFERENCE, b, variableExpressionGenerator) + ")";
case "&&":
case "||":
if ((resultType != getAnyTypeReference()) && !resultType.isBooleanTypeReference()) {
throw new IllegalArgumentException("'A" + operation + "B' expression requires boolean result type");
throw new IllegalArgumentException("'A" + operation + "B' expression requires boolean result type. Actual type " + resultType);
}
return tracer + "(" + toExpression(field, resultType, a, variableExpressionGenerator) + ") " + operation + " (" + toExpression(field, resultType, b, variableExpressionGenerator) + ")";
case "&":
case "|":
if ((resultType != getAnyTypeReference()) && !resultType.isIntegerTypeReference() && !resultType.isByteTypeReference()) {
throw new IllegalArgumentException("'A" + operation + "B' expression requires boolean result type");
throw new IllegalArgumentException("'A" + operation + "B' expression requires byte or integer result type. Actual type " + resultType);
}
return tracer + "(" + toExpression(field, resultType, a, variableExpressionGenerator) + ") " + operation + " (" + toExpression(field, resultType, b, variableExpressionGenerator) + ")";
default:
Expand All @@ -801,7 +801,7 @@ private String toTernaryTermExpression(Field field, TypeReference resultType, Te
toExpression(field, resultType, c, variableExpressionGenerator) + "" +
")";
} else {
throw new IllegalArgumentException("Unsupported ternary operation type " + ternaryTerm.getOperation());
throw new IllegalArgumentException("Unsupported ternary operation type " + ternaryTerm.getOperation() + ". Actual type " + resultType);
}
}

Expand Down

0 comments on commit ed71d06

Please sign in to comment.