Search in sources :

Example 6 with Trail

use of suite.lp.Trail in project suite by stupidsing.

the class GeneralizerFactoryTest method test.

private void test(String pattern, String match) {
    for (GeneralizerFactory gf : new GeneralizerFactory[] { new CompileGeneralizerImpl(), new SewingGeneralizerImpl() }) {
        Generalize_ p = gf.generalizer(Suite.parse(pattern));
        assertTrue(Binder.bind(p.apply(gf.mapper().env()), Suite.parse(match), new Trail()));
    }
}
Also used : CompileGeneralizerImpl(suite.lp.compile.impl.CompileGeneralizerImpl) Trail(suite.lp.Trail) Generalize_(suite.lp.doer.GeneralizerFactory.Generalize_) SewingGeneralizerImpl(suite.lp.sewing.impl.SewingGeneralizerImpl)

Example 7 with Trail

use of suite.lp.Trail in project suite by stupidsing.

the class Chr method chrThen.

private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
    Generalizer generalizer = new Generalizer();
    Atom a = atom(".a"), b = atom(".b");
    if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
        // built-in syntactic equality
        Reference from = generalizer.getVariable(a);
        Reference to = generalizer.getVariable(b);
        states = states.map(new Fun<>() {

            public State apply(State state) {
                IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
                for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
                return new State(factsByPrototype1);
            }

            private ISet<Node> replace(ISet<Node> facts) {
                ISet<Node> facts1 = ISet.empty();
                for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
                return facts1;
            }
        });
    }
    return states.map(state -> {
        Prototype prototype = Prototype.of(then);
        ISet<Node> facts = getFacts(state, prototype);
        return setFacts(state, prototype, facts.replace(then));
    });
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Prototype(suite.lp.kb.Prototype) Reference(suite.node.Reference) Node(suite.node.Node) ISet(suite.immutable.ISet) Atom(suite.node.Atom) Fun(suite.util.FunUtil.Fun)

Example 8 with Trail

use of suite.lp.Trail in project suite by stupidsing.

the class Assembler method assemble.

public Bytes assemble(Node input) {
    Generalizer generalizer = new Generalizer();
    Trail trail = new Trail();
    List<Pair<Reference, Node>> lnis = new ArrayList<>();
    for (Node node0 : Tree.iter(input)) {
        Node node = generalizer.generalize(node0);
        Tree tree;
        if ((tree = Tree.decompose(node, TermOp.EQUAL_)) != null)
            if (!Binder.bind(tree.getLeft(), tree.getRight(), trail))
                Fail.t("bind failed");
            else
                ;
        else if ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null)
            lnis.add(Pair.of((Reference) tree.getLeft(), tree.getRight()));
        else
            Fail.t("cannot assemble " + node);
    }
    return assemble(generalizer, preassemble.apply(lnis));
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair)

Example 9 with Trail

use of suite.lp.Trail in project suite by stupidsing.

the class StackAssembler method preassemble.

