Search in sources :

Example 1 with Container.newContainer

use of org.projectnessie.cel.common.containers.Container.newContainer in project cel-java by projectnessie.

the class InterpreterTest method program.

static Program program(TestCase tst, InterpretableDecorator... opts) {
    // Configure the package.
    Container cont = Container.defaultContainer;
    if (tst.container != null) {
        cont = testContainer(tst.container);
    }
    if (tst.abbrevs != null) {
        cont = Container.newContainer(Container.name(cont.name()), Container.abbrevs(tst.abbrevs));
    }
    TypeRegistry reg;
    reg = newRegistry();
    if (tst.types != null) {
        reg = newRegistry(tst.types);
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    if (tst.attrs != null) {
        attrs = tst.attrs;
    }
    // Configure the environment.
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tst.env != null) {
        env.add(tst.env);
    }
    // Configure the program input.
    Activation vars = emptyActivation();
    if (tst.in != null) {
        vars = newActivation(tst.in);
    }
    // Adapt the test output, if needed.
    if (tst.out != null) {
        tst.out = reg.nativeToValue(tst.out);
    }
    Dispatcher disp = newDispatcher();
    disp.add(standardOverloads());
    if (tst.funcs != null) {
        disp.add(tst.funcs);
    }
    Interpreter interp = newInterpreter(disp, cont, reg, reg, attrs);
    // Parse the expression.
    Source s = newTextSource(tst.expr);
    ParseResult parsed = Parser.parseAllMacros(s);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Interpretable prg;
    if (tst.unchecked) {
        // Build the program plan.
        prg = interp.newUncheckedInterpretable(parsed.getExpr(), opts);
        return new Program(prg, vars);
    }
    // Check the expression.
    CheckResult checkResult = Checker.Check(parsed, s, env);
    assertThat(checkResult.hasErrors()).withFailMessage(() -> checkResult.getErrors().toDisplayString()).isFalse();
    // Build the program plan.
    prg = interp.newInterpretable(checkResult.getCheckedExpr(), opts);
    return new Program(prg, vars);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Dispatcher.newDispatcher(org.projectnessie.cel.interpreter.Dispatcher.newDispatcher) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource)

Aggregations

MethodSource (org.junit.jupiter.params.provider.MethodSource)1 CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)1 CheckerEnv (org.projectnessie.cel.checker.CheckerEnv)1 CheckerEnv.newStandardCheckerEnv (org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv)1 Source (org.projectnessie.cel.common.Source)1 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)1 Container (org.projectnessie.cel.common.containers.Container)1 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)1 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)1 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)1 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)1 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)1 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)1 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)1 Dispatcher.newDispatcher (org.projectnessie.cel.interpreter.Dispatcher.newDispatcher)1 Interpreter.newInterpreter (org.projectnessie.cel.interpreter.Interpreter.newInterpreter)1 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)1 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)1