Search in sources :

Example 41 with Tree

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

the class InstructionExtractor method getRegisterNumber.

private int getRegisterNumber(List<Node> rs, int index) {
    if (index < rs.size()) {
        Node node = rs.get(index).finalNode();
        Tree tree;
        if (node instanceof Int)
            return ((Int) node).number;
        else if (node instanceof Reference) {
            // transient register
            // allocates new register in current local frame
            Instruction frameBegin = frameBegins.getFirst();
            int registerNumber = frameBegin.op0++;
            Binder.bind(node, Int.of(registerNumber), trail);
            return registerNumber;
        } else if ((tree = Tree.decompose(node, TermOp.COLON_)) != null) {
            Node key = tree.getLeft(), value = tree.getRight();
            if (key == KEYC)
                return allocateInPool(value);
            else if (key == KEYL)
                return ipByLabelId.get(IdentityKey.of(value));
            else if (key == KEYR)
                return 0;
        }
        return Fail.t("cannot parse instruction " + rs.get(0) + " operand " + node);
    } else
        return 0;
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Instruction(suite.instructionexecutor.InstructionUtil.Instruction) Int(suite.node.Int)

Example 42 with Tree

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

the class LogicInstructionExecutor method handle.

@Override
protected void handle(Exec exec, Instruction insn) {
    Activation current = exec.current;
    Frame frame = current.frame;
    Node[] regs = frame != null ? frame.registers : null;
    Trail trail = prover.getTrail();
    Instruction insn1;
    switch(insn.insn) {
        case BACKUPCSP_____:
            regs[insn.op0] = exec.current.previous;
            break;
        case BACKUPDSP_____:
            regs[insn.op0] = number(exec.sp);
            break;
        case BIND__________:
            if (!Binder.bind(regs[insn.op0], regs[insn.op1], trail))
                // fail
                current.ip = insn.op2;
            break;
        case BINDMARK______:
            regs[insn.op0] = number(trail.getPointInTime());
            break;
        case BINDUNDO______:
            trail.unwind(i(regs[insn.op0]));
            break;
        case DECOMPOSETREE0:
            Node node = regs[insn.op0].finalNode();
            insn1 = getInstructions()[current.ip++];
            TermOp op = TermOp.find(((Atom) constantPool.get(insn1.op0)).name);
            int rl = insn1.op1;
            int rr = insn1.op2;
            if (node instanceof Tree) {
                Tree tree = (Tree) node;
                if (tree.getOperator() == op) {
                    regs[rl] = tree.getLeft();
                    regs[rr] = tree.getRight();
                } else
                    current.ip = insn.op1;
            } else if (node instanceof Reference) {
                Tree tree = Tree.of(op, regs[rl] = new Reference(), regs[rr] = new Reference());
                trail.addBind((Reference) node, tree);
            } else
                current.ip = insn.op1;
            break;
        case PROVEINTERPRET:
            if (!prover.prove(regs[insn.op0]))
                current.ip = insn.op1;
            break;
        case PROVESYS______:
            if (!systemPredicates.call(regs[insn.op0]))
                current.ip = insn.op1;
            break;
        case RESTORECSP____:
            exec.current.previous = (Activation) regs[insn.op0];
            break;
        case RESTOREDSP____:
            exec.sp = i(regs[insn.op0]);
            break;
        default:
            Fail.t("unknown instruction " + insn);
    }
}
Also used : Frame(suite.instructionexecutor.InstructionUtil.Frame) Trail(suite.lp.Trail) TermOp(suite.node.io.TermOp) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Activation(suite.instructionexecutor.InstructionUtil.Activation) Instruction(suite.instructionexecutor.InstructionUtil.Instruction)

Example 43 with Tree

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

the class Complexity method complexity.

public int complexity(Node node) {
    int max = 0, base = 0;
    while (true) {
        Tree tree = Tree.decompose(node);
        if (tree != null) {
            base++;
            max = max(max, base + complexity(tree.getLeft()));
            node = tree.getRight();
        } else
            return max;
    }
}
Also used : Tree(suite.node.Tree)

Example 44 with Tree

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

the class Cyclic method isCyclic.

public boolean isCyclic(Node node) {
    IdentityKey<Node> idHashNode = IdentityKey.of(node);
    boolean isCyclic;
    if (!checkedNodes.contains(idHashNode)) {
        if (checkingNodes.add(idHashNode)) {
            Tree tree = Tree.decompose(node);
            isCyclic = tree != null && (isCyclic(tree.getLeft()) || isCyclic(tree.getRight()));
            checkingNodes.remove(idHashNode);
        } else
            isCyclic = true;
        checkedNodes.add(idHashNode);
    } else
        isCyclic = false;
    return isCyclic;
}
Also used : Node(suite.node.Node) Tree(suite.node.Tree)

Example 45 with Tree

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

the class TreeUtil method elements.

public static Node[] elements(Node node0, int n) {
    Node[] params = new Node[n];
    Node node = node0;
    Tree tree;
    for (int i = 0; i < n - 1; i++) if ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null) {
        params[i] = tree.getLeft();
        node = tree.getRight();
    } else
        Fail.t("not enough parameters in " + node0);
    params[n - 1] = node;
    return params;
}
Also used : Node(suite.node.Node) 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