Search in sources :

Example 11 with Node

use of suite.node.Node 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 12 with Node

use of suite.node.Node 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 13 with Node

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

the class EvaluateUtil method configureFunExecutor.

private FunInstructionExecutor configureFunExecutor(FunCompilerConfig fcc) {
    Node node = fccNodeFun.apply(fcc.isLazy());
    Node code = doFcc(node, fcc);
    if (code != null)
        if (fcc.isLazy())
            return new LazyFunInstructionExecutor(code);
        else
            return new EagerFunInstructionExecutor(code);
    else
        return Fail.t("function compilation failure");
}
Also used : EagerFunInstructionExecutor(suite.instructionexecutor.EagerFunInstructionExecutor) Node(suite.node.Node) LazyFunInstructionExecutor(suite.instructionexecutor.LazyFunInstructionExecutor)

Example 14 with Node

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

the class Suite method evaluateFilterFun.

public static void evaluateFilterFun(String program, Reader reader, Writer writer, boolean isLazy, boolean isDo) {
    try {
        Node node0 = parse(program);
        Node node1 = applyStringReader(node0, reader);
        Node node2 = isDo ? Suite.applyPerform(node1, Atom.of("string")) : node1;
        Node node3 = applyWriter(node2);
        evaluateFunToWriter(fcc(node3, isLazy), writer);
    } catch (IOException ex) {
        Fail.t(ex);
    }
}
Also used : Node(suite.node.Node) IOException(java.io.IOException)

Example 15 with Node

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

Node (suite.node.Node)139 Tree (suite.node.Tree)50 Reference (suite.node.Reference)41 Atom (suite.node.Atom)37 Int (suite.node.Int)33 ArrayList (java.util.ArrayList)32 Pair (suite.adt.pair.Pair)25 List (java.util.List)24 TermOp (suite.node.io.TermOp)21 Read (suite.streamlet.Read)21 Test (org.junit.Test)20 Map (java.util.Map)19 Suite (suite.Suite)18 Generalizer (suite.lp.doer.Generalizer)18 Fail (suite.util.Fail)18 Fun (suite.util.FunUtil.Fun)18 Str (suite.node.Str)17 Trail (suite.lp.Trail)16 RuleSet (suite.lp.kb.RuleSet)16 Tuple (suite.node.Tuple)16