Search in sources :

Example 11 with Int

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

the class FunCreatorTest method testIndex.

@Test
public void testIndex() {
    int[] ints = { 0, 1, 4, 9, 16 };
    Int_Int fun = LambdaInstance.of(Int_Int.class, i -> f.object(ints).index(i)).newFun();
    assertEquals(9, fun.apply(3));
    assertEquals(16, fun.apply(4));
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Flt_Flt(suite.primitive.Flt_Flt) Dump(suite.inspect.Dump) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) Test(org.junit.Test) Fun(suite.util.FunUtil.Fun) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) Tree(suite.node.Tree) Node(suite.node.Node) IntSource(suite.primitive.IntPrimitives.IntSource) Iterate(suite.util.FunUtil.Iterate) BiPredicate(java.util.function.BiPredicate) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Int(suite.node.Int) LambdaInterface(suite.jdk.lambda.LambdaInterface) Int_Int(suite.primitive.Int_Int) Assert.assertEquals(org.junit.Assert.assertEquals) Int_Int(suite.primitive.Int_Int) Test(org.junit.Test)

Example 12 with Int

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

the class FunCreatorTest method testExpression.

@Test
public void testExpression() {
    Int N1 = Int.of(1);
    @SuppressWarnings({ "rawtypes", "unchecked" }) FunCreator<Source<Node>> fc = (FunCreator) FunCreator.of(Source.class);
    assertEquals(Suite.parse("1"), fc.create(() -> f.object(N1)).apply(void_).source());
    assertEquals(Suite.parse("1 + 1"), fc.create(() -> // 
    f.invokeStatic(// 
    Tree.class, // 
    "of", // 
    f.object(TermOp.PLUS__), // 
    f.object(N1).cast_(Node.class), // 
    f.object(N1).cast_(Node.class))).apply(// 
    void_).source());
}
Also used : Node(suite.node.Node) Int(suite.node.Int) Int_Int(suite.primitive.Int_Int) Source(suite.util.FunUtil.Source) IntSource(suite.primitive.IntPrimitives.IntSource) Test(org.junit.Test)

Example 13 with Int

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

the class Assembler method convertByteStream.

private Bytes convertByteStream(Node node) {
    BytesBuilder bb = new BytesBuilder();
    Tree tree;
    while ((tree = Tree.decompose(node, TermOp.AND___)) != null) {
        bb.append((byte) ((Int) tree.getLeft()).number);
        node = tree.getRight();
    }
    return bb.toBytes();
}
Also used : Tree(suite.node.Tree) Int(suite.node.Int) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 14 with Int

use of suite.node.Int 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 15 with Int

use of suite.node.Int 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)

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