use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.
the class SewingExpressionImpl method evaluator.
public Evaluate_ evaluator(Node node) {
Tree tree = Tree.decompose(node);
if (tree != null) {
Operator op = tree.getOperator();
Evaluate_ lhs, rhs;
IntInt_Int fun;
if (op == TermOp.TUPLE_) {
Tree rightTree = Tree.decompose(tree.getRight());
lhs = evaluator(tree.getLeft());
rhs = evaluator(rightTree.getRight());
fun = TreeUtil.evaluateOp(rightTree.getLeft());
} else {
lhs = evaluator(tree.getLeft());
rhs = evaluator(tree.getRight());
fun = TreeUtil.evaluateOp(op);
}
return env -> fun.apply(lhs.evaluate(env), rhs.evaluate(env));
} else if (node instanceof Int) {
int i = ((Int) node).number;
return env -> i;
} else {
Clone_ f = clonerFactory.cloner(node);
return env -> TreeUtil.evaluate(f.apply(env));
}
}
use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.
the class SewingProverImpl method compileCps.
private Cps compileCps(BinderFactory bf, Node node, Cps cpsx) {
List<Node> list;
Tree tree;
Node[] m;
Cps cps;
if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size()) {
cps = cpsx;
for (Node n : List_.reverse(list)) cps = compileCps(bf, n, cps);
} else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
cps = orCps(Read.from(list).map(n -> compileCps(bf, n, cpsx)));
else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
boolean b = complexity(m[0]) <= complexity(m[1]);
Node n0 = b ? m[0] : m[1];
Node n1 = b ? m[1] : m[0];
Bind_ p = bf.binder(n1);
Clone_ f = bf.cloner(n0);
cps = rt -> p.test(rt, f.apply(rt.env)) ? cpsx : null;
} else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
cps = compileCpsCallPredicate(bf, ((Atom) m[0]).name, m[1], node, cpsx);
else if (node instanceof Atom) {
String name = ((Atom) node).name;
if (String_.equals(name, ""))
cps = cpsx;
else if (String_.equals(name, "fail"))
cps = rt -> null;
else
cps = compileCpsCallPredicate(bf, name, Atom.NIL, node, cpsx);
} else if (node instanceof Reference) {
Clone_ f = bf.cloner(node);
cps = rt -> compileCps(passThru, f.apply(rt.env), cpsx);
} else if ((tree = Tree.decompose(node)) != null)
cps = compileCpsCallPredicate(bf, tree.getOperator().getName(), node, node, cpsx);
else if (node instanceof Tuple)
cps = compileCpsCallPredicate(bf, node, cpsx);
else
cps = Fail.t("cannot understand " + node);
return cps;
}
use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.
the class SewingProverImpl method compileTrCallPredicate.
private Trampoline compileTrCallPredicate(BinderFactory bf, Node node) {
Prototype prototype = Prototype.of(node);
if (rules.containsKey(prototype)) {
Clone_ f = bf.cloner(node);
Trampoline tr;
if (isHasCutByPrototype.get(prototype)) {
Mutable<Trampoline> mtr = getTrampolineByPrototype(prototype);
tr = rt -> {
rt.query = f.apply(rt.env);
return mtr.get()::prove;
};
} else {
Mutable<Cps> mcps = getCpsByPrototype(prototype);
Cps cpsx = rt -> {
IList<Trampoline> rems = rt.rems;
rt.rems = IList.cons(fail, IList.end());
new Runtime(rt, rt_ -> {
rt_.rems = rems;
return okay;
}).trampoline();
return null;
};
tr = rt -> {
Cps cps0 = rt.cps;
rt.cps = rt_ -> {
rt.cps = cps0;
return cpsx;
};
rt.query = f.apply(rt.env);
rt.cont(mcps.get());
return fail;
};
}
return tr;
} else
return Fail.t("cannot find predicate " + prototype);
}
use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.
the class ClonerFactoryTest method test.
private void test(String pattern, String match) {
for (ClonerFactory cf : new ClonerFactory[] { new CompileClonerImpl(), new SewingClonerImpl() }) {
Node node = new Generalizer().generalize(Suite.parse(pattern));
Clone_ p = cf.cloner(node);
assertTrue(Binder.bind(p.apply(cf.mapper().env()), Suite.parse(match), new Trail()));
}
}
use of suite.lp.doer.ClonerFactory.Clone_ 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());
}
Aggregations