private List<Pair<Reference, Node>> preassemble(List<Pair<Reference, Node>> lnis0) {
    List<Pair<Reference, Node>> lnis1 = new ArrayList<>();
    Deque<int[]> deque = new ArrayDeque<>();
    Trail trail = new Trail();
    int fs = 0, rs = 0;
    for (Pair<Reference, Node> lni0 : lnis0) {
        Node node0 = lni0.t1;
        Node node1;
        Node[] m;
        if ((m = FRBGN_.match(node0)) != null) {
            deque.push(new int[] { fs, rs });
            fs = 0;
            rs = 0;
            node1 = Atom.NIL;
        } else if ((m = FREND_.match(node0)) != null) {
            if (fs != 0)
                node1 = Fail.t("unbalanced frame stack in subroutine definition");
            else if (rs != 0)
                node1 = Fail.t("unbalanced register stack in subroutine definition");
            else {
                int[] arr = deque.pop();
                fs = arr[0];
                rs = arr[1];
            }
            node1 = Atom.NIL;
        } else if ((m = FRGET_.match(node0)) != null)
            if (Binder.bind(m[0], Int.of(-fs), trail))
                node1 = Atom.NIL;
            else
                node1 = Fail.t("cannot bind local variable offset");
        else if ((m = FRPOP_.match(node0)) != null) {
            fs -= 4;
            node1 = Suite.substitute("POP .0", rewrite(rs, m[0]));
        } else if ((m = FRPOPN.match(node0)) != null) {
            Int int_ = (Int) m[0].finalNode();
            fs -= int_.number;
            node1 = Atom.NIL;
        } else if ((m = FRPSH_.match(node0)) != null) {
            fs += 4;
            node1 = Suite.substitute("PUSH .0", rewrite(rs, m[0]));
        } else if ((m = FRPSHN.match(node0)) != null) {
            Int int_ = (Int) m[0].finalNode();
            fs += int_.number;
            node1 = Atom.NIL;
        } else if ((m = LET___.match(node0)) != null)
            if (Binder.bind(m[0], Int.of(TreeUtil.evaluate(m[1])), trail))
                node1 = Atom.NIL;
            else
                node1 = Fail.t("cannot calculate expression");
        else if (node0 == RPOP__) {
            rs--;
            node1 = Atom.NIL;
        } else if (node0 == RPSH__) {
            rs++;
            node1 = Atom.NIL;
        } else if (node0 == RRESTA) {
            fs -= 4 * rs;
            for (int r = rs - 1; 0 <= r; r--) lnis1.add(Pair.of(new Reference(), Suite.substitute("POP .0", getRegister(r))));
            node1 = Atom.NIL;
        } else if (node0 == RSAVEA) {
            for (int r = 0; r < rs; r++) lnis1.add(Pair.of(new Reference(), Suite.substitute("PUSH .0", getRegister(r))));
            fs += 4 * rs;
            node1 = Atom.NIL;
        } else
            node1 = rewrite(rs, node0);
        lnis1.add(Pair.of(lni0.t0, node1));
    }
    return new PeepholeOptimizer().optimize(lnis1);
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Trail(suite.lp.Trail) Pair(suite.adt.pair.Pair)

Example 10 with Trail

use of suite.lp.Trail in project suite by stupidsing.

the class P0Parse method expandMacros.

private Node expandMacros(Node node0) {
    class Expand {

        private IMap<Prototype, Node[]> macros;

        private Expand(IMap<Prototype, Node[]> macros) {
            this.macros = macros;
        }

        private Node expand(Node node) {
            Tree tree;
            Node[] m;
            Node[] ht;
            if ((m = Suite.pattern("expand .0 := .1 >> .2").match(node)) != null) {
                Node head = m[0];
                return new Expand(macros.put(Prototype.of(head), new Node[] { head, m[1] })).expand(m[2]);
            } else if ((ht = macros.get(Prototype.of(node))) != null) {
                Generalizer g = new Generalizer();
                Node t0_ = g.generalize(ht[0]);
                Node t1_ = g.generalize(ht[1]);
                if (Binder.bind(node, t0_, new Trail()))
                    return expand(t1_);
            }
            if ((tree = Tree.decompose(node)) != null)
                return Tree.of(tree.getOperator(), expand(tree.getLeft()), expand(tree.getRight()));
            else
                return node;
        }
    }
    return new Expand(IMap.empty()).expand(node0);
}
Also used : IMap(suite.immutable.IMap) Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) FunpTree(suite.funp.P0.FunpTree)

Aggregations

Trail (suite.lp.Trail)11 Node (suite.node.Node)10 Reference (suite.node.Reference)6 Generalizer (suite.lp.doer.Generalizer)5 Tree (suite.node.Tree)4 ArrayList (java.util.ArrayList)3 Pair (suite.adt.pair.Pair)3 IMap (suite.immutable.IMap)2 ISet (suite.immutable.ISet)2 Prototype (suite.lp.kb.Prototype)2 Atom (suite.node.Atom)2 TermOp (suite.node.io.TermOp)2 Fun (suite.util.FunUtil.Fun)2 ArrayDeque (java.util.ArrayDeque)1 Collection (java.util.Collection)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Suite (suite.Suite)1 FunpTree (suite.funp.P0.FunpTree)1 Activation (suite.instructionexecutor.InstructionUtil.Activation)1