All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.io.github.moonlightsuite.moonlight.script.BiParametricBooleanExpressionEvaluator Maven / Gradle / Ivy

package io.github.moonlightsuite.moonlight.script;

import io.github.moonlightsuite.moonlight.core.base.MoonLightRecord;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class BiParametricBooleanExpressionEvaluator extends MoonLightScriptBaseVisitor> {

    private final BiParametricExpressionEvaluator biParametricExpressionEvaluator;

    private static final Map> relationsMap = new HashMap<>();

    static {
        relationsMap.put("<",(x,y) -> x x<=y);
        relationsMap.put("==", Double::equals);
        relationsMap.put(">",(x,y) -> x>y);
        relationsMap.put(">=",(x,y) -> x>=y);
        relationsMap.put("=>",(x,y) -> x>=y);
    }

    public BiParametricBooleanExpressionEvaluator(BiParametricExpressionEvaluator biParametricExpressionEvaluator) {
        this.biParametricExpressionEvaluator = biParametricExpressionEvaluator;
    }

    public boolean eval(MoonLightScriptParser.ExpressionContext guard) {
        return false;
    }

    @Override
    public BiFunctionvisitImplyExpression(MoonLightScriptParser.ImplyExpressionContext ctx) {
        BiFunction left = ctx.left.accept(this);
        BiFunction right = ctx.right.accept(this);
        return (r,s) -> !left.apply(r,s)||right.apply(r,s);
    }

    @Override
    public BiFunction visitNotExpression(MoonLightScriptParser.NotExpressionContext ctx) {
        BiFunction arg = ctx.arg.accept(this);
        return (r,s) -> !arg.apply(r,s);
    }

    @Override
    public BiFunction visitFalseExpression(MoonLightScriptParser.FalseExpressionContext ctx) {
        return (r,s) -> false;
    }

    @Override
    public BiFunction visitBracketExpression(MoonLightScriptParser.BracketExpressionContext ctx) {
        return ctx.expression().accept(this);
    }

    @Override
    public BiFunction visitAndExpression(MoonLightScriptParser.AndExpressionContext ctx) {
        BiFunction left = ctx.left.accept(this);
        BiFunction right = ctx.right.accept(this);
        return (r,s) -> left.apply(r,s)&&right.apply(r,s);
    }

    @Override
    public BiFunction visitOrExpression(MoonLightScriptParser.OrExpressionContext ctx) {
        BiFunction left = ctx.left.accept(this);
        BiFunction right = ctx.right.accept(this);
        return (r,s) -> left.apply(r,s)||right.apply(r,s);
    }

    @Override
    public BiFunction visitTrueExpression(MoonLightScriptParser.TrueExpressionContext ctx) {
        return (r,s) -> true;
    }

    @Override
    public BiFunction visitIfThenElseExpression(MoonLightScriptParser.IfThenElseExpressionContext ctx) {
        BiFunction guard = ctx.guard.accept(this);
        BiFunction thenBranch = ctx.thenExpression.accept(this);
        BiFunction elseBranch = ctx.elseExpression.accept(this);
        return (r,s) -> (guard.apply(r,s)?thenBranch.apply(r,s):elseBranch.apply(r,s));
    }

    @Override
    protected BiFunction defaultResult() {
        return (r,s) -> false;
    }

    @Override
    public BiFunction visitRelationExpression(MoonLightScriptParser.RelationExpressionContext ctx) {
        BiFunction left = ctx.left.accept(biParametricExpressionEvaluator);
        BiFunction right = ctx.right.accept(biParametricExpressionEvaluator);
        BiFunction op = relationsMap.get(ctx.op.getText());
        return (r,s) -> op.apply(left.apply(r,s), right.apply(r,s));
    }


    @Override
    public BiFunction visitReferenceExpression(MoonLightScriptParser.ReferenceExpressionContext ctx) {
        BiFunction arg = ctx.accept(biParametricExpressionEvaluator);
        return (r,s) -> arg.apply(r,s)>0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy