Search in sources :

Example 1 with Int

use of suite.node.Int in project suite by stupidsing.

the class Assembler method assemble.

private Bytes assemble(Generalizer generalizer, List<Pair<Reference, Node>> lnis) {
    int org = ((Int) generalizer.getVariable(Atom.of(".org")).finalNode()).number;
    BytesBuilder out = new BytesBuilder();
    for (boolean isPass2 : new boolean[] { false, true }) {
        AssemblePredicates.isPass2 = isPass2;
        out.clear();
        for (Pair<Reference, Node> lni : lnis) {
            int address = org + out.size();
            if (lni.t0 != null)
                if (!isPass2)
                    lni.t0.bound(Int.of(address));
                else if (((Int) lni.t0.finalNode()).number != address)
                    Fail.t("address varied between passes at " + Integer.toHexString(address));
            out.append(assemble(isPass2, address, lni.t1));
        }
        for (Pair<Reference, Node> lni : lnis) if (lni.t0 != null && isPass2)
            lni.t0.unbound();
    }
    return out.toBytes();
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Int(suite.node.Int) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 2 with Int

use of suite.node.Int in project suite by stupidsing.

the class Assembler method assemble.

public Bytes assemble(String in0) {
    Set<Character> whitespaces = Collections.singleton('\n');
    Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
    Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
    String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
    Generalizer generalizer = new Generalizer();
    List<String> lines = List.of(in1.split("\n"));
    Pair<String, String> pe;
    int start = 0;
    while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
        generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
        start++;
    }
    List<Pair<Reference, Node>> lnis = // 
    Read.from(// 
    List_.right(lines, start)).map(line -> {
        Pair<String, String> pt = String_.split2(line, "\t");
        String label = pt.t0;
        String command = pt.t1;
        Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
        Node instruction = generalizer.generalize(Suite.parse(command));
        return Pair.of(reference, instruction);
    }).toList();
    return assemble(generalizer, lnis);
}
Also used : Read(suite.streamlet.Read) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Preprocess(suite.text.Preprocess) Run(suite.text.Preprocess.Run) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Finder(suite.lp.search.ProverBuilder.Finder) Set(java.util.Set) TermOp(suite.node.io.TermOp) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) List_(suite.util.List_) To(suite.util.To) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Int(suite.node.Int) Collections(java.util.Collections) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Generalizer(suite.lp.doer.Generalizer) ArrayList(java.util.ArrayList) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 3 with Int

use of suite.node.Int in project suite by stupidsing.

the class CompileBinderImpl method binder.

public Bind_ binder(Node node) {
    FunCreator<Bind_> fc = FunCreator.of(Bind_.class, false);
    return fc.create(new BinOp<>() {

        private FunExpr env, trail, b;

        public FunExpr apply(FunExpr bindEnv, FunExpr target) {
            this.env = bindEnv.field("env");
            this.trail = bindEnv.field("trail");
            return f.declare(ok, b -> {
                this.b = b;
                return compile_(node, target);
            });
        }

        private FunExpr compile_(Node node, FunExpr target) {
            FunExpr br = bind(f.object_(node, Node.class), target);
            FunExpr brc = bindClone(node, target);
            return new // 
            SwitchNode<FunExpr>(// 
            node).applyIf(Atom.class, n -> {
                return f.ifEquals(target, f.object(node), ok, br);
            }).applyIf(Int.class, n -> {
                return f.ifInstance(Int.class, target, i -> f.ifEquals(i.field("number"), f.int_(n.number), ok, fail), br);
            }).applyIf(Reference.class, n -> {
                FunExpr ref = env.field("refs").index(f.int_(mapper().computeIndex(n)));
                return f.invokeStatic(Binder.class, "bind", target, ref.cast_(Node.class), trail);
            }).applyIf(Str.class, n -> {
                return f.ifInstance(Str.class, target, s -> f.object(n.value).invoke("equals", s.field("value").cast_(Object.class)), br);
            }).applyIf(Tree.class, tree -> {
                return f.declare(f.invokeStatic(Tree.class, "decompose", target, f.object(tree.getOperator())), t -> {
                    Node lt = tree.getLeft();
                    Node rt = tree.getRight();
                    return f.ifNonNull(t, compile_(lt, t.invoke("getLeft"), compile_(rt, t.invoke("getRight"), ok)), brc);
                });
            }).applyIf(Tuple.class, n -> {
                return f.ifInstance(Tuple.class, target, tuple -> f.declare(tuple.field("nodes"), targets -> {
                    Node[] nodes = n.nodes;
                    FunExpr fe = ok;
                    for (int i = 0; i < nodes.length; i++) fe = compile_(nodes[i], targets.index(f.int_(i)), fe);
                    return f.if_(targets.length(), fe, brc);
                }), brc);
            }).applyIf(Node.class, n -> {
                Clone_ cloner = cloner(n);
                return f.invokeStatic(Binder.class, "bind", target, f.object(cloner).invoke("apply", env), trail);
            }).nonNullResult();
        }

        private FunExpr compile_(Node node, FunExpr target, FunExpr cps) {
            return f.assign(b, compile_(node, target), f.if_(b, cps, fail));
        }

        private FunExpr bindClone(Node node, FunExpr target) {
            if (isBindTrees)
                return bind(f.object(cloner(node)).apply(env), target);
            else
                return fail;
        }

        private FunExpr bind(FunExpr node, FunExpr target) {
            return f.ifInstanceAnd(Reference.class, target, ref -> f.seq(trail.invoke("addBind", ref, node), ok));
        }
    }).apply(Map.ofEntries());
}
Also used : Reference(suite.node.Reference) BinderFactory(suite.lp.doer.BinderFactory) Tree(suite.node.Tree) BinOp(suite.util.FunUtil2.BinOp) Node(suite.node.Node) Atom(suite.node.Atom) Map(java.util.Map) FunCreator(suite.jdk.gen.FunCreator) Binder(suite.lp.doer.Binder) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) Tuple(suite.node.Tuple) Str(suite.node.Str) Binder(suite.lp.doer.Binder) Reference(suite.node.Reference) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) BinOp(suite.util.FunUtil2.BinOp) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Atom(suite.node.Atom)

Example 4 with Int

use of suite.node.Int 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));
    }
}
Also used : Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) Clone_(suite.lp.doer.ClonerFactory.Clone_) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) TermOp(suite.node.io.TermOp) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Tree(suite.node.Tree) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Int(suite.primitive.IntInt_Int) Int(suite.node.Int)

Example 5 with Int

use of suite.node.Int in project suite by stupidsing.

the class SewingProverImpl method save.

private Restore save(Runtime rt) {
    Cps cps0 = rt.cps;
    Env env0 = rt.env;
    Node query0 = rt.query;
    IList<Trampoline> cutPoint0 = rt.cutPoint;
    IList<Trampoline> rems0 = rt.rems;
    int pit0 = rt.trail.getPointInTime();
    Sink<Node> handler0 = rt.handler;
    return rt_ -> {
        rt_.cps = cps0;
        rt_.env = env0;
        rt_.query = query0;
        rt_.cutPoint = cutPoint0;
        rt_.rems = rems0;
        rt_.trail.unwind(pit0);
        rt_.handler = handler0;
    };
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Node(suite.node.Node) BindEnv(suite.lp.doer.BinderFactory.BindEnv) Env(suite.lp.sewing.Env)

Aggregations

Int (suite.node.Int)24 Node (suite.node.Node)22 Reference (suite.node.Reference)16 Tree (suite.node.Tree)16 Atom (suite.node.Atom)12 Pair (suite.adt.pair.Pair)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Str (suite.node.Str)8 Tuple (suite.node.Tuple)8 List (java.util.List)7 Binder (suite.lp.doer.Binder)7 Read (suite.streamlet.Read)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Dict (suite.node.Dict)6 Fail (suite.util.Fail)6 ArrayDeque (java.util.ArrayDeque)5 Entry (java.util.Map.Entry)5