use of org.projectnessie.cel.common.types.Err.ErrException in project cel-java by projectnessie.
the class Checker method resolveOverload.
OverloadResolution resolveOverload(Location loc, Decl fn, Expr.Builder target, List<Expr.Builder> args) {
List<Type> argTypes = new ArrayList<>();
if (target != null) {
Type argType = getType(target);
if (argType == null) {
throw new ErrException("Could not resolve type for target '%s'", target);
}
argTypes.add(argType);
}
for (int i = 0; i < args.size(); i++) {
Expr.Builder arg = args.get(i);
Type argType = getType(arg);
if (argType == null) {
throw new ErrException("Could not resolve type for argument %d '%s'", i, arg);
}
argTypes.add(argType);
}
Type resultType = null;
Reference checkedRef = null;
for (Overload overload : fn.getFunction().getOverloadsList()) {
if ((target == null && overload.getIsInstanceFunction()) || (target != null && !overload.getIsInstanceFunction())) {
// not a compatible call style.
continue;
}
Type overloadType = Decls.newFunctionType(overload.getResultType(), overload.getParamsList());
if (overload.getTypeParamsCount() > 0) {
// Instantiate overload's type with fresh type variables.
Mapping substitutions = newMapping();
for (String typePar : overload.getTypeParamsList()) {
substitutions.add(Decls.newTypeParamType(typePar), newTypeVar());
}
overloadType = substitute(substitutions, overloadType, false);
}
List<Type> candidateArgTypes = overloadType.getFunction().getArgTypesList();
if (isAssignableList(argTypes, candidateArgTypes)) {
if (checkedRef == null) {
checkedRef = newFunctionReference(Collections.singletonList(overload.getOverloadId()));
} else {
checkedRef = checkedRef.toBuilder().addOverloadId(overload.getOverloadId()).build();
}
// First matching overload, determines result type.
Type fnResultType = substitute(mappings, overloadType.getFunction().getResultType(), false);
if (resultType == null) {
resultType = fnResultType;
} else if (!isDyn(resultType) && !fnResultType.equals(resultType)) {
resultType = Decls.Dyn;
}
}
}
if (resultType == null) {
errors.noMatchingOverload(loc, fn.getName(), argTypes, target != null);
// resultType = Decls.Error;
return null;
}
return newResolution(checkedRef, resultType);
}
use of org.projectnessie.cel.common.types.Err.ErrException in project cel-java by projectnessie.
the class Prog method eval.
/**
* Eval implements the Program interface method.
*/
@Override
public EvalResult eval(Object input) {
Val v;
EvalDetails evalDetails = new EvalDetails(state);
try {
// Build a hierarchical activation if there are default vars set.
Activation vars = newActivation(input);
if (defaultVars != null) {
vars = newHierarchicalActivation(defaultVars, vars);
}
v = interpretable.eval(vars);
} catch (ErrException e) {
v = e.getErr();
} catch (Exception e) {
throw new RuntimeException(String.format("internal error: %s", e.getMessage()), e);
}
return newEvalResult(v, evalDetails);
}
Aggregations