Search in sources :

Example 11 with FunExpr

use of suite.jdk.gen.FunExpression.FunExpr in project suite by stupidsing.

the class CompileExpressionImpl method evaluator.

public Evaluate_ evaluator(Node node) {
    FunCreator<Evaluate_> fc = FunCreator.of(Evaluate_.class, false);
    return fc.create(new Iterate<>() {

        private FunExpr env;

        public FunExpr apply(FunExpr env) {
            this.env = env;
            return compile_(node);
        }

        private FunExpr compile_(Node node) {
            return new // 
            SwitchNode<FunExpr>(// 
            node).match2(".0 + .1", (a, b) -> {
                return compileOperator(a, b, "+");
            }).match2(".0 - .1", (a, b) -> {
                return compileOperator(a, b, "-");
            }).match2(".0 * .1", (a, b) -> {
                return compileOperator(a, b, "*");
            }).match2(".0 / .1", (a, b) -> {
                return compileOperator(a, b, "/");
            }).match2(".0 and .1", (a, b) -> {
                return compileOperator(a, b, "&&");
            }).match2(".0 or .1", (a, b) -> {
                return compileOperator(a, b, "||");
            }).match2(".0 shl .1", (a, b) -> {
                return compileOperator(a, b, "<<");
            }).match2(".0 shr .1", (a, b) -> {
                return compileOperator(a, b, ">>");
            }).applyIf(Int.class, i -> {
                return f.int_(i.number);
            }).applyIf(Node.class, i -> {
                Clone_ n_ = clonerFactory.cloner(node);
                Evaluate_ evaluate = env -> TreeUtil.evaluate(n_.apply(env));
                return f.object(evaluate).invoke("evaluate", env);
            }).nonNullResult();
        }

        private FunExpr compileOperator(Node a, Node b, String op) {
            FunExpr fe0 = compile_(a);
            FunExpr fe1 = compile_(b);
            return f.bi(op, fe0, fe1);
        }
    }).apply(Map.ofEntries());
}
Also used : Iterate(suite.util.FunUtil.Iterate) Clone_(suite.lp.doer.ClonerFactory.Clone_) Map(java.util.Map) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) FunCreator(suite.jdk.gen.FunCreator) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) Node(suite.node.Node) Iterate(suite.util.FunUtil.Iterate) SwitchNode(suite.node.io.SwitchNode) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) FunExpr(suite.jdk.gen.FunExpression.FunExpr)

Aggregations

FunExpr (suite.jdk.gen.FunExpression.FunExpr)11 Map (java.util.Map)7 FunFactory (suite.jdk.gen.FunFactory)7 Node (suite.node.Node)6 Type (org.apache.bcel.generic.Type)5 FunCreator (suite.jdk.gen.FunCreator)5 Declare0ParameterFunExpr (suite.jdk.gen.FunExprK.Declare0ParameterFunExpr)5 Declare1ParameterFunExpr (suite.jdk.gen.FunExprK.Declare1ParameterFunExpr)5 Declare2ParameterFunExpr (suite.jdk.gen.FunExprK.Declare2ParameterFunExpr)5 DeclareLocalFunExpr (suite.jdk.gen.FunExprL.DeclareLocalFunExpr)5 FieldInjectFunExpr (suite.jdk.gen.FunExprL.FieldInjectFunExpr)5 InvokeLambdaFunExpr (suite.jdk.gen.FunExprL.InvokeLambdaFunExpr)5 ProfileFunExpr (suite.jdk.gen.FunExprM.ProfileFunExpr)5 Int (suite.node.Int)5 ConstantFunExpr (suite.jdk.gen.FunExprM.ConstantFunExpr)4 If1FunExpr (suite.jdk.gen.FunExprM.If1FunExpr)4 LambdaInstance (suite.jdk.lambda.LambdaInstance)4 LambdaInterface (suite.jdk.lambda.LambdaInterface)4 Reference (suite.node.Reference)4 Tree (suite.node.Tree)4