Search in sources :

Example 46 with Tree

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

the class TreeUtil method evaluate.

public static int evaluate(Node node) {
    Tree tree = Tree.decompose(node);
    int result;
    if (tree != null) {
        Operator op = tree.getOperator();
        IntInt_Int fun;
        int lhs, rhs;
        if (op == TermOp.TUPLE_) {
            Tree rightTree = Tree.decompose(tree.getRight());
            lhs = evaluate(tree.getLeft());
            rhs = evaluate(rightTree.getRight());
            fun = evaluateOp(rightTree.getLeft());
        } else {
            lhs = evaluate(tree.getLeft());
            rhs = evaluate(tree.getRight());
            fun = evaluateOp(op);
        }
        result = fun.apply(lhs, rhs);
    } else if (node instanceof Int)
        result = ((Int) node).number;
    else
        result = Fail.t("cannot evaluate expression: " + node);
    return result;
}
Also used : Operator(suite.node.io.Operator) Tree(suite.node.Tree) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Int(suite.primitive.IntInt_Int) Int(suite.node.Int)

Example 47 with Tree

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

the class TreeUtil method nElements.

public static int nElements(Node node) {
    int n = 1;
    Tree tree;
    while ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null) {
        node = tree.getRight();
        n++;
    }
    return n;
}
Also used : Tree(suite.node.Tree)

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