Search in sources :

Example 1 with Tree

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

the class InstructionExtractor method tupleToList.

private List<Node> tupleToList(Node node) {
    List<Node> results = new ArrayList<>();
    Tree tree;
    while ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null) {
        results.add(tree.getLeft());
        node = tree.getRight();
    }
    results.add(node);
    return results;
}
Also used : Node(suite.node.Node) ArrayList(java.util.ArrayList) Tree(suite.node.Tree)

Example 2 with Tree

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

the class InstructionExtractor method extractInstructions.

private void extractInstructions(Node snippet, List<List<Node>> rsList) {
    Deque<Node> deque = new ArrayDeque<>();
    deque.add(snippet);
    Tree tree;
    Node value;
    while (!deque.isEmpty()) if ((tree = Tree.decompose(deque.pop(), TermOp.AND___)) != null) {
        IdentityKey<Node> key = IdentityKey.of(tree);
        Integer ip = ipByLabelId.get(key);
        if (ip == null) {
            ipByLabelId.put(key, ip = rsList.size());
            List<Node> rs = tupleToList(tree.getLeft());
            if (rs.get(0) == FRAME)
                if ((value = label(rs.get(1))) != null) {
                    rsList.add(List.of(Atom.of("FRAME-BEGIN")));
                    extractInstructions(value, rsList);
                    rsList.add(List.of(Atom.of("FRAME-END")));
                } else
                    Fail.t("bad frame definition");
            else {
                rsList.add(rs);
                for (Node op : List_.right(rs, 1)) if ((value = label(op)) != null)
                    deque.push(value);
                deque.push(tree.getRight());
            }
        } else
            rsList.add(List.of(Atom.of("JUMP"), Int.of(ip)));
    }
}
Also used : IdentityKey(suite.adt.IdentityKey) Node(suite.node.Node) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) List(java.util.List) ArrayDeque(java.util.ArrayDeque)

Example 3 with Tree

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

the class ThunkUtil method yawnList.

public static Outlet<Node> yawnList(Iterate<Node> yawn, Node node, boolean isFacilitateGc) {
    return Outlet.of(new Source<>() {

        private Node node_ = node;

        private boolean first = true;

        public Node source() {
            Tree tree;
            // first node is not a thunk, remainings are
            if (!first)
                node_ = yawn.apply(node_);
            else
                first = false;
            if ((tree = Tree.decompose(node_)) != null) {
                Node result = yawn.apply(tree.getLeft());
                node_ = tree.getRight();
                if (isFacilitateGc)
                    Tree.forceSetRight(tree, null);
                return result;
            } else if (node_ == Atom.NIL)
                return null;
            else
                return Fail.t("not a list, unable to expand");
        }
    });
}
Also used : Node(suite.node.Node) Tree(suite.node.Tree)

Example 4 with Tree

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

the class ThunkUtil method deepYawn.

/**
 * Evaluates the whole (lazy) term to actual by invoking all the thunks.
 */
public static Node deepYawn(Iterate<Node> yawn, Node node) {
    node = yawn.apply(node);
    if (node instanceof Tree) {
        Tree tree = (Tree) node;
        Node left = deepYawn(yawn, tree.getLeft());
        Node right = deepYawn(yawn, tree.getRight());
        node = Tree.of(tree.getOperator(), left, right);
    }
    return node;
}
Also used : Node(suite.node.Node) Tree(suite.node.Tree)

Example 5 with Tree

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

the class TypeChecker method getType.

private Node getType(Node data) {
    Node type;
    Tree tree;
    if (data instanceof Reference)
        type = variableTypes.computeIfAbsent(IdentityKey.of(data), k -> new Reference()).finalNode();
    else if ((tree = Tree.decompose(data)) != null)
        if (tree.getOperator() == TermOp.AND___) {
            type = Suite.substitute(".0;", getType(tree.getLeft()));
            bind(type, getType(tree.getRight()));
        } else if (tree.getOperator() == TermOp.TUPLE_) {
            Node name = tree.getLeft();
            if (name instanceof Atom) {
                Node node = tree.getRight();
                Node[] ps = TreeUtil.elements(node, TreeUtil.nElements(node));
                type = getEnumType(name, Tree.of(TermOp.TUPLE_, Read.from(ps).map(this::getType).toList()));
            } else
                // free type
                return new Reference();
        } else {
            Atom name = Atom.of(tree.getOperator().getName());
            Node lt = getType(tree.getLeft());
            Node rt = getType(tree.getRight());
            type = getEnumType(name, Tree.of(TermOp.TUPLE_, lt, rt));
        }
    else if (data == Atom.NIL)
        type = Suite.substitute("_;");
    else if (data instanceof Atom)
        type = getEnumType(data, Atom.NIL);
    else
        type = Atom.of(data.getClass().getSimpleName());
    return type;
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) TermOp(suite.node.io.TermOp) HashMap(java.util.HashMap) Tree(suite.node.Tree) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) TreeUtil(suite.node.util.TreeUtil) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

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