Search in sources :

Example 26 with Pair

use of suite.adt.pair.Pair 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 27 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class PeepholeOptimizer method optimize.

public List<Pair<Reference, Node>> optimize(List<Pair<Reference, Node>> lnis0) {
    List<Pair<Reference, Node>> lnis1 = new ArrayList<>();
    for (Pair<Reference, Node> lni0 : lnis0) {
        Node node0 = lni0.t1;
        Node node1;
        Node[] m;
        if ((m = ADDI__.match(node0)) != null) {
            Node m0 = m[0];
            int i = TreeUtil.evaluate(m[1]);
            if (i == 1)
                node1 = Suite.substitute("INC .0", m0);
            else if (i == -1)
                node1 = Suite.substitute("DEC .0", m0);
            else if (0 < i)
                node1 = Suite.substitute("ADD (.0, .1)", m0, Int.of(i));
            else if (i < 0)
                node1 = Suite.substitute("SUB (.0, .1)", m0, Int.of(-i));
            else
                node1 = Atom.NIL;
        } else if ((m = MOV___.match(node0)) != null) {
            Node m0 = m[0];
            Node m1 = m[1];
            if (m0 == m1)
                node1 = Atom.NIL;
            else if (m0 instanceof Atom && m1 instanceof Int && ((Int) m1).number == 0)
                node1 = Suite.substitute("XOR (.0, .0)", m0);
            else
                node1 = Suite.substitute("MOV (.0, .1)", m0, m1);
        } else
            node1 = node0;
        lnis1.add(Pair.of(lni0.t0, node1));
    }
    return lnis1;
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Atom(suite.node.Atom) Int(suite.node.Int) Pair(suite.adt.pair.Pair)

Example 28 with Pair

use of suite.adt.pair.Pair 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 29 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class LrParse method parse.

private Ast parse(Source<Ast> tokens, State state) {
    Deque<Pair<Ast, State>> stack = new ArrayDeque<>();
    Ast token = tokens.source();
    while (true) {
        String lookahead = token != null ? token.entity : "EOF";
        Pair<State, Reduce> sr = shift(stack, state, lookahead);
        if (sr.t0 != null) {
            // shift
            stack.push(Pair.of(token, state));
            state = sr.t0;
            token = tokens.source();
        } else {
            // reduce
            Reduce reduce = sr.t1;
            IList<Ast> nodes = IList.end();
            for (int i = 0; i < reduce.n(); i++) {
                Pair<Ast, State> ns = stack.pop();
                nodes = IList.cons(ns.t0, nodes);
                state = ns.t1;
            }
            Ast token1 = new Ast(reduce.name(), 0, 0, Read.from(nodes).toList());
            if (rootEntity.equals(reduce.name()) && stack.size() == 0 && token == null)
                return token1;
            // force shift after reduce
            stack.push(Pair.of(token1, state));
            state = shift(stack, state, token1.entity).t0;
        }
    }
}
Also used : Ast(suite.ebnf.Ebnf.Ast) State(suite.ebnf.lr.BuildLr.State) Reduce(suite.ebnf.lr.BuildLr.Reduce) ArrayDeque(java.util.ArrayDeque) Pair(suite.adt.pair.Pair)

Example 30 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class InterpretFunEager method eager.

public Node eager(Node node) {
    Node mode = isLazyify ? Atom.of("LAZY") : Atom.of("EAGER");
    Node query = Suite.substitute("source .in, fc-process-function .0 .in .out, sink .out", mode);
    RuleSet rs = Suite.newRuleSet(List.of("auto.sl", "fc/fc.sl"));
    Finder finder = new SewingProverBuilder2().build(rs).apply(query);
    Node parsed = FindUtil.collectSingle(finder, node);
    IntrinsicCallback ic = isLazyify ? lazyIntrinsicCallback() : Intrinsics.eagerIntrinsicCallback;
    Map<String, Node> df = new HashMap<>();
    df.put(TermOp.AND___.name, f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
    df.put("+call%i-t1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-t2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-t3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-v1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-v2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-v3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+compare", f2((a, b) -> Int.of(Comparer.comparer.compare(a, b))));
    df.put("+get%i", f1(a -> new Data<>(Intrinsics.intrinsics.get(((Atom) a).name.split("!")[1]))));
    df.put("+is-list", f1(a -> b(Tree.decompose(a) != null)));
    df.put("+is-pair", f1(a -> b(Tree.decompose(a) != null)));
    df.put("+lcons", f2((a, b) -> Tree.of(TermOp.OR____, a, b)));
    df.put("+lhead", f1(a -> Tree.decompose(a).getLeft()));
    df.put("+ltail", f1(a -> Tree.decompose(a).getRight()));
    df.put("+pcons", f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
    df.put("+pleft", f1(a -> Tree.decompose(a).getLeft()));
    df.put("+pright", f1(a -> Tree.decompose(a).getRight()));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        df.put(e.getKey().getName(), f2((a, b) -> b(fun.apply(compare(a, b), 0))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        df.put(e.getKey().getName(), f2((a, b) -> Int.of(fun.apply(i(a), i(b)))));
    }
    List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
    Eager_ eager0 = new Eager_(0, IMap.empty());
    Frame frame = new Frame(null);
    for (String key : keys) {
        eager0 = eager0.put(Atom.of(key));
        frame.add(df.get(key));
    }
    return eager0.eager_(parsed).apply(frame);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) DECONS(suite.fp.match.Matchers.DECONS) Fun(suite.util.FunUtil.Fun) BinOp(suite.util.FunUtil2.BinOp) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) FindUtil(suite.lp.search.FindUtil) RuleSet(suite.lp.kb.RuleSet) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) IntrinsicCallback(suite.fp.intrinsic.Intrinsics.IntrinsicCallback) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic) Collectors(java.util.stream.Collectors) Matcher(suite.fp.match.Matcher) PRAGMA(suite.fp.match.Matchers.PRAGMA) Tree(suite.node.Tree) UNWRAP(suite.fp.match.Matchers.UNWRAP) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) Entry(java.util.Map.Entry) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) IF(suite.fp.match.Matchers.IF) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Comparer(suite.node.util.Comparer) HashMap(java.util.HashMap) TCO(suite.fp.match.Matchers.TCO) ArrayList(java.util.ArrayList) CONS(suite.fp.match.Matchers.CONS) Data(suite.node.Data) Formatter(suite.node.io.Formatter) Intrinsics(suite.fp.intrinsic.Intrinsics) DEFVARS(suite.fp.match.Matchers.DEFVARS) Suite(suite.Suite) Source(suite.util.FunUtil.Source) Finder(suite.lp.search.ProverBuilder.Finder) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) Operator(suite.node.io.Operator) RuleSet(suite.lp.kb.RuleSet) IntrinsicCallback(suite.fp.intrinsic.Intrinsics.IntrinsicCallback) HashMap(java.util.HashMap) Node(suite.node.Node) Finder(suite.lp.search.ProverBuilder.Finder) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Data(suite.node.Data) Atom(suite.node.Atom) IntInt_Int(suite.primitive.IntInt_Int) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Aggregations

Pair (suite.adt.pair.Pair)36 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Read (suite.streamlet.Read)22 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Node (suite.node.Node)14 Fail (suite.util.Fail)13 Fun (suite.util.FunUtil.Fun)12 Reference (suite.node.Reference)11 Tree (suite.node.Tree)10 TermOp (suite.node.io.TermOp)10 Set (java.util.Set)9 Atom (suite.node.Atom)9 Int (suite.node.Int)9 As (suite.streamlet.As)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Streamlet (suite.streamlet.Streamlet)7 Time (suite.trade.Time)6