Search in sources :

Example 21 with Tree

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

the class SldResolution method negate.

private Node negate(Node key) {
    Tree tree = Tree.decompose(key, TermOp.TUPLE_);
    boolean isAlreadyNegated = tree != null && tree.getLeft() == not;
    return isAlreadyNegated ? tree.getRight() : Tree.of(TermOp.TUPLE_, not, key);
}
Also used : Tree(suite.node.Tree)

Example 22 with Tree

use of suite.node.Tree 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 23 with Tree

use of suite.node.Tree 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 24 with Tree

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

the class Amd64Parse method parse.

public Instruction parse(Node node) {
    Tree tree = Tree.decompose(node, TermOp.TUPLE_);
    Insn insn = Enum.valueOf(Insn.class, ((Atom) tree.getLeft()).name);
    Node ops = tree.getRight();
    List<Operand> operands = scan(ops, ".0, .1").map(this::parseOperand).toList();
    return // 
    amd64.instruction(// 
    insn, // 
    0 < operands.size() ? operands.get(0) : amd64.none, // 
    1 < operands.size() ? operands.get(1) : amd64.none, 2 < operands.size() ? operands.get(2) : amd64.none);
}
Also used : Insn(suite.assembler.Amd64.Insn) Operand(suite.assembler.Amd64.Operand) Node(suite.node.Node) Tree(suite.node.Tree)

Example 25 with Tree

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

the class InterpretFunLazy0 method lazy_.

private Fun<IMap<String, Thunk_>, Thunk_> lazy_(Node node) {
    Fun<IMap<String, Thunk_>, Thunk_> result;
    Tree tree;
    Node[] m;
    if ((m = Suite.pattern("define .0 := .1 >> .2").match(node)) != null) {
        String vk = v(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
        Fun<IMap<String, Thunk_>, Thunk_> expr = lazy_(m[2]);
        result = env -> {
            Mutable<Thunk_> val = Mutable.nil();
            IMap<String, Thunk_> env1 = env.put(vk, () -> val.get().get());
            val.set(value.apply(env1)::get);
            return expr.apply(env1);
        };
    } else if ((m = Suite.pattern("if .0 then .1 else .2").match(node)) != null) {
        Fun<IMap<String, Thunk_>, Thunk_> if_ = lazy_(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> then_ = lazy_(m[1]);
        Fun<IMap<String, Thunk_>, Thunk_> else_ = lazy_(m[2]);
        result = env -> (if_.apply(env).get() == Atom.TRUE ? then_ : else_).apply(env);
    } else if ((m = Suite.pattern(".0 => .1").match(node)) != null) {
        String vk = v(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
        result = env -> () -> new Fun_(in -> value.apply(env.put(vk, in)));
    } else if ((m = Suite.pattern(".0 {.1}").match(node)) != null) {
        Fun<IMap<String, Thunk_>, Thunk_> fun = lazy_(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> param = lazy_(m[1]);
        result = env -> fun(fun.apply(env).get()).apply(param.apply(env));
    } else if ((tree = Tree.decompose(node)) != null) {
        Operator operator = tree.getOperator();
        Fun<IMap<String, Thunk_>, Thunk_> p0 = lazy_(tree.getLeft());
        Fun<IMap<String, Thunk_>, Thunk_> p1 = lazy_(tree.getRight());
        result = env -> {
            Thunk_ r0 = env.get(operator.getName());
            Thunk_ r1 = fun(r0.get()).apply(p0.apply(env));
            Thunk_ r2 = fun(r1.get()).apply(p1.apply(env));
            return r2;
        };
    } else if (node instanceof Atom) {
        String vk = v(node);
        result = env -> env.get(vk);
    } else
        result = env -> () -> node;
    return result;
}
Also used : Suite(suite.Suite) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IMap(suite.immutable.IMap) Mutable(suite.adt.Mutable) TermOp(suite.node.io.TermOp) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Fail(suite.util.Fail) Operator(suite.node.io.Operator) Node(suite.node.Node) Atom(suite.node.Atom) IMap(suite.immutable.IMap) Tree(suite.node.Tree) Fun(suite.util.FunUtil.Fun)

Aggregations

Tree (suite.node.Tree)47 Node (suite.node.Node)36 Reference (suite.node.Reference)19 Atom (suite.node.Atom)14 Int (suite.node.Int)13 TermOp (suite.node.io.TermOp)11 Tuple (suite.node.Tuple)10 ArrayList (java.util.ArrayList)9 Pair (suite.adt.pair.Pair)9 Operator (suite.node.io.Operator)9 Read (suite.streamlet.Read)9 List (java.util.List)8 Dict (suite.node.Dict)8 Map (java.util.Map)7 Str (suite.node.Str)7 Binder (suite.lp.doer.Binder)6 HashMap (java.util.HashMap)5 Generalizer (suite.lp.doer.Generalizer)5 VariableMapper (suite.lp.sewing.VariableMapper)5 TreeUtil (suite.node.util.TreeUtil)5