Search in sources :

Example 1 with ErrException

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);
}
Also used : CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) Overload(com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload) ErrException(org.projectnessie.cel.common.types.Err.ErrException) Reference(com.google.api.expr.v1alpha1.Reference) ArrayList(java.util.ArrayList) Mapping.newMapping(org.projectnessie.cel.checker.Mapping.newMapping)

Example 2 with ErrException

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);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ErrException(org.projectnessie.cel.common.types.Err.ErrException) Activation.newHierarchicalActivation(org.projectnessie.cel.interpreter.Activation.newHierarchicalActivation) Activation(org.projectnessie.cel.interpreter.Activation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) ErrException(org.projectnessie.cel.common.types.Err.ErrException)

Aggregations

ErrException (org.projectnessie.cel.common.types.Err.ErrException)2 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)1 Overload (com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload)1 Expr (com.google.api.expr.v1alpha1.Expr)1 Reference (com.google.api.expr.v1alpha1.Reference)1 Type (com.google.api.expr.v1alpha1.Type)1 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 ArrayList (java.util.ArrayList)1 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)1 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)1 CheckerEnv.isObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType)1 Mapping.newMapping (org.projectnessie.cel.checker.Mapping.newMapping)1 FieldType (org.projectnessie.cel.common.types.ref.FieldType)1 Val (org.projectnessie.cel.common.types.ref.Val)1 Activation (org.projectnessie.cel.interpreter.Activation)1 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)1 Activation.newHierarchicalActivation (org.projectnessie.cel.interpreter.Activation.newHierarchicalActivation)